blob: e97d080e9f659a2e56a073993a3b8be8bd96c1f5 [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 **/
302static inline void
303lpfc_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);
313 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
314}
315
316/**
James Smart4f774512009-05-22 14:52:35 -0400317 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
318 * @q: The Event Queue that the host has completed processing for.
319 * @arm: Indicates whether the host wants to arms this CQ.
320 *
321 * This routine will mark all Event Queue Entries on @q, from the last
322 * known completed entry to the last entry that was processed, as completed
323 * by clearing the valid bit for each completion queue entry. Then it will
324 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
325 * The internal host index in the @q will be updated by this routine to indicate
326 * that the host has finished processing the entries. The @arm parameter
327 * indicates that the queue should be rearmed when ringing the doorbell.
328 *
329 * This function will return the number of EQEs that were popped.
330 **/
331uint32_t
332lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
333{
334 uint32_t released = 0;
335 struct lpfc_eqe *temp_eqe;
336 struct lpfc_register doorbell;
337
James Smart2e90f4b2011-12-13 13:22:37 -0500338 /* sanity check on queue memory */
339 if (unlikely(!q))
340 return 0;
341
James Smart4f774512009-05-22 14:52:35 -0400342 /* while there are valid entries */
343 while (q->hba_index != q->host_index) {
344 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400345 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400346 released++;
347 q->host_index = ((q->host_index + 1) % q->entry_count);
348 }
349 if (unlikely(released == 0 && !arm))
350 return 0;
351
352 /* ring doorbell for number popped */
353 doorbell.word0 = 0;
354 if (arm) {
355 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
356 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
357 }
358 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
359 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500360 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
361 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
362 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400363 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500364 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
365 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
366 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400367 return released;
368}
369
370/**
371 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
372 * @q: The Completion Queue to get the first valid CQE from
373 *
374 * This routine will get the first valid Completion Queue Entry from @q, update
375 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
376 * the Queue (no more work to do), or the Queue is full of CQEs that have been
377 * processed, but not popped back to the HBA then this routine will return NULL.
378 **/
379static struct lpfc_cqe *
380lpfc_sli4_cq_get(struct lpfc_queue *q)
381{
382 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400383 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400384
James Smart2e90f4b2011-12-13 13:22:37 -0500385 /* sanity check on queue memory */
386 if (unlikely(!q))
387 return NULL;
388
James Smart4f774512009-05-22 14:52:35 -0400389 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400390 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400391 return NULL;
392 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400393 idx = ((q->hba_index + 1) % q->entry_count);
394 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400395 return NULL;
396
397 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400398 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400399
400 /*
401 * insert barrier for instruction interlock : data from the hardware
402 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800403 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
404 * instructions allowing action on content before valid bit checked,
405 * add barrier here as well. May not be needed as "content" is a
406 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400407 */
408 mb();
James Smart4f774512009-05-22 14:52:35 -0400409 return cqe;
410}
411
412/**
413 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
414 * @q: The Completion Queue that the host has completed processing for.
415 * @arm: Indicates whether the host wants to arms this CQ.
416 *
417 * This routine will mark all Completion queue entries on @q, from the last
418 * known completed entry to the last entry that was processed, as completed
419 * by clearing the valid bit for each completion queue entry. Then it will
420 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
421 * The internal host index in the @q will be updated by this routine to indicate
422 * that the host has finished processing the entries. The @arm parameter
423 * indicates that the queue should be rearmed when ringing the doorbell.
424 *
425 * This function will return the number of CQEs that were released.
426 **/
427uint32_t
428lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
429{
430 uint32_t released = 0;
431 struct lpfc_cqe *temp_qe;
432 struct lpfc_register doorbell;
433
James Smart2e90f4b2011-12-13 13:22:37 -0500434 /* sanity check on queue memory */
435 if (unlikely(!q))
436 return 0;
James Smart4f774512009-05-22 14:52:35 -0400437 /* while there are valid entries */
438 while (q->hba_index != q->host_index) {
439 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400440 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400441 released++;
442 q->host_index = ((q->host_index + 1) % q->entry_count);
443 }
444 if (unlikely(released == 0 && !arm))
445 return 0;
446
447 /* ring doorbell for number popped */
448 doorbell.word0 = 0;
449 if (arm)
450 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
451 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
452 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500453 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
454 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
455 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400456 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
457 return released;
458}
459
460/**
461 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
462 * @q: The Header Receive Queue to operate on.
463 * @wqe: The Receive Queue Entry to put on the Receive queue.
464 *
465 * This routine will copy the contents of @wqe to the next available entry on
466 * the @q. This function will then ring the Receive Queue Doorbell to signal the
467 * HBA to start processing the Receive Queue Entry. This function returns the
468 * index that the rqe was copied to if successful. If no entries are available
469 * on @q then this function will return -ENOMEM.
470 * The caller is expected to hold the hbalock when calling this routine.
471 **/
James Smart895427b2017-02-12 13:52:30 -0800472int
James Smart4f774512009-05-22 14:52:35 -0400473lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
474 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
475{
James Smart2e90f4b2011-12-13 13:22:37 -0500476 struct lpfc_rqe *temp_hrqe;
477 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400478 struct lpfc_register doorbell;
James Smartcbc5de12017-12-08 17:18:04 -0800479 int hq_put_index;
480 int dq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400481
James Smart2e90f4b2011-12-13 13:22:37 -0500482 /* sanity check on queue memory */
483 if (unlikely(!hq) || unlikely(!dq))
484 return -ENOMEM;
James Smartcbc5de12017-12-08 17:18:04 -0800485 hq_put_index = hq->host_index;
486 dq_put_index = dq->host_index;
487 temp_hrqe = hq->qe[hq_put_index].rqe;
488 temp_drqe = dq->qe[dq_put_index].rqe;
James Smart2e90f4b2011-12-13 13:22:37 -0500489
James Smart4f774512009-05-22 14:52:35 -0400490 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
491 return -EINVAL;
James Smartcbc5de12017-12-08 17:18:04 -0800492 if (hq_put_index != dq_put_index)
James Smart4f774512009-05-22 14:52:35 -0400493 return -EINVAL;
494 /* If the host has not yet processed the next entry then we are done */
James Smartcbc5de12017-12-08 17:18:04 -0800495 if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
James Smart4f774512009-05-22 14:52:35 -0400496 return -EBUSY;
497 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
498 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
499
500 /* Update the host index to point to the next slot */
James Smartcbc5de12017-12-08 17:18:04 -0800501 hq->host_index = ((hq_put_index + 1) % hq->entry_count);
502 dq->host_index = ((dq_put_index + 1) % dq->entry_count);
James Smart61f3d4b2017-05-15 15:20:41 -0700503 hq->RQ_buf_posted++;
James Smart4f774512009-05-22 14:52:35 -0400504
505 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400506 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400507 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500508 if (hq->db_format == LPFC_DB_RING_FORMAT) {
509 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
510 hq->entry_repost);
511 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
512 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
513 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
514 hq->entry_repost);
515 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
516 hq->host_index);
517 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
518 } else {
519 return -EINVAL;
520 }
521 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400522 }
James Smartcbc5de12017-12-08 17:18:04 -0800523 return hq_put_index;
James Smart4f774512009-05-22 14:52:35 -0400524}
525
526/**
527 * lpfc_sli4_rq_release - Updates internal hba index for RQ
528 * @q: The Header Receive Queue to operate on.
529 *
530 * This routine will update the HBA index of a queue to reflect consumption of
531 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
532 * consumed an entry the host calls this function to update the queue's
533 * internal pointers. This routine returns the number of entries that were
534 * consumed by the HBA.
535 **/
536static uint32_t
537lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
538{
James Smart2e90f4b2011-12-13 13:22:37 -0500539 /* sanity check on queue memory */
540 if (unlikely(!hq) || unlikely(!dq))
541 return 0;
542
James Smart4f774512009-05-22 14:52:35 -0400543 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
544 return 0;
545 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
546 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
547 return 1;
548}
549
James Smarte59058c2008-08-24 21:49:00 -0400550/**
James Smart3621a712009-04-06 18:47:14 -0400551 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400552 * @phba: Pointer to HBA context object.
553 * @pring: Pointer to driver SLI ring object.
554 *
555 * This function returns pointer to next command iocb entry
556 * in the command ring. The caller must hold hbalock to prevent
557 * other threads consume the next command iocb.
558 * SLI-2/SLI-3 provide different sized iocbs.
559 **/
James Smarted957682007-06-17 19:56:37 -0500560static inline IOCB_t *
561lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
562{
James Smart7e56aa22012-08-03 12:35:34 -0400563 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
564 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500565}
566
James Smarte59058c2008-08-24 21:49:00 -0400567/**
James Smart3621a712009-04-06 18:47:14 -0400568 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400569 * @phba: Pointer to HBA context object.
570 * @pring: Pointer to driver SLI ring object.
571 *
572 * This function returns pointer to next response iocb entry
573 * in the response ring. The caller must hold hbalock to make sure
574 * that no other thread consume the next response iocb.
575 * SLI-2/SLI-3 provide different sized iocbs.
576 **/
James Smarted957682007-06-17 19:56:37 -0500577static inline IOCB_t *
578lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
579{
James Smart7e56aa22012-08-03 12:35:34 -0400580 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
581 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500582}
583
James Smarte59058c2008-08-24 21:49:00 -0400584/**
James Smart3621a712009-04-06 18:47:14 -0400585 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400586 * @phba: Pointer to HBA context object.
587 *
588 * This function is called with hbalock held. This function
589 * allocates a new driver iocb object from the iocb pool. If the
590 * allocation is successful, it returns pointer to the newly
591 * allocated iocb object else it returns NULL.
592 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400593struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500594__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400595{
596 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
597 struct lpfc_iocbq * iocbq = NULL;
598
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100599 lockdep_assert_held(&phba->hbalock);
600
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400601 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400602 if (iocbq)
603 phba->iocb_cnt++;
604 if (phba->iocb_cnt > phba->iocb_max)
605 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400606 return iocbq;
607}
608
James Smarte59058c2008-08-24 21:49:00 -0400609/**
James Smartda0436e2009-05-22 14:51:39 -0400610 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
611 * @phba: Pointer to HBA context object.
612 * @xritag: XRI value.
613 *
614 * This function clears the sglq pointer from the array of acive
615 * sglq's. The xritag that is passed in is used to index into the
616 * array. Before the xritag can be used it needs to be adjusted
617 * by subtracting the xribase.
618 *
619 * Returns sglq ponter = success, NULL = Failure.
620 **/
James Smart895427b2017-02-12 13:52:30 -0800621struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400622__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
623{
James Smartda0436e2009-05-22 14:51:39 -0400624 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400625
626 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
627 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400628 return sglq;
629}
630
631/**
632 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
633 * @phba: Pointer to HBA context object.
634 * @xritag: XRI value.
635 *
636 * This function returns the sglq pointer from the array of acive
637 * sglq's. The xritag that is passed in is used to index into the
638 * array. Before the xritag can be used it needs to be adjusted
639 * by subtracting the xribase.
640 *
641 * Returns sglq ponter = success, NULL = Failure.
642 **/
James Smart0f65ff62010-02-26 14:14:23 -0500643struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400644__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
645{
James Smartda0436e2009-05-22 14:51:39 -0400646 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400647
648 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400649 return sglq;
650}
651
652/**
James Smart1151e3e2011-02-16 12:39:35 -0500653 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500654 * @phba: Pointer to HBA context object.
655 * @xritag: xri used in this exchange.
656 * @rrq: The RRQ to be cleared.
657 *
James Smart19ca7602010-11-20 23:11:55 -0500658 **/
James Smart1151e3e2011-02-16 12:39:35 -0500659void
660lpfc_clr_rrq_active(struct lpfc_hba *phba,
661 uint16_t xritag,
662 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500663{
James Smart1151e3e2011-02-16 12:39:35 -0500664 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500665
James Smart1151e3e2011-02-16 12:39:35 -0500666 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
667 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500668
669 /* The target DID could have been swapped (cable swap)
670 * we should use the ndlp from the findnode if it is
671 * available.
672 */
James Smart1151e3e2011-02-16 12:39:35 -0500673 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500674 ndlp = rrq->ndlp;
675
James Smart1151e3e2011-02-16 12:39:35 -0500676 if (!ndlp)
677 goto out;
678
James Smartcff261f2013-12-17 20:29:47 -0500679 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500680 rrq->send_rrq = 0;
681 rrq->xritag = 0;
682 rrq->rrq_stop_time = 0;
683 }
James Smart1151e3e2011-02-16 12:39:35 -0500684out:
James Smart19ca7602010-11-20 23:11:55 -0500685 mempool_free(rrq, phba->rrq_pool);
686}
687
688/**
689 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
690 * @phba: Pointer to HBA context object.
691 *
692 * This function is called with hbalock held. This function
693 * Checks if stop_time (ratov from setting rrq active) has
694 * been reached, if it has and the send_rrq flag is set then
695 * it will call lpfc_send_rrq. If the send_rrq flag is not set
696 * then it will just call the routine to clear the rrq and
697 * free the rrq resource.
698 * The timer is set to the next rrq that is going to expire before
699 * leaving the routine.
700 *
701 **/
702void
703lpfc_handle_rrq_active(struct lpfc_hba *phba)
704{
705 struct lpfc_node_rrq *rrq;
706 struct lpfc_node_rrq *nextrrq;
707 unsigned long next_time;
708 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500709 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500710
711 spin_lock_irqsave(&phba->hbalock, iflags);
712 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400713 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500714 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500715 &phba->active_rrq_list, list) {
716 if (time_after(jiffies, rrq->rrq_stop_time))
717 list_move(&rrq->list, &send_rrq);
718 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500719 next_time = rrq->rrq_stop_time;
720 }
721 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500722 if ((!list_empty(&phba->active_rrq_list)) &&
723 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500724 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500725 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
726 list_del(&rrq->list);
727 if (!rrq->send_rrq)
728 /* this call will free the rrq */
729 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
730 else if (lpfc_send_rrq(phba, rrq)) {
731 /* if we send the rrq then the completion handler
732 * will clear the bit in the xribitmap.
733 */
734 lpfc_clr_rrq_active(phba, rrq->xritag,
735 rrq);
736 }
737 }
James Smart19ca7602010-11-20 23:11:55 -0500738}
739
740/**
741 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
742 * @vport: Pointer to vport context object.
743 * @xri: The xri used in the exchange.
744 * @did: The targets DID for this exchange.
745 *
746 * returns NULL = rrq not found in the phba->active_rrq_list.
747 * rrq = rrq for this xri and target.
748 **/
749struct lpfc_node_rrq *
750lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
751{
752 struct lpfc_hba *phba = vport->phba;
753 struct lpfc_node_rrq *rrq;
754 struct lpfc_node_rrq *nextrrq;
755 unsigned long iflags;
756
757 if (phba->sli_rev != LPFC_SLI_REV4)
758 return NULL;
759 spin_lock_irqsave(&phba->hbalock, iflags);
760 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
761 if (rrq->vport == vport && rrq->xritag == xri &&
762 rrq->nlp_DID == did){
763 list_del(&rrq->list);
764 spin_unlock_irqrestore(&phba->hbalock, iflags);
765 return rrq;
766 }
767 }
768 spin_unlock_irqrestore(&phba->hbalock, iflags);
769 return NULL;
770}
771
772/**
773 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
774 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500775 * @ndlp: Pointer to the lpfc_node_list structure.
776 * If ndlp is NULL Remove all active RRQs for this vport from the
777 * phba->active_rrq_list and clear the rrq.
778 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500779 **/
780void
James Smart1151e3e2011-02-16 12:39:35 -0500781lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500782
783{
784 struct lpfc_hba *phba = vport->phba;
785 struct lpfc_node_rrq *rrq;
786 struct lpfc_node_rrq *nextrrq;
787 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500788 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500789
790 if (phba->sli_rev != LPFC_SLI_REV4)
791 return;
James Smart1151e3e2011-02-16 12:39:35 -0500792 if (!ndlp) {
793 lpfc_sli4_vport_delete_els_xri_aborted(vport);
794 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500795 }
James Smart1151e3e2011-02-16 12:39:35 -0500796 spin_lock_irqsave(&phba->hbalock, iflags);
797 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
798 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
799 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500800 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500801
802 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
803 list_del(&rrq->list);
804 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
805 }
James Smart19ca7602010-11-20 23:11:55 -0500806}
807
808/**
James Smart1151e3e2011-02-16 12:39:35 -0500809 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500810 * @phba: Pointer to HBA context object.
811 * @ndlp: Targets nodelist pointer for this exchange.
812 * @xritag the xri in the bitmap to test.
813 *
814 * This function is called with hbalock held. This function
815 * returns 0 = rrq not active for this xri
816 * 1 = rrq is valid for this xri.
817 **/
James Smart1151e3e2011-02-16 12:39:35 -0500818int
819lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500820 uint16_t xritag)
821{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100822 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -0500823 if (!ndlp)
824 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500825 if (!ndlp->active_rrqs_xri_bitmap)
826 return 0;
827 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500828 return 1;
829 else
830 return 0;
831}
832
833/**
834 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
835 * @phba: Pointer to HBA context object.
836 * @ndlp: nodelist pointer for this target.
837 * @xritag: xri used in this exchange.
838 * @rxid: Remote Exchange ID.
839 * @send_rrq: Flag used to determine if we should send rrq els cmd.
840 *
841 * This function takes the hbalock.
842 * The active bit is always set in the active rrq xri_bitmap even
843 * if there is no slot avaiable for the other rrq information.
844 *
845 * returns 0 rrq actived for this xri
846 * < 0 No memory or invalid ndlp.
847 **/
848int
849lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500850 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500851{
James Smart19ca7602010-11-20 23:11:55 -0500852 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500853 struct lpfc_node_rrq *rrq;
854 int empty;
855
856 if (!ndlp)
857 return -EINVAL;
858
859 if (!phba->cfg_enable_rrq)
860 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500861
862 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500863 if (phba->pport->load_flag & FC_UNLOADING) {
864 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
865 goto out;
866 }
867
868 /*
869 * set the active bit even if there is no mem available.
870 */
871 if (NLP_CHK_FREE_REQ(ndlp))
872 goto out;
873
874 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
875 goto out;
876
James Smartcff261f2013-12-17 20:29:47 -0500877 if (!ndlp->active_rrqs_xri_bitmap)
878 goto out;
879
880 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -0500881 goto out;
882
James Smart19ca7602010-11-20 23:11:55 -0500883 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500884 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
885 if (!rrq) {
886 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
887 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
888 " DID:0x%x Send:%d\n",
889 xritag, rxid, ndlp->nlp_DID, send_rrq);
890 return -EINVAL;
891 }
James Smarte5771b42013-03-01 16:37:14 -0500892 if (phba->cfg_enable_rrq == 1)
893 rrq->send_rrq = send_rrq;
894 else
895 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500896 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -0400897 rrq->rrq_stop_time = jiffies +
898 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -0500899 rrq->ndlp = ndlp;
900 rrq->nlp_DID = ndlp->nlp_DID;
901 rrq->vport = ndlp->vport;
902 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500903 spin_lock_irqsave(&phba->hbalock, iflags);
904 empty = list_empty(&phba->active_rrq_list);
905 list_add_tail(&rrq->list, &phba->active_rrq_list);
906 phba->hba_flag |= HBA_RRQ_ACTIVE;
907 if (empty)
908 lpfc_worker_wake_up(phba);
909 spin_unlock_irqrestore(&phba->hbalock, iflags);
910 return 0;
911out:
912 spin_unlock_irqrestore(&phba->hbalock, iflags);
913 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
914 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
915 " DID:0x%x Send:%d\n",
916 xritag, rxid, ndlp->nlp_DID, send_rrq);
917 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500918}
919
920/**
James Smart895427b2017-02-12 13:52:30 -0800921 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -0400922 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500923 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400924 *
James Smartdafe8ce2014-09-03 12:56:40 -0400925 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400926 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400927 * list is not empty then it is successful, it returns pointer to the newly
928 * allocated sglq object else it returns NULL.
929 **/
930static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -0800931__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400932{
James Smart895427b2017-02-12 13:52:30 -0800933 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -0400934 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500935 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500936 struct lpfc_scsi_buf *lpfc_cmd;
937 struct lpfc_nodelist *ndlp;
938 int found = 0;
939
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100940 lockdep_assert_held(&phba->hbalock);
941
James Smart19ca7602010-11-20 23:11:55 -0500942 if (piocbq->iocb_flag & LPFC_IO_FCP) {
943 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
944 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500945 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -0400946 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -0500947 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -0400948 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
949 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
950 ndlp = NULL;
951 else
952 ndlp = piocbq->context_un.ndlp;
953 } else {
James Smart19ca7602010-11-20 23:11:55 -0500954 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -0400955 }
James Smart19ca7602010-11-20 23:11:55 -0500956
James Smart895427b2017-02-12 13:52:30 -0800957 spin_lock(&phba->sli4_hba.sgl_list_lock);
958 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500959 start_sglq = sglq;
960 while (!found) {
961 if (!sglq)
James Smartd11f54b2017-03-04 09:30:24 -0800962 break;
James Smart895427b2017-02-12 13:52:30 -0800963 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
964 test_bit(sglq->sli4_lxritag,
965 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500966 /* This xri has an rrq outstanding for this DID.
967 * put it back in the list and get another xri.
968 */
James Smart895427b2017-02-12 13:52:30 -0800969 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -0500970 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -0800971 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -0500972 struct lpfc_sglq, list);
973 if (sglq == start_sglq) {
James Smart14041bd2017-06-01 21:07:01 -0700974 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -0500975 sglq = NULL;
976 break;
977 } else
978 continue;
979 }
980 sglq->ndlp = ndlp;
981 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400982 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500983 sglq->state = SGL_ALLOCATED;
984 }
James Smart895427b2017-02-12 13:52:30 -0800985 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400986 return sglq;
987}
988
989/**
James Smartf358dd02017-02-12 13:52:34 -0800990 * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
991 * @phba: Pointer to HBA context object.
992 * @piocb: Pointer to the iocbq.
993 *
994 * This function is called with the sgl_list lock held. This function
995 * gets a new driver sglq object from the sglq list. If the
996 * list is not empty then it is successful, it returns pointer to the newly
997 * allocated sglq object else it returns NULL.
998 **/
999struct lpfc_sglq *
1000__lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
1001{
1002 struct list_head *lpfc_nvmet_sgl_list;
1003 struct lpfc_sglq *sglq = NULL;
1004
1005 lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
1006
1007 lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
1008
1009 list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
1010 if (!sglq)
1011 return NULL;
1012 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
1013 sglq->state = SGL_ALLOCATED;
dea31012005-04-17 16:05:31 -05001014 return sglq;
1015}
1016
James Smarte59058c2008-08-24 21:49:00 -04001017/**
James Smart3621a712009-04-06 18:47:14 -04001018 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001019 * @phba: Pointer to HBA context object.
1020 *
1021 * This function is called with no lock held. This function
1022 * allocates a new driver iocb object from the iocb pool. If the
1023 * allocation is successful, it returns pointer to the newly
1024 * allocated iocb object else it returns NULL.
1025 **/
James Smart2e0fef82007-06-17 19:56:36 -05001026struct lpfc_iocbq *
1027lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -05001028{
James Smart2e0fef82007-06-17 19:56:36 -05001029 struct lpfc_iocbq * iocbq = NULL;
1030 unsigned long iflags;
1031
1032 spin_lock_irqsave(&phba->hbalock, iflags);
1033 iocbq = __lpfc_sli_get_iocbq(phba);
1034 spin_unlock_irqrestore(&phba->hbalock, iflags);
1035 return iocbq;
1036}
1037
James Smarte59058c2008-08-24 21:49:00 -04001038/**
James Smart4f774512009-05-22 14:52:35 -04001039 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1040 * @phba: Pointer to HBA context object.
1041 * @iocbq: Pointer to driver iocb object.
1042 *
1043 * This function is called with hbalock held to release driver
1044 * iocb object to the iocb pool. The iotag in the iocb object
1045 * does not change for each use of the iocb object. This function
1046 * clears all other fields of the iocb object when it is freed.
1047 * The sqlq structure that holds the xritag and phys and virtual
1048 * mappings for the scatter gather list is retrieved from the
1049 * active array of sglq. The get of the sglq pointer also clears
1050 * the entry in the array. If the status of the IO indiactes that
1051 * this IO was aborted then the sglq entry it put on the
1052 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1053 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001054 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001055 **/
1056static void
1057__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1058{
1059 struct lpfc_sglq *sglq;
1060 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001061 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001062 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001063
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001064 lockdep_assert_held(&phba->hbalock);
1065
James Smart4f774512009-05-22 14:52:35 -04001066 if (iocbq->sli4_xritag == NO_XRI)
1067 sglq = NULL;
1068 else
James Smart6d368e52011-05-24 11:44:12 -04001069 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1070
James Smart0e9bb8d2013-03-01 16:35:12 -05001071
James Smart4f774512009-05-22 14:52:35 -04001072 if (sglq) {
James Smartf358dd02017-02-12 13:52:34 -08001073 if (iocbq->iocb_flag & LPFC_IO_NVMET) {
1074 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1075 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001076 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001077 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001078 list_add_tail(&sglq->list,
James Smartf358dd02017-02-12 13:52:34 -08001079 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1080 spin_unlock_irqrestore(
1081 &phba->sli4_hba.sgl_list_lock, iflag);
1082 goto out;
1083 }
1084
James Smart895427b2017-02-12 13:52:30 -08001085 pring = phba->sli4_hba.els_wq->pring;
James Smart4f774512009-05-22 14:52:35 -04001086 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1087 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001088 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1089 iflag);
James Smart341af102010-01-26 23:07:37 -05001090 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001091 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001092 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001093 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart4f774512009-05-22 14:52:35 -04001094 } else {
James Smart895427b2017-02-12 13:52:30 -08001095 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1096 iflag);
James Smart4f774512009-05-22 14:52:35 -04001097 sglq->state = SGL_FREED;
1098 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001099 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001100 &phba->sli4_hba.lpfc_els_sgl_list);
1101 spin_unlock_irqrestore(
1102 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001103
1104 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001105 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001106 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001107 }
James Smart4f774512009-05-22 14:52:35 -04001108 }
1109
James Smartf358dd02017-02-12 13:52:34 -08001110out:
James Smart4f774512009-05-22 14:52:35 -04001111 /*
1112 * Clean all volatile data fields, preserve iotag and node struct.
1113 */
1114 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001115 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001116 iocbq->sli4_xritag = NO_XRI;
James Smartf358dd02017-02-12 13:52:34 -08001117 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
1118 LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001119 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1120}
1121
James Smart2a9bf3d2010-06-07 15:24:45 -04001122
James Smart4f774512009-05-22 14:52:35 -04001123/**
James Smart3772a992009-05-22 14:50:54 -04001124 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1125 * @phba: Pointer to HBA context object.
1126 * @iocbq: Pointer to driver iocb object.
1127 *
1128 * This function is called with hbalock held to release driver
1129 * iocb object to the iocb pool. The iotag in the iocb object
1130 * does not change for each use of the iocb object. This function
1131 * clears all other fields of the iocb object when it is freed.
1132 **/
1133static void
1134__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1135{
1136 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1137
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001138 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001139
1140 /*
James Smart3772a992009-05-22 14:50:54 -04001141 * Clean all volatile data fields, preserve iotag and node struct.
1142 */
1143 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1144 iocbq->sli4_xritag = NO_XRI;
1145 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1146}
1147
1148/**
James Smart3621a712009-04-06 18:47:14 -04001149 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001150 * @phba: Pointer to HBA context object.
1151 * @iocbq: Pointer to driver iocb object.
1152 *
1153 * This function is called with hbalock held to release driver
1154 * iocb object to the iocb pool. The iotag in the iocb object
1155 * does not change for each use of the iocb object. This function
1156 * clears all other fields of the iocb object when it is freed.
1157 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001158static void
James Smart2e0fef82007-06-17 19:56:36 -05001159__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1160{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001161 lockdep_assert_held(&phba->hbalock);
1162
James Smart3772a992009-05-22 14:50:54 -04001163 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001164 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001165}
1166
James Smarte59058c2008-08-24 21:49:00 -04001167/**
James Smart3621a712009-04-06 18:47:14 -04001168 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001169 * @phba: Pointer to HBA context object.
1170 * @iocbq: Pointer to driver iocb object.
1171 *
1172 * This function is called with no lock held to release the iocb to
1173 * iocb pool.
1174 **/
James Smart2e0fef82007-06-17 19:56:36 -05001175void
1176lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1177{
1178 unsigned long iflags;
1179
1180 /*
1181 * Clean all volatile data fields, preserve iotag and node struct.
1182 */
1183 spin_lock_irqsave(&phba->hbalock, iflags);
1184 __lpfc_sli_release_iocbq(phba, iocbq);
1185 spin_unlock_irqrestore(&phba->hbalock, iflags);
1186}
1187
James Smarte59058c2008-08-24 21:49:00 -04001188/**
James Smarta257bf92009-04-06 18:48:10 -04001189 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1190 * @phba: Pointer to HBA context object.
1191 * @iocblist: List of IOCBs.
1192 * @ulpstatus: ULP status in IOCB command field.
1193 * @ulpWord4: ULP word-4 in IOCB command field.
1194 *
1195 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1196 * on the list by invoking the complete callback function associated with the
1197 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1198 * fields.
1199 **/
1200void
1201lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1202 uint32_t ulpstatus, uint32_t ulpWord4)
1203{
1204 struct lpfc_iocbq *piocb;
1205
1206 while (!list_empty(iocblist)) {
1207 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001208 if (!piocb->iocb_cmpl)
1209 lpfc_sli_release_iocbq(phba, piocb);
1210 else {
1211 piocb->iocb.ulpStatus = ulpstatus;
1212 piocb->iocb.un.ulpWord[4] = ulpWord4;
1213 (piocb->iocb_cmpl) (phba, piocb, piocb);
1214 }
1215 }
1216 return;
1217}
1218
1219/**
James Smart3621a712009-04-06 18:47:14 -04001220 * lpfc_sli_iocb_cmd_type - Get the iocb type
1221 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001222 *
1223 * This function is called by ring event handler function to get the iocb type.
1224 * This function translates the iocb command to an iocb command type used to
1225 * decide the final disposition of each completed IOCB.
1226 * The function returns
1227 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1228 * LPFC_SOL_IOCB if it is a solicited iocb completion
1229 * LPFC_ABORT_IOCB if it is an abort iocb
1230 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1231 *
1232 * The caller is not required to hold any lock.
1233 **/
dea31012005-04-17 16:05:31 -05001234static lpfc_iocb_type
1235lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1236{
1237 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1238
1239 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1240 return 0;
1241
1242 switch (iocb_cmnd) {
1243 case CMD_XMIT_SEQUENCE_CR:
1244 case CMD_XMIT_SEQUENCE_CX:
1245 case CMD_XMIT_BCAST_CN:
1246 case CMD_XMIT_BCAST_CX:
1247 case CMD_ELS_REQUEST_CR:
1248 case CMD_ELS_REQUEST_CX:
1249 case CMD_CREATE_XRI_CR:
1250 case CMD_CREATE_XRI_CX:
1251 case CMD_GET_RPI_CN:
1252 case CMD_XMIT_ELS_RSP_CX:
1253 case CMD_GET_RPI_CR:
1254 case CMD_FCP_IWRITE_CR:
1255 case CMD_FCP_IWRITE_CX:
1256 case CMD_FCP_IREAD_CR:
1257 case CMD_FCP_IREAD_CX:
1258 case CMD_FCP_ICMND_CR:
1259 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001260 case CMD_FCP_TSEND_CX:
1261 case CMD_FCP_TRSP_CX:
1262 case CMD_FCP_TRECEIVE_CX:
1263 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001264 case CMD_ADAPTER_MSG:
1265 case CMD_ADAPTER_DUMP:
1266 case CMD_XMIT_SEQUENCE64_CR:
1267 case CMD_XMIT_SEQUENCE64_CX:
1268 case CMD_XMIT_BCAST64_CN:
1269 case CMD_XMIT_BCAST64_CX:
1270 case CMD_ELS_REQUEST64_CR:
1271 case CMD_ELS_REQUEST64_CX:
1272 case CMD_FCP_IWRITE64_CR:
1273 case CMD_FCP_IWRITE64_CX:
1274 case CMD_FCP_IREAD64_CR:
1275 case CMD_FCP_IREAD64_CX:
1276 case CMD_FCP_ICMND64_CR:
1277 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001278 case CMD_FCP_TSEND64_CX:
1279 case CMD_FCP_TRSP64_CX:
1280 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001281 case CMD_GEN_REQUEST64_CR:
1282 case CMD_GEN_REQUEST64_CX:
1283 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001284 case DSSCMD_IWRITE64_CR:
1285 case DSSCMD_IWRITE64_CX:
1286 case DSSCMD_IREAD64_CR:
1287 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001288 type = LPFC_SOL_IOCB;
1289 break;
1290 case CMD_ABORT_XRI_CN:
1291 case CMD_ABORT_XRI_CX:
1292 case CMD_CLOSE_XRI_CN:
1293 case CMD_CLOSE_XRI_CX:
1294 case CMD_XRI_ABORTED_CX:
1295 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001296 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001297 type = LPFC_ABORT_IOCB;
1298 break;
1299 case CMD_RCV_SEQUENCE_CX:
1300 case CMD_RCV_ELS_REQ_CX:
1301 case CMD_RCV_SEQUENCE64_CX:
1302 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001303 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001304 case CMD_IOCB_RCV_SEQ64_CX:
1305 case CMD_IOCB_RCV_ELS64_CX:
1306 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001307 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001308 type = LPFC_UNSOL_IOCB;
1309 break;
James Smart3163f722008-02-08 18:50:25 -05001310 case CMD_IOCB_XMIT_MSEQ64_CR:
1311 case CMD_IOCB_XMIT_MSEQ64_CX:
1312 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1313 case CMD_IOCB_RCV_ELS_LIST64_CX:
1314 case CMD_IOCB_CLOSE_EXTENDED_CN:
1315 case CMD_IOCB_ABORT_EXTENDED_CN:
1316 case CMD_IOCB_RET_HBQE64_CN:
1317 case CMD_IOCB_FCP_IBIDIR64_CR:
1318 case CMD_IOCB_FCP_IBIDIR64_CX:
1319 case CMD_IOCB_FCP_ITASKMGT64_CX:
1320 case CMD_IOCB_LOGENTRY_CN:
1321 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1322 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001323 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001324 type = LPFC_UNKNOWN_IOCB;
1325 break;
dea31012005-04-17 16:05:31 -05001326 default:
1327 type = LPFC_UNKNOWN_IOCB;
1328 break;
1329 }
1330
1331 return type;
1332}
1333
James Smarte59058c2008-08-24 21:49:00 -04001334/**
James Smart3621a712009-04-06 18:47:14 -04001335 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001336 * @phba: Pointer to HBA context object.
1337 *
1338 * This function is called from SLI initialization code
1339 * to configure every ring of the HBA's SLI interface. The
1340 * caller is not required to hold any lock. This function issues
1341 * a config_ring mailbox command for each ring.
1342 * This function returns zero if successful else returns a negative
1343 * error code.
1344 **/
dea31012005-04-17 16:05:31 -05001345static int
James Smarted957682007-06-17 19:56:37 -05001346lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001347{
1348 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001349 LPFC_MBOXQ_t *pmb;
1350 MAILBOX_t *pmbox;
1351 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001352
James Smarted957682007-06-17 19:56:37 -05001353 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1354 if (!pmb)
1355 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001356 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001357 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001358 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001359 lpfc_config_ring(phba, i, pmb);
1360 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1361 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001363 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001364 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1365 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001366 rc, pmbox->mbxCommand,
1367 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001368 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001369 ret = -ENXIO;
1370 break;
dea31012005-04-17 16:05:31 -05001371 }
1372 }
James Smarted957682007-06-17 19:56:37 -05001373 mempool_free(pmb, phba->mbox_mem_pool);
1374 return ret;
dea31012005-04-17 16:05:31 -05001375}
1376
James Smarte59058c2008-08-24 21:49:00 -04001377/**
James Smart3621a712009-04-06 18:47:14 -04001378 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001379 * @phba: Pointer to HBA context object.
1380 * @pring: Pointer to driver SLI ring object.
1381 * @piocb: Pointer to the driver iocb object.
1382 *
1383 * This function is called with hbalock held. The function adds the
1384 * new iocb to txcmplq of the given ring. This function always returns
1385 * 0. If this function is called for ELS ring, this function checks if
1386 * there is a vport associated with the ELS command. This function also
1387 * starts els_tmofunc timer if this is an ELS command.
1388 **/
dea31012005-04-17 16:05:31 -05001389static int
James Smart2e0fef82007-06-17 19:56:36 -05001390lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1391 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001392{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001393 lockdep_assert_held(&phba->hbalock);
1394
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001395 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001396
dea31012005-04-17 16:05:31 -05001397 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001398 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001399
James Smart92d7f7b2007-06-17 19:56:38 -05001400 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1401 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001402 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1403 BUG_ON(!piocb->vport);
1404 if (!(piocb->vport->load_flag & FC_UNLOADING))
1405 mod_timer(&piocb->vport->els_tmofunc,
1406 jiffies +
1407 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1408 }
dea31012005-04-17 16:05:31 -05001409
James Smart2e0fef82007-06-17 19:56:36 -05001410 return 0;
dea31012005-04-17 16:05:31 -05001411}
1412
James Smarte59058c2008-08-24 21:49:00 -04001413/**
James Smart3621a712009-04-06 18:47:14 -04001414 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001415 * @phba: Pointer to HBA context object.
1416 * @pring: Pointer to driver SLI ring object.
1417 *
1418 * This function is called with hbalock held to get next
1419 * iocb in txq of the given ring. If there is any iocb in
1420 * the txq, the function returns first iocb in the list after
1421 * removing the iocb from the list, else it returns NULL.
1422 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001423struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001424lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001425{
dea31012005-04-17 16:05:31 -05001426 struct lpfc_iocbq *cmd_iocb;
1427
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001428 lockdep_assert_held(&phba->hbalock);
1429
James Smart858c9f62007-06-17 19:56:39 -05001430 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001431 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001432}
1433
James Smarte59058c2008-08-24 21:49:00 -04001434/**
James Smart3621a712009-04-06 18:47:14 -04001435 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001436 * @phba: Pointer to HBA context object.
1437 * @pring: Pointer to driver SLI ring object.
1438 *
1439 * This function is called with hbalock held and the caller must post the
1440 * iocb without releasing the lock. If the caller releases the lock,
1441 * iocb slot returned by the function is not guaranteed to be available.
1442 * The function returns pointer to the next available iocb slot if there
1443 * is available slot in the ring, else it returns NULL.
1444 * If the get index of the ring is ahead of the put index, the function
1445 * will post an error attention event to the worker thread to take the
1446 * HBA to offline state.
1447 **/
dea31012005-04-17 16:05:31 -05001448static IOCB_t *
1449lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1450{
James Smart34b02dc2008-08-24 21:49:55 -04001451 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001452 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001453
1454 lockdep_assert_held(&phba->hbalock);
1455
James Smart7e56aa22012-08-03 12:35:34 -04001456 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1457 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1458 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001459
James Smart7e56aa22012-08-03 12:35:34 -04001460 if (unlikely(pring->sli.sli3.local_getidx ==
1461 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001462
James Smart7e56aa22012-08-03 12:35:34 -04001463 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001464
James Smart7e56aa22012-08-03 12:35:34 -04001465 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001466 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001467 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001468 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001469 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001470 pring->sli.sli3.local_getidx,
1471 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001472
James Smart2e0fef82007-06-17 19:56:36 -05001473 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001474 /*
1475 * All error attention handlers are posted to
1476 * worker thread
1477 */
1478 phba->work_ha |= HA_ERATT;
1479 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001480
James Smart5e9d9b82008-06-14 22:52:53 -04001481 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001482
1483 return NULL;
1484 }
1485
James Smart7e56aa22012-08-03 12:35:34 -04001486 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001487 return NULL;
1488 }
1489
James Smarted957682007-06-17 19:56:37 -05001490 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001491}
1492
James Smarte59058c2008-08-24 21:49:00 -04001493/**
James Smart3621a712009-04-06 18:47:14 -04001494 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001495 * @phba: Pointer to HBA context object.
1496 * @iocbq: Pointer to driver iocb object.
1497 *
1498 * This function gets an iotag for the iocb. If there is no unused iotag and
1499 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1500 * array and assigns a new iotag.
1501 * The function returns the allocated iotag if successful, else returns zero.
1502 * Zero is not a valid iotag.
1503 * The caller is not required to hold any lock.
1504 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001505uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001506lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001507{
James Smart2e0fef82007-06-17 19:56:36 -05001508 struct lpfc_iocbq **new_arr;
1509 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001510 size_t new_len;
1511 struct lpfc_sli *psli = &phba->sli;
1512 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001513
James Smart2e0fef82007-06-17 19:56:36 -05001514 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001515 iotag = psli->last_iotag;
1516 if(++iotag < psli->iocbq_lookup_len) {
1517 psli->last_iotag = iotag;
1518 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001519 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001520 iocbq->iotag = iotag;
1521 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001522 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001523 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1524 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001525 spin_unlock_irq(&phba->hbalock);
1526 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001527 GFP_KERNEL);
1528 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001529 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001530 old_arr = psli->iocbq_lookup;
1531 if (new_len <= psli->iocbq_lookup_len) {
1532 /* highly unprobable case */
1533 kfree(new_arr);
1534 iotag = psli->last_iotag;
1535 if(++iotag < psli->iocbq_lookup_len) {
1536 psli->last_iotag = iotag;
1537 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001538 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001539 iocbq->iotag = iotag;
1540 return iotag;
1541 }
James Smart2e0fef82007-06-17 19:56:36 -05001542 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001543 return 0;
1544 }
1545 if (psli->iocbq_lookup)
1546 memcpy(new_arr, old_arr,
1547 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001548 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001549 psli->iocbq_lookup = new_arr;
1550 psli->iocbq_lookup_len = new_len;
1551 psli->last_iotag = iotag;
1552 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001553 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001554 iocbq->iotag = iotag;
1555 kfree(old_arr);
1556 return iotag;
1557 }
James Smart8f6d98d2006-08-01 07:34:00 -04001558 } else
James Smart2e0fef82007-06-17 19:56:36 -05001559 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001560
James Smartbc739052010-08-04 16:11:18 -04001561 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001562 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1563 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001564
James Bottomley604a3e32005-10-29 10:28:33 -05001565 return 0;
dea31012005-04-17 16:05:31 -05001566}
1567
James Smarte59058c2008-08-24 21:49:00 -04001568/**
James Smart3621a712009-04-06 18:47:14 -04001569 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001570 * @phba: Pointer to HBA context object.
1571 * @pring: Pointer to driver SLI ring object.
1572 * @iocb: Pointer to iocb slot in the ring.
1573 * @nextiocb: Pointer to driver iocb object which need to be
1574 * posted to firmware.
1575 *
1576 * This function is called with hbalock held to post a new iocb to
1577 * the firmware. This function copies the new iocb to ring iocb slot and
1578 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1579 * a completion call back for this iocb else the function will free the
1580 * iocb object.
1581 **/
dea31012005-04-17 16:05:31 -05001582static void
1583lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1584 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1585{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001586 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001587 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001588 * Set up an iotag
dea31012005-04-17 16:05:31 -05001589 */
James Bottomley604a3e32005-10-29 10:28:33 -05001590 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001591
James Smarte2a0a9d2008-12-04 22:40:02 -05001592
James Smarta58cbd52007-08-02 11:09:43 -04001593 if (pring->ringno == LPFC_ELS_RING) {
1594 lpfc_debugfs_slow_ring_trc(phba,
1595 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1596 *(((uint32_t *) &nextiocb->iocb) + 4),
1597 *(((uint32_t *) &nextiocb->iocb) + 6),
1598 *(((uint32_t *) &nextiocb->iocb) + 7));
1599 }
1600
dea31012005-04-17 16:05:31 -05001601 /*
1602 * Issue iocb command to adapter
1603 */
James Smart92d7f7b2007-06-17 19:56:38 -05001604 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001605 wmb();
1606 pring->stats.iocb_cmd++;
1607
1608 /*
1609 * If there is no completion routine to call, we can release the
1610 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1611 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1612 */
1613 if (nextiocb->iocb_cmpl)
1614 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001615 else
James Smart2e0fef82007-06-17 19:56:36 -05001616 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001617
1618 /*
1619 * Let the HBA know what IOCB slot will be the next one the
1620 * driver will put a command into.
1621 */
James Smart7e56aa22012-08-03 12:35:34 -04001622 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1623 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001624}
1625
James Smarte59058c2008-08-24 21:49:00 -04001626/**
James Smart3621a712009-04-06 18:47:14 -04001627 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001628 * @phba: Pointer to HBA context object.
1629 * @pring: Pointer to driver SLI ring object.
1630 *
1631 * The caller is not required to hold any lock for calling this function.
1632 * This function updates the chip attention bits for the ring to inform firmware
1633 * that there are pending work to be done for this ring and requests an
1634 * interrupt when there is space available in the ring. This function is
1635 * called when the driver is unable to post more iocbs to the ring due
1636 * to unavailability of space in the ring.
1637 **/
dea31012005-04-17 16:05:31 -05001638static void
James Smart2e0fef82007-06-17 19:56:36 -05001639lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001640{
1641 int ringno = pring->ringno;
1642
1643 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1644
1645 wmb();
1646
1647 /*
1648 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1649 * The HBA will tell us when an IOCB entry is available.
1650 */
1651 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1652 readl(phba->CAregaddr); /* flush */
1653
1654 pring->stats.iocb_cmd_full++;
1655}
1656
James Smarte59058c2008-08-24 21:49:00 -04001657/**
James Smart3621a712009-04-06 18:47:14 -04001658 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001659 * @phba: Pointer to HBA context object.
1660 * @pring: Pointer to driver SLI ring object.
1661 *
1662 * This function updates the chip attention register bit for the
1663 * given ring to inform HBA that there is more work to be done
1664 * in this ring. The caller is not required to hold any lock.
1665 **/
dea31012005-04-17 16:05:31 -05001666static void
James Smart2e0fef82007-06-17 19:56:36 -05001667lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001668{
1669 int ringno = pring->ringno;
1670
1671 /*
1672 * Tell the HBA that there is work to do in this ring.
1673 */
James Smart34b02dc2008-08-24 21:49:55 -04001674 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1675 wmb();
1676 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1677 readl(phba->CAregaddr); /* flush */
1678 }
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_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001683 * @phba: Pointer to HBA context object.
1684 * @pring: Pointer to driver SLI ring object.
1685 *
1686 * This function is called with hbalock held to post pending iocbs
1687 * in the txq to the firmware. This function is called when driver
1688 * detects space available in the ring.
1689 **/
dea31012005-04-17 16:05:31 -05001690static void
James Smart2e0fef82007-06-17 19:56:36 -05001691lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001692{
1693 IOCB_t *iocb;
1694 struct lpfc_iocbq *nextiocb;
1695
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001696 lockdep_assert_held(&phba->hbalock);
1697
dea31012005-04-17 16:05:31 -05001698 /*
1699 * Check to see if:
1700 * (a) there is anything on the txq to send
1701 * (b) link is up
1702 * (c) link attention events can be processed (fcp ring only)
1703 * (d) IOCB processing is not blocked by the outstanding mbox command.
1704 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001705
1706 if (lpfc_is_link_up(phba) &&
1707 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001708 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001709 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001710
1711 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1712 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1713 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1714
1715 if (iocb)
1716 lpfc_sli_update_ring(phba, pring);
1717 else
1718 lpfc_sli_update_full_ring(phba, pring);
1719 }
1720
1721 return;
1722}
1723
James Smarte59058c2008-08-24 21:49:00 -04001724/**
James Smart3621a712009-04-06 18:47:14 -04001725 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001726 * @phba: Pointer to HBA context object.
1727 * @hbqno: HBQ number.
1728 *
1729 * This function is called with hbalock held to get the next
1730 * available slot for the given HBQ. If there is free slot
1731 * available for the HBQ it will return pointer to the next available
1732 * HBQ entry else it will return NULL.
1733 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001734static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001735lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1736{
1737 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1738
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001739 lockdep_assert_held(&phba->hbalock);
1740
James Smarted957682007-06-17 19:56:37 -05001741 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1742 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1743 hbqp->next_hbqPutIdx = 0;
1744
1745 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001746 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001747 uint32_t getidx = le32_to_cpu(raw_index);
1748
1749 hbqp->local_hbqGetIdx = getidx;
1750
1751 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1752 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001753 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001754 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001755 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001756 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001757 hbqp->entry_count);
1758
1759 phba->link_state = LPFC_HBA_ERROR;
1760 return NULL;
1761 }
1762
1763 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1764 return NULL;
1765 }
1766
James Smart51ef4c22007-08-02 11:10:31 -04001767 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1768 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001769}
1770
James Smarte59058c2008-08-24 21:49:00 -04001771/**
James Smart3621a712009-04-06 18:47:14 -04001772 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001773 * @phba: Pointer to HBA context object.
1774 *
1775 * This function is called with no lock held to free all the
1776 * hbq buffers while uninitializing the SLI interface. It also
1777 * frees the HBQ buffers returned by the firmware but not yet
1778 * processed by the upper layers.
1779 **/
James Smarted957682007-06-17 19:56:37 -05001780void
1781lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1782{
James Smart92d7f7b2007-06-17 19:56:38 -05001783 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1784 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001785 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001786 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001787
James Smart51ef4c22007-08-02 11:10:31 -04001788 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001789 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001790 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001791 for (i = 0; i < hbq_count; ++i) {
1792 list_for_each_entry_safe(dmabuf, next_dmabuf,
1793 &phba->hbqs[i].hbq_buffer_list, list) {
1794 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1795 list_del(&hbq_buf->dbuf.list);
1796 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1797 }
James Smarta8adb832007-10-27 13:37:53 -04001798 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001799 }
James Smart3163f722008-02-08 18:50:25 -05001800
1801 /* Mark the HBQs not in use */
1802 phba->hbq_in_use = 0;
1803 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001804}
1805
James Smarte59058c2008-08-24 21:49:00 -04001806/**
James Smart3621a712009-04-06 18:47:14 -04001807 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001808 * @phba: Pointer to HBA context object.
1809 * @hbqno: HBQ number.
1810 * @hbq_buf: Pointer to HBQ buffer.
1811 *
1812 * This function is called with the hbalock held to post a
1813 * hbq buffer to the firmware. If the function finds an empty
1814 * slot in the HBQ, it will post the buffer. The function will return
1815 * pointer to the hbq entry if it successfully post the buffer
1816 * else it will return NULL.
1817 **/
James Smart3772a992009-05-22 14:50:54 -04001818static int
James Smarted957682007-06-17 19:56:37 -05001819lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001820 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001821{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001822 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001823 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1824}
1825
1826/**
1827 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1828 * @phba: Pointer to HBA context object.
1829 * @hbqno: HBQ number.
1830 * @hbq_buf: Pointer to HBQ buffer.
1831 *
1832 * This function is called with the hbalock held to post a hbq buffer to the
1833 * firmware. If the function finds an empty slot in the HBQ, it will post the
1834 * buffer and place it on the hbq_buffer_list. The function will return zero if
1835 * it successfully post the buffer else it will return an error.
1836 **/
1837static int
1838lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1839 struct hbq_dmabuf *hbq_buf)
1840{
James Smarted957682007-06-17 19:56:37 -05001841 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001842 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001843
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001844 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05001845 /* Get next HBQ entry slot to use */
1846 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1847 if (hbqe) {
1848 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1849
James Smart92d7f7b2007-06-17 19:56:38 -05001850 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1851 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08001852 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05001853 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001854 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1855 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1856 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001857 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1858 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001859 /* flush */
James Smarted957682007-06-17 19:56:37 -05001860 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001861 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001862 return 0;
1863 } else
1864 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001865}
1866
James Smart4f774512009-05-22 14:52:35 -04001867/**
1868 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1869 * @phba: Pointer to HBA context object.
1870 * @hbqno: HBQ number.
1871 * @hbq_buf: Pointer to HBQ buffer.
1872 *
1873 * This function is called with the hbalock held to post an RQE to the SLI4
1874 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1875 * the hbq_buffer_list and return zero, otherwise it will return an error.
1876 **/
1877static int
1878lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1879 struct hbq_dmabuf *hbq_buf)
1880{
1881 int rc;
1882 struct lpfc_rqe hrqe;
1883 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08001884 struct lpfc_queue *hrq;
1885 struct lpfc_queue *drq;
1886
1887 if (hbqno != LPFC_ELS_HBQ)
1888 return 1;
1889 hrq = phba->sli4_hba.hdr_rq;
1890 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04001891
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001892 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04001893 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1894 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1895 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1896 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08001897 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04001898 if (rc < 0)
1899 return rc;
James Smart895427b2017-02-12 13:52:30 -08001900 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04001901 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1902 return 0;
1903}
1904
James Smarte59058c2008-08-24 21:49:00 -04001905/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001906static struct lpfc_hbq_init lpfc_els_hbq = {
1907 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001908 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001909 .mask_count = 0,
1910 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001911 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001912 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001913 .init_count = 40,
1914 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001915};
James Smarted957682007-06-17 19:56:37 -05001916
James Smarte59058c2008-08-24 21:49:00 -04001917/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001918struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001919 &lpfc_els_hbq,
1920};
1921
James Smarte59058c2008-08-24 21:49:00 -04001922/**
James Smart3621a712009-04-06 18:47:14 -04001923 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001924 * @phba: Pointer to HBA context object.
1925 * @hbqno: HBQ number.
1926 * @count: Number of HBQ buffers to be posted.
1927 *
James Smartd7c255b2008-08-24 21:50:00 -04001928 * This function is called with no lock held to post more hbq buffers to the
1929 * given HBQ. The function returns the number of HBQ buffers successfully
1930 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001931 **/
James Smart311464e2007-08-02 11:10:37 -04001932static int
James Smart92d7f7b2007-06-17 19:56:38 -05001933lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1934{
James Smartd7c255b2008-08-24 21:50:00 -04001935 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001936 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001937 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001938 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001939 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001940 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001941
James Smartd7c255b2008-08-24 21:50:00 -04001942 if ((phba->hbqs[hbqno].buffer_count + count) >
1943 lpfc_hbq_defs[hbqno]->entry_count)
1944 count = lpfc_hbq_defs[hbqno]->entry_count -
1945 phba->hbqs[hbqno].buffer_count;
1946 if (!count)
1947 return 0;
1948 /* Allocate HBQ entries */
1949 for (i = 0; i < count; i++) {
1950 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1951 if (!hbq_buffer)
1952 break;
1953 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1954 }
James Smart3163f722008-02-08 18:50:25 -05001955 /* Check whether HBQ is still in use */
1956 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001957 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001958 goto err;
1959 while (!list_empty(&hbq_buf_list)) {
1960 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1961 dbuf.list);
1962 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1963 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001964 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001965 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001966 posted++;
1967 } else
James Smart51ef4c22007-08-02 11:10:31 -04001968 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001969 }
James Smart3163f722008-02-08 18:50:25 -05001970 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001971 return posted;
1972err:
1973 spin_unlock_irqrestore(&phba->hbalock, flags);
1974 while (!list_empty(&hbq_buf_list)) {
1975 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1976 dbuf.list);
1977 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1978 }
James Smart92d7f7b2007-06-17 19:56:38 -05001979 return 0;
James Smarted957682007-06-17 19:56:37 -05001980}
1981
James Smarte59058c2008-08-24 21:49:00 -04001982/**
James Smart3621a712009-04-06 18:47:14 -04001983 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001984 * @phba: Pointer to HBA context object.
1985 * @qno: HBQ number.
1986 *
1987 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001988 * is called with no lock held. The function returns the number of HBQ entries
1989 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001990 **/
James Smarted957682007-06-17 19:56:37 -05001991int
James Smart92d7f7b2007-06-17 19:56:38 -05001992lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001993{
James Smartdef9c7a2009-12-21 17:02:28 -05001994 if (phba->sli_rev == LPFC_SLI_REV4)
1995 return 0;
1996 else
1997 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1998 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001999}
2000
James Smarte59058c2008-08-24 21:49:00 -04002001/**
James Smart3621a712009-04-06 18:47:14 -04002002 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04002003 * @phba: Pointer to HBA context object.
2004 * @qno: HBQ queue number.
2005 *
2006 * This function is called from SLI initialization code path with
2007 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04002008 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04002009 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002010static int
James Smart92d7f7b2007-06-17 19:56:38 -05002011lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05002012{
James Smartdef9c7a2009-12-21 17:02:28 -05002013 if (phba->sli_rev == LPFC_SLI_REV4)
2014 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04002015 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05002016 else
2017 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
2018 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05002019}
2020
James Smarte59058c2008-08-24 21:49:00 -04002021/**
James Smart3772a992009-05-22 14:50:54 -04002022 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
2023 * @phba: Pointer to HBA context object.
2024 * @hbqno: HBQ number.
2025 *
2026 * This function removes the first hbq buffer on an hbq list and returns a
2027 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2028 **/
2029static struct hbq_dmabuf *
2030lpfc_sli_hbqbuf_get(struct list_head *rb_list)
2031{
2032 struct lpfc_dmabuf *d_buf;
2033
2034 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
2035 if (!d_buf)
2036 return NULL;
2037 return container_of(d_buf, struct hbq_dmabuf, dbuf);
2038}
2039
2040/**
James Smart2d7dbc42017-02-12 13:52:35 -08002041 * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
2042 * @phba: Pointer to HBA context object.
2043 * @hbqno: HBQ number.
2044 *
2045 * This function removes the first RQ buffer on an RQ buffer list and returns a
2046 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
2047 **/
2048static struct rqb_dmabuf *
2049lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
2050{
2051 struct lpfc_dmabuf *h_buf;
2052 struct lpfc_rqb *rqbp;
2053
2054 rqbp = hrq->rqbp;
2055 list_remove_head(&rqbp->rqb_buffer_list, h_buf,
2056 struct lpfc_dmabuf, list);
2057 if (!h_buf)
2058 return NULL;
2059 rqbp->buffer_count--;
2060 return container_of(h_buf, struct rqb_dmabuf, hbuf);
2061}
2062
2063/**
James Smart3621a712009-04-06 18:47:14 -04002064 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002065 * @phba: Pointer to HBA context object.
2066 * @tag: Tag of the hbq buffer.
2067 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002068 * This function searches for the hbq buffer associated with the given tag in
2069 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2070 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002071 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002072static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002073lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2074{
James Smart92d7f7b2007-06-17 19:56:38 -05002075 struct lpfc_dmabuf *d_buf;
2076 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002077 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002078
James Smart51ef4c22007-08-02 11:10:31 -04002079 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002080 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002081 return NULL;
2082
James Smart3772a992009-05-22 14:50:54 -04002083 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002084 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002085 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002086 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002087 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002088 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002089 }
2090 }
James Smart3772a992009-05-22 14:50:54 -04002091 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002092 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002093 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002094 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002095 return NULL;
James Smarted957682007-06-17 19:56:37 -05002096}
2097
James Smarte59058c2008-08-24 21:49:00 -04002098/**
James Smart3621a712009-04-06 18:47:14 -04002099 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002100 * @phba: Pointer to HBA context object.
2101 * @hbq_buffer: Pointer to HBQ buffer.
2102 *
2103 * This function is called with hbalock. This function gives back
2104 * the hbq buffer to firmware. If the HBQ does not have space to
2105 * post the buffer, it will free the buffer.
2106 **/
James Smarted957682007-06-17 19:56:37 -05002107void
James Smart51ef4c22007-08-02 11:10:31 -04002108lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002109{
2110 uint32_t hbqno;
2111
James Smart51ef4c22007-08-02 11:10:31 -04002112 if (hbq_buffer) {
2113 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002114 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002115 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002116 }
2117}
2118
James Smarte59058c2008-08-24 21:49:00 -04002119/**
James Smart3621a712009-04-06 18:47:14 -04002120 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002121 * @mbxCommand: mailbox command code.
2122 *
2123 * This function is called by the mailbox event handler function to verify
2124 * that the completed mailbox command is a legitimate mailbox command. If the
2125 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2126 * and the mailbox event handler will take the HBA offline.
2127 **/
dea31012005-04-17 16:05:31 -05002128static int
2129lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2130{
2131 uint8_t ret;
2132
2133 switch (mbxCommand) {
2134 case MBX_LOAD_SM:
2135 case MBX_READ_NV:
2136 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002137 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002138 case MBX_RUN_BIU_DIAG:
2139 case MBX_INIT_LINK:
2140 case MBX_DOWN_LINK:
2141 case MBX_CONFIG_LINK:
2142 case MBX_CONFIG_RING:
2143 case MBX_RESET_RING:
2144 case MBX_READ_CONFIG:
2145 case MBX_READ_RCONFIG:
2146 case MBX_READ_SPARM:
2147 case MBX_READ_STATUS:
2148 case MBX_READ_RPI:
2149 case MBX_READ_XRI:
2150 case MBX_READ_REV:
2151 case MBX_READ_LNK_STAT:
2152 case MBX_REG_LOGIN:
2153 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002154 case MBX_CLEAR_LA:
2155 case MBX_DUMP_MEMORY:
2156 case MBX_DUMP_CONTEXT:
2157 case MBX_RUN_DIAGS:
2158 case MBX_RESTART:
2159 case MBX_UPDATE_CFG:
2160 case MBX_DOWN_LOAD:
2161 case MBX_DEL_LD_ENTRY:
2162 case MBX_RUN_PROGRAM:
2163 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002164 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002165 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002166 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002167 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002168 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002169 case MBX_LOAD_AREA:
2170 case MBX_RUN_BIU_DIAG64:
2171 case MBX_CONFIG_PORT:
2172 case MBX_READ_SPARM64:
2173 case MBX_READ_RPI64:
2174 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002175 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002176 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002177 case MBX_SET_DEBUG:
2178 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002179 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002180 case MBX_REG_VPI:
2181 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002182 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002183 case MBX_PORT_CAPABILITIES:
2184 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002185 case MBX_SLI4_CONFIG:
2186 case MBX_SLI4_REQ_FTRS:
2187 case MBX_REG_FCFI:
2188 case MBX_UNREG_FCFI:
2189 case MBX_REG_VFI:
2190 case MBX_UNREG_VFI:
2191 case MBX_INIT_VPI:
2192 case MBX_INIT_VFI:
2193 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002194 case MBX_READ_EVENT_LOG_STATUS:
2195 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002196 case MBX_SECURITY_MGMT:
2197 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002198 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002199 ret = mbxCommand;
2200 break;
2201 default:
2202 ret = MBX_SHUTDOWN;
2203 break;
2204 }
James Smart2e0fef82007-06-17 19:56:36 -05002205 return ret;
dea31012005-04-17 16:05:31 -05002206}
James Smarte59058c2008-08-24 21:49:00 -04002207
2208/**
James Smart3621a712009-04-06 18:47:14 -04002209 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002210 * @phba: Pointer to HBA context object.
2211 * @pmboxq: Pointer to mailbox command.
2212 *
2213 * This is completion handler function for mailbox commands issued from
2214 * lpfc_sli_issue_mbox_wait function. This function is called by the
2215 * mailbox event handler function with no lock held. This function
2216 * will wake up thread waiting on the wait queue pointed by context1
2217 * of the mailbox.
2218 **/
James Smart04c68492009-05-22 14:52:52 -04002219void
James Smart2e0fef82007-06-17 19:56:36 -05002220lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002221{
2222 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002223 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002224
2225 /*
2226 * If pdone_q is empty, the driver thread gave up waiting and
2227 * continued running.
2228 */
James Smart7054a602007-04-25 09:52:34 -04002229 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002230 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002231 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2232 if (pdone_q)
2233 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002234 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002235 return;
2236}
2237
James Smarte59058c2008-08-24 21:49:00 -04002238
2239/**
James Smart3621a712009-04-06 18:47:14 -04002240 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002241 * @phba: Pointer to HBA context object.
2242 * @pmb: Pointer to mailbox object.
2243 *
2244 * This function is the default mailbox completion handler. It
2245 * frees the memory resources associated with the completed mailbox
2246 * command. If the completed command is a REG_LOGIN mailbox command,
2247 * this function will issue a UREG_LOGIN to re-claim the RPI.
2248 **/
dea31012005-04-17 16:05:31 -05002249void
James Smart2e0fef82007-06-17 19:56:36 -05002250lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002251{
James Smartd439d282010-09-29 11:18:45 -04002252 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002253 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002254 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002255 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002256 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002257 int rc;
2258
dea31012005-04-17 16:05:31 -05002259 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002260
dea31012005-04-17 16:05:31 -05002261 if (mp) {
2262 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2263 kfree(mp);
2264 }
James Smart7054a602007-04-25 09:52:34 -04002265
2266 /*
2267 * If a REG_LOGIN succeeded after node is destroyed or node
2268 * is in re-discovery driver need to cleanup the RPI.
2269 */
James Smart2e0fef82007-06-17 19:56:36 -05002270 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002271 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2272 !pmb->u.mb.mbxStatus) {
2273 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002274 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002275 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002276 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002277 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002278 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2279 if (rc != MBX_NOT_FINISHED)
2280 return;
2281 }
2282
James Smart695a8142010-01-26 23:08:03 -05002283 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2284 !(phba->pport->load_flag & FC_UNLOADING) &&
2285 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002286 shost = lpfc_shost_from_vport(vport);
2287 spin_lock_irq(shost->host_lock);
2288 vport->vpi_state |= LPFC_VPI_REGISTERED;
2289 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2290 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002291 }
2292
James Smartd439d282010-09-29 11:18:45 -04002293 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2294 ndlp = (struct lpfc_nodelist *)pmb->context2;
2295 lpfc_nlp_put(ndlp);
2296 pmb->context2 = NULL;
2297 }
2298
James Smartdcf2a4e2010-09-29 11:18:53 -04002299 /* Check security permission status on INIT_LINK mailbox command */
2300 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2301 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2302 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2303 "2860 SLI authentication is required "
2304 "for INIT_LINK but has not done yet\n");
2305
James Smart04c68492009-05-22 14:52:52 -04002306 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2307 lpfc_sli4_mbox_cmd_free(phba, pmb);
2308 else
2309 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002310}
James Smartbe6bb942015-04-07 15:07:22 -04002311 /**
2312 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2313 * @phba: Pointer to HBA context object.
2314 * @pmb: Pointer to mailbox object.
2315 *
2316 * This function is the unreg rpi mailbox completion handler. It
2317 * frees the memory resources associated with the completed mailbox
2318 * command. An additional refrenece is put on the ndlp to prevent
2319 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2320 * the unreg mailbox command completes, this routine puts the
2321 * reference back.
2322 *
2323 **/
2324void
2325lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2326{
2327 struct lpfc_vport *vport = pmb->vport;
2328 struct lpfc_nodelist *ndlp;
2329
2330 ndlp = pmb->context1;
2331 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2332 if (phba->sli_rev == LPFC_SLI_REV4 &&
2333 (bf_get(lpfc_sli_intf_if_type,
2334 &phba->sli4_hba.sli_intf) ==
2335 LPFC_SLI_INTF_IF_TYPE_2)) {
2336 if (ndlp) {
2337 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2338 "0010 UNREG_LOGIN vpi:%x "
2339 "rpi:%x DID:%x map:%x %p\n",
2340 vport->vpi, ndlp->nlp_rpi,
2341 ndlp->nlp_DID,
2342 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002343 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002344 lpfc_nlp_put(ndlp);
2345 }
2346 }
2347 }
2348
2349 mempool_free(pmb, phba->mbox_mem_pool);
2350}
dea31012005-04-17 16:05:31 -05002351
James Smarte59058c2008-08-24 21:49:00 -04002352/**
James Smart3621a712009-04-06 18:47:14 -04002353 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002354 * @phba: Pointer to HBA context object.
2355 *
2356 * This function is called with no lock held. This function processes all
2357 * the completed mailbox commands and gives it to upper layers. The interrupt
2358 * service routine processes mailbox completion interrupt and adds completed
2359 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2360 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2361 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2362 * function returns the mailbox commands to the upper layer by calling the
2363 * completion handler function of each mailbox.
2364 **/
dea31012005-04-17 16:05:31 -05002365int
James Smart2e0fef82007-06-17 19:56:36 -05002366lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002367{
James Smart92d7f7b2007-06-17 19:56:38 -05002368 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002369 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002370 int rc;
2371 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002372
2373 phba->sli.slistat.mbox_event++;
2374
James Smart92d7f7b2007-06-17 19:56:38 -05002375 /* Get all completed mailboxe buffers into the cmplq */
2376 spin_lock_irq(&phba->hbalock);
2377 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2378 spin_unlock_irq(&phba->hbalock);
2379
dea31012005-04-17 16:05:31 -05002380 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002381 do {
2382 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2383 if (pmb == NULL)
2384 break;
2385
James Smart04c68492009-05-22 14:52:52 -04002386 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002387
James Smart858c9f62007-06-17 19:56:39 -05002388 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2389 if (pmb->vport) {
2390 lpfc_debugfs_disc_trc(pmb->vport,
2391 LPFC_DISC_TRC_MBOX_VPORT,
2392 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2393 (uint32_t)pmbox->mbxCommand,
2394 pmbox->un.varWords[0],
2395 pmbox->un.varWords[1]);
2396 }
2397 else {
2398 lpfc_debugfs_disc_trc(phba->pport,
2399 LPFC_DISC_TRC_MBOX,
2400 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2401 (uint32_t)pmbox->mbxCommand,
2402 pmbox->un.varWords[0],
2403 pmbox->un.varWords[1]);
2404 }
2405 }
2406
dea31012005-04-17 16:05:31 -05002407 /*
2408 * It is a fatal error if unknown mbox command completion.
2409 */
2410 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2411 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002412 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002413 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002414 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002415 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002416 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002417 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002418 lpfc_sli_config_mbox_subsys_get(phba,
2419 pmb),
2420 lpfc_sli_config_mbox_opcode_get(phba,
2421 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002422 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002423 phba->work_hs = HS_FFER3;
2424 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002425 continue;
dea31012005-04-17 16:05:31 -05002426 }
2427
dea31012005-04-17 16:05:31 -05002428 if (pmbox->mbxStatus) {
2429 phba->sli.slistat.mbox_stat_err++;
2430 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2431 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002432 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002433 LOG_MBOX | LOG_SLI,
2434 "(%d):0305 Mbox cmd cmpl "
2435 "error - RETRYing Data: x%x "
2436 "(x%x/x%x) x%x x%x x%x\n",
2437 pmb->vport ? pmb->vport->vpi : 0,
2438 pmbox->mbxCommand,
2439 lpfc_sli_config_mbox_subsys_get(phba,
2440 pmb),
2441 lpfc_sli_config_mbox_opcode_get(phba,
2442 pmb),
2443 pmbox->mbxStatus,
2444 pmbox->un.varWords[0],
2445 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002446 pmbox->mbxStatus = 0;
2447 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002448 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002449 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002450 continue;
dea31012005-04-17 16:05:31 -05002451 }
2452 }
2453
2454 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002455 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002456 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002457 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2458 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002459 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002460 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002461 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2462 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002463 pmb->mbox_cmpl,
2464 *((uint32_t *) pmbox),
2465 pmbox->un.varWords[0],
2466 pmbox->un.varWords[1],
2467 pmbox->un.varWords[2],
2468 pmbox->un.varWords[3],
2469 pmbox->un.varWords[4],
2470 pmbox->un.varWords[5],
2471 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002472 pmbox->un.varWords[7],
2473 pmbox->un.varWords[8],
2474 pmbox->un.varWords[9],
2475 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002476
James Smart92d7f7b2007-06-17 19:56:38 -05002477 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002478 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002479 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002480 return 0;
dea31012005-04-17 16:05:31 -05002481}
James Smart92d7f7b2007-06-17 19:56:38 -05002482
James Smarte59058c2008-08-24 21:49:00 -04002483/**
James Smart3621a712009-04-06 18:47:14 -04002484 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002485 * @phba: Pointer to HBA context object.
2486 * @pring: Pointer to driver SLI ring object.
2487 * @tag: buffer tag.
2488 *
2489 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2490 * is set in the tag the buffer is posted for a particular exchange,
2491 * the function will return the buffer without replacing the buffer.
2492 * If the buffer is for unsolicited ELS or CT traffic, this function
2493 * returns the buffer and also posts another buffer to the firmware.
2494 **/
James Smart76bb24e2007-10-27 13:38:00 -04002495static struct lpfc_dmabuf *
2496lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002497 struct lpfc_sli_ring *pring,
2498 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002499{
James Smart9f1e1b52008-12-04 22:39:40 -05002500 struct hbq_dmabuf *hbq_entry;
2501
James Smart76bb24e2007-10-27 13:38:00 -04002502 if (tag & QUE_BUFTAG_BIT)
2503 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002504 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2505 if (!hbq_entry)
2506 return NULL;
2507 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002508}
James Smart57127f12007-10-27 13:37:05 -04002509
James Smart3772a992009-05-22 14:50:54 -04002510/**
2511 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2512 * @phba: Pointer to HBA context object.
2513 * @pring: Pointer to driver SLI ring object.
2514 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2515 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2516 * @fch_type: the type for the first frame of the sequence.
2517 *
2518 * This function is called with no lock held. This function uses the r_ctl and
2519 * type of the received sequence to find the correct callback function to call
2520 * to process the sequence.
2521 **/
2522static int
2523lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2524 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2525 uint32_t fch_type)
2526{
2527 int i;
2528
James Smartf358dd02017-02-12 13:52:34 -08002529 switch (fch_type) {
2530 case FC_TYPE_NVME:
James Smartd613b6a2017-02-12 13:52:37 -08002531 lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
James Smartf358dd02017-02-12 13:52:34 -08002532 return 1;
2533 default:
2534 break;
2535 }
2536
James Smart3772a992009-05-22 14:50:54 -04002537 /* unSolicited Responses */
2538 if (pring->prt[0].profile) {
2539 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2540 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2541 saveq);
2542 return 1;
2543 }
2544 /* We must search, based on rctl / type
2545 for the right routine */
2546 for (i = 0; i < pring->num_mask; i++) {
2547 if ((pring->prt[i].rctl == fch_r_ctl) &&
2548 (pring->prt[i].type == fch_type)) {
2549 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2550 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2551 (phba, pring, saveq);
2552 return 1;
2553 }
2554 }
2555 return 0;
2556}
James Smarte59058c2008-08-24 21:49:00 -04002557
2558/**
James Smart3621a712009-04-06 18:47:14 -04002559 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002560 * @phba: Pointer to HBA context object.
2561 * @pring: Pointer to driver SLI ring object.
2562 * @saveq: Pointer to the unsolicited iocb.
2563 *
2564 * This function is called with no lock held by the ring event handler
2565 * when there is an unsolicited iocb posted to the response ring by the
2566 * firmware. This function gets the buffer associated with the iocbs
2567 * and calls the event handler for the ring. This function handles both
2568 * qring buffers and hbq buffers.
2569 * When the function returns 1 the caller can free the iocb object otherwise
2570 * upper layer functions will free the iocb objects.
2571 **/
dea31012005-04-17 16:05:31 -05002572static int
2573lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2574 struct lpfc_iocbq *saveq)
2575{
2576 IOCB_t * irsp;
2577 WORD5 * w5p;
2578 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002579 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002580 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002581
dea31012005-04-17 16:05:31 -05002582 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002583
2584 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2585 if (pring->lpfc_sli_rcv_async_status)
2586 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2587 else
2588 lpfc_printf_log(phba,
2589 KERN_WARNING,
2590 LOG_SLI,
2591 "0316 Ring %d handler: unexpected "
2592 "ASYNC_STATUS iocb received evt_code "
2593 "0x%x\n",
2594 pring->ringno,
2595 irsp->un.asyncstat.evt_code);
2596 return 1;
2597 }
2598
James Smart3163f722008-02-08 18:50:25 -05002599 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2600 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2601 if (irsp->ulpBdeCount > 0) {
2602 dmzbuf = lpfc_sli_get_buff(phba, pring,
2603 irsp->un.ulpWord[3]);
2604 lpfc_in_buf_free(phba, dmzbuf);
2605 }
2606
2607 if (irsp->ulpBdeCount > 1) {
2608 dmzbuf = lpfc_sli_get_buff(phba, pring,
2609 irsp->unsli3.sli3Words[3]);
2610 lpfc_in_buf_free(phba, dmzbuf);
2611 }
2612
2613 if (irsp->ulpBdeCount > 2) {
2614 dmzbuf = lpfc_sli_get_buff(phba, pring,
2615 irsp->unsli3.sli3Words[7]);
2616 lpfc_in_buf_free(phba, dmzbuf);
2617 }
2618
2619 return 1;
2620 }
2621
James Smart92d7f7b2007-06-17 19:56:38 -05002622 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002623 if (irsp->ulpBdeCount != 0) {
2624 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002625 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002626 if (!saveq->context2)
2627 lpfc_printf_log(phba,
2628 KERN_ERR,
2629 LOG_SLI,
2630 "0341 Ring %d Cannot find buffer for "
2631 "an unsolicited iocb. tag 0x%x\n",
2632 pring->ringno,
2633 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002634 }
2635 if (irsp->ulpBdeCount == 2) {
2636 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002637 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002638 if (!saveq->context3)
2639 lpfc_printf_log(phba,
2640 KERN_ERR,
2641 LOG_SLI,
2642 "0342 Ring %d Cannot find buffer for an"
2643 " unsolicited iocb. tag 0x%x\n",
2644 pring->ringno,
2645 irsp->unsli3.sli3Words[7]);
2646 }
2647 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002648 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002649 if (irsp->ulpBdeCount != 0) {
2650 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2651 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002652 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002653 lpfc_printf_log(phba,
2654 KERN_ERR,
2655 LOG_SLI,
2656 "0343 Ring %d Cannot find "
2657 "buffer for an unsolicited iocb"
2658 ". tag 0x%x\n", pring->ringno,
2659 irsp->un.ulpWord[3]);
2660 }
2661 if (irsp->ulpBdeCount == 2) {
2662 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2663 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002664 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002665 lpfc_printf_log(phba,
2666 KERN_ERR,
2667 LOG_SLI,
2668 "0344 Ring %d Cannot find "
2669 "buffer for an unsolicited "
2670 "iocb. tag 0x%x\n",
2671 pring->ringno,
2672 irsp->unsli3.sli3Words[7]);
2673 }
2674 }
James Smart92d7f7b2007-06-17 19:56:38 -05002675 }
James Smart9c2face2008-01-11 01:53:18 -05002676 if (irsp->ulpBdeCount != 0 &&
2677 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2678 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2679 int found = 0;
2680
2681 /* search continue save q for same XRI */
2682 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002683 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2684 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002685 list_add_tail(&saveq->list, &iocbq->list);
2686 found = 1;
2687 break;
2688 }
2689 }
2690 if (!found)
2691 list_add_tail(&saveq->clist,
2692 &pring->iocb_continue_saveq);
2693 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2694 list_del_init(&iocbq->clist);
2695 saveq = iocbq;
2696 irsp = &(saveq->iocb);
2697 } else
2698 return 0;
2699 }
2700 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2701 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2702 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002703 Rctl = FC_RCTL_ELS_REQ;
2704 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002705 } else {
2706 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2707 Rctl = w5p->hcsw.Rctl;
2708 Type = w5p->hcsw.Type;
2709
2710 /* Firmware Workaround */
2711 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2712 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2713 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002714 Rctl = FC_RCTL_ELS_REQ;
2715 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002716 w5p->hcsw.Rctl = Rctl;
2717 w5p->hcsw.Type = Type;
2718 }
2719 }
James Smart92d7f7b2007-06-17 19:56:38 -05002720
James Smart3772a992009-05-22 14:50:54 -04002721 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002722 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002723 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002724 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002725 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002726
James Smart92d7f7b2007-06-17 19:56:38 -05002727 return 1;
dea31012005-04-17 16:05:31 -05002728}
2729
James Smarte59058c2008-08-24 21:49:00 -04002730/**
James Smart3621a712009-04-06 18:47:14 -04002731 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002732 * @phba: Pointer to HBA context object.
2733 * @pring: Pointer to driver SLI ring object.
2734 * @prspiocb: Pointer to response iocb object.
2735 *
2736 * This function looks up the iocb_lookup table to get the command iocb
2737 * corresponding to the given response iocb using the iotag of the
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002738 * response iocb. This function is called with the hbalock held
2739 * for sli3 devices or the ring_lock for sli4 devices.
James Smarte59058c2008-08-24 21:49:00 -04002740 * This function returns the command iocb object if it finds the command
2741 * iocb else returns NULL.
2742 **/
dea31012005-04-17 16:05:31 -05002743static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002744lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2745 struct lpfc_sli_ring *pring,
2746 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002747{
dea31012005-04-17 16:05:31 -05002748 struct lpfc_iocbq *cmd_iocb = NULL;
2749 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002750 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002751
James Bottomley604a3e32005-10-29 10:28:33 -05002752 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002753
James Bottomley604a3e32005-10-29 10:28:33 -05002754 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2755 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002756 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002757 /* remove from txcmpl queue list */
2758 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002759 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002760 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002761 }
dea31012005-04-17 16:05:31 -05002762 }
2763
dea31012005-04-17 16:05:31 -05002764 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002765 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002766 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002767 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002768 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002769 return NULL;
2770}
2771
James Smarte59058c2008-08-24 21:49:00 -04002772/**
James Smart3772a992009-05-22 14:50:54 -04002773 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2774 * @phba: Pointer to HBA context object.
2775 * @pring: Pointer to driver SLI ring object.
2776 * @iotag: IOCB tag.
2777 *
2778 * This function looks up the iocb_lookup table to get the command iocb
2779 * corresponding to the given iotag. This function is called with the
2780 * hbalock held.
2781 * This function returns the command iocb object if it finds the command
2782 * iocb else returns NULL.
2783 **/
2784static struct lpfc_iocbq *
2785lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2786 struct lpfc_sli_ring *pring, uint16_t iotag)
2787{
James Smart895427b2017-02-12 13:52:30 -08002788 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04002789
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002790 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002791 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2792 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002793 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2794 /* remove from txcmpl queue list */
2795 list_del_init(&cmd_iocb->list);
2796 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002797 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002798 }
James Smart3772a992009-05-22 14:50:54 -04002799 }
James Smart89533e92016-10-13 15:06:15 -07002800
James Smart3772a992009-05-22 14:50:54 -04002801 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08002802 "0372 iotag x%x lookup error: max iotag (x%x) "
2803 "iocb_flag x%x\n",
2804 iotag, phba->sli.last_iotag,
2805 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04002806 return NULL;
2807}
2808
2809/**
James Smart3621a712009-04-06 18:47:14 -04002810 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002811 * @phba: Pointer to HBA context object.
2812 * @pring: Pointer to driver SLI ring object.
2813 * @saveq: Pointer to the response iocb to be processed.
2814 *
2815 * This function is called by the ring event handler for non-fcp
2816 * rings when there is a new response iocb in the response ring.
2817 * The caller is not required to hold any locks. This function
2818 * gets the command iocb associated with the response iocb and
2819 * calls the completion handler for the command iocb. If there
2820 * is no completion handler, the function will free the resources
2821 * associated with command iocb. If the response iocb is for
2822 * an already aborted command iocb, the status of the completion
2823 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2824 * This function always returns 1.
2825 **/
dea31012005-04-17 16:05:31 -05002826static int
James Smart2e0fef82007-06-17 19:56:36 -05002827lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002828 struct lpfc_iocbq *saveq)
2829{
James Smart2e0fef82007-06-17 19:56:36 -05002830 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002831 int rc = 1;
2832 unsigned long iflag;
2833
2834 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002835 if (phba->sli_rev == LPFC_SLI_REV4)
2836 spin_lock_irqsave(&pring->ring_lock, iflag);
2837 else
2838 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002839 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
Dick Kennedy341b2aa2017-11-07 12:59:02 -08002840 if (phba->sli_rev == LPFC_SLI_REV4)
2841 spin_unlock_irqrestore(&pring->ring_lock, iflag);
2842 else
2843 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart2e0fef82007-06-17 19:56:36 -05002844
dea31012005-04-17 16:05:31 -05002845 if (cmdiocbp) {
2846 if (cmdiocbp->iocb_cmpl) {
2847 /*
James Smartea2151b2008-09-07 11:52:10 -04002848 * If an ELS command failed send an event to mgmt
2849 * application.
2850 */
2851 if (saveq->iocb.ulpStatus &&
2852 (pring->ringno == LPFC_ELS_RING) &&
2853 (cmdiocbp->iocb.ulpCommand ==
2854 CMD_ELS_REQUEST64_CR))
2855 lpfc_send_els_failure_event(phba,
2856 cmdiocbp, saveq);
2857
2858 /*
dea31012005-04-17 16:05:31 -05002859 * Post all ELS completions to the worker thread.
2860 * All other are passed to the completion callback.
2861 */
2862 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002863 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2864 (cmdiocbp->iocb_flag &
2865 LPFC_DRIVER_ABORTED)) {
2866 spin_lock_irqsave(&phba->hbalock,
2867 iflag);
James Smart07951072007-04-25 09:51:38 -04002868 cmdiocbp->iocb_flag &=
2869 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002870 spin_unlock_irqrestore(&phba->hbalock,
2871 iflag);
James Smart07951072007-04-25 09:51:38 -04002872 saveq->iocb.ulpStatus =
2873 IOSTAT_LOCAL_REJECT;
2874 saveq->iocb.un.ulpWord[4] =
2875 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002876
2877 /* Firmware could still be in progress
2878 * of DMAing payload, so don't free data
2879 * buffer till after a hbeat.
2880 */
James Smart341af102010-01-26 23:07:37 -05002881 spin_lock_irqsave(&phba->hbalock,
2882 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002883 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002884 spin_unlock_irqrestore(&phba->hbalock,
2885 iflag);
2886 }
James Smart0f65ff62010-02-26 14:14:23 -05002887 if (phba->sli_rev == LPFC_SLI_REV4) {
2888 if (saveq->iocb_flag &
2889 LPFC_EXCHANGE_BUSY) {
2890 /* Set cmdiocb flag for the
2891 * exchange busy so sgl (xri)
2892 * will not be released until
2893 * the abort xri is received
2894 * from hba.
2895 */
2896 spin_lock_irqsave(
2897 &phba->hbalock, iflag);
2898 cmdiocbp->iocb_flag |=
2899 LPFC_EXCHANGE_BUSY;
2900 spin_unlock_irqrestore(
2901 &phba->hbalock, iflag);
2902 }
2903 if (cmdiocbp->iocb_flag &
2904 LPFC_DRIVER_ABORTED) {
2905 /*
2906 * Clear LPFC_DRIVER_ABORTED
2907 * bit in case it was driver
2908 * initiated abort.
2909 */
2910 spin_lock_irqsave(
2911 &phba->hbalock, iflag);
2912 cmdiocbp->iocb_flag &=
2913 ~LPFC_DRIVER_ABORTED;
2914 spin_unlock_irqrestore(
2915 &phba->hbalock, iflag);
2916 cmdiocbp->iocb.ulpStatus =
2917 IOSTAT_LOCAL_REJECT;
2918 cmdiocbp->iocb.un.ulpWord[4] =
2919 IOERR_ABORT_REQUESTED;
2920 /*
2921 * For SLI4, irsiocb contains
2922 * NO_XRI in sli_xritag, it
2923 * shall not affect releasing
2924 * sgl (xri) process.
2925 */
2926 saveq->iocb.ulpStatus =
2927 IOSTAT_LOCAL_REJECT;
2928 saveq->iocb.un.ulpWord[4] =
2929 IOERR_SLI_ABORTED;
2930 spin_lock_irqsave(
2931 &phba->hbalock, iflag);
2932 saveq->iocb_flag |=
2933 LPFC_DELAY_MEM_FREE;
2934 spin_unlock_irqrestore(
2935 &phba->hbalock, iflag);
2936 }
James Smart07951072007-04-25 09:51:38 -04002937 }
dea31012005-04-17 16:05:31 -05002938 }
James Smart2e0fef82007-06-17 19:56:36 -05002939 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002940 } else
2941 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002942 } else {
2943 /*
2944 * Unknown initiating command based on the response iotag.
2945 * This could be the case on the ELS ring because of
2946 * lpfc_els_abort().
2947 */
2948 if (pring->ringno != LPFC_ELS_RING) {
2949 /*
2950 * Ring <ringno> handler: unexpected completion IoTag
2951 * <IoTag>
2952 */
James Smarta257bf92009-04-06 18:48:10 -04002953 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002954 "0322 Ring %d handler: "
2955 "unexpected completion IoTag x%x "
2956 "Data: x%x x%x x%x x%x\n",
2957 pring->ringno,
2958 saveq->iocb.ulpIoTag,
2959 saveq->iocb.ulpStatus,
2960 saveq->iocb.un.ulpWord[4],
2961 saveq->iocb.ulpCommand,
2962 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002963 }
2964 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002965
dea31012005-04-17 16:05:31 -05002966 return rc;
2967}
2968
James Smarte59058c2008-08-24 21:49:00 -04002969/**
James Smart3621a712009-04-06 18:47:14 -04002970 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002971 * @phba: Pointer to HBA context object.
2972 * @pring: Pointer to driver SLI ring object.
2973 *
2974 * This function is called from the iocb ring event handlers when
2975 * put pointer is ahead of the get pointer for a ring. This function signal
2976 * an error attention condition to the worker thread and the worker
2977 * thread will transition the HBA to offline state.
2978 **/
James Smart2e0fef82007-06-17 19:56:36 -05002979static void
2980lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002981{
James Smart34b02dc2008-08-24 21:49:55 -04002982 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002983 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002984 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002985 * rsp ring <portRspMax>
2986 */
2987 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002988 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002989 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002990 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002991 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002992
James Smart2e0fef82007-06-17 19:56:36 -05002993 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002994
2995 /*
2996 * All error attention handlers are posted to
2997 * worker thread
2998 */
2999 phba->work_ha |= HA_ERATT;
3000 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05003001
James Smart5e9d9b82008-06-14 22:52:53 -04003002 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003003
3004 return;
3005}
3006
James Smarte59058c2008-08-24 21:49:00 -04003007/**
James Smart3621a712009-04-06 18:47:14 -04003008 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04003009 * @ptr: Pointer to address of HBA context object.
3010 *
3011 * This function is invoked by the Error Attention polling timer when the
3012 * timer times out. It will check the SLI Error Attention register for
3013 * possible attention events. If so, it will post an Error Attention event
3014 * and wake up worker thread to process it. Otherwise, it will set up the
3015 * Error Attention polling timer for the next poll.
3016 **/
Kees Cookf22eb4d2017-09-06 20:24:26 -07003017void lpfc_poll_eratt(struct timer_list *t)
James Smart93996272008-08-24 21:50:30 -04003018{
3019 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04003020 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04003021 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04003022
Kees Cookf22eb4d2017-09-06 20:24:26 -07003023 phba = from_timer(phba, t, eratt_poll);
James Smart93996272008-08-24 21:50:30 -04003024
James Smartaa6fbb72012-08-03 12:36:03 -04003025 /* Here we will also keep track of interrupts per sec of the hba */
3026 sli_intr = phba->sli.slistat.sli_intr;
3027
3028 if (phba->sli.slistat.sli_prev_intr > sli_intr)
3029 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
3030 sli_intr);
3031 else
3032 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
3033
James Smart65791f12016-07-06 12:35:56 -07003034 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
3035 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04003036 phba->sli.slistat.sli_ips = cnt;
3037
3038 phba->sli.slistat.sli_prev_intr = sli_intr;
3039
James Smart93996272008-08-24 21:50:30 -04003040 /* Check chip HA register for error event */
3041 eratt = lpfc_sli_check_eratt(phba);
3042
3043 if (eratt)
3044 /* Tell the worker thread there is work to do */
3045 lpfc_worker_wake_up(phba);
3046 else
3047 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04003048 mod_timer(&phba->eratt_poll,
3049 jiffies +
James Smart65791f12016-07-06 12:35:56 -07003050 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04003051 return;
3052}
3053
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003054
James Smarte59058c2008-08-24 21:49:00 -04003055/**
James Smart3621a712009-04-06 18:47:14 -04003056 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04003057 * @phba: Pointer to HBA context object.
3058 * @pring: Pointer to driver SLI ring object.
3059 * @mask: Host attention register mask for this ring.
3060 *
3061 * This function is called from the interrupt context when there is a ring
3062 * event for the fcp ring. The caller does not hold any lock.
3063 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003064 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04003065 * LE bit set. The function will call the completion handler of the command iocb
3066 * if the response iocb indicates a completion for a command iocb or it is
3067 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
3068 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05003069 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04003070 * to check it explicitly.
3071 */
3072int
James Smart2e0fef82007-06-17 19:56:36 -05003073lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
3074 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003075{
James Smart34b02dc2008-08-24 21:49:55 -04003076 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05003077 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003078 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05003079 struct lpfc_iocbq *cmdiocbq = NULL;
3080 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003081 uint32_t status;
3082 uint32_t portRspPut, portRspMax;
3083 int rc = 1;
3084 lpfc_iocb_type type;
3085 unsigned long iflag;
3086 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003087
James Smart2e0fef82007-06-17 19:56:36 -05003088 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003089 pring->stats.iocb_event++;
3090
dea31012005-04-17 16:05:31 -05003091 /*
3092 * The next available response entry should never exceed the maximum
3093 * entries. If it does, treat it as an adapter hardware error.
3094 */
James Smart7e56aa22012-08-03 12:35:34 -04003095 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003096 portRspPut = le32_to_cpu(pgp->rspPutInx);
3097 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003098 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003099 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003100 return 1;
3101 }
James Smart45ed1192009-10-02 15:17:02 -04003102 if (phba->fcp_ring_in_use) {
3103 spin_unlock_irqrestore(&phba->hbalock, iflag);
3104 return 1;
3105 } else
3106 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003107
3108 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003109 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003110 /*
3111 * Fetch an entry off the ring and copy it into a local data
3112 * structure. The copy involves a byte-swap since the
3113 * network byte order and pci byte orders are different.
3114 */
James Smarted957682007-06-17 19:56:37 -05003115 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003116 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003117
James Smart7e56aa22012-08-03 12:35:34 -04003118 if (++pring->sli.sli3.rspidx >= portRspMax)
3119 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003120
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003121 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3122 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003123 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003124 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003125 irsp = &rspiocbq.iocb;
3126
dea31012005-04-17 16:05:31 -05003127 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3128 pring->stats.iocb_rsp++;
3129 rsp_cmpl++;
3130
3131 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003132 /*
3133 * If resource errors reported from HBA, reduce
3134 * queuedepths of the SCSI device.
3135 */
3136 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003137 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3138 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003139 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003140 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003141 spin_lock_irqsave(&phba->hbalock, iflag);
3142 }
3143
dea31012005-04-17 16:05:31 -05003144 /* Rsp ring <ringno> error: IOCB */
3145 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003146 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003147 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003148 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003149 irsp->un.ulpWord[0],
3150 irsp->un.ulpWord[1],
3151 irsp->un.ulpWord[2],
3152 irsp->un.ulpWord[3],
3153 irsp->un.ulpWord[4],
3154 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003155 *(uint32_t *)&irsp->un1,
3156 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003157 }
3158
3159 switch (type) {
3160 case LPFC_ABORT_IOCB:
3161 case LPFC_SOL_IOCB:
3162 /*
3163 * Idle exchange closed via ABTS from port. No iocb
3164 * resources need to be recovered.
3165 */
3166 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003167 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003168 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003169 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003170 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003171 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003172 break;
3173 }
3174
James Bottomley604a3e32005-10-29 10:28:33 -05003175 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3176 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003177 if (unlikely(!cmdiocbq))
3178 break;
3179 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3180 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3181 if (cmdiocbq->iocb_cmpl) {
3182 spin_unlock_irqrestore(&phba->hbalock, iflag);
3183 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3184 &rspiocbq);
3185 spin_lock_irqsave(&phba->hbalock, iflag);
3186 }
dea31012005-04-17 16:05:31 -05003187 break;
James Smarta4bc3372006-12-02 13:34:16 -05003188 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003189 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003190 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003191 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003192 break;
dea31012005-04-17 16:05:31 -05003193 default:
3194 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3195 char adaptermsg[LPFC_MAX_ADPTMSG];
3196 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3197 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3198 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003199 dev_warn(&((phba->pcidev)->dev),
3200 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003201 phba->brd_no, adaptermsg);
3202 } else {
3203 /* Unknown IOCB command */
3204 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003205 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003206 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003207 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003208 irsp->ulpStatus,
3209 irsp->ulpIoTag,
3210 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003211 }
3212 break;
3213 }
3214
3215 /*
3216 * The response IOCB has been processed. Update the ring
3217 * pointer in SLIM. If the port response put pointer has not
3218 * been updated, sync the pgp->rspPutInx and fetch the new port
3219 * response put pointer.
3220 */
James Smart7e56aa22012-08-03 12:35:34 -04003221 writel(pring->sli.sli3.rspidx,
3222 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003223
James Smart7e56aa22012-08-03 12:35:34 -04003224 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003225 portRspPut = le32_to_cpu(pgp->rspPutInx);
3226 }
3227
3228 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3229 pring->stats.iocb_rsp_full++;
3230 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3231 writel(status, phba->CAregaddr);
3232 readl(phba->CAregaddr);
3233 }
3234 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3235 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3236 pring->stats.iocb_cmd_empty++;
3237
3238 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003239 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003240 lpfc_sli_resume_iocb(phba, pring);
3241
3242 if ((pring->lpfc_sli_cmd_available))
3243 (pring->lpfc_sli_cmd_available) (phba, pring);
3244
3245 }
3246
James Smart45ed1192009-10-02 15:17:02 -04003247 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003248 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003249 return rc;
3250}
3251
James Smarte59058c2008-08-24 21:49:00 -04003252/**
James Smart3772a992009-05-22 14:50:54 -04003253 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3254 * @phba: Pointer to HBA context object.
3255 * @pring: Pointer to driver SLI ring object.
3256 * @rspiocbp: Pointer to driver response IOCB object.
3257 *
3258 * This function is called from the worker thread when there is a slow-path
3259 * response IOCB to process. This function chains all the response iocbs until
3260 * seeing the iocb with the LE bit set. The function will call
3261 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3262 * completion of a command iocb. The function will call the
3263 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3264 * The function frees the resources or calls the completion handler if this
3265 * iocb is an abort completion. The function returns NULL when the response
3266 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3267 * this function shall chain the iocb on to the iocb_continueq and return the
3268 * response iocb passed in.
3269 **/
3270static struct lpfc_iocbq *
3271lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3272 struct lpfc_iocbq *rspiocbp)
3273{
3274 struct lpfc_iocbq *saveq;
3275 struct lpfc_iocbq *cmdiocbp;
3276 struct lpfc_iocbq *next_iocb;
3277 IOCB_t *irsp = NULL;
3278 uint32_t free_saveq;
3279 uint8_t iocb_cmd_type;
3280 lpfc_iocb_type type;
3281 unsigned long iflag;
3282 int rc;
3283
3284 spin_lock_irqsave(&phba->hbalock, iflag);
3285 /* First add the response iocb to the countinueq list */
3286 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3287 pring->iocb_continueq_cnt++;
3288
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003289 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003290 irsp = &rspiocbp->iocb;
3291 if (irsp->ulpLe) {
3292 /*
3293 * By default, the driver expects to free all resources
3294 * associated with this iocb completion.
3295 */
3296 free_saveq = 1;
3297 saveq = list_get_first(&pring->iocb_continueq,
3298 struct lpfc_iocbq, list);
3299 irsp = &(saveq->iocb);
3300 list_del_init(&pring->iocb_continueq);
3301 pring->iocb_continueq_cnt = 0;
3302
3303 pring->stats.iocb_rsp++;
3304
3305 /*
3306 * If resource errors reported from HBA, reduce
3307 * queuedepths of the SCSI device.
3308 */
3309 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003310 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3311 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003312 spin_unlock_irqrestore(&phba->hbalock, iflag);
3313 phba->lpfc_rampdown_queue_depth(phba);
3314 spin_lock_irqsave(&phba->hbalock, iflag);
3315 }
3316
3317 if (irsp->ulpStatus) {
3318 /* Rsp ring <ringno> error: IOCB */
3319 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3320 "0328 Rsp Ring %d error: "
3321 "IOCB Data: "
3322 "x%x x%x x%x x%x "
3323 "x%x x%x x%x x%x "
3324 "x%x x%x x%x x%x "
3325 "x%x x%x x%x x%x\n",
3326 pring->ringno,
3327 irsp->un.ulpWord[0],
3328 irsp->un.ulpWord[1],
3329 irsp->un.ulpWord[2],
3330 irsp->un.ulpWord[3],
3331 irsp->un.ulpWord[4],
3332 irsp->un.ulpWord[5],
3333 *(((uint32_t *) irsp) + 6),
3334 *(((uint32_t *) irsp) + 7),
3335 *(((uint32_t *) irsp) + 8),
3336 *(((uint32_t *) irsp) + 9),
3337 *(((uint32_t *) irsp) + 10),
3338 *(((uint32_t *) irsp) + 11),
3339 *(((uint32_t *) irsp) + 12),
3340 *(((uint32_t *) irsp) + 13),
3341 *(((uint32_t *) irsp) + 14),
3342 *(((uint32_t *) irsp) + 15));
3343 }
3344
3345 /*
3346 * Fetch the IOCB command type and call the correct completion
3347 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3348 * get freed back to the lpfc_iocb_list by the discovery
3349 * kernel thread.
3350 */
3351 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3352 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3353 switch (type) {
3354 case LPFC_SOL_IOCB:
3355 spin_unlock_irqrestore(&phba->hbalock, iflag);
3356 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3357 spin_lock_irqsave(&phba->hbalock, iflag);
3358 break;
3359
3360 case LPFC_UNSOL_IOCB:
3361 spin_unlock_irqrestore(&phba->hbalock, iflag);
3362 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3363 spin_lock_irqsave(&phba->hbalock, iflag);
3364 if (!rc)
3365 free_saveq = 0;
3366 break;
3367
3368 case LPFC_ABORT_IOCB:
3369 cmdiocbp = NULL;
3370 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3371 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3372 saveq);
3373 if (cmdiocbp) {
3374 /* Call the specified completion routine */
3375 if (cmdiocbp->iocb_cmpl) {
3376 spin_unlock_irqrestore(&phba->hbalock,
3377 iflag);
3378 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3379 saveq);
3380 spin_lock_irqsave(&phba->hbalock,
3381 iflag);
3382 } else
3383 __lpfc_sli_release_iocbq(phba,
3384 cmdiocbp);
3385 }
3386 break;
3387
3388 case LPFC_UNKNOWN_IOCB:
3389 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3390 char adaptermsg[LPFC_MAX_ADPTMSG];
3391 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3392 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3393 MAX_MSG_DATA);
3394 dev_warn(&((phba->pcidev)->dev),
3395 "lpfc%d: %s\n",
3396 phba->brd_no, adaptermsg);
3397 } else {
3398 /* Unknown IOCB command */
3399 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3400 "0335 Unknown IOCB "
3401 "command Data: x%x "
3402 "x%x x%x x%x\n",
3403 irsp->ulpCommand,
3404 irsp->ulpStatus,
3405 irsp->ulpIoTag,
3406 irsp->ulpContext);
3407 }
3408 break;
3409 }
3410
3411 if (free_saveq) {
3412 list_for_each_entry_safe(rspiocbp, next_iocb,
3413 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003414 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003415 __lpfc_sli_release_iocbq(phba, rspiocbp);
3416 }
3417 __lpfc_sli_release_iocbq(phba, saveq);
3418 }
3419 rspiocbp = NULL;
3420 }
3421 spin_unlock_irqrestore(&phba->hbalock, iflag);
3422 return rspiocbp;
3423}
3424
3425/**
3426 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003427 * @phba: Pointer to HBA context object.
3428 * @pring: Pointer to driver SLI ring object.
3429 * @mask: Host attention register mask for this ring.
3430 *
James Smart3772a992009-05-22 14:50:54 -04003431 * This routine wraps the actual slow_ring event process routine from the
3432 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003433 **/
James Smart3772a992009-05-22 14:50:54 -04003434void
James Smart2e0fef82007-06-17 19:56:36 -05003435lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3436 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003437{
James Smart3772a992009-05-22 14:50:54 -04003438 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3439}
3440
3441/**
3442 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3443 * @phba: Pointer to HBA context object.
3444 * @pring: Pointer to driver SLI ring object.
3445 * @mask: Host attention register mask for this ring.
3446 *
3447 * This function is called from the worker thread when there is a ring event
3448 * for non-fcp rings. The caller does not hold any lock. The function will
3449 * remove each response iocb in the response ring and calls the handle
3450 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3451 **/
3452static void
3453lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3454 struct lpfc_sli_ring *pring, uint32_t mask)
3455{
James Smart34b02dc2008-08-24 21:49:55 -04003456 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003457 IOCB_t *entry;
3458 IOCB_t *irsp = NULL;
3459 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003460 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003461 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003462 uint32_t status;
dea31012005-04-17 16:05:31 -05003463
James Smart34b02dc2008-08-24 21:49:55 -04003464 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003465 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003466 pring->stats.iocb_event++;
3467
dea31012005-04-17 16:05:31 -05003468 /*
3469 * The next available response entry should never exceed the maximum
3470 * entries. If it does, treat it as an adapter hardware error.
3471 */
James Smart7e56aa22012-08-03 12:35:34 -04003472 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003473 portRspPut = le32_to_cpu(pgp->rspPutInx);
3474 if (portRspPut >= portRspMax) {
3475 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003476 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003477 * rsp ring <portRspMax>
3478 */
James Smarted957682007-06-17 19:56:37 -05003479 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003480 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003481 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003482 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003483
James Smart2e0fef82007-06-17 19:56:36 -05003484 phba->link_state = LPFC_HBA_ERROR;
3485 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003486
3487 phba->work_hs = HS_FFER3;
3488 lpfc_handle_eratt(phba);
3489
James Smart3772a992009-05-22 14:50:54 -04003490 return;
dea31012005-04-17 16:05:31 -05003491 }
3492
3493 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003494 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003495 /*
3496 * Build a completion list and call the appropriate handler.
3497 * The process is to get the next available response iocb, get
3498 * a free iocb from the list, copy the response data into the
3499 * free iocb, insert to the continuation list, and update the
3500 * next response index to slim. This process makes response
3501 * iocb's in the ring available to DMA as fast as possible but
3502 * pays a penalty for a copy operation. Since the iocb is
3503 * only 32 bytes, this penalty is considered small relative to
3504 * the PCI reads for register values and a slim write. When
3505 * the ulpLe field is set, the entire Command has been
3506 * received.
3507 */
James Smarted957682007-06-17 19:56:37 -05003508 entry = lpfc_resp_iocb(phba, pring);
3509
James Smart858c9f62007-06-17 19:56:39 -05003510 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003511 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003512 if (rspiocbp == NULL) {
3513 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003514 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003515 break;
3516 }
3517
James Smarted957682007-06-17 19:56:37 -05003518 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3519 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003520 irsp = &rspiocbp->iocb;
3521
James Smart7e56aa22012-08-03 12:35:34 -04003522 if (++pring->sli.sli3.rspidx >= portRspMax)
3523 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003524
James Smarta58cbd52007-08-02 11:09:43 -04003525 if (pring->ringno == LPFC_ELS_RING) {
3526 lpfc_debugfs_slow_ring_trc(phba,
3527 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3528 *(((uint32_t *) irsp) + 4),
3529 *(((uint32_t *) irsp) + 6),
3530 *(((uint32_t *) irsp) + 7));
3531 }
3532
James Smart7e56aa22012-08-03 12:35:34 -04003533 writel(pring->sli.sli3.rspidx,
3534 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003535
James Smart3772a992009-05-22 14:50:54 -04003536 spin_unlock_irqrestore(&phba->hbalock, iflag);
3537 /* Handle the response IOCB */
3538 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3539 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003540
3541 /*
3542 * If the port response put pointer has not been updated, sync
3543 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3544 * response put pointer.
3545 */
James Smart7e56aa22012-08-03 12:35:34 -04003546 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003547 portRspPut = le32_to_cpu(pgp->rspPutInx);
3548 }
James Smart7e56aa22012-08-03 12:35:34 -04003549 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003550
James Smart92d7f7b2007-06-17 19:56:38 -05003551 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003552 /* At least one response entry has been freed */
3553 pring->stats.iocb_rsp_full++;
3554 /* SET RxRE_RSP in Chip Att register */
3555 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3556 writel(status, phba->CAregaddr);
3557 readl(phba->CAregaddr); /* flush */
3558 }
3559 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3560 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3561 pring->stats.iocb_cmd_empty++;
3562
3563 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003564 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003565 lpfc_sli_resume_iocb(phba, pring);
3566
3567 if ((pring->lpfc_sli_cmd_available))
3568 (pring->lpfc_sli_cmd_available) (phba, pring);
3569
3570 }
3571
James Smart2e0fef82007-06-17 19:56:36 -05003572 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003573 return;
dea31012005-04-17 16:05:31 -05003574}
3575
James Smarte59058c2008-08-24 21:49:00 -04003576/**
James Smart4f774512009-05-22 14:52:35 -04003577 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3578 * @phba: Pointer to HBA context object.
3579 * @pring: Pointer to driver SLI ring object.
3580 * @mask: Host attention register mask for this ring.
3581 *
3582 * This function is called from the worker thread when there is a pending
3583 * ELS response iocb on the driver internal slow-path response iocb worker
3584 * queue. The caller does not hold any lock. The function will remove each
3585 * response iocb from the response worker queue and calls the handle
3586 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3587 **/
3588static void
3589lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3590 struct lpfc_sli_ring *pring, uint32_t mask)
3591{
3592 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003593 struct hbq_dmabuf *dmabuf;
3594 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003595 unsigned long iflag;
3596
James Smart45ed1192009-10-02 15:17:02 -04003597 spin_lock_irqsave(&phba->hbalock, iflag);
3598 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3599 spin_unlock_irqrestore(&phba->hbalock, iflag);
3600 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003601 /* Get the response iocb from the head of work queue */
3602 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003603 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003604 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003605 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003606
3607 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3608 case CQE_CODE_COMPL_WQE:
3609 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3610 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003611 /* Translate ELS WCQE to response IOCBQ */
3612 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3613 irspiocbq);
3614 if (irspiocbq)
3615 lpfc_sli_sp_handle_rspiocb(phba, pring,
3616 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003617 break;
3618 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003619 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003620 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3621 cq_event);
3622 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3623 break;
3624 default:
3625 break;
3626 }
James Smart4f774512009-05-22 14:52:35 -04003627 }
3628}
3629
3630/**
James Smart3621a712009-04-06 18:47:14 -04003631 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003632 * @phba: Pointer to HBA context object.
3633 * @pring: Pointer to driver SLI ring object.
3634 *
3635 * This function aborts all iocbs in the given ring and frees all the iocb
3636 * objects in txq. This function issues an abort iocb for all the iocb commands
3637 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3638 * the return of this function. The caller is not required to hold any locks.
3639 **/
James Smart2e0fef82007-06-17 19:56:36 -05003640void
dea31012005-04-17 16:05:31 -05003641lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3642{
James Smart2534ba72007-04-25 09:52:20 -04003643 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003644 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003645
James Smart92d7f7b2007-06-17 19:56:38 -05003646 if (pring->ringno == LPFC_ELS_RING) {
3647 lpfc_fabric_abort_hba(phba);
3648 }
3649
dea31012005-04-17 16:05:31 -05003650 /* Error everything on txq and txcmplq
3651 * First do the txq.
3652 */
James Smartdb55fba2014-04-04 13:52:02 -04003653 if (phba->sli_rev >= LPFC_SLI_REV4) {
3654 spin_lock_irq(&pring->ring_lock);
3655 list_splice_init(&pring->txq, &completions);
3656 pring->txq_cnt = 0;
3657 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003658
James Smartdb55fba2014-04-04 13:52:02 -04003659 spin_lock_irq(&phba->hbalock);
3660 /* Next issue ABTS for everything on the txcmplq */
3661 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3662 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3663 spin_unlock_irq(&phba->hbalock);
3664 } else {
3665 spin_lock_irq(&phba->hbalock);
3666 list_splice_init(&pring->txq, &completions);
3667 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003668
James Smartdb55fba2014-04-04 13:52:02 -04003669 /* Next issue ABTS for everything on the txcmplq */
3670 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3671 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3672 spin_unlock_irq(&phba->hbalock);
3673 }
James Smart2534ba72007-04-25 09:52:20 -04003674
James Smarta257bf92009-04-06 18:48:10 -04003675 /* Cancel all the IOCBs from the completions list */
3676 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3677 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003678}
3679
James Smarte59058c2008-08-24 21:49:00 -04003680/**
James Smart895427b2017-02-12 13:52:30 -08003681 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3682 * @phba: Pointer to HBA context object.
3683 * @pring: Pointer to driver SLI ring object.
3684 *
3685 * This function aborts all iocbs in the given ring and frees all the iocb
3686 * objects in txq. This function issues an abort iocb for all the iocb commands
3687 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3688 * the return of this function. The caller is not required to hold any locks.
3689 **/
3690void
3691lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3692{
3693 LIST_HEAD(completions);
3694 struct lpfc_iocbq *iocb, *next_iocb;
3695
3696 if (pring->ringno == LPFC_ELS_RING)
3697 lpfc_fabric_abort_hba(phba);
3698
3699 spin_lock_irq(&phba->hbalock);
3700 /* Next issue ABTS for everything on the txcmplq */
3701 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3702 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3703 spin_unlock_irq(&phba->hbalock);
3704}
3705
3706
3707/**
James Smartdb55fba2014-04-04 13:52:02 -04003708 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3709 * @phba: Pointer to HBA context object.
3710 * @pring: Pointer to driver SLI ring object.
3711 *
3712 * This function aborts all iocbs in FCP rings and frees all the iocb
3713 * objects in txq. This function issues an abort iocb for all the iocb commands
3714 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3715 * the return of this function. The caller is not required to hold any locks.
3716 **/
3717void
3718lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3719{
3720 struct lpfc_sli *psli = &phba->sli;
3721 struct lpfc_sli_ring *pring;
3722 uint32_t i;
3723
3724 /* Look on all the FCP Rings for the iotag */
3725 if (phba->sli_rev >= LPFC_SLI_REV4) {
3726 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003727 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003728 lpfc_sli_abort_iocb_ring(phba, pring);
3729 }
3730 } else {
James Smart895427b2017-02-12 13:52:30 -08003731 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003732 lpfc_sli_abort_iocb_ring(phba, pring);
3733 }
3734}
3735
James Smart895427b2017-02-12 13:52:30 -08003736/**
3737 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3738 * @phba: Pointer to HBA context object.
3739 *
3740 * This function aborts all wqes in NVME rings. This function issues an
3741 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3742 * the txcmplq is not guaranteed to complete before the return of this
3743 * function. The caller is not required to hold any locks.
3744 **/
3745void
3746lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3747{
3748 struct lpfc_sli_ring *pring;
3749 uint32_t i;
3750
3751 if (phba->sli_rev < LPFC_SLI_REV4)
3752 return;
3753
3754 /* Abort all IO on each NVME ring. */
3755 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3756 pring = phba->sli4_hba.nvme_wq[i]->pring;
3757 lpfc_sli_abort_wqe_ring(phba, pring);
3758 }
3759}
3760
James Smartdb55fba2014-04-04 13:52:02 -04003761
3762/**
James Smart3621a712009-04-06 18:47:14 -04003763 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003764 * @phba: Pointer to HBA context object.
3765 *
3766 * This function flushes all iocbs in the fcp ring and frees all the iocb
3767 * objects in txq and txcmplq. This function will not issue abort iocbs
3768 * for all the iocb commands in txcmplq, they will just be returned with
3769 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3770 * slot has been permanently disabled.
3771 **/
3772void
3773lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3774{
3775 LIST_HEAD(txq);
3776 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003777 struct lpfc_sli *psli = &phba->sli;
3778 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003779 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08003780 struct lpfc_iocbq *piocb, *next_iocb;
James Smarta8e497d2008-08-24 21:50:11 -04003781
3782 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003783 /* Indicate the I/O queues are flushed */
3784 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003785 spin_unlock_irq(&phba->hbalock);
3786
James Smartdb55fba2014-04-04 13:52:02 -04003787 /* Look on all the FCP Rings for the iotag */
3788 if (phba->sli_rev >= LPFC_SLI_REV4) {
3789 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003790 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003791
James Smartdb55fba2014-04-04 13:52:02 -04003792 spin_lock_irq(&pring->ring_lock);
3793 /* Retrieve everything on txq */
3794 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08003795 list_for_each_entry_safe(piocb, next_iocb,
3796 &pring->txcmplq, list)
3797 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04003798 /* Retrieve everything on the txcmplq */
3799 list_splice_init(&pring->txcmplq, &txcmplq);
3800 pring->txq_cnt = 0;
3801 pring->txcmplq_cnt = 0;
3802 spin_unlock_irq(&pring->ring_lock);
3803
3804 /* Flush the txq */
3805 lpfc_sli_cancel_iocbs(phba, &txq,
3806 IOSTAT_LOCAL_REJECT,
3807 IOERR_SLI_DOWN);
3808 /* Flush the txcmpq */
3809 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3810 IOSTAT_LOCAL_REJECT,
3811 IOERR_SLI_DOWN);
3812 }
3813 } else {
James Smart895427b2017-02-12 13:52:30 -08003814 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003815
3816 spin_lock_irq(&phba->hbalock);
3817 /* Retrieve everything on txq */
3818 list_splice_init(&pring->txq, &txq);
James Smartc1dd9112018-01-30 15:58:57 -08003819 list_for_each_entry_safe(piocb, next_iocb,
3820 &pring->txcmplq, list)
3821 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smartdb55fba2014-04-04 13:52:02 -04003822 /* Retrieve everything on the txcmplq */
3823 list_splice_init(&pring->txcmplq, &txcmplq);
3824 pring->txq_cnt = 0;
3825 pring->txcmplq_cnt = 0;
3826 spin_unlock_irq(&phba->hbalock);
3827
3828 /* Flush the txq */
3829 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3830 IOERR_SLI_DOWN);
3831 /* Flush the txcmpq */
3832 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3833 IOERR_SLI_DOWN);
3834 }
James Smarta8e497d2008-08-24 21:50:11 -04003835}
3836
3837/**
James Smart895427b2017-02-12 13:52:30 -08003838 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3839 * @phba: Pointer to HBA context object.
3840 *
3841 * This function flushes all wqes in the nvme rings and frees all resources
3842 * in the txcmplq. This function does not issue abort wqes for the IO
3843 * commands in txcmplq, they will just be returned with
3844 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3845 * slot has been permanently disabled.
3846 **/
3847void
3848lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3849{
3850 LIST_HEAD(txcmplq);
3851 struct lpfc_sli_ring *pring;
3852 uint32_t i;
James Smartc1dd9112018-01-30 15:58:57 -08003853 struct lpfc_iocbq *piocb, *next_iocb;
James Smart895427b2017-02-12 13:52:30 -08003854
3855 if (phba->sli_rev < LPFC_SLI_REV4)
3856 return;
3857
3858 /* Hint to other driver operations that a flush is in progress. */
3859 spin_lock_irq(&phba->hbalock);
3860 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3861 spin_unlock_irq(&phba->hbalock);
3862
3863 /* Cycle through all NVME rings and complete each IO with
3864 * a local driver reason code. This is a flush so no
3865 * abort exchange to FW.
3866 */
3867 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3868 pring = phba->sli4_hba.nvme_wq[i]->pring;
3869
James Smart895427b2017-02-12 13:52:30 -08003870 spin_lock_irq(&pring->ring_lock);
James Smartc1dd9112018-01-30 15:58:57 -08003871 list_for_each_entry_safe(piocb, next_iocb,
3872 &pring->txcmplq, list)
3873 piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
3874 /* Retrieve everything on the txcmplq */
James Smart895427b2017-02-12 13:52:30 -08003875 list_splice_init(&pring->txcmplq, &txcmplq);
3876 pring->txcmplq_cnt = 0;
3877 spin_unlock_irq(&pring->ring_lock);
3878
3879 /* Flush the txcmpq &&&PAE */
3880 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3881 IOSTAT_LOCAL_REJECT,
3882 IOERR_SLI_DOWN);
3883 }
3884}
3885
3886/**
James Smart3772a992009-05-22 14:50:54 -04003887 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003888 * @phba: Pointer to HBA context object.
3889 * @mask: Bit mask to be checked.
3890 *
3891 * This function reads the host status register and compares
3892 * with the provided bit mask to check if HBA completed
3893 * the restart. This function will wait in a loop for the
3894 * HBA to complete restart. If the HBA does not restart within
3895 * 15 iterations, the function will reset the HBA again. The
3896 * function returns 1 when HBA fail to restart otherwise returns
3897 * zero.
3898 **/
James Smart3772a992009-05-22 14:50:54 -04003899static int
3900lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003901{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003902 uint32_t status;
3903 int i = 0;
3904 int retval = 0;
dea31012005-04-17 16:05:31 -05003905
Jamie Wellnitz41415862006-02-28 19:25:27 -05003906 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003907 if (lpfc_readl(phba->HSregaddr, &status))
3908 return 1;
dea31012005-04-17 16:05:31 -05003909
Jamie Wellnitz41415862006-02-28 19:25:27 -05003910 /*
3911 * Check status register every 100ms for 5 retries, then every
3912 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3913 * every 2.5 sec for 4.
3914 * Break our of the loop if errors occurred during init.
3915 */
3916 while (((status & mask) != mask) &&
3917 !(status & HS_FFERM) &&
3918 i++ < 20) {
dea31012005-04-17 16:05:31 -05003919
Jamie Wellnitz41415862006-02-28 19:25:27 -05003920 if (i <= 5)
3921 msleep(10);
3922 else if (i <= 10)
3923 msleep(500);
3924 else
3925 msleep(2500);
dea31012005-04-17 16:05:31 -05003926
Jamie Wellnitz41415862006-02-28 19:25:27 -05003927 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003928 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003929 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003930 lpfc_sli_brdrestart(phba);
3931 }
3932 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003933 if (lpfc_readl(phba->HSregaddr, &status)) {
3934 retval = 1;
3935 break;
3936 }
dea31012005-04-17 16:05:31 -05003937 }
dea31012005-04-17 16:05:31 -05003938
Jamie Wellnitz41415862006-02-28 19:25:27 -05003939 /* Check to see if any errors occurred during init */
3940 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003941 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3942 "2751 Adapter failed to restart, "
3943 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3944 status,
3945 readl(phba->MBslimaddr + 0xa8),
3946 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003947 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003948 retval = 1;
3949 }
dea31012005-04-17 16:05:31 -05003950
Jamie Wellnitz41415862006-02-28 19:25:27 -05003951 return retval;
dea31012005-04-17 16:05:31 -05003952}
3953
James Smartda0436e2009-05-22 14:51:39 -04003954/**
3955 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3956 * @phba: Pointer to HBA context object.
3957 * @mask: Bit mask to be checked.
3958 *
3959 * This function checks the host status register to check if HBA is
3960 * ready. This function will wait in a loop for the HBA to be ready
3961 * If the HBA is not ready , the function will will reset the HBA PCI
3962 * function again. The function returns 1 when HBA fail to be ready
3963 * otherwise returns zero.
3964 **/
3965static int
3966lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3967{
3968 uint32_t status;
3969 int retval = 0;
3970
3971 /* Read the HBA Host Status Register */
3972 status = lpfc_sli4_post_status_check(phba);
3973
3974 if (status) {
3975 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3976 lpfc_sli_brdrestart(phba);
3977 status = lpfc_sli4_post_status_check(phba);
3978 }
3979
3980 /* Check to see if any errors occurred during init */
3981 if (status) {
3982 phba->link_state = LPFC_HBA_ERROR;
3983 retval = 1;
3984 } else
3985 phba->sli4_hba.intr_enable = 0;
3986
3987 return retval;
3988}
3989
3990/**
3991 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3992 * @phba: Pointer to HBA context object.
3993 * @mask: Bit mask to be checked.
3994 *
3995 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3996 * from the API jump table function pointer from the lpfc_hba struct.
3997 **/
3998int
3999lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
4000{
4001 return phba->lpfc_sli_brdready(phba, mask);
4002}
4003
James Smart92908312006-03-07 15:04:13 -05004004#define BARRIER_TEST_PATTERN (0xdeadbeef)
4005
James Smarte59058c2008-08-24 21:49:00 -04004006/**
James Smart3621a712009-04-06 18:47:14 -04004007 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04004008 * @phba: Pointer to HBA context object.
4009 *
James Smart1b511972011-12-13 13:23:09 -05004010 * This function is called before resetting an HBA. This function is called
4011 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04004012 **/
James Smart2e0fef82007-06-17 19:56:36 -05004013void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05004014{
James Smart65a29c12006-07-06 15:50:50 -04004015 uint32_t __iomem *resp_buf;
4016 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05004017 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05004018 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05004019 int i;
4020 uint8_t hdrtype;
4021
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01004022 lockdep_assert_held(&phba->hbalock);
4023
James Smart92908312006-03-07 15:04:13 -05004024 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
4025 if (hdrtype != 0x80 ||
4026 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
4027 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
4028 return;
4029
4030 /*
4031 * Tell the other part of the chip to suspend temporarily all
4032 * its DMA activity.
4033 */
James Smart65a29c12006-07-06 15:50:50 -04004034 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004035
4036 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05004037 if (lpfc_readl(phba->HCregaddr, &hc_copy))
4038 return;
James Smart92908312006-03-07 15:04:13 -05004039 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
4040 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004041 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004042
James Smart9940b972011-03-11 16:06:12 -05004043 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4044 return;
4045 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05004046 /* Clear Chip error bit */
4047 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004048 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004049 }
4050
4051 mbox = 0;
4052 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
4053 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
4054
4055 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04004056 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05004057 writel(mbox, mbox_buf);
4058
James Smart9940b972011-03-11 16:06:12 -05004059 for (i = 0; i < 50; i++) {
4060 if (lpfc_readl((resp_buf + 1), &resp_data))
4061 return;
4062 if (resp_data != ~(BARRIER_TEST_PATTERN))
4063 mdelay(1);
4064 else
4065 break;
4066 }
4067 resp_data = 0;
4068 if (lpfc_readl((resp_buf + 1), &resp_data))
4069 return;
4070 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04004071 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05004072 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05004073 goto restore_hc;
4074 else
4075 goto clear_errat;
4076 }
4077
4078 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05004079 resp_data = 0;
4080 for (i = 0; i < 500; i++) {
4081 if (lpfc_readl(resp_buf, &resp_data))
4082 return;
4083 if (resp_data != mbox)
4084 mdelay(1);
4085 else
4086 break;
4087 }
James Smart92908312006-03-07 15:04:13 -05004088
4089clear_errat:
4090
James Smart9940b972011-03-11 16:06:12 -05004091 while (++i < 500) {
4092 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4093 return;
4094 if (!(ha_copy & HA_ERATT))
4095 mdelay(1);
4096 else
4097 break;
4098 }
James Smart92908312006-03-07 15:04:13 -05004099
4100 if (readl(phba->HAregaddr) & HA_ERATT) {
4101 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004102 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004103 }
4104
4105restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004106 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004107 writel(hc_copy, phba->HCregaddr);
4108 readl(phba->HCregaddr); /* flush */
4109}
4110
James Smarte59058c2008-08-24 21:49:00 -04004111/**
James Smart3621a712009-04-06 18:47:14 -04004112 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004113 * @phba: Pointer to HBA context object.
4114 *
4115 * This function issues a kill_board mailbox command and waits for
4116 * the error attention interrupt. This function is called for stopping
4117 * the firmware processing. The caller is not required to hold any
4118 * locks. This function calls lpfc_hba_down_post function to free
4119 * any pending commands after the kill. The function will return 1 when it
4120 * fails to kill the board else will return 0.
4121 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004122int
James Smart2e0fef82007-06-17 19:56:36 -05004123lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004124{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004125 struct lpfc_sli *psli;
4126 LPFC_MBOXQ_t *pmb;
4127 uint32_t status;
4128 uint32_t ha_copy;
4129 int retval;
4130 int i = 0;
4131
4132 psli = &phba->sli;
4133
4134 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004135 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004136 "0329 Kill HBA Data: x%x x%x\n",
4137 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004138
James Smart98c9ea52007-10-27 13:37:33 -04004139 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4140 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004141 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004142
4143 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004144 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004145 if (lpfc_readl(phba->HCregaddr, &status)) {
4146 spin_unlock_irq(&phba->hbalock);
4147 mempool_free(pmb, phba->mbox_mem_pool);
4148 return 1;
4149 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004150 status &= ~HC_ERINT_ENA;
4151 writel(status, phba->HCregaddr);
4152 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004153 phba->link_flag |= LS_IGNORE_ERATT;
4154 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004155
4156 lpfc_kill_board(phba, pmb);
4157 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4158 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4159
4160 if (retval != MBX_SUCCESS) {
4161 if (retval != MBX_BUSY)
4162 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004163 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4164 "2752 KILL_BOARD command failed retval %d\n",
4165 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004166 spin_lock_irq(&phba->hbalock);
4167 phba->link_flag &= ~LS_IGNORE_ERATT;
4168 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004169 return 1;
4170 }
4171
James Smartf4b4c682009-05-22 14:53:12 -04004172 spin_lock_irq(&phba->hbalock);
4173 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4174 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004175
Jamie Wellnitz41415862006-02-28 19:25:27 -05004176 mempool_free(pmb, phba->mbox_mem_pool);
4177
4178 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4179 * attention every 100ms for 3 seconds. If we don't get ERATT after
4180 * 3 seconds we still set HBA_ERROR state because the status of the
4181 * board is now undefined.
4182 */
James Smart9940b972011-03-11 16:06:12 -05004183 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4184 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004185 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4186 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004187 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4188 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004189 }
4190
4191 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004192 if (ha_copy & HA_ERATT) {
4193 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004194 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004195 }
James Smart2e0fef82007-06-17 19:56:36 -05004196 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004197 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004198 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004199 phba->link_flag &= ~LS_IGNORE_ERATT;
4200 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004201
Jamie Wellnitz41415862006-02-28 19:25:27 -05004202 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004203 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004204
James Smart2e0fef82007-06-17 19:56:36 -05004205 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004206}
4207
James Smarte59058c2008-08-24 21:49:00 -04004208/**
James Smart3772a992009-05-22 14:50:54 -04004209 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004210 * @phba: Pointer to HBA context object.
4211 *
4212 * This function resets the HBA by writing HC_INITFF to the control
4213 * register. After the HBA resets, this function resets all the iocb ring
4214 * indices. This function disables PCI layer parity checking during
4215 * the reset.
4216 * This function returns 0 always.
4217 * The caller is not required to hold any locks.
4218 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004219int
James Smart2e0fef82007-06-17 19:56:36 -05004220lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004221{
4222 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004223 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004224 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004225 int i;
dea31012005-04-17 16:05:31 -05004226
Jamie Wellnitz41415862006-02-28 19:25:27 -05004227 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004228
Jamie Wellnitz41415862006-02-28 19:25:27 -05004229 /* Reset HBA */
4230 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004231 "0325 Reset HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004232 (phba->pport) ? phba->pport->port_state : 0,
4233 psli->sli_flag);
dea31012005-04-17 16:05:31 -05004234
4235 /* perform board reset */
4236 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004237 phba->link_events = 0;
James Smart4492b732017-04-27 15:08:26 -07004238 if (phba->pport) {
4239 phba->pport->fc_myDID = 0;
4240 phba->pport->fc_prevDID = 0;
4241 }
dea31012005-04-17 16:05:31 -05004242
Jamie Wellnitz41415862006-02-28 19:25:27 -05004243 /* Turn off parity checking and serr during the physical reset */
4244 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4245 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4246 (cfg_value &
4247 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4248
James Smart3772a992009-05-22 14:50:54 -04004249 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4250
Jamie Wellnitz41415862006-02-28 19:25:27 -05004251 /* Now toggle INITFF bit in the Host Control Register */
4252 writel(HC_INITFF, phba->HCregaddr);
4253 mdelay(1);
4254 readl(phba->HCregaddr); /* flush */
4255 writel(0, phba->HCregaddr);
4256 readl(phba->HCregaddr); /* flush */
4257
4258 /* Restore PCI cmd register */
4259 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004260
4261 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004262 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004263 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004264 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004265 pring->sli.sli3.rspidx = 0;
4266 pring->sli.sli3.next_cmdidx = 0;
4267 pring->sli.sli3.local_getidx = 0;
4268 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004269 pring->missbufcnt = 0;
4270 }
dea31012005-04-17 16:05:31 -05004271
James Smart2e0fef82007-06-17 19:56:36 -05004272 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004273 return 0;
4274}
4275
James Smarte59058c2008-08-24 21:49:00 -04004276/**
James Smartda0436e2009-05-22 14:51:39 -04004277 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4278 * @phba: Pointer to HBA context object.
4279 *
4280 * This function resets a SLI4 HBA. This function disables PCI layer parity
4281 * checking during resets the device. The caller is not required to hold
4282 * any locks.
4283 *
4284 * This function returns 0 always.
4285 **/
4286int
4287lpfc_sli4_brdreset(struct lpfc_hba *phba)
4288{
4289 struct lpfc_sli *psli = &phba->sli;
4290 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004291 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004292
4293 /* Reset HBA */
4294 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004295 "0295 Reset HBA Data: x%x x%x x%x\n",
4296 phba->pport->port_state, psli->sli_flag,
4297 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004298
4299 /* perform board reset */
4300 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004301 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004302 phba->pport->fc_myDID = 0;
4303 phba->pport->fc_prevDID = 0;
4304
James Smartda0436e2009-05-22 14:51:39 -04004305 spin_lock_irq(&phba->hbalock);
4306 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4307 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004308 spin_unlock_irq(&phba->hbalock);
4309
James Smart02936352014-04-04 13:52:12 -04004310 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4311 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4312 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4313 return rc;
4314 }
4315
James Smartda0436e2009-05-22 14:51:39 -04004316 /* Now physically reset the device */
4317 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4318 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004319
4320 /* Turn off parity checking and serr during the physical reset */
4321 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4322 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4323 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4324
James Smart88318812012-09-29 11:29:29 -04004325 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004326 rc = lpfc_pci_function_reset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004327
James Smartbe858b62010-12-15 17:57:20 -05004328 /* Restore PCI cmd register */
4329 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4330
James Smart27b01b82012-05-09 21:19:44 -04004331 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004332}
4333
4334/**
4335 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004336 * @phba: Pointer to HBA context object.
4337 *
4338 * This function is called in the SLI initialization code path to
4339 * restart the HBA. The caller is not required to hold any lock.
4340 * This function writes MBX_RESTART mailbox command to the SLIM and
4341 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4342 * function to free any pending commands. The function enables
4343 * POST only during the first initialization. The function returns zero.
4344 * The function does not guarantee completion of MBX_RESTART mailbox
4345 * command before the return of this function.
4346 **/
James Smartda0436e2009-05-22 14:51:39 -04004347static int
4348lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004349{
4350 MAILBOX_t *mb;
4351 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004352 volatile uint32_t word0;
4353 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004354 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004355
James Smart2e0fef82007-06-17 19:56:36 -05004356 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004357
James Smart0d878412009-10-02 15:16:56 -04004358 /* Take PCIe device Advanced Error Reporting (AER) state */
4359 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4360
Jamie Wellnitz41415862006-02-28 19:25:27 -05004361 psli = &phba->sli;
4362
4363 /* Restart HBA */
4364 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004365 "0337 Restart HBA Data: x%x x%x\n",
James Smart4492b732017-04-27 15:08:26 -07004366 (phba->pport) ? phba->pport->port_state : 0,
4367 psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004368
4369 word0 = 0;
4370 mb = (MAILBOX_t *) &word0;
4371 mb->mbxCommand = MBX_RESTART;
4372 mb->mbxHc = 1;
4373
James Smart92908312006-03-07 15:04:13 -05004374 lpfc_reset_barrier(phba);
4375
Jamie Wellnitz41415862006-02-28 19:25:27 -05004376 to_slim = phba->MBslimaddr;
4377 writel(*(uint32_t *) mb, to_slim);
4378 readl(to_slim); /* flush */
4379
4380 /* Only skip post after fc_ffinit is completed */
James Smart4492b732017-04-27 15:08:26 -07004381 if (phba->pport && phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004382 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004383 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004384 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004385 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004386 writel(*(uint32_t *) mb, to_slim);
4387 readl(to_slim); /* flush */
4388
4389 lpfc_sli_brdreset(phba);
James Smart4492b732017-04-27 15:08:26 -07004390 if (phba->pport)
4391 phba->pport->stopped = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004392 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004393 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004394 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004395
James Smart64ba8812006-08-02 15:24:34 -04004396 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4397 psli->stats_start = get_seconds();
4398
James Smarteaf15d52008-12-04 22:39:29 -05004399 /* Give the INITFF and Post time to settle. */
4400 mdelay(100);
dea31012005-04-17 16:05:31 -05004401
James Smart0d878412009-10-02 15:16:56 -04004402 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4403 if (hba_aer_enabled)
4404 pci_disable_pcie_error_reporting(phba->pcidev);
4405
Jamie Wellnitz41415862006-02-28 19:25:27 -05004406 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004407
4408 return 0;
4409}
4410
James Smarte59058c2008-08-24 21:49:00 -04004411/**
James Smartda0436e2009-05-22 14:51:39 -04004412 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4413 * @phba: Pointer to HBA context object.
4414 *
4415 * This function is called in the SLI initialization code path to restart
4416 * a SLI4 HBA. The caller is not required to hold any lock.
4417 * At the end of the function, it calls lpfc_hba_down_post function to
4418 * free any pending commands.
4419 **/
4420static int
4421lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4422{
4423 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004424 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004425 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004426
4427 /* Restart HBA */
4428 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4429 "0296 Restart HBA Data: x%x x%x\n",
4430 phba->pport->port_state, psli->sli_flag);
4431
James Smart75baf692010-06-08 18:31:21 -04004432 /* Take PCIe device Advanced Error Reporting (AER) state */
4433 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4434
James Smart27b01b82012-05-09 21:19:44 -04004435 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004436
4437 spin_lock_irq(&phba->hbalock);
4438 phba->pport->stopped = 0;
4439 phba->link_state = LPFC_INIT_START;
4440 phba->hba_flag = 0;
4441 spin_unlock_irq(&phba->hbalock);
4442
4443 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4444 psli->stats_start = get_seconds();
4445
James Smart75baf692010-06-08 18:31:21 -04004446 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4447 if (hba_aer_enabled)
4448 pci_disable_pcie_error_reporting(phba->pcidev);
4449
James Smartda0436e2009-05-22 14:51:39 -04004450 lpfc_hba_down_post(phba);
James Smart569dbe82017-06-15 22:56:47 -07004451 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004452
James Smart27b01b82012-05-09 21:19:44 -04004453 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004454}
4455
4456/**
4457 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4458 * @phba: Pointer to HBA context object.
4459 *
4460 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4461 * API jump table function pointer from the lpfc_hba struct.
4462**/
4463int
4464lpfc_sli_brdrestart(struct lpfc_hba *phba)
4465{
4466 return phba->lpfc_sli_brdrestart(phba);
4467}
4468
4469/**
James Smart3621a712009-04-06 18:47:14 -04004470 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004471 * @phba: Pointer to HBA context object.
4472 *
4473 * This function is called after a HBA restart to wait for successful
4474 * restart of the HBA. Successful restart of the HBA is indicated by
4475 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4476 * iteration, the function will restart the HBA again. The function returns
4477 * zero if HBA successfully restarted else returns negative error code.
4478 **/
James Smart4492b732017-04-27 15:08:26 -07004479int
dea31012005-04-17 16:05:31 -05004480lpfc_sli_chipset_init(struct lpfc_hba *phba)
4481{
4482 uint32_t status, i = 0;
4483
4484 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004485 if (lpfc_readl(phba->HSregaddr, &status))
4486 return -EIO;
dea31012005-04-17 16:05:31 -05004487
4488 /* Check status register to see what current state is */
4489 i = 0;
4490 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4491
James Smartdcf2a4e2010-09-29 11:18:53 -04004492 /* Check every 10ms for 10 retries, then every 100ms for 90
4493 * retries, then every 1 sec for 50 retires for a total of
4494 * ~60 seconds before reset the board again and check every
4495 * 1 sec for 50 retries. The up to 60 seconds before the
4496 * board ready is required by the Falcon FIPS zeroization
4497 * complete, and any reset the board in between shall cause
4498 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004499 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004500 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004501 /* Adapter failed to init, timeout, status reg
4502 <status> */
James Smarted957682007-06-17 19:56:37 -05004503 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004504 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004505 "timeout, status reg x%x, "
4506 "FW Data: A8 x%x AC x%x\n", status,
4507 readl(phba->MBslimaddr + 0xa8),
4508 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004509 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004510 return -ETIMEDOUT;
4511 }
4512
4513 /* Check to see if any errors occurred during init */
4514 if (status & HS_FFERM) {
4515 /* ERROR: During chipset initialization */
4516 /* Adapter failed to init, chipset, status reg
4517 <status> */
James Smarted957682007-06-17 19:56:37 -05004518 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004519 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004520 "chipset, status reg x%x, "
4521 "FW Data: A8 x%x AC x%x\n", status,
4522 readl(phba->MBslimaddr + 0xa8),
4523 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004524 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004525 return -EIO;
4526 }
4527
James Smartdcf2a4e2010-09-29 11:18:53 -04004528 if (i <= 10)
dea31012005-04-17 16:05:31 -05004529 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004530 else if (i <= 100)
4531 msleep(100);
4532 else
4533 msleep(1000);
dea31012005-04-17 16:05:31 -05004534
James Smartdcf2a4e2010-09-29 11:18:53 -04004535 if (i == 150) {
4536 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004537 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004538 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004539 }
4540 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004541 if (lpfc_readl(phba->HSregaddr, &status))
4542 return -EIO;
dea31012005-04-17 16:05:31 -05004543 }
4544
4545 /* Check to see if any errors occurred during init */
4546 if (status & HS_FFERM) {
4547 /* ERROR: During chipset initialization */
4548 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004549 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004550 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004551 "status reg x%x, "
4552 "FW Data: A8 x%x AC x%x\n", status,
4553 readl(phba->MBslimaddr + 0xa8),
4554 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004555 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004556 return -EIO;
4557 }
4558
4559 /* Clear all interrupt enable conditions */
4560 writel(0, phba->HCregaddr);
4561 readl(phba->HCregaddr); /* flush */
4562
4563 /* setup host attn register */
4564 writel(0xffffffff, phba->HAregaddr);
4565 readl(phba->HAregaddr); /* flush */
4566 return 0;
4567}
4568
James Smarte59058c2008-08-24 21:49:00 -04004569/**
James Smart3621a712009-04-06 18:47:14 -04004570 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004571 *
4572 * This function calculates and returns the number of HBQs required to be
4573 * configured.
4574 **/
James Smart78b2d852007-08-02 11:10:21 -04004575int
James Smarted957682007-06-17 19:56:37 -05004576lpfc_sli_hbq_count(void)
4577{
James Smart92d7f7b2007-06-17 19:56:38 -05004578 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004579}
4580
James Smarte59058c2008-08-24 21:49:00 -04004581/**
James Smart3621a712009-04-06 18:47:14 -04004582 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004583 *
4584 * This function adds the number of hbq entries in every HBQ to get
4585 * the total number of hbq entries required for the HBA and returns
4586 * the total count.
4587 **/
James Smarted957682007-06-17 19:56:37 -05004588static int
4589lpfc_sli_hbq_entry_count(void)
4590{
4591 int hbq_count = lpfc_sli_hbq_count();
4592 int count = 0;
4593 int i;
4594
4595 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004596 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004597 return count;
4598}
4599
James Smarte59058c2008-08-24 21:49:00 -04004600/**
James Smart3621a712009-04-06 18:47:14 -04004601 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004602 *
4603 * This function calculates amount of memory required for all hbq entries
4604 * to be configured and returns the total memory required.
4605 **/
dea31012005-04-17 16:05:31 -05004606int
James Smarted957682007-06-17 19:56:37 -05004607lpfc_sli_hbq_size(void)
4608{
4609 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4610}
4611
James Smarte59058c2008-08-24 21:49:00 -04004612/**
James Smart3621a712009-04-06 18:47:14 -04004613 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004614 * @phba: Pointer to HBA context object.
4615 *
4616 * This function is called during the SLI initialization to configure
4617 * all the HBQs and post buffers to the HBQ. The caller is not
4618 * required to hold any locks. This function will return zero if successful
4619 * else it will return negative error code.
4620 **/
James Smarted957682007-06-17 19:56:37 -05004621static int
4622lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4623{
4624 int hbq_count = lpfc_sli_hbq_count();
4625 LPFC_MBOXQ_t *pmb;
4626 MAILBOX_t *pmbox;
4627 uint32_t hbqno;
4628 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004629
James Smart92d7f7b2007-06-17 19:56:38 -05004630 /* Get a Mailbox buffer to setup mailbox
4631 * commands for HBA initialization
4632 */
James Smarted957682007-06-17 19:56:37 -05004633 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4634
4635 if (!pmb)
4636 return -ENOMEM;
4637
James Smart04c68492009-05-22 14:52:52 -04004638 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004639
4640 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4641 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004642 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004643
4644 hbq_entry_index = 0;
4645 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4646 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4647 phba->hbqs[hbqno].hbqPutIdx = 0;
4648 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4649 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004650 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004651 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4652 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004653 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4654
4655 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4656 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4657 mbxStatus <status>, ring <num> */
4658
4659 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004660 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004661 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004662 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004663 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004664 pmbox->mbxStatus, hbqno);
4665
4666 phba->link_state = LPFC_HBA_ERROR;
4667 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004668 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004669 }
4670 }
4671 phba->hbq_count = hbq_count;
4672
James Smarted957682007-06-17 19:56:37 -05004673 mempool_free(pmb, phba->mbox_mem_pool);
4674
James Smart92d7f7b2007-06-17 19:56:38 -05004675 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004676 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4677 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004678 return 0;
4679}
4680
James Smarte59058c2008-08-24 21:49:00 -04004681/**
James Smart4f774512009-05-22 14:52:35 -04004682 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4683 * @phba: Pointer to HBA context object.
4684 *
4685 * This function is called during the SLI initialization to configure
4686 * all the HBQs and post buffers to the HBQ. The caller is not
4687 * required to hold any locks. This function will return zero if successful
4688 * else it will return negative error code.
4689 **/
4690static int
4691lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4692{
4693 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004694 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4695 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004696 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004697 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004698 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004699 return 0;
4700}
4701
4702/**
James Smart3621a712009-04-06 18:47:14 -04004703 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004704 * @phba: Pointer to HBA context object.
4705 * @sli_mode: sli mode - 2/3
4706 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004707 * This function is called by the sli initialization code path
James Smarte59058c2008-08-24 21:49:00 -04004708 * to issue config_port mailbox command. This function restarts the
4709 * HBA firmware and issues a config_port mailbox command to configure
4710 * the SLI interface in the sli mode specified by sli_mode
4711 * variable. The caller is not required to hold any locks.
4712 * The function returns 0 if successful, else returns negative error
4713 * code.
4714 **/
James Smart93996272008-08-24 21:50:30 -04004715int
4716lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004717{
4718 LPFC_MBOXQ_t *pmb;
4719 uint32_t resetcount = 0, rc = 0, done = 0;
4720
4721 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4722 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004723 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004724 return -ENOMEM;
4725 }
4726
James Smarted957682007-06-17 19:56:37 -05004727 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004728 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004729 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004730 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004731 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004732 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004733 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004734 rc = lpfc_sli_chipset_init(phba);
4735 if (rc)
4736 break;
4737
James Smart2e0fef82007-06-17 19:56:36 -05004738 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004739 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004740 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004741 resetcount++;
4742
James Smarted957682007-06-17 19:56:37 -05004743 /* Call pre CONFIG_PORT mailbox command initialization. A
4744 * value of 0 means the call was successful. Any other
4745 * nonzero value is a failure, but if ERESTART is returned,
4746 * the driver may reset the HBA and try again.
4747 */
dea31012005-04-17 16:05:31 -05004748 rc = lpfc_config_port_prep(phba);
4749 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004750 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004751 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004752 } else if (rc)
dea31012005-04-17 16:05:31 -05004753 break;
James Smart6d368e52011-05-24 11:44:12 -04004754
James Smart2e0fef82007-06-17 19:56:36 -05004755 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004756 lpfc_config_port(phba, pmb);
4757 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004758 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4759 LPFC_SLI3_HBQ_ENABLED |
4760 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004761 LPFC_SLI3_BG_ENABLED |
4762 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004763 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004764 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004765 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004766 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004767 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004768 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004769 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004770 spin_unlock_irq(&phba->hbalock);
4771 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004772 } else {
4773 /* Allow asynchronous mailbox command to go through */
4774 spin_lock_irq(&phba->hbalock);
4775 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4776 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004777 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004778
4779 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4780 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4781 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4782 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004783 }
dea31012005-04-17 16:05:31 -05004784 }
James Smarted957682007-06-17 19:56:37 -05004785 if (!done) {
4786 rc = -EINVAL;
4787 goto do_prep_failed;
4788 }
James Smart04c68492009-05-22 14:52:52 -04004789 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4790 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004791 rc = -ENXIO;
4792 goto do_prep_failed;
4793 }
James Smart04c68492009-05-22 14:52:52 -04004794 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004795 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004796 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4797 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4798 phba->max_vpi : phba->max_vports;
4799
James Smart34b02dc2008-08-24 21:49:55 -04004800 } else
4801 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004802 phba->fips_level = 0;
4803 phba->fips_spec_rev = 0;
4804 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004805 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004806 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4807 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4808 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4809 "2850 Security Crypto Active. FIPS x%d "
4810 "(Spec Rev: x%d)",
4811 phba->fips_level, phba->fips_spec_rev);
4812 }
4813 if (pmb->u.mb.un.varCfgPort.sec_err) {
4814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4815 "2856 Config Port Security Crypto "
4816 "Error: x%x ",
4817 pmb->u.mb.un.varCfgPort.sec_err);
4818 }
James Smart04c68492009-05-22 14:52:52 -04004819 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004820 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004821 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004822 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004823
4824 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4825 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004826
4827 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004828 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004829 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4830 else
4831 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4832 "0443 Adapter did not grant "
4833 "BlockGuard\n");
4834 }
James Smart34b02dc2008-08-24 21:49:55 -04004835 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004836 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004837 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004838 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004839 }
James Smart92d7f7b2007-06-17 19:56:38 -05004840do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004841 mempool_free(pmb, phba->mbox_mem_pool);
4842 return rc;
4843}
4844
James Smarte59058c2008-08-24 21:49:00 -04004845
4846/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08004847 * lpfc_sli_hba_setup - SLI initialization function
James Smarte59058c2008-08-24 21:49:00 -04004848 * @phba: Pointer to HBA context object.
4849 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08004850 * This function is the main SLI initialization function. This function
4851 * is called by the HBA initialization code, HBA reset code and HBA
James Smarte59058c2008-08-24 21:49:00 -04004852 * error attention handler code. Caller is not required to hold any
4853 * locks. This function issues config_port mailbox command to configure
4854 * the SLI, setup iocb rings and HBQ rings. In the end the function
4855 * calls the config_port_post function to issue init_link mailbox
4856 * command and to start the discovery. The function will return zero
4857 * if successful, else it will return negative error code.
4858 **/
James Smarted957682007-06-17 19:56:37 -05004859int
4860lpfc_sli_hba_setup(struct lpfc_hba *phba)
4861{
4862 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004863 int mode = 3, i;
4864 int longs;
James Smarted957682007-06-17 19:56:37 -05004865
James Smart12247e82016-07-06 12:36:09 -07004866 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05004867 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004868 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004870 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004871 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07004872 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004873 break;
4874 }
James Smarted957682007-06-17 19:56:37 -05004875 mode = 2;
4876 break;
4877 case 0:
4878 case 3:
4879 break;
4880 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004882 "1819 Unrecognized sli_mode parameter: %d.\n",
4883 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004884
4885 break;
4886 }
James Smartb5c53952016-03-31 14:12:30 -07004887 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004888
James Smart93996272008-08-24 21:50:30 -04004889 rc = lpfc_sli_config_port(phba, mode);
4890
James Smart12247e82016-07-06 12:36:09 -07004891 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004892 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004893 "1820 Unable to select SLI-3. "
4894 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004895 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004896 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07004897 else if (rc && mode == 2)
4898 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05004899 if (rc)
dea31012005-04-17 16:05:31 -05004900 goto lpfc_sli_hba_setup_error;
4901
James Smart0d878412009-10-02 15:16:56 -04004902 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4903 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4904 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4905 if (!rc) {
4906 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4907 "2709 This device supports "
4908 "Advanced Error Reporting (AER)\n");
4909 spin_lock_irq(&phba->hbalock);
4910 phba->hba_flag |= HBA_AER_ENABLED;
4911 spin_unlock_irq(&phba->hbalock);
4912 } else {
4913 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4914 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004915 "Advanced Error Reporting (AER): %d\n",
4916 rc);
James Smart0d878412009-10-02 15:16:56 -04004917 phba->cfg_aer_support = 0;
4918 }
4919 }
4920
James Smarted957682007-06-17 19:56:37 -05004921 if (phba->sli_rev == 3) {
4922 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4923 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004924 } else {
4925 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4926 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004927 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004928 }
4929
4930 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004931 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4932 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004933 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004934
4935 if (rc)
4936 goto lpfc_sli_hba_setup_error;
4937
James Smart6d368e52011-05-24 11:44:12 -04004938 /* Initialize VPIs. */
4939 if (phba->sli_rev == LPFC_SLI_REV3) {
4940 /*
4941 * The VPI bitmask and physical ID array are allocated
4942 * and initialized once only - at driver load. A port
4943 * reset doesn't need to reinitialize this memory.
4944 */
4945 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4946 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4947 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4948 GFP_KERNEL);
4949 if (!phba->vpi_bmask) {
4950 rc = -ENOMEM;
4951 goto lpfc_sli_hba_setup_error;
4952 }
4953
4954 phba->vpi_ids = kzalloc(
4955 (phba->max_vpi+1) * sizeof(uint16_t),
4956 GFP_KERNEL);
4957 if (!phba->vpi_ids) {
4958 kfree(phba->vpi_bmask);
4959 rc = -ENOMEM;
4960 goto lpfc_sli_hba_setup_error;
4961 }
4962 for (i = 0; i < phba->max_vpi; i++)
4963 phba->vpi_ids[i] = i;
4964 }
4965 }
4966
James Smart93996272008-08-24 21:50:30 -04004967 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004968 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4969 rc = lpfc_sli_hbq_setup(phba);
4970 if (rc)
4971 goto lpfc_sli_hba_setup_error;
4972 }
James Smart04c68492009-05-22 14:52:52 -04004973 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004974 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004975 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004976
4977 rc = lpfc_config_port_post(phba);
4978 if (rc)
4979 goto lpfc_sli_hba_setup_error;
4980
James Smarted957682007-06-17 19:56:37 -05004981 return rc;
4982
James Smart92d7f7b2007-06-17 19:56:38 -05004983lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004984 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004985 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004986 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004987 return rc;
4988}
4989
James Smartda0436e2009-05-22 14:51:39 -04004990/**
4991 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4992 * @phba: Pointer to HBA context object.
4993 * @mboxq: mailbox pointer.
4994 * This function issue a dump mailbox command to read config region
4995 * 23 and parse the records in the region and populate driver
4996 * data structure.
4997 **/
4998static int
James Smartff78d8f2011-12-13 13:21:35 -05004999lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04005000{
James Smartff78d8f2011-12-13 13:21:35 -05005001 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005002 struct lpfc_dmabuf *mp;
5003 struct lpfc_mqe *mqe;
5004 uint32_t data_length;
5005 int rc;
5006
5007 /* Program the default value of vlan_id and fc_map */
5008 phba->valid_vlan = 0;
5009 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
5010 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
5011 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
5012
James Smartff78d8f2011-12-13 13:21:35 -05005013 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5014 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04005015 return -ENOMEM;
5016
James Smartff78d8f2011-12-13 13:21:35 -05005017 mqe = &mboxq->u.mqe;
5018 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
5019 rc = -ENOMEM;
5020 goto out_free_mboxq;
5021 }
5022
James Smartda0436e2009-05-22 14:51:39 -04005023 mp = (struct lpfc_dmabuf *) mboxq->context1;
5024 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5025
5026 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5027 "(%d):2571 Mailbox cmd x%x Status x%x "
5028 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5029 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
5030 "CQ: x%x x%x x%x x%x\n",
5031 mboxq->vport ? mboxq->vport->vpi : 0,
5032 bf_get(lpfc_mqe_command, mqe),
5033 bf_get(lpfc_mqe_status, mqe),
5034 mqe->un.mb_words[0], mqe->un.mb_words[1],
5035 mqe->un.mb_words[2], mqe->un.mb_words[3],
5036 mqe->un.mb_words[4], mqe->un.mb_words[5],
5037 mqe->un.mb_words[6], mqe->un.mb_words[7],
5038 mqe->un.mb_words[8], mqe->un.mb_words[9],
5039 mqe->un.mb_words[10], mqe->un.mb_words[11],
5040 mqe->un.mb_words[12], mqe->un.mb_words[13],
5041 mqe->un.mb_words[14], mqe->un.mb_words[15],
5042 mqe->un.mb_words[16], mqe->un.mb_words[50],
5043 mboxq->mcqe.word0,
5044 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5045 mboxq->mcqe.trailer);
5046
5047 if (rc) {
5048 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5049 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005050 rc = -EIO;
5051 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04005052 }
5053 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04005054 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04005055 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5056 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005057 rc = -EIO;
5058 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04005059 }
James Smartda0436e2009-05-22 14:51:39 -04005060
5061 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
5062 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5063 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05005064 rc = 0;
5065
5066out_free_mboxq:
5067 mempool_free(mboxq, phba->mbox_mem_pool);
5068 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005069}
5070
5071/**
5072 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
5073 * @phba: pointer to lpfc hba data structure.
5074 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
5075 * @vpd: pointer to the memory to hold resulting port vpd data.
5076 * @vpd_size: On input, the number of bytes allocated to @vpd.
5077 * On output, the number of data bytes in @vpd.
5078 *
5079 * This routine executes a READ_REV SLI4 mailbox command. In
5080 * addition, this routine gets the port vpd data.
5081 *
5082 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005083 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04005084 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04005085 **/
5086static int
5087lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5088 uint8_t *vpd, uint32_t *vpd_size)
5089{
5090 int rc = 0;
5091 uint32_t dma_size;
5092 struct lpfc_dmabuf *dmabuf;
5093 struct lpfc_mqe *mqe;
5094
5095 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5096 if (!dmabuf)
5097 return -ENOMEM;
5098
5099 /*
5100 * Get a DMA buffer for the vpd data resulting from the READ_REV
5101 * mailbox command.
5102 */
5103 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04005104 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
5105 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04005106 if (!dmabuf->virt) {
5107 kfree(dmabuf);
5108 return -ENOMEM;
5109 }
James Smartda0436e2009-05-22 14:51:39 -04005110
5111 /*
5112 * The SLI4 implementation of READ_REV conflicts at word1,
5113 * bits 31:16 and SLI4 adds vpd functionality not present
5114 * in SLI3. This code corrects the conflicts.
5115 */
5116 lpfc_read_rev(phba, mboxq);
5117 mqe = &mboxq->u.mqe;
5118 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5119 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5120 mqe->un.read_rev.word1 &= 0x0000FFFF;
5121 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5122 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5123
5124 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5125 if (rc) {
5126 dma_free_coherent(&phba->pcidev->dev, dma_size,
5127 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005128 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005129 return -EIO;
5130 }
5131
James Smartda0436e2009-05-22 14:51:39 -04005132 /*
5133 * The available vpd length cannot be bigger than the
5134 * DMA buffer passed to the port. Catch the less than
5135 * case and update the caller's size.
5136 */
5137 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5138 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5139
James Smartd7c47992010-06-08 18:31:54 -04005140 memcpy(vpd, dmabuf->virt, *vpd_size);
5141
James Smartda0436e2009-05-22 14:51:39 -04005142 dma_free_coherent(&phba->pcidev->dev, dma_size,
5143 dmabuf->virt, dmabuf->phys);
5144 kfree(dmabuf);
5145 return 0;
5146}
5147
5148/**
James Smartcd1c8302011-10-10 21:33:25 -04005149 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5150 * @phba: pointer to lpfc hba data structure.
5151 *
5152 * This routine retrieves SLI4 device physical port name this PCI function
5153 * is attached to.
5154 *
5155 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005156 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005157 * otherwise - failed to retrieve physical port name
5158 **/
5159static int
5160lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5161{
5162 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005163 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5164 struct lpfc_controller_attribute *cntl_attr;
5165 struct lpfc_mbx_get_port_name *get_port_name;
5166 void *virtaddr = NULL;
5167 uint32_t alloclen, reqlen;
5168 uint32_t shdr_status, shdr_add_status;
5169 union lpfc_sli4_cfg_shdr *shdr;
5170 char cport_name = 0;
5171 int rc;
5172
5173 /* We assume nothing at this point */
5174 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5175 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5176
5177 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5178 if (!mboxq)
5179 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005180 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005181 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5182 lpfc_sli4_read_config(phba);
5183 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5184 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005185
5186 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5187 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5188 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5189 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5190 LPFC_SLI4_MBX_NEMBED);
5191 if (alloclen < reqlen) {
5192 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5193 "3084 Allocated DMA memory size (%d) is "
5194 "less than the requested DMA memory size "
5195 "(%d)\n", alloclen, reqlen);
5196 rc = -ENOMEM;
5197 goto out_free_mboxq;
5198 }
5199 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5200 virtaddr = mboxq->sge_array->addr[0];
5201 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5202 shdr = &mbx_cntl_attr->cfg_shdr;
5203 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5204 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5205 if (shdr_status || shdr_add_status || rc) {
5206 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5207 "3085 Mailbox x%x (x%x/x%x) failed, "
5208 "rc:x%x, status:x%x, add_status:x%x\n",
5209 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5210 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5211 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5212 rc, shdr_status, shdr_add_status);
5213 rc = -ENXIO;
5214 goto out_free_mboxq;
5215 }
5216 cntl_attr = &mbx_cntl_attr->cntl_attr;
5217 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5218 phba->sli4_hba.lnk_info.lnk_tp =
5219 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5220 phba->sli4_hba.lnk_info.lnk_no =
5221 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5222 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5223 "3086 lnk_type:%d, lnk_numb:%d\n",
5224 phba->sli4_hba.lnk_info.lnk_tp,
5225 phba->sli4_hba.lnk_info.lnk_no);
5226
5227retrieve_ppname:
5228 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5229 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5230 sizeof(struct lpfc_mbx_get_port_name) -
5231 sizeof(struct lpfc_sli4_cfg_mhdr),
5232 LPFC_SLI4_MBX_EMBED);
5233 get_port_name = &mboxq->u.mqe.un.get_port_name;
5234 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5235 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5236 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5237 phba->sli4_hba.lnk_info.lnk_tp);
5238 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5239 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5240 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5241 if (shdr_status || shdr_add_status || rc) {
5242 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5243 "3087 Mailbox x%x (x%x/x%x) failed: "
5244 "rc:x%x, status:x%x, add_status:x%x\n",
5245 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5246 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5247 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5248 rc, shdr_status, shdr_add_status);
5249 rc = -ENXIO;
5250 goto out_free_mboxq;
5251 }
5252 switch (phba->sli4_hba.lnk_info.lnk_no) {
5253 case LPFC_LINK_NUMBER_0:
5254 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5255 &get_port_name->u.response);
5256 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5257 break;
5258 case LPFC_LINK_NUMBER_1:
5259 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5260 &get_port_name->u.response);
5261 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5262 break;
5263 case LPFC_LINK_NUMBER_2:
5264 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5265 &get_port_name->u.response);
5266 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5267 break;
5268 case LPFC_LINK_NUMBER_3:
5269 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5270 &get_port_name->u.response);
5271 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5272 break;
5273 default:
5274 break;
5275 }
5276
5277 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5278 phba->Port[0] = cport_name;
5279 phba->Port[1] = '\0';
5280 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5281 "3091 SLI get port name: %s\n", phba->Port);
5282 }
5283
5284out_free_mboxq:
5285 if (rc != MBX_TIMEOUT) {
5286 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5287 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5288 else
5289 mempool_free(mboxq, phba->mbox_mem_pool);
5290 }
5291 return rc;
5292}
5293
5294/**
James Smartda0436e2009-05-22 14:51:39 -04005295 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5296 * @phba: pointer to lpfc hba data structure.
5297 *
5298 * This routine is called to explicitly arm the SLI4 device's completion and
5299 * event queues
5300 **/
5301static void
5302lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5303{
James Smart895427b2017-02-12 13:52:30 -08005304 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04005305
5306 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5307 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005308 if (phba->sli4_hba.nvmels_cq)
5309 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5310 LPFC_QUEUE_REARM);
5311
5312 if (phba->sli4_hba.fcp_cq)
5313 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5314 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5315 LPFC_QUEUE_REARM);
5316
5317 if (phba->sli4_hba.nvme_cq)
5318 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5319 lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5320 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005321
James Smartf38fa0b2014-04-04 13:52:21 -04005322 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005323 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5324
James Smart895427b2017-02-12 13:52:30 -08005325 if (phba->sli4_hba.hba_eq)
5326 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5327 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5328 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005329
James Smart2d7dbc42017-02-12 13:52:35 -08005330 if (phba->nvmet_support) {
5331 for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
5332 lpfc_sli4_cq_release(
5333 phba->sli4_hba.nvmet_cqset[qidx],
5334 LPFC_QUEUE_REARM);
5335 }
James Smart2e90f4b2011-12-13 13:22:37 -05005336 }
James Smart1ba981f2014-02-20 09:56:45 -05005337
5338 if (phba->cfg_fof)
5339 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005340}
5341
5342/**
James Smart6d368e52011-05-24 11:44:12 -04005343 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5344 * @phba: Pointer to HBA context object.
5345 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005346 * @extnt_count: buffer to hold port available extent count.
5347 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005348 *
James Smartb76f2dc2011-07-22 18:37:42 -04005349 * This function calls the port and retrievs the number of available
5350 * extents and their size for a particular extent type.
5351 *
5352 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005353 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005354int
James Smart6d368e52011-05-24 11:44:12 -04005355lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5356 uint16_t *extnt_count, uint16_t *extnt_size)
5357{
5358 int rc = 0;
5359 uint32_t length;
5360 uint32_t mbox_tmo;
5361 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5362 LPFC_MBOXQ_t *mbox;
5363
5364 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5365 if (!mbox)
5366 return -ENOMEM;
5367
5368 /* Find out how many extents are available for this resource type */
5369 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5370 sizeof(struct lpfc_sli4_cfg_mhdr));
5371 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5372 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5373 length, LPFC_SLI4_MBX_EMBED);
5374
5375 /* Send an extents count of 0 - the GET doesn't use it. */
5376 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5377 LPFC_SLI4_MBX_EMBED);
5378 if (unlikely(rc)) {
5379 rc = -EIO;
5380 goto err_exit;
5381 }
5382
5383 if (!phba->sli4_hba.intr_enable)
5384 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5385 else {
James Smarta183a152011-10-10 21:32:43 -04005386 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005387 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5388 }
5389 if (unlikely(rc)) {
5390 rc = -EIO;
5391 goto err_exit;
5392 }
5393
5394 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5395 if (bf_get(lpfc_mbox_hdr_status,
5396 &rsrc_info->header.cfg_shdr.response)) {
5397 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5398 "2930 Failed to get resource extents "
5399 "Status 0x%x Add'l Status 0x%x\n",
5400 bf_get(lpfc_mbox_hdr_status,
5401 &rsrc_info->header.cfg_shdr.response),
5402 bf_get(lpfc_mbox_hdr_add_status,
5403 &rsrc_info->header.cfg_shdr.response));
5404 rc = -EIO;
5405 goto err_exit;
5406 }
5407
5408 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5409 &rsrc_info->u.rsp);
5410 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5411 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005412
5413 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5414 "3162 Retrieved extents type-%d from port: count:%d, "
5415 "size:%d\n", type, *extnt_count, *extnt_size);
5416
5417err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005418 mempool_free(mbox, phba->mbox_mem_pool);
5419 return rc;
5420}
5421
5422/**
5423 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5424 * @phba: Pointer to HBA context object.
5425 * @type: The extent type to check.
5426 *
5427 * This function reads the current available extents from the port and checks
5428 * if the extent count or extent size has changed since the last access.
5429 * Callers use this routine post port reset to understand if there is a
5430 * extent reprovisioning requirement.
5431 *
5432 * Returns:
5433 * -Error: error indicates problem.
5434 * 1: Extent count or size has changed.
5435 * 0: No changes.
5436 **/
5437static int
5438lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5439{
5440 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5441 uint16_t size_diff, rsrc_ext_size;
5442 int rc = 0;
5443 struct lpfc_rsrc_blks *rsrc_entry;
5444 struct list_head *rsrc_blk_list = NULL;
5445
5446 size_diff = 0;
5447 curr_ext_cnt = 0;
5448 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5449 &rsrc_ext_cnt,
5450 &rsrc_ext_size);
5451 if (unlikely(rc))
5452 return -EIO;
5453
5454 switch (type) {
5455 case LPFC_RSC_TYPE_FCOE_RPI:
5456 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5457 break;
5458 case LPFC_RSC_TYPE_FCOE_VPI:
5459 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5460 break;
5461 case LPFC_RSC_TYPE_FCOE_XRI:
5462 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5463 break;
5464 case LPFC_RSC_TYPE_FCOE_VFI:
5465 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5466 break;
5467 default:
5468 break;
5469 }
5470
5471 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5472 curr_ext_cnt++;
5473 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5474 size_diff++;
5475 }
5476
5477 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5478 rc = 1;
5479
5480 return rc;
5481}
5482
5483/**
5484 * lpfc_sli4_cfg_post_extnts -
5485 * @phba: Pointer to HBA context object.
5486 * @extnt_cnt - number of available extents.
5487 * @type - the extent type (rpi, xri, vfi, vpi).
5488 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5489 * @mbox - pointer to the caller's allocated mailbox structure.
5490 *
5491 * This function executes the extents allocation request. It also
5492 * takes care of the amount of memory needed to allocate or get the
5493 * allocated extents. It is the caller's responsibility to evaluate
5494 * the response.
5495 *
5496 * Returns:
5497 * -Error: Error value describes the condition found.
5498 * 0: if successful
5499 **/
5500static int
James Smart8a9d2e82012-05-09 21:16:12 -04005501lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005502 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5503{
5504 int rc = 0;
5505 uint32_t req_len;
5506 uint32_t emb_len;
5507 uint32_t alloc_len, mbox_tmo;
5508
5509 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005510 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005511
5512 /*
5513 * Calculate the size of an embedded mailbox. The uint32_t
5514 * accounts for extents-specific word.
5515 */
5516 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5517 sizeof(uint32_t);
5518
5519 /*
5520 * Presume the allocation and response will fit into an embedded
5521 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5522 */
5523 *emb = LPFC_SLI4_MBX_EMBED;
5524 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005525 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005526 sizeof(union lpfc_sli4_cfg_shdr) +
5527 sizeof(uint32_t);
5528 *emb = LPFC_SLI4_MBX_NEMBED;
5529 }
5530
5531 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5532 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5533 req_len, *emb);
5534 if (alloc_len < req_len) {
5535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005536 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005537 "less than the requested DMA memory "
5538 "size (x%x)\n", alloc_len, req_len);
5539 return -ENOMEM;
5540 }
James Smart8a9d2e82012-05-09 21:16:12 -04005541 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005542 if (unlikely(rc))
5543 return -EIO;
5544
5545 if (!phba->sli4_hba.intr_enable)
5546 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5547 else {
James Smarta183a152011-10-10 21:32:43 -04005548 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005549 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5550 }
5551
5552 if (unlikely(rc))
5553 rc = -EIO;
5554 return rc;
5555}
5556
5557/**
5558 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5559 * @phba: Pointer to HBA context object.
5560 * @type: The resource extent type to allocate.
5561 *
5562 * This function allocates the number of elements for the specified
5563 * resource type.
5564 **/
5565static int
5566lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5567{
5568 bool emb = false;
5569 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5570 uint16_t rsrc_id, rsrc_start, j, k;
5571 uint16_t *ids;
5572 int i, rc;
5573 unsigned long longs;
5574 unsigned long *bmask;
5575 struct lpfc_rsrc_blks *rsrc_blks;
5576 LPFC_MBOXQ_t *mbox;
5577 uint32_t length;
5578 struct lpfc_id_range *id_array = NULL;
5579 void *virtaddr = NULL;
5580 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5581 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5582 struct list_head *ext_blk_list;
5583
5584 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5585 &rsrc_cnt,
5586 &rsrc_size);
5587 if (unlikely(rc))
5588 return -EIO;
5589
5590 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5591 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5592 "3009 No available Resource Extents "
5593 "for resource type 0x%x: Count: 0x%x, "
5594 "Size 0x%x\n", type, rsrc_cnt,
5595 rsrc_size);
5596 return -ENOMEM;
5597 }
5598
James Smart8a9d2e82012-05-09 21:16:12 -04005599 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5600 "2903 Post resource extents type-0x%x: "
5601 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005602
5603 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5604 if (!mbox)
5605 return -ENOMEM;
5606
James Smart8a9d2e82012-05-09 21:16:12 -04005607 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005608 if (unlikely(rc)) {
5609 rc = -EIO;
5610 goto err_exit;
5611 }
5612
5613 /*
5614 * Figure out where the response is located. Then get local pointers
5615 * to the response data. The port does not guarantee to respond to
5616 * all extents counts request so update the local variable with the
5617 * allocated count from the port.
5618 */
5619 if (emb == LPFC_SLI4_MBX_EMBED) {
5620 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5621 id_array = &rsrc_ext->u.rsp.id[0];
5622 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5623 } else {
5624 virtaddr = mbox->sge_array->addr[0];
5625 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5626 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5627 id_array = &n_rsrc->id;
5628 }
5629
5630 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5631 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5632
5633 /*
5634 * Based on the resource size and count, correct the base and max
5635 * resource values.
5636 */
5637 length = sizeof(struct lpfc_rsrc_blks);
5638 switch (type) {
5639 case LPFC_RSC_TYPE_FCOE_RPI:
5640 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5641 sizeof(unsigned long),
5642 GFP_KERNEL);
5643 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5644 rc = -ENOMEM;
5645 goto err_exit;
5646 }
5647 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5648 sizeof(uint16_t),
5649 GFP_KERNEL);
5650 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5651 kfree(phba->sli4_hba.rpi_bmask);
5652 rc = -ENOMEM;
5653 goto err_exit;
5654 }
5655
5656 /*
5657 * The next_rpi was initialized with the maximum available
5658 * count but the port may allocate a smaller number. Catch
5659 * that case and update the next_rpi.
5660 */
5661 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5662
5663 /* Initialize local ptrs for common extent processing later. */
5664 bmask = phba->sli4_hba.rpi_bmask;
5665 ids = phba->sli4_hba.rpi_ids;
5666 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5667 break;
5668 case LPFC_RSC_TYPE_FCOE_VPI:
5669 phba->vpi_bmask = kzalloc(longs *
5670 sizeof(unsigned long),
5671 GFP_KERNEL);
5672 if (unlikely(!phba->vpi_bmask)) {
5673 rc = -ENOMEM;
5674 goto err_exit;
5675 }
5676 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5677 sizeof(uint16_t),
5678 GFP_KERNEL);
5679 if (unlikely(!phba->vpi_ids)) {
5680 kfree(phba->vpi_bmask);
5681 rc = -ENOMEM;
5682 goto err_exit;
5683 }
5684
5685 /* Initialize local ptrs for common extent processing later. */
5686 bmask = phba->vpi_bmask;
5687 ids = phba->vpi_ids;
5688 ext_blk_list = &phba->lpfc_vpi_blk_list;
5689 break;
5690 case LPFC_RSC_TYPE_FCOE_XRI:
5691 phba->sli4_hba.xri_bmask = kzalloc(longs *
5692 sizeof(unsigned long),
5693 GFP_KERNEL);
5694 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5695 rc = -ENOMEM;
5696 goto err_exit;
5697 }
James Smart8a9d2e82012-05-09 21:16:12 -04005698 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005699 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5700 sizeof(uint16_t),
5701 GFP_KERNEL);
5702 if (unlikely(!phba->sli4_hba.xri_ids)) {
5703 kfree(phba->sli4_hba.xri_bmask);
5704 rc = -ENOMEM;
5705 goto err_exit;
5706 }
5707
5708 /* Initialize local ptrs for common extent processing later. */
5709 bmask = phba->sli4_hba.xri_bmask;
5710 ids = phba->sli4_hba.xri_ids;
5711 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5712 break;
5713 case LPFC_RSC_TYPE_FCOE_VFI:
5714 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5715 sizeof(unsigned long),
5716 GFP_KERNEL);
5717 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5718 rc = -ENOMEM;
5719 goto err_exit;
5720 }
5721 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5722 sizeof(uint16_t),
5723 GFP_KERNEL);
5724 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5725 kfree(phba->sli4_hba.vfi_bmask);
5726 rc = -ENOMEM;
5727 goto err_exit;
5728 }
5729
5730 /* Initialize local ptrs for common extent processing later. */
5731 bmask = phba->sli4_hba.vfi_bmask;
5732 ids = phba->sli4_hba.vfi_ids;
5733 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5734 break;
5735 default:
5736 /* Unsupported Opcode. Fail call. */
5737 id_array = NULL;
5738 bmask = NULL;
5739 ids = NULL;
5740 ext_blk_list = NULL;
5741 goto err_exit;
5742 }
5743
5744 /*
5745 * Complete initializing the extent configuration with the
5746 * allocated ids assigned to this function. The bitmask serves
5747 * as an index into the array and manages the available ids. The
5748 * array just stores the ids communicated to the port via the wqes.
5749 */
5750 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5751 if ((i % 2) == 0)
5752 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5753 &id_array[k]);
5754 else
5755 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5756 &id_array[k]);
5757
5758 rsrc_blks = kzalloc(length, GFP_KERNEL);
5759 if (unlikely(!rsrc_blks)) {
5760 rc = -ENOMEM;
5761 kfree(bmask);
5762 kfree(ids);
5763 goto err_exit;
5764 }
5765 rsrc_blks->rsrc_start = rsrc_id;
5766 rsrc_blks->rsrc_size = rsrc_size;
5767 list_add_tail(&rsrc_blks->list, ext_blk_list);
5768 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005769 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005770 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005771 lpfc_sli4_get_iocb_cnt(phba);
5772 phba->sli4_hba.nvme_xri_start =
5773 phba->sli4_hba.scsi_xri_start +
5774 phba->sli4_hba.scsi_xri_max;
5775 }
James Smart6d368e52011-05-24 11:44:12 -04005776
5777 while (rsrc_id < (rsrc_start + rsrc_size)) {
5778 ids[j] = rsrc_id;
5779 rsrc_id++;
5780 j++;
5781 }
5782 /* Entire word processed. Get next word.*/
5783 if ((i % 2) == 1)
5784 k++;
5785 }
5786 err_exit:
5787 lpfc_sli4_mbox_cmd_free(phba, mbox);
5788 return rc;
5789}
5790
James Smart895427b2017-02-12 13:52:30 -08005791
5792
James Smart6d368e52011-05-24 11:44:12 -04005793/**
5794 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5795 * @phba: Pointer to HBA context object.
5796 * @type: the extent's type.
5797 *
5798 * This function deallocates all extents of a particular resource type.
5799 * SLI4 does not allow for deallocating a particular extent range. It
5800 * is the caller's responsibility to release all kernel memory resources.
5801 **/
5802static int
5803lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5804{
5805 int rc;
5806 uint32_t length, mbox_tmo = 0;
5807 LPFC_MBOXQ_t *mbox;
5808 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5809 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5810
5811 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5812 if (!mbox)
5813 return -ENOMEM;
5814
5815 /*
5816 * This function sends an embedded mailbox because it only sends the
5817 * the resource type. All extents of this type are released by the
5818 * port.
5819 */
5820 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5821 sizeof(struct lpfc_sli4_cfg_mhdr));
5822 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5823 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5824 length, LPFC_SLI4_MBX_EMBED);
5825
5826 /* Send an extents count of 0 - the dealloc doesn't use it. */
5827 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5828 LPFC_SLI4_MBX_EMBED);
5829 if (unlikely(rc)) {
5830 rc = -EIO;
5831 goto out_free_mbox;
5832 }
5833 if (!phba->sli4_hba.intr_enable)
5834 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5835 else {
James Smarta183a152011-10-10 21:32:43 -04005836 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005837 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5838 }
5839 if (unlikely(rc)) {
5840 rc = -EIO;
5841 goto out_free_mbox;
5842 }
5843
5844 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5845 if (bf_get(lpfc_mbox_hdr_status,
5846 &dealloc_rsrc->header.cfg_shdr.response)) {
5847 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5848 "2919 Failed to release resource extents "
5849 "for type %d - Status 0x%x Add'l Status 0x%x. "
5850 "Resource memory not released.\n",
5851 type,
5852 bf_get(lpfc_mbox_hdr_status,
5853 &dealloc_rsrc->header.cfg_shdr.response),
5854 bf_get(lpfc_mbox_hdr_add_status,
5855 &dealloc_rsrc->header.cfg_shdr.response));
5856 rc = -EIO;
5857 goto out_free_mbox;
5858 }
5859
5860 /* Release kernel memory resources for the specific type. */
5861 switch (type) {
5862 case LPFC_RSC_TYPE_FCOE_VPI:
5863 kfree(phba->vpi_bmask);
5864 kfree(phba->vpi_ids);
5865 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5866 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5867 &phba->lpfc_vpi_blk_list, list) {
5868 list_del_init(&rsrc_blk->list);
5869 kfree(rsrc_blk);
5870 }
James Smart16a3a202013-04-17 20:14:38 -04005871 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005872 break;
5873 case LPFC_RSC_TYPE_FCOE_XRI:
5874 kfree(phba->sli4_hba.xri_bmask);
5875 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005876 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5877 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5878 list_del_init(&rsrc_blk->list);
5879 kfree(rsrc_blk);
5880 }
5881 break;
5882 case LPFC_RSC_TYPE_FCOE_VFI:
5883 kfree(phba->sli4_hba.vfi_bmask);
5884 kfree(phba->sli4_hba.vfi_ids);
5885 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5886 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5887 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5888 list_del_init(&rsrc_blk->list);
5889 kfree(rsrc_blk);
5890 }
5891 break;
5892 case LPFC_RSC_TYPE_FCOE_RPI:
5893 /* RPI bitmask and physical id array are cleaned up earlier. */
5894 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5895 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5896 list_del_init(&rsrc_blk->list);
5897 kfree(rsrc_blk);
5898 }
5899 break;
5900 default:
5901 break;
5902 }
5903
5904 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5905
5906 out_free_mbox:
5907 mempool_free(mbox, phba->mbox_mem_pool);
5908 return rc;
5909}
5910
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08005911static void
James Smart7bdedb32016-07-06 12:36:00 -07005912lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5913 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07005914{
James Smart65791f12016-07-06 12:35:56 -07005915 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07005916
James Smart65791f12016-07-06 12:35:56 -07005917 len = sizeof(struct lpfc_mbx_set_feature) -
5918 sizeof(struct lpfc_sli4_cfg_mhdr);
5919 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5920 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5921 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07005922
James Smart7bdedb32016-07-06 12:36:00 -07005923 switch (feature) {
5924 case LPFC_SET_UE_RECOVERY:
5925 bf_set(lpfc_mbx_set_feature_UER,
5926 &mbox->u.mqe.un.set_feature, 1);
5927 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5928 mbox->u.mqe.un.set_feature.param_len = 8;
5929 break;
5930 case LPFC_SET_MDS_DIAGS:
5931 bf_set(lpfc_mbx_set_feature_mds,
5932 &mbox->u.mqe.un.set_feature, 1);
5933 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
James Smartae9e28f2017-05-15 15:20:51 -07005934 &mbox->u.mqe.un.set_feature, 1);
James Smart7bdedb32016-07-06 12:36:00 -07005935 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5936 mbox->u.mqe.un.set_feature.param_len = 8;
5937 break;
James Smart65791f12016-07-06 12:35:56 -07005938 }
James Smart7bdedb32016-07-06 12:36:00 -07005939
5940 return;
James Smart65791f12016-07-06 12:35:56 -07005941}
5942
James Smart6d368e52011-05-24 11:44:12 -04005943/**
5944 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5945 * @phba: Pointer to HBA context object.
5946 *
5947 * This function allocates all SLI4 resource identifiers.
5948 **/
5949int
5950lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5951{
5952 int i, rc, error = 0;
5953 uint16_t count, base;
5954 unsigned long longs;
5955
James Smartff78d8f2011-12-13 13:21:35 -05005956 if (!phba->sli4_hba.rpi_hdrs_in_use)
5957 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005958 if (phba->sli4_hba.extents_in_use) {
5959 /*
5960 * The port supports resource extents. The XRI, VPI, VFI, RPI
5961 * resource extent count must be read and allocated before
5962 * provisioning the resource id arrays.
5963 */
5964 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5965 LPFC_IDX_RSRC_RDY) {
5966 /*
5967 * Extent-based resources are set - the driver could
5968 * be in a port reset. Figure out if any corrective
5969 * actions need to be taken.
5970 */
5971 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5972 LPFC_RSC_TYPE_FCOE_VFI);
5973 if (rc != 0)
5974 error++;
5975 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5976 LPFC_RSC_TYPE_FCOE_VPI);
5977 if (rc != 0)
5978 error++;
5979 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5980 LPFC_RSC_TYPE_FCOE_XRI);
5981 if (rc != 0)
5982 error++;
5983 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5984 LPFC_RSC_TYPE_FCOE_RPI);
5985 if (rc != 0)
5986 error++;
5987
5988 /*
5989 * It's possible that the number of resources
5990 * provided to this port instance changed between
5991 * resets. Detect this condition and reallocate
5992 * resources. Otherwise, there is no action.
5993 */
5994 if (error) {
5995 lpfc_printf_log(phba, KERN_INFO,
5996 LOG_MBOX | LOG_INIT,
5997 "2931 Detected extent resource "
5998 "change. Reallocating all "
5999 "extents.\n");
6000 rc = lpfc_sli4_dealloc_extent(phba,
6001 LPFC_RSC_TYPE_FCOE_VFI);
6002 rc = lpfc_sli4_dealloc_extent(phba,
6003 LPFC_RSC_TYPE_FCOE_VPI);
6004 rc = lpfc_sli4_dealloc_extent(phba,
6005 LPFC_RSC_TYPE_FCOE_XRI);
6006 rc = lpfc_sli4_dealloc_extent(phba,
6007 LPFC_RSC_TYPE_FCOE_RPI);
6008 } else
6009 return 0;
6010 }
6011
6012 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6013 if (unlikely(rc))
6014 goto err_exit;
6015
6016 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6017 if (unlikely(rc))
6018 goto err_exit;
6019
6020 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6021 if (unlikely(rc))
6022 goto err_exit;
6023
6024 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6025 if (unlikely(rc))
6026 goto err_exit;
6027 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6028 LPFC_IDX_RSRC_RDY);
6029 return rc;
6030 } else {
6031 /*
6032 * The port does not support resource extents. The XRI, VPI,
6033 * VFI, RPI resource ids were determined from READ_CONFIG.
6034 * Just allocate the bitmasks and provision the resource id
6035 * arrays. If a port reset is active, the resources don't
6036 * need any action - just exit.
6037 */
6038 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05006039 LPFC_IDX_RSRC_RDY) {
6040 lpfc_sli4_dealloc_resource_identifiers(phba);
6041 lpfc_sli4_remove_rpis(phba);
6042 }
James Smart6d368e52011-05-24 11:44:12 -04006043 /* RPIs. */
6044 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05006045 if (count <= 0) {
6046 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6047 "3279 Invalid provisioning of "
6048 "rpi:%d\n", count);
6049 rc = -EINVAL;
6050 goto err_exit;
6051 }
James Smart6d368e52011-05-24 11:44:12 -04006052 base = phba->sli4_hba.max_cfg_param.rpi_base;
6053 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6054 phba->sli4_hba.rpi_bmask = kzalloc(longs *
6055 sizeof(unsigned long),
6056 GFP_KERNEL);
6057 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
6058 rc = -ENOMEM;
6059 goto err_exit;
6060 }
6061 phba->sli4_hba.rpi_ids = kzalloc(count *
6062 sizeof(uint16_t),
6063 GFP_KERNEL);
6064 if (unlikely(!phba->sli4_hba.rpi_ids)) {
6065 rc = -ENOMEM;
6066 goto free_rpi_bmask;
6067 }
6068
6069 for (i = 0; i < count; i++)
6070 phba->sli4_hba.rpi_ids[i] = base + i;
6071
6072 /* VPIs. */
6073 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05006074 if (count <= 0) {
6075 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6076 "3280 Invalid provisioning of "
6077 "vpi:%d\n", count);
6078 rc = -EINVAL;
6079 goto free_rpi_ids;
6080 }
James Smart6d368e52011-05-24 11:44:12 -04006081 base = phba->sli4_hba.max_cfg_param.vpi_base;
6082 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6083 phba->vpi_bmask = kzalloc(longs *
6084 sizeof(unsigned long),
6085 GFP_KERNEL);
6086 if (unlikely(!phba->vpi_bmask)) {
6087 rc = -ENOMEM;
6088 goto free_rpi_ids;
6089 }
6090 phba->vpi_ids = kzalloc(count *
6091 sizeof(uint16_t),
6092 GFP_KERNEL);
6093 if (unlikely(!phba->vpi_ids)) {
6094 rc = -ENOMEM;
6095 goto free_vpi_bmask;
6096 }
6097
6098 for (i = 0; i < count; i++)
6099 phba->vpi_ids[i] = base + i;
6100
6101 /* XRIs. */
6102 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05006103 if (count <= 0) {
6104 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6105 "3281 Invalid provisioning of "
6106 "xri:%d\n", count);
6107 rc = -EINVAL;
6108 goto free_vpi_ids;
6109 }
James Smart6d368e52011-05-24 11:44:12 -04006110 base = phba->sli4_hba.max_cfg_param.xri_base;
6111 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6112 phba->sli4_hba.xri_bmask = kzalloc(longs *
6113 sizeof(unsigned long),
6114 GFP_KERNEL);
6115 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6116 rc = -ENOMEM;
6117 goto free_vpi_ids;
6118 }
James Smart41899be2012-03-01 22:34:19 -05006119 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006120 phba->sli4_hba.xri_ids = kzalloc(count *
6121 sizeof(uint16_t),
6122 GFP_KERNEL);
6123 if (unlikely(!phba->sli4_hba.xri_ids)) {
6124 rc = -ENOMEM;
6125 goto free_xri_bmask;
6126 }
6127
6128 for (i = 0; i < count; i++)
6129 phba->sli4_hba.xri_ids[i] = base + i;
6130
6131 /* VFIs. */
6132 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006133 if (count <= 0) {
6134 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6135 "3282 Invalid provisioning of "
6136 "vfi:%d\n", count);
6137 rc = -EINVAL;
6138 goto free_xri_ids;
6139 }
James Smart6d368e52011-05-24 11:44:12 -04006140 base = phba->sli4_hba.max_cfg_param.vfi_base;
6141 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6142 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6143 sizeof(unsigned long),
6144 GFP_KERNEL);
6145 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6146 rc = -ENOMEM;
6147 goto free_xri_ids;
6148 }
6149 phba->sli4_hba.vfi_ids = kzalloc(count *
6150 sizeof(uint16_t),
6151 GFP_KERNEL);
6152 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6153 rc = -ENOMEM;
6154 goto free_vfi_bmask;
6155 }
6156
6157 for (i = 0; i < count; i++)
6158 phba->sli4_hba.vfi_ids[i] = base + i;
6159
6160 /*
6161 * Mark all resources ready. An HBA reset doesn't need
6162 * to reset the initialization.
6163 */
6164 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6165 LPFC_IDX_RSRC_RDY);
6166 return 0;
6167 }
6168
6169 free_vfi_bmask:
6170 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006171 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006172 free_xri_ids:
6173 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006174 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006175 free_xri_bmask:
6176 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006177 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006178 free_vpi_ids:
6179 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006180 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006181 free_vpi_bmask:
6182 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006183 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006184 free_rpi_ids:
6185 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006186 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006187 free_rpi_bmask:
6188 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006189 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006190 err_exit:
6191 return rc;
6192}
6193
6194/**
6195 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6196 * @phba: Pointer to HBA context object.
6197 *
6198 * This function allocates the number of elements for the specified
6199 * resource type.
6200 **/
6201int
6202lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6203{
6204 if (phba->sli4_hba.extents_in_use) {
6205 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6206 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6207 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6208 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6209 } else {
6210 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006211 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006212 kfree(phba->vpi_ids);
6213 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6214 kfree(phba->sli4_hba.xri_bmask);
6215 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006216 kfree(phba->sli4_hba.vfi_bmask);
6217 kfree(phba->sli4_hba.vfi_ids);
6218 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6219 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6220 }
6221
6222 return 0;
6223}
6224
6225/**
James Smartb76f2dc2011-07-22 18:37:42 -04006226 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6227 * @phba: Pointer to HBA context object.
6228 * @type: The resource extent type.
6229 * @extnt_count: buffer to hold port extent count response
6230 * @extnt_size: buffer to hold port extent size response.
6231 *
6232 * This function calls the port to read the host allocated extents
6233 * for a particular type.
6234 **/
6235int
6236lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6237 uint16_t *extnt_cnt, uint16_t *extnt_size)
6238{
6239 bool emb;
6240 int rc = 0;
6241 uint16_t curr_blks = 0;
6242 uint32_t req_len, emb_len;
6243 uint32_t alloc_len, mbox_tmo;
6244 struct list_head *blk_list_head;
6245 struct lpfc_rsrc_blks *rsrc_blk;
6246 LPFC_MBOXQ_t *mbox;
6247 void *virtaddr = NULL;
6248 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6249 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6250 union lpfc_sli4_cfg_shdr *shdr;
6251
6252 switch (type) {
6253 case LPFC_RSC_TYPE_FCOE_VPI:
6254 blk_list_head = &phba->lpfc_vpi_blk_list;
6255 break;
6256 case LPFC_RSC_TYPE_FCOE_XRI:
6257 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6258 break;
6259 case LPFC_RSC_TYPE_FCOE_VFI:
6260 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6261 break;
6262 case LPFC_RSC_TYPE_FCOE_RPI:
6263 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6264 break;
6265 default:
6266 return -EIO;
6267 }
6268
6269 /* Count the number of extents currently allocatd for this type. */
6270 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6271 if (curr_blks == 0) {
6272 /*
6273 * The GET_ALLOCATED mailbox does not return the size,
6274 * just the count. The size should be just the size
6275 * stored in the current allocated block and all sizes
6276 * for an extent type are the same so set the return
6277 * value now.
6278 */
6279 *extnt_size = rsrc_blk->rsrc_size;
6280 }
6281 curr_blks++;
6282 }
6283
James Smartb76f2dc2011-07-22 18:37:42 -04006284 /*
6285 * Calculate the size of an embedded mailbox. The uint32_t
6286 * accounts for extents-specific word.
6287 */
6288 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6289 sizeof(uint32_t);
6290
6291 /*
6292 * Presume the allocation and response will fit into an embedded
6293 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6294 */
6295 emb = LPFC_SLI4_MBX_EMBED;
6296 req_len = emb_len;
6297 if (req_len > emb_len) {
6298 req_len = curr_blks * sizeof(uint16_t) +
6299 sizeof(union lpfc_sli4_cfg_shdr) +
6300 sizeof(uint32_t);
6301 emb = LPFC_SLI4_MBX_NEMBED;
6302 }
6303
6304 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6305 if (!mbox)
6306 return -ENOMEM;
6307 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6308
6309 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6310 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6311 req_len, emb);
6312 if (alloc_len < req_len) {
6313 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6314 "2983 Allocated DMA memory size (x%x) is "
6315 "less than the requested DMA memory "
6316 "size (x%x)\n", alloc_len, req_len);
6317 rc = -ENOMEM;
6318 goto err_exit;
6319 }
6320 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6321 if (unlikely(rc)) {
6322 rc = -EIO;
6323 goto err_exit;
6324 }
6325
6326 if (!phba->sli4_hba.intr_enable)
6327 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6328 else {
James Smarta183a152011-10-10 21:32:43 -04006329 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006330 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6331 }
6332
6333 if (unlikely(rc)) {
6334 rc = -EIO;
6335 goto err_exit;
6336 }
6337
6338 /*
6339 * Figure out where the response is located. Then get local pointers
6340 * to the response data. The port does not guarantee to respond to
6341 * all extents counts request so update the local variable with the
6342 * allocated count from the port.
6343 */
6344 if (emb == LPFC_SLI4_MBX_EMBED) {
6345 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6346 shdr = &rsrc_ext->header.cfg_shdr;
6347 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6348 } else {
6349 virtaddr = mbox->sge_array->addr[0];
6350 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6351 shdr = &n_rsrc->cfg_shdr;
6352 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6353 }
6354
6355 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6356 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6357 "2984 Failed to read allocated resources "
6358 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6359 type,
6360 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6361 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6362 rc = -EIO;
6363 goto err_exit;
6364 }
6365 err_exit:
6366 lpfc_sli4_mbox_cmd_free(phba, mbox);
6367 return rc;
6368}
6369
6370/**
James Smart0ef69962017-04-21 16:04:50 -07006371 * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006372 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006373 * @pring: Pointer to driver SLI ring object.
6374 * @sgl_list: linked link of sgl buffers to post
6375 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006376 *
James Smart895427b2017-02-12 13:52:30 -08006377 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006378 * repost them to the port by using SGL block post. This is needed after a
6379 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006380 * of buffer sgls which contains contiguous xris and uses the non-embedded
6381 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006382 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6383 * mailbox command for posting.
6384 *
6385 * Returns: 0 = success, non-zero failure.
6386 **/
6387static int
James Smart895427b2017-02-12 13:52:30 -08006388lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6389 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006390{
6391 struct lpfc_sglq *sglq_entry = NULL;
6392 struct lpfc_sglq *sglq_entry_next = NULL;
6393 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006394 int status, total_cnt;
6395 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006396 int last_xritag = NO_XRI;
6397 LIST_HEAD(prep_sgl_list);
6398 LIST_HEAD(blck_sgl_list);
6399 LIST_HEAD(allc_sgl_list);
6400 LIST_HEAD(post_sgl_list);
6401 LIST_HEAD(free_sgl_list);
6402
James Smart38c20672013-03-01 16:37:44 -05006403 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006404 spin_lock(&phba->sli4_hba.sgl_list_lock);
6405 list_splice_init(sgl_list, &allc_sgl_list);
6406 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006407 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006408
James Smart895427b2017-02-12 13:52:30 -08006409 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006410 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6411 &allc_sgl_list, list) {
6412 list_del_init(&sglq_entry->list);
6413 block_cnt++;
6414 if ((last_xritag != NO_XRI) &&
6415 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6416 /* a hole in xri block, form a sgl posting block */
6417 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6418 post_cnt = block_cnt - 1;
6419 /* prepare list for next posting block */
6420 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6421 block_cnt = 1;
6422 } else {
6423 /* prepare list for next posting block */
6424 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6425 /* enough sgls for non-embed sgl mbox command */
6426 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6427 list_splice_init(&prep_sgl_list,
6428 &blck_sgl_list);
6429 post_cnt = block_cnt;
6430 block_cnt = 0;
6431 }
6432 }
6433 num_posted++;
6434
6435 /* keep track of last sgl's xritag */
6436 last_xritag = sglq_entry->sli4_xritag;
6437
James Smart895427b2017-02-12 13:52:30 -08006438 /* end of repost sgl list condition for buffers */
6439 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006440 if (post_cnt == 0) {
6441 list_splice_init(&prep_sgl_list,
6442 &blck_sgl_list);
6443 post_cnt = block_cnt;
6444 } else if (block_cnt == 1) {
6445 status = lpfc_sli4_post_sgl(phba,
6446 sglq_entry->phys, 0,
6447 sglq_entry->sli4_xritag);
6448 if (!status) {
6449 /* successful, put sgl to posted list */
6450 list_add_tail(&sglq_entry->list,
6451 &post_sgl_list);
6452 } else {
6453 /* Failure, put sgl to free list */
6454 lpfc_printf_log(phba, KERN_WARNING,
6455 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006456 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006457 "sgl, xritag:x%x\n",
6458 sglq_entry->sli4_xritag);
6459 list_add_tail(&sglq_entry->list,
6460 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006461 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006462 }
6463 }
6464 }
6465
6466 /* continue until a nembed page worth of sgls */
6467 if (post_cnt == 0)
6468 continue;
6469
James Smart895427b2017-02-12 13:52:30 -08006470 /* post the buffer list sgls as a block */
6471 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6472 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006473
6474 if (!status) {
6475 /* success, put sgl list to posted sgl list */
6476 list_splice_init(&blck_sgl_list, &post_sgl_list);
6477 } else {
6478 /* Failure, put sgl list to free sgl list */
6479 sglq_entry_first = list_first_entry(&blck_sgl_list,
6480 struct lpfc_sglq,
6481 list);
6482 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006483 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006484 "xritag:x%x-x%x\n",
6485 sglq_entry_first->sli4_xritag,
6486 (sglq_entry_first->sli4_xritag +
6487 post_cnt - 1));
6488 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006489 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006490 }
6491
6492 /* don't reset xirtag due to hole in xri block */
6493 if (block_cnt == 0)
6494 last_xritag = NO_XRI;
6495
James Smart895427b2017-02-12 13:52:30 -08006496 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006497 post_cnt = 0;
6498 }
6499
James Smart895427b2017-02-12 13:52:30 -08006500 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006501 lpfc_free_sgl_list(phba, &free_sgl_list);
6502
James Smart895427b2017-02-12 13:52:30 -08006503 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006504 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006505 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006506 spin_lock(&phba->sli4_hba.sgl_list_lock);
6507 list_splice_init(&post_sgl_list, sgl_list);
6508 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006509 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006510 } else {
6511 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006512 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006513 return -EIO;
6514 }
James Smart895427b2017-02-12 13:52:30 -08006515
6516 /* return the number of XRIs actually posted */
6517 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006518}
6519
James Smart61bda8f2016-10-13 15:06:05 -07006520void
6521lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6522{
6523 uint32_t len;
6524
6525 len = sizeof(struct lpfc_mbx_set_host_data) -
6526 sizeof(struct lpfc_sli4_cfg_mhdr);
6527 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6528 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6529 LPFC_SLI4_MBX_EMBED);
6530
6531 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006532 mbox->u.mqe.un.set_host_data.param_len =
6533 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006534 snprintf(mbox->u.mqe.un.set_host_data.data,
6535 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6536 "Linux %s v"LPFC_DRIVER_VERSION,
6537 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6538}
6539
James Smarta8cf5df2017-05-15 15:20:46 -07006540int
James Smart6c621a22017-05-15 15:20:45 -07006541lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
James Smarta8cf5df2017-05-15 15:20:46 -07006542 struct lpfc_queue *drq, int count, int idx)
James Smart6c621a22017-05-15 15:20:45 -07006543{
6544 int rc, i;
6545 struct lpfc_rqe hrqe;
6546 struct lpfc_rqe drqe;
6547 struct lpfc_rqb *rqbp;
James Smart411de512018-01-30 15:58:52 -08006548 unsigned long flags;
James Smart6c621a22017-05-15 15:20:45 -07006549 struct rqb_dmabuf *rqb_buffer;
6550 LIST_HEAD(rqb_buf_list);
6551
James Smart411de512018-01-30 15:58:52 -08006552 spin_lock_irqsave(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006553 rqbp = hrq->rqbp;
6554 for (i = 0; i < count; i++) {
6555 /* IF RQ is already full, don't bother */
6556 if (rqbp->buffer_count + i >= rqbp->entry_count - 1)
6557 break;
6558 rqb_buffer = rqbp->rqb_alloc_buffer(phba);
6559 if (!rqb_buffer)
6560 break;
6561 rqb_buffer->hrq = hrq;
6562 rqb_buffer->drq = drq;
James Smarta8cf5df2017-05-15 15:20:46 -07006563 rqb_buffer->idx = idx;
James Smart6c621a22017-05-15 15:20:45 -07006564 list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
6565 }
6566 while (!list_empty(&rqb_buf_list)) {
6567 list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
6568 hbuf.list);
6569
6570 hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
6571 hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
6572 drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
6573 drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
6574 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
6575 if (rc < 0) {
James Smart411de512018-01-30 15:58:52 -08006576 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6577 "6421 Cannot post to HRQ %d: %x %x %x "
6578 "DRQ %x %x\n",
6579 hrq->queue_id,
6580 hrq->host_index,
6581 hrq->hba_index,
6582 hrq->entry_count,
6583 drq->host_index,
6584 drq->hba_index);
James Smart6c621a22017-05-15 15:20:45 -07006585 rqbp->rqb_free_buffer(phba, rqb_buffer);
6586 } else {
6587 list_add_tail(&rqb_buffer->hbuf.list,
6588 &rqbp->rqb_buffer_list);
6589 rqbp->buffer_count++;
6590 }
6591 }
James Smart411de512018-01-30 15:58:52 -08006592 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smart6c621a22017-05-15 15:20:45 -07006593 return 1;
6594}
6595
James Smart8a9d2e82012-05-09 21:16:12 -04006596/**
Masahiro Yamada183b8022017-02-27 14:29:20 -08006597 * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
James Smartda0436e2009-05-22 14:51:39 -04006598 * @phba: Pointer to HBA context object.
6599 *
Masahiro Yamada183b8022017-02-27 14:29:20 -08006600 * This function is the main SLI4 device initialization PCI function. This
6601 * function is called by the HBA initialization code, HBA reset code and
James Smartda0436e2009-05-22 14:51:39 -04006602 * HBA error attention handler code. Caller is not required to hold any
6603 * locks.
6604 **/
6605int
6606lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6607{
James Smart6c621a22017-05-15 15:20:45 -07006608 int rc, i, cnt;
James Smartda0436e2009-05-22 14:51:39 -04006609 LPFC_MBOXQ_t *mboxq;
6610 struct lpfc_mqe *mqe;
6611 uint8_t *vpd;
6612 uint32_t vpd_size;
6613 uint32_t ftr_rsp = 0;
6614 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6615 struct lpfc_vport *vport = phba->pport;
6616 struct lpfc_dmabuf *mp;
James Smart2d7dbc42017-02-12 13:52:35 -08006617 struct lpfc_rqb *rqbp;
James Smartda0436e2009-05-22 14:51:39 -04006618
6619 /* Perform a PCI function reset to start from clean */
6620 rc = lpfc_pci_function_reset(phba);
6621 if (unlikely(rc))
6622 return -ENODEV;
6623
6624 /* Check the HBA Host Status Register for readyness */
6625 rc = lpfc_sli4_post_status_check(phba);
6626 if (unlikely(rc))
6627 return -ENODEV;
6628 else {
6629 spin_lock_irq(&phba->hbalock);
6630 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6631 spin_unlock_irq(&phba->hbalock);
6632 }
6633
6634 /*
6635 * Allocate a single mailbox container for initializing the
6636 * port.
6637 */
6638 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6639 if (!mboxq)
6640 return -ENOMEM;
6641
James Smartda0436e2009-05-22 14:51:39 -04006642 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006643 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006644 vpd = kzalloc(vpd_size, GFP_KERNEL);
6645 if (!vpd) {
6646 rc = -ENOMEM;
6647 goto out_free_mbox;
6648 }
6649
6650 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006651 if (unlikely(rc)) {
6652 kfree(vpd);
6653 goto out_free_mbox;
6654 }
James Smart572709e2013-07-15 18:32:43 -04006655
James Smartda0436e2009-05-22 14:51:39 -04006656 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006657 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006658 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006659 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006660 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6661 } else {
James Smart76a95d72010-11-20 23:11:48 -05006662 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006663 }
James Smart45ed1192009-10-02 15:17:02 -04006664
6665 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6666 LPFC_DCBX_CEE_MODE)
6667 phba->hba_flag |= HBA_FIP_SUPPORT;
6668 else
6669 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6670
James Smart4f2e66c2012-05-09 21:17:07 -04006671 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6672
James Smartc31098c2011-04-16 11:03:33 -04006673 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006674 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6675 "0376 READ_REV Error. SLI Level %d "
6676 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006677 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006678 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006679 kfree(vpd);
6680 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006681 }
James Smartcd1c8302011-10-10 21:33:25 -04006682
6683 /*
James Smartff78d8f2011-12-13 13:21:35 -05006684 * Continue initialization with default values even if driver failed
6685 * to read FCoE param config regions, only read parameters if the
6686 * board is FCoE
6687 */
6688 if (phba->hba_flag & HBA_FCOE_MODE &&
6689 lpfc_sli4_read_fcoe_params(phba))
6690 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6691 "2570 Failed to read FCoE parameters\n");
6692
6693 /*
James Smartcd1c8302011-10-10 21:33:25 -04006694 * Retrieve sli4 device physical port name, failure of doing it
6695 * is considered as non-fatal.
6696 */
6697 rc = lpfc_sli4_retrieve_pport_name(phba);
6698 if (!rc)
6699 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6700 "3080 Successful retrieving SLI4 device "
6701 "physical port name: %s.\n", phba->Port);
6702
James Smartda0436e2009-05-22 14:51:39 -04006703 /*
6704 * Evaluate the read rev and vpd data. Populate the driver
6705 * state with the results. If this routine fails, the failure
6706 * is not fatal as the driver will use generic values.
6707 */
6708 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6709 if (unlikely(!rc)) {
6710 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6711 "0377 Error %d parsing vpd. "
6712 "Using defaults.\n", rc);
6713 rc = 0;
6714 }
James Smart76a95d72010-11-20 23:11:48 -05006715 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006716
James Smartf1126682009-06-10 17:22:44 -04006717 /* Save information as VPD data */
6718 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6719 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6720 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6721 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6722 &mqe->un.read_rev);
6723 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6724 &mqe->un.read_rev);
6725 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6726 &mqe->un.read_rev);
6727 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6728 &mqe->un.read_rev);
6729 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6730 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6731 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6732 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6733 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6734 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6735 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6736 "(%d):0380 READ_REV Status x%x "
6737 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6738 mboxq->vport ? mboxq->vport->vpi : 0,
6739 bf_get(lpfc_mqe_status, mqe),
6740 phba->vpd.rev.opFwName,
6741 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6742 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006743
James Smart572709e2013-07-15 18:32:43 -04006744 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6745 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6746 if (phba->pport->cfg_lun_queue_depth > rc) {
6747 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6748 "3362 LUN queue depth changed from %d to %d\n",
6749 phba->pport->cfg_lun_queue_depth, rc);
6750 phba->pport->cfg_lun_queue_depth = rc;
6751 }
6752
James Smart65791f12016-07-06 12:35:56 -07006753 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006754 LPFC_SLI_INTF_IF_TYPE_0) {
6755 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6756 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6757 if (rc == MBX_SUCCESS) {
6758 phba->hba_flag |= HBA_RECOVERABLE_UE;
6759 /* Set 1Sec interval to detect UE */
6760 phba->eratt_poll_interval = 1;
6761 phba->sli4_hba.ue_to_sr = bf_get(
6762 lpfc_mbx_set_feature_UESR,
6763 &mboxq->u.mqe.un.set_feature);
6764 phba->sli4_hba.ue_to_rp = bf_get(
6765 lpfc_mbx_set_feature_UERP,
6766 &mboxq->u.mqe.un.set_feature);
6767 }
6768 }
6769
6770 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6771 /* Enable MDS Diagnostics only if the SLI Port supports it */
6772 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6773 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6774 if (rc != MBX_SUCCESS)
6775 phba->mds_diags_support = 0;
6776 }
James Smart572709e2013-07-15 18:32:43 -04006777
James Smartda0436e2009-05-22 14:51:39 -04006778 /*
6779 * Discover the port's supported feature set and match it against the
6780 * hosts requests.
6781 */
6782 lpfc_request_features(phba, mboxq);
6783 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6784 if (unlikely(rc)) {
6785 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006786 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006787 }
6788
6789 /*
6790 * The port must support FCP initiator mode as this is the
6791 * only mode running in the host.
6792 */
6793 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6794 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6795 "0378 No support for fcpi mode.\n");
6796 ftr_rsp++;
6797 }
James Smartfedd3b72011-02-16 12:39:24 -05006798 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6799 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6800 else
6801 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006802 /*
6803 * If the port cannot support the host's requested features
6804 * then turn off the global config parameters to disable the
6805 * feature in the driver. This is not a fatal error.
6806 */
James Smartbf086112011-08-21 21:48:13 -04006807 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6808 if (phba->cfg_enable_bg) {
6809 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6810 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6811 else
6812 ftr_rsp++;
6813 }
James Smartda0436e2009-05-22 14:51:39 -04006814
6815 if (phba->max_vpi && phba->cfg_enable_npiv &&
6816 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6817 ftr_rsp++;
6818
6819 if (ftr_rsp) {
6820 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6821 "0379 Feature Mismatch Data: x%08x %08x "
6822 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6823 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6824 phba->cfg_enable_npiv, phba->max_vpi);
6825 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6826 phba->cfg_enable_bg = 0;
6827 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6828 phba->cfg_enable_npiv = 0;
6829 }
6830
6831 /* These SLI3 features are assumed in SLI4 */
6832 spin_lock_irq(&phba->hbalock);
6833 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6834 spin_unlock_irq(&phba->hbalock);
6835
James Smart6d368e52011-05-24 11:44:12 -04006836 /*
6837 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6838 * calls depends on these resources to complete port setup.
6839 */
6840 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6841 if (rc) {
6842 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6843 "2920 Failed to alloc Resource IDs "
6844 "rc = x%x\n", rc);
6845 goto out_free_mbox;
6846 }
6847
James Smart61bda8f2016-10-13 15:06:05 -07006848 lpfc_set_host_data(phba, mboxq);
6849
6850 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6851 if (rc) {
6852 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6853 "2134 Failed to set host os driver version %x",
6854 rc);
6855 }
6856
James Smartda0436e2009-05-22 14:51:39 -04006857 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006858 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6859 if (rc) {
6860 phba->link_state = LPFC_HBA_ERROR;
6861 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006862 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006863 }
6864
James Smartda0436e2009-05-22 14:51:39 -04006865 mboxq->vport = vport;
6866 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6867 mp = (struct lpfc_dmabuf *) mboxq->context1;
6868 if (rc == MBX_SUCCESS) {
6869 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6870 rc = 0;
6871 }
6872
6873 /*
6874 * This memory was allocated by the lpfc_read_sparam routine. Release
6875 * it to the mbuf pool.
6876 */
6877 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6878 kfree(mp);
6879 mboxq->context1 = NULL;
6880 if (unlikely(rc)) {
6881 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6882 "0382 READ_SPARAM command failed "
6883 "status %d, mbxStatus x%x\n",
6884 rc, bf_get(lpfc_mqe_status, mqe));
6885 phba->link_state = LPFC_HBA_ERROR;
6886 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006887 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006888 }
6889
James Smart05580562011-05-24 11:40:48 -04006890 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006891
6892 /* Update the fc_host data structures with new wwn. */
6893 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6894 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6895
James Smart895427b2017-02-12 13:52:30 -08006896 /* Create all the SLI4 queues */
6897 rc = lpfc_sli4_queue_create(phba);
6898 if (rc) {
6899 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6900 "3089 Failed to allocate queues\n");
6901 rc = -ENODEV;
6902 goto out_free_mbox;
6903 }
6904 /* Set up all the queues to the device */
6905 rc = lpfc_sli4_queue_setup(phba);
6906 if (unlikely(rc)) {
6907 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6908 "0381 Error %d during queue setup.\n ", rc);
6909 goto out_stop_timers;
6910 }
6911 /* Initialize the driver internal SLI layer lists. */
6912 lpfc_sli4_setup(phba);
6913 lpfc_sli4_queue_init(phba);
6914
6915 /* update host els xri-sgl sizes and mappings */
6916 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006917 if (unlikely(rc)) {
6918 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6919 "1400 Failed to update xri-sgl size and "
6920 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08006921 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006922 }
6923
James Smart8a9d2e82012-05-09 21:16:12 -04006924 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08006925 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6926 phba->sli4_hba.els_xri_cnt);
6927 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04006928 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6929 "0582 Error %d during els sgl post "
6930 "operation\n", rc);
6931 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08006932 goto out_destroy_queue;
6933 }
6934 phba->sli4_hba.els_xri_cnt = rc;
6935
James Smartf358dd02017-02-12 13:52:34 -08006936 if (phba->nvmet_support) {
6937 /* update host nvmet xri-sgl sizes and mappings */
6938 rc = lpfc_sli4_nvmet_sgl_update(phba);
6939 if (unlikely(rc)) {
6940 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6941 "6308 Failed to update nvmet-sgl size "
6942 "and mapping: %d\n", rc);
6943 goto out_destroy_queue;
6944 }
6945
6946 /* register the nvmet sgl pool to the port */
6947 rc = lpfc_sli4_repost_sgl_list(
6948 phba,
6949 &phba->sli4_hba.lpfc_nvmet_sgl_list,
6950 phba->sli4_hba.nvmet_xri_cnt);
6951 if (unlikely(rc < 0)) {
6952 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6953 "3117 Error %d during nvmet "
6954 "sgl post\n", rc);
6955 rc = -ENODEV;
6956 goto out_destroy_queue;
6957 }
6958 phba->sli4_hba.nvmet_xri_cnt = rc;
James Smart6c621a22017-05-15 15:20:45 -07006959
6960 cnt = phba->cfg_iocb_cnt * 1024;
6961 /* We need 1 iocbq for every SGL, for IO processing */
6962 cnt += phba->sli4_hba.nvmet_xri_cnt;
James Smartf358dd02017-02-12 13:52:34 -08006963 } else {
James Smart895427b2017-02-12 13:52:30 -08006964 /* update host scsi xri-sgl sizes and mappings */
6965 rc = lpfc_sli4_scsi_sgl_update(phba);
6966 if (unlikely(rc)) {
6967 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6968 "6309 Failed to update scsi-sgl size "
6969 "and mapping: %d\n", rc);
6970 goto out_destroy_queue;
6971 }
6972
6973 /* update host nvme xri-sgl sizes and mappings */
6974 rc = lpfc_sli4_nvme_sgl_update(phba);
6975 if (unlikely(rc)) {
6976 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6977 "6082 Failed to update nvme-sgl size "
6978 "and mapping: %d\n", rc);
6979 goto out_destroy_queue;
6980 }
James Smart6c621a22017-05-15 15:20:45 -07006981
6982 cnt = phba->cfg_iocb_cnt * 1024;
James Smart11e644e2017-06-15 22:56:48 -07006983 }
6984
6985 if (!phba->sli.iocbq_lookup) {
James Smart6c621a22017-05-15 15:20:45 -07006986 /* Initialize and populate the iocb list per host */
6987 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07006988 "2821 initialize iocb list %d total %d\n",
James Smart6c621a22017-05-15 15:20:45 -07006989 phba->cfg_iocb_cnt, cnt);
6990 rc = lpfc_init_iocb_list(phba, cnt);
6991 if (rc) {
6992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart11e644e2017-06-15 22:56:48 -07006993 "1413 Failed to init iocb list.\n");
James Smart6c621a22017-05-15 15:20:45 -07006994 goto out_destroy_queue;
6995 }
James Smart8a9d2e82012-05-09 21:16:12 -04006996 }
6997
James Smart11e644e2017-06-15 22:56:48 -07006998 if (phba->nvmet_support)
6999 lpfc_nvmet_create_targetport(phba);
7000
James Smart2d7dbc42017-02-12 13:52:35 -08007001 if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
James Smart2d7dbc42017-02-12 13:52:35 -08007002 /* Post initial buffers to all RQs created */
7003 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
7004 rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
7005 INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
7006 rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
7007 rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
James Smart61f3d4b2017-05-15 15:20:41 -07007008 rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
James Smart2d7dbc42017-02-12 13:52:35 -08007009 rqbp->buffer_count = 0;
7010
James Smart2d7dbc42017-02-12 13:52:35 -08007011 lpfc_post_rq_buffer(
7012 phba, phba->sli4_hba.nvmet_mrq_hdr[i],
7013 phba->sli4_hba.nvmet_mrq_data[i],
James Smarta8cf5df2017-05-15 15:20:46 -07007014 LPFC_NVMET_RQE_DEF_COUNT, i);
James Smart2d7dbc42017-02-12 13:52:35 -08007015 }
7016 }
7017
James Smart895427b2017-02-12 13:52:30 -08007018 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
7019 /* register the allocated scsi sgl pool to the port */
7020 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
7021 if (unlikely(rc)) {
7022 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7023 "0383 Error %d during scsi sgl post "
7024 "operation\n", rc);
7025 /* Some Scsi buffers were moved to abort scsi list */
7026 /* A pci function reset will repost them */
7027 rc = -ENODEV;
7028 goto out_destroy_queue;
7029 }
James Smartda0436e2009-05-22 14:51:39 -04007030 }
7031
James Smart01649562017-02-12 13:52:32 -08007032 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
7033 (phba->nvmet_support == 0)) {
7034
7035 /* register the allocated nvme sgl pool to the port */
7036 rc = lpfc_repost_nvme_sgl_list(phba);
7037 if (unlikely(rc)) {
7038 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7039 "6116 Error %d during nvme sgl post "
7040 "operation\n", rc);
7041 /* Some NVME buffers were moved to abort nvme list */
7042 /* A pci function reset will repost them */
7043 rc = -ENODEV;
7044 goto out_destroy_queue;
7045 }
James Smartda0436e2009-05-22 14:51:39 -04007046 }
7047
7048 /* Post the rpi header region to the device. */
7049 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
7050 if (unlikely(rc)) {
7051 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7052 "0393 Error %d during rpi post operation\n",
7053 rc);
7054 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08007055 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04007056 }
James Smart97f2ecf2012-03-01 22:35:23 -05007057 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04007058
James Smart895427b2017-02-12 13:52:30 -08007059 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart2d7dbc42017-02-12 13:52:35 -08007060 if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
James Smart895427b2017-02-12 13:52:30 -08007061 /*
7062 * The FC Port needs to register FCFI (index 0)
7063 */
7064 lpfc_reg_fcfi(phba, mboxq);
7065 mboxq->vport = phba->pport;
7066 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7067 if (rc != MBX_SUCCESS)
7068 goto out_unset_queue;
7069 rc = 0;
7070 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
7071 &mboxq->u.mqe.un.reg_fcfi);
James Smart2d7dbc42017-02-12 13:52:35 -08007072 } else {
7073 /* We are a NVME Target mode with MRQ > 1 */
7074
7075 /* First register the FCFI */
7076 lpfc_reg_fcfi_mrq(phba, mboxq, 0);
7077 mboxq->vport = phba->pport;
7078 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7079 if (rc != MBX_SUCCESS)
7080 goto out_unset_queue;
7081 rc = 0;
7082 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
7083 &mboxq->u.mqe.un.reg_fcfi_mrq);
7084
7085 /* Next register the MRQs */
7086 lpfc_reg_fcfi_mrq(phba, mboxq, 1);
7087 mboxq->vport = phba->pport;
7088 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7089 if (rc != MBX_SUCCESS)
7090 goto out_unset_queue;
7091 rc = 0;
James Smart895427b2017-02-12 13:52:30 -08007092 }
7093 /* Check if the port is configured to be disabled */
7094 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04007095 }
7096
7097 /* Arm the CQs and then EQs on device */
7098 lpfc_sli4_arm_cqeq_intr(phba);
7099
7100 /* Indicate device interrupt mode */
7101 phba->sli4_hba.intr_enable = 1;
7102
7103 /* Allow asynchronous mailbox command to go through */
7104 spin_lock_irq(&phba->hbalock);
7105 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7106 spin_unlock_irq(&phba->hbalock);
7107
7108 /* Post receive buffers to the device */
7109 lpfc_sli4_rb_setup(phba);
7110
James Smartfc2b9892010-02-26 14:15:29 -05007111 /* Reset HBA FCF states after HBA reset */
7112 phba->fcf.fcf_flag = 0;
7113 phba->fcf.current_rec.flag = 0;
7114
James Smartda0436e2009-05-22 14:51:39 -04007115 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04007116 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007117 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04007118
7119 /* Start heart beat timer */
7120 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04007121 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04007122 phba->hb_outstanding = 0;
7123 phba->last_completion_time = jiffies;
7124
7125 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04007126 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07007127 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04007128
James Smart75baf692010-06-08 18:31:21 -04007129 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
7130 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
7131 rc = pci_enable_pcie_error_reporting(phba->pcidev);
7132 if (!rc) {
7133 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7134 "2829 This device supports "
7135 "Advanced Error Reporting (AER)\n");
7136 spin_lock_irq(&phba->hbalock);
7137 phba->hba_flag |= HBA_AER_ENABLED;
7138 spin_unlock_irq(&phba->hbalock);
7139 } else {
7140 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7141 "2830 This device does not support "
7142 "Advanced Error Reporting (AER)\n");
7143 phba->cfg_aer_support = 0;
7144 }
James Smart0a96e972011-07-22 18:37:28 -04007145 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04007146 }
7147
James Smartda0436e2009-05-22 14:51:39 -04007148 /*
7149 * The port is ready, set the host's link state to LINK_DOWN
7150 * in preparation for link interrupts.
7151 */
James Smartda0436e2009-05-22 14:51:39 -04007152 spin_lock_irq(&phba->hbalock);
7153 phba->link_state = LPFC_LINK_DOWN;
7154 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05007155 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
7156 (phba->hba_flag & LINK_DISABLED)) {
7157 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7158 "3103 Adapter Link is disabled.\n");
7159 lpfc_down_link(phba, mboxq);
7160 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7161 if (rc != MBX_SUCCESS) {
7162 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
7163 "3104 Adapter failed to issue "
7164 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
7165 goto out_unset_queue;
7166 }
7167 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05007168 /* don't perform init_link on SLI4 FC port loopback test */
7169 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
7170 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
7171 if (rc)
7172 goto out_unset_queue;
7173 }
James Smart5350d872011-10-10 21:33:49 -04007174 }
7175 mempool_free(mboxq, phba->mbox_mem_pool);
7176 return rc;
James Smart76a95d72010-11-20 23:11:48 -05007177out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04007178 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04007179 lpfc_sli4_queue_unset(phba);
7180out_destroy_queue:
James Smart6c621a22017-05-15 15:20:45 -07007181 lpfc_free_iocb_list(phba);
James Smart5350d872011-10-10 21:33:49 -04007182 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04007183out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04007184 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04007185out_free_mbox:
7186 mempool_free(mboxq, phba->mbox_mem_pool);
7187 return rc;
7188}
James Smarte59058c2008-08-24 21:49:00 -04007189
7190/**
James Smart3621a712009-04-06 18:47:14 -04007191 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04007192 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05007193 *
James Smarte59058c2008-08-24 21:49:00 -04007194 * This is the callback function for mailbox timer. The mailbox
7195 * timer is armed when a new mailbox command is issued and the timer
7196 * is deleted when the mailbox complete. The function is called by
7197 * the kernel timer code when a mailbox does not complete within
7198 * expected time. This function wakes up the worker thread to
7199 * process the mailbox timeout and returns. All the processing is
7200 * done by the worker thread function lpfc_mbox_timeout_handler.
7201 **/
dea31012005-04-17 16:05:31 -05007202void
Kees Cookf22eb4d2017-09-06 20:24:26 -07007203lpfc_mbox_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05007204{
Kees Cookf22eb4d2017-09-06 20:24:26 -07007205 struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
dea31012005-04-17 16:05:31 -05007206 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05007207 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05007208
James Smart2e0fef82007-06-17 19:56:36 -05007209 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007210 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05007211 if (!tmo_posted)
7212 phba->pport->work_port_events |= WORKER_MBOX_TMO;
7213 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
7214
James Smart5e9d9b82008-06-14 22:52:53 -04007215 if (!tmo_posted)
7216 lpfc_worker_wake_up(phba);
7217 return;
dea31012005-04-17 16:05:31 -05007218}
7219
James Smarte8d3c3b2013-10-10 12:21:30 -04007220/**
7221 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
7222 * are pending
7223 * @phba: Pointer to HBA context object.
7224 *
7225 * This function checks if any mailbox completions are present on the mailbox
7226 * completion queue.
7227 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04007228static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04007229lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
7230{
7231
7232 uint32_t idx;
7233 struct lpfc_queue *mcq;
7234 struct lpfc_mcqe *mcqe;
7235 bool pending_completions = false;
7236
7237 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7238 return false;
7239
7240 /* Check for completions on mailbox completion queue */
7241
7242 mcq = phba->sli4_hba.mbx_cq;
7243 idx = mcq->hba_index;
7244 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
7245 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
7246 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
7247 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
7248 pending_completions = true;
7249 break;
7250 }
7251 idx = (idx + 1) % mcq->entry_count;
7252 if (mcq->hba_index == idx)
7253 break;
7254 }
7255 return pending_completions;
7256
7257}
7258
7259/**
7260 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7261 * that were missed.
7262 * @phba: Pointer to HBA context object.
7263 *
7264 * For sli4, it is possible to miss an interrupt. As such mbox completions
7265 * maybe missed causing erroneous mailbox timeouts to occur. This function
7266 * checks to see if mbox completions are on the mailbox completion queue
7267 * and will process all the completions associated with the eq for the
7268 * mailbox completion queue.
7269 **/
7270bool
7271lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7272{
7273
7274 uint32_t eqidx;
7275 struct lpfc_queue *fpeq = NULL;
7276 struct lpfc_eqe *eqe;
7277 bool mbox_pending;
7278
7279 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7280 return false;
7281
7282 /* Find the eq associated with the mcq */
7283
7284 if (phba->sli4_hba.hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007285 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smarte8d3c3b2013-10-10 12:21:30 -04007286 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7287 phba->sli4_hba.mbx_cq->assoc_qid) {
7288 fpeq = phba->sli4_hba.hba_eq[eqidx];
7289 break;
7290 }
7291 if (!fpeq)
7292 return false;
7293
7294 /* Turn off interrupts from this EQ */
7295
7296 lpfc_sli4_eq_clr_intr(fpeq);
7297
7298 /* Check to see if a mbox completion is pending */
7299
7300 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7301
7302 /*
7303 * If a mbox completion is pending, process all the events on EQ
7304 * associated with the mbox completion queue (this could include
7305 * mailbox commands, async events, els commands, receive queue data
7306 * and fcp commands)
7307 */
7308
7309 if (mbox_pending)
7310 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7311 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7312 fpeq->EQ_processed++;
7313 }
7314
7315 /* Always clear and re-arm the EQ */
7316
7317 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7318
7319 return mbox_pending;
7320
7321}
James Smarte59058c2008-08-24 21:49:00 -04007322
7323/**
James Smart3621a712009-04-06 18:47:14 -04007324 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007325 * @phba: Pointer to HBA context object.
7326 *
7327 * This function is called from worker thread when a mailbox command times out.
7328 * The caller is not required to hold any locks. This function will reset the
7329 * HBA and recover all the pending commands.
7330 **/
dea31012005-04-17 16:05:31 -05007331void
7332lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7333{
James Smart2e0fef82007-06-17 19:56:36 -05007334 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007335 MAILBOX_t *mb = NULL;
7336
James Smart1dcb58e2007-04-25 09:51:30 -04007337 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007338
James Smarte8d3c3b2013-10-10 12:21:30 -04007339 /* If the mailbox completed, process the completion and return */
7340 if (lpfc_sli4_process_missed_mbox_completions(phba))
7341 return;
7342
James Smarteb016562014-09-03 12:58:06 -04007343 if (pmbox != NULL)
7344 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007345 /* Check the pmbox pointer first. There is a race condition
7346 * between the mbox timeout handler getting executed in the
7347 * worklist and the mailbox actually completing. When this
7348 * race condition occurs, the mbox_active will be NULL.
7349 */
7350 spin_lock_irq(&phba->hbalock);
7351 if (pmbox == NULL) {
7352 lpfc_printf_log(phba, KERN_WARNING,
7353 LOG_MBOX | LOG_SLI,
7354 "0353 Active Mailbox cleared - mailbox timeout "
7355 "exiting\n");
7356 spin_unlock_irq(&phba->hbalock);
7357 return;
7358 }
7359
dea31012005-04-17 16:05:31 -05007360 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007361 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007362 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007363 mb->mbxCommand,
7364 phba->pport->port_state,
7365 phba->sli.sli_flag,
7366 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007367 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007368
James Smart1dcb58e2007-04-25 09:51:30 -04007369 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7370 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007371 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007372 */
James Smart2e0fef82007-06-17 19:56:36 -05007373 spin_lock_irq(&phba->pport->work_port_lock);
7374 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7375 spin_unlock_irq(&phba->pport->work_port_lock);
7376 spin_lock_irq(&phba->hbalock);
7377 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007378 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007379 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007380
James Smartdb55fba2014-04-04 13:52:02 -04007381 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007382
7383 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007384 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007385
7386 /* Reset the HBA device */
7387 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007388}
7389
James Smarte59058c2008-08-24 21:49:00 -04007390/**
James Smart3772a992009-05-22 14:50:54 -04007391 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007392 * @phba: Pointer to HBA context object.
7393 * @pmbox: Pointer to mailbox object.
7394 * @flag: Flag indicating how the mailbox need to be processed.
7395 *
7396 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007397 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7398 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007399 * The mailbox command can be submitted in polling mode, in which case
7400 * this function will wait in a polling loop for the completion of the
7401 * mailbox.
7402 * If the mailbox is submitted in no_wait mode (not polling) the
7403 * function will submit the command and returns immediately without waiting
7404 * for the mailbox completion. The no_wait is supported only when HBA
7405 * is in SLI2/SLI3 mode - interrupts are enabled.
7406 * The SLI interface allows only one mailbox pending at a time. If the
7407 * mailbox is issued in polling mode and there is already a mailbox
7408 * pending, then the function will return an error. If the mailbox is issued
7409 * in NO_WAIT mode and there is a mailbox pending already, the function
7410 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7411 * The sli layer owns the mailbox object until the completion of mailbox
7412 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7413 * return codes the caller owns the mailbox command after the return of
7414 * the function.
7415 **/
James Smart3772a992009-05-22 14:50:54 -04007416static int
7417lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7418 uint32_t flag)
dea31012005-04-17 16:05:31 -05007419{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007420 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007421 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007422 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007423 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007424 int i;
James Smart09372822008-01-11 01:52:54 -05007425 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007426 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007427 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007428 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007429 int processing_queue = 0;
7430
7431 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7432 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007433 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007434 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007435 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7436 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7437 return MBX_SUCCESS;
7438 }
James Smart58da1ff2008-04-07 10:15:56 -04007439 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007440 pmbox = lpfc_mbox_get(phba);
7441 if (!pmbox) {
7442 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7443 return MBX_SUCCESS;
7444 }
7445 }
dea31012005-04-17 16:05:31 -05007446
James Smarted957682007-06-17 19:56:37 -05007447 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007448 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007449 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007450 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007451 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007452 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007453 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007454 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007455 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007456 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007457 }
7458 }
7459
Linas Vepstas8d63f372007-02-14 14:28:36 -06007460 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007461 if (unlikely(pci_channel_offline(phba->pcidev))) {
7462 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7463 goto out_not_finished;
7464 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007465
James Smarta257bf92009-04-06 18:48:10 -04007466 /* If HBA has a deferred error attention, fail the iocb. */
7467 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7468 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7469 goto out_not_finished;
7470 }
7471
dea31012005-04-17 16:05:31 -05007472 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007473
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007474 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007475 status = MBX_SUCCESS;
7476
James Smart2e0fef82007-06-17 19:56:36 -05007477 if (phba->link_state == LPFC_HBA_ERROR) {
7478 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007479
7480 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007481 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7482 "(%d):0311 Mailbox command x%x cannot "
7483 "issue Data: x%x x%x\n",
7484 pmbox->vport ? pmbox->vport->vpi : 0,
7485 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007486 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007487 }
7488
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007489 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007490 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7491 !(hc_copy & HC_MBINT_ENA)) {
7492 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7493 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007494 "(%d):2528 Mailbox command x%x cannot "
7495 "issue Data: x%x x%x\n",
7496 pmbox->vport ? pmbox->vport->vpi : 0,
7497 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007498 goto out_not_finished;
7499 }
James Smart92908312006-03-07 15:04:13 -05007500 }
7501
dea31012005-04-17 16:05:31 -05007502 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7503 /* Polling for a mbox command when another one is already active
7504 * is not allowed in SLI. Also, the driver must have established
7505 * SLI2 mode to queue and process multiple mbox commands.
7506 */
7507
7508 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007509 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007510
7511 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007512 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7513 "(%d):2529 Mailbox command x%x "
7514 "cannot issue Data: x%x x%x\n",
7515 pmbox->vport ? pmbox->vport->vpi : 0,
7516 pmbox->u.mb.mbxCommand,
7517 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007518 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007519 }
7520
James Smart3772a992009-05-22 14:50:54 -04007521 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007522 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007523 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007524 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7525 "(%d):2530 Mailbox command x%x "
7526 "cannot issue Data: x%x x%x\n",
7527 pmbox->vport ? pmbox->vport->vpi : 0,
7528 pmbox->u.mb.mbxCommand,
7529 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007530 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007531 }
7532
dea31012005-04-17 16:05:31 -05007533 /* Another mailbox command is still being processed, queue this
7534 * command to be processed later.
7535 */
7536 lpfc_mbox_put(phba, pmbox);
7537
7538 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007539 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007540 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007541 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007542 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
James Smarte92974f2017-06-01 21:07:06 -07007543 mbx->mbxCommand,
7544 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007545 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007546
7547 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007548 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007549
James Smart858c9f62007-06-17 19:56:39 -05007550 if (pmbox->vport) {
7551 lpfc_debugfs_disc_trc(pmbox->vport,
7552 LPFC_DISC_TRC_MBOX_VPORT,
7553 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007554 (uint32_t)mbx->mbxCommand,
7555 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007556 }
7557 else {
7558 lpfc_debugfs_disc_trc(phba->pport,
7559 LPFC_DISC_TRC_MBOX,
7560 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007561 (uint32_t)mbx->mbxCommand,
7562 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007563 }
7564
James Smart2e0fef82007-06-17 19:56:36 -05007565 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007566 }
7567
dea31012005-04-17 16:05:31 -05007568 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7569
7570 /* If we are not polling, we MUST be in SLI2 mode */
7571 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007572 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007573 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007574 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007575 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007576 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007577 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7578 "(%d):2531 Mailbox command x%x "
7579 "cannot issue Data: x%x x%x\n",
7580 pmbox->vport ? pmbox->vport->vpi : 0,
7581 pmbox->u.mb.mbxCommand,
7582 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007583 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007584 }
7585 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007586 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7587 1000);
7588 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007589 }
7590
7591 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007592 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007593 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007594 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007595 pmbox->vport ? pmbox->vport->vpi : 0,
James Smarte92974f2017-06-01 21:07:06 -07007596 mbx->mbxCommand,
7597 phba->pport ? phba->pport->port_state : 0xff,
James Smart92d7f7b2007-06-17 19:56:38 -05007598 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007599
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007600 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007601 if (pmbox->vport) {
7602 lpfc_debugfs_disc_trc(pmbox->vport,
7603 LPFC_DISC_TRC_MBOX_VPORT,
7604 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007605 (uint32_t)mbx->mbxCommand,
7606 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007607 }
7608 else {
7609 lpfc_debugfs_disc_trc(phba->pport,
7610 LPFC_DISC_TRC_MBOX,
7611 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007612 (uint32_t)mbx->mbxCommand,
7613 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007614 }
7615 }
7616
dea31012005-04-17 16:05:31 -05007617 psli->slistat.mbox_cmd++;
7618 evtctr = psli->slistat.mbox_event;
7619
7620 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007621 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007622
James Smart3772a992009-05-22 14:50:54 -04007623 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007624 /* Populate mbox extension offset word. */
7625 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007626 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007627 = (uint8_t *)phba->mbox_ext
7628 - (uint8_t *)phba->mbox;
7629 }
7630
7631 /* Copy the mailbox extension data */
7632 if (pmbox->in_ext_byte_len && pmbox->context2) {
7633 lpfc_sli_pcimem_bcopy(pmbox->context2,
7634 (uint8_t *)phba->mbox_ext,
7635 pmbox->in_ext_byte_len);
7636 }
7637 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007638 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007639 } else {
James Smart7a470272010-03-15 11:25:20 -04007640 /* Populate mbox extension offset word. */
7641 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007642 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007643 = MAILBOX_HBA_EXT_OFFSET;
7644
7645 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007646 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007647 lpfc_memcpy_to_slim(phba->MBslimaddr +
7648 MAILBOX_HBA_EXT_OFFSET,
7649 pmbox->context2, pmbox->in_ext_byte_len);
7650
James Smart895427b2017-02-12 13:52:30 -08007651 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007652 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007653 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7654 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007655
7656 /* First copy mbox command data to HBA SLIM, skip past first
7657 word */
7658 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007659 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007660 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7661
7662 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007663 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007664 to_slim = phba->MBslimaddr;
7665 writel(ldata, to_slim);
7666 readl(to_slim); /* flush */
7667
James Smart895427b2017-02-12 13:52:30 -08007668 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007669 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007670 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007671 }
7672
7673 wmb();
dea31012005-04-17 16:05:31 -05007674
7675 switch (flag) {
7676 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007677 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007678 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007679 /* Interrupt board to do it */
7680 writel(CA_MBATT, phba->CAregaddr);
7681 readl(phba->CAregaddr); /* flush */
7682 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007683 break;
7684
7685 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007686 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007687 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007688 /* Interrupt board to do it */
7689 writel(CA_MBATT, phba->CAregaddr);
7690 readl(phba->CAregaddr); /* flush */
7691
James Smart3772a992009-05-22 14:50:54 -04007692 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007693 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007694 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007695 word0 = le32_to_cpu(word0);
7696 } else {
7697 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007698 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7699 spin_unlock_irqrestore(&phba->hbalock,
7700 drvr_flag);
7701 goto out_not_finished;
7702 }
dea31012005-04-17 16:05:31 -05007703 }
7704
7705 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007706 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7707 spin_unlock_irqrestore(&phba->hbalock,
7708 drvr_flag);
7709 goto out_not_finished;
7710 }
James Smarta183a152011-10-10 21:32:43 -04007711 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7712 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007713 i = 0;
dea31012005-04-17 16:05:31 -05007714 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007715 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7716 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007717 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007718 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007719 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007720 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007721 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007722 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007723 }
7724
7725 /* Check if we took a mbox interrupt while we were
7726 polling */
7727 if (((word0 & OWN_CHIP) != OWN_CHIP)
7728 && (evtctr != psli->slistat.mbox_event))
7729 break;
7730
James Smart09372822008-01-11 01:52:54 -05007731 if (i++ > 10) {
7732 spin_unlock_irqrestore(&phba->hbalock,
7733 drvr_flag);
7734 msleep(1);
7735 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7736 }
dea31012005-04-17 16:05:31 -05007737
James Smart3772a992009-05-22 14:50:54 -04007738 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007739 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007740 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007741 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007742 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007743 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007744 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007745 /* Check real SLIM for any errors */
7746 slimword0 = readl(phba->MBslimaddr);
7747 slimmb = (MAILBOX_t *) & slimword0;
7748 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7749 && slimmb->mbxStatus) {
7750 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007751 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007752 word0 = slimword0;
7753 }
7754 }
7755 } else {
7756 /* First copy command data */
7757 word0 = readl(phba->MBslimaddr);
7758 }
7759 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007760 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7761 spin_unlock_irqrestore(&phba->hbalock,
7762 drvr_flag);
7763 goto out_not_finished;
7764 }
dea31012005-04-17 16:05:31 -05007765 }
7766
James Smart3772a992009-05-22 14:50:54 -04007767 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007768 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007769 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7770 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007771 /* Copy the mailbox extension data */
7772 if (pmbox->out_ext_byte_len && pmbox->context2) {
7773 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7774 pmbox->context2,
7775 pmbox->out_ext_byte_len);
7776 }
dea31012005-04-17 16:05:31 -05007777 } else {
7778 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007779 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007780 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007781 /* Copy the mailbox extension data */
7782 if (pmbox->out_ext_byte_len && pmbox->context2) {
7783 lpfc_memcpy_from_slim(pmbox->context2,
7784 phba->MBslimaddr +
7785 MAILBOX_HBA_EXT_OFFSET,
7786 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007787 }
7788 }
7789
7790 writel(HA_MBATT, phba->HAregaddr);
7791 readl(phba->HAregaddr); /* flush */
7792
7793 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007794 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007795 }
7796
James Smart2e0fef82007-06-17 19:56:36 -05007797 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7798 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007799
7800out_not_finished:
7801 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007802 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007803 lpfc_mbox_cmpl_put(phba, pmbox);
7804 }
7805 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007806}
7807
James Smarte59058c2008-08-24 21:49:00 -04007808/**
James Smartf1126682009-06-10 17:22:44 -04007809 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7810 * @phba: Pointer to HBA context object.
7811 *
7812 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7813 * the driver internal pending mailbox queue. It will then try to wait out the
7814 * possible outstanding mailbox command before return.
7815 *
7816 * Returns:
7817 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7818 * the outstanding mailbox command timed out.
7819 **/
7820static int
7821lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7822{
7823 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007824 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007825 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007826
7827 /* Mark the asynchronous mailbox command posting as blocked */
7828 spin_lock_irq(&phba->hbalock);
7829 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007830 /* Determine how long we might wait for the active mailbox
7831 * command to be gracefully completed by firmware.
7832 */
James Smarta183a152011-10-10 21:32:43 -04007833 if (phba->sli.mbox_active)
7834 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7835 phba->sli.mbox_active) *
7836 1000) + jiffies;
7837 spin_unlock_irq(&phba->hbalock);
7838
James Smarte8d3c3b2013-10-10 12:21:30 -04007839 /* Make sure the mailbox is really active */
7840 if (timeout)
7841 lpfc_sli4_process_missed_mbox_completions(phba);
7842
James Smartf1126682009-06-10 17:22:44 -04007843 /* Wait for the outstnading mailbox command to complete */
7844 while (phba->sli.mbox_active) {
7845 /* Check active mailbox complete status every 2ms */
7846 msleep(2);
7847 if (time_after(jiffies, timeout)) {
7848 /* Timeout, marked the outstanding cmd not complete */
7849 rc = 1;
7850 break;
7851 }
7852 }
7853
7854 /* Can not cleanly block async mailbox command, fails it */
7855 if (rc) {
7856 spin_lock_irq(&phba->hbalock);
7857 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7858 spin_unlock_irq(&phba->hbalock);
7859 }
7860 return rc;
7861}
7862
7863/**
7864 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7865 * @phba: Pointer to HBA context object.
7866 *
7867 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7868 * commands from the driver internal pending mailbox queue. It makes sure
7869 * that there is no outstanding mailbox command before resuming posting
7870 * asynchronous mailbox commands. If, for any reason, there is outstanding
7871 * mailbox command, it will try to wait it out before resuming asynchronous
7872 * mailbox command posting.
7873 **/
7874static void
7875lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7876{
7877 struct lpfc_sli *psli = &phba->sli;
7878
7879 spin_lock_irq(&phba->hbalock);
7880 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7881 /* Asynchronous mailbox posting is not blocked, do nothing */
7882 spin_unlock_irq(&phba->hbalock);
7883 return;
7884 }
7885
7886 /* Outstanding synchronous mailbox command is guaranteed to be done,
7887 * successful or timeout, after timing-out the outstanding mailbox
7888 * command shall always be removed, so just unblock posting async
7889 * mailbox command and resume
7890 */
7891 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7892 spin_unlock_irq(&phba->hbalock);
7893
7894 /* wake up worker thread to post asynchronlous mailbox command */
7895 lpfc_worker_wake_up(phba);
7896}
7897
7898/**
James Smart2d843ed2012-09-29 11:29:06 -04007899 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7900 * @phba: Pointer to HBA context object.
7901 * @mboxq: Pointer to mailbox object.
7902 *
7903 * The function waits for the bootstrap mailbox register ready bit from
7904 * port for twice the regular mailbox command timeout value.
7905 *
7906 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7907 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7908 **/
7909static int
7910lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7911{
7912 uint32_t db_ready;
7913 unsigned long timeout;
7914 struct lpfc_register bmbx_reg;
7915
7916 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7917 * 1000) + jiffies;
7918
7919 do {
7920 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7921 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7922 if (!db_ready)
7923 msleep(2);
7924
7925 if (time_after(jiffies, timeout))
7926 return MBXERR_ERROR;
7927 } while (!db_ready);
7928
7929 return 0;
7930}
7931
7932/**
James Smartda0436e2009-05-22 14:51:39 -04007933 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7934 * @phba: Pointer to HBA context object.
7935 * @mboxq: Pointer to mailbox object.
7936 *
7937 * The function posts a mailbox to the port. The mailbox is expected
7938 * to be comletely filled in and ready for the port to operate on it.
7939 * This routine executes a synchronous completion operation on the
7940 * mailbox by polling for its completion.
7941 *
7942 * The caller must not be holding any locks when calling this routine.
7943 *
7944 * Returns:
7945 * MBX_SUCCESS - mailbox posted successfully
7946 * Any of the MBX error values.
7947 **/
7948static int
7949lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7950{
7951 int rc = MBX_SUCCESS;
7952 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007953 uint32_t mcqe_status;
7954 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007955 struct lpfc_sli *psli = &phba->sli;
7956 struct lpfc_mqe *mb = &mboxq->u.mqe;
7957 struct lpfc_bmbx_create *mbox_rgn;
7958 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007959
7960 /*
7961 * Only one mailbox can be active to the bootstrap mailbox region
7962 * at a time and there is no queueing provided.
7963 */
7964 spin_lock_irqsave(&phba->hbalock, iflag);
7965 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7966 spin_unlock_irqrestore(&phba->hbalock, iflag);
7967 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007968 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007969 "cannot issue Data: x%x x%x\n",
7970 mboxq->vport ? mboxq->vport->vpi : 0,
7971 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007972 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7973 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007974 psli->sli_flag, MBX_POLL);
7975 return MBXERR_ERROR;
7976 }
7977 /* The server grabs the token and owns it until release */
7978 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7979 phba->sli.mbox_active = mboxq;
7980 spin_unlock_irqrestore(&phba->hbalock, iflag);
7981
James Smart2d843ed2012-09-29 11:29:06 -04007982 /* wait for bootstrap mbox register for readyness */
7983 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7984 if (rc)
7985 goto exit;
7986
James Smartda0436e2009-05-22 14:51:39 -04007987 /*
7988 * Initialize the bootstrap memory region to avoid stale data areas
7989 * in the mailbox post. Then copy the caller's mailbox contents to
7990 * the bmbx mailbox region.
7991 */
7992 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7993 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7994 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7995 sizeof(struct lpfc_mqe));
7996
7997 /* Post the high mailbox dma address to the port and wait for ready. */
7998 dma_address = &phba->sli4_hba.bmbx.dma_address;
7999 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
8000
James Smart2d843ed2012-09-29 11:29:06 -04008001 /* wait for bootstrap mbox register for hi-address write done */
8002 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8003 if (rc)
8004 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008005
8006 /* Post the low mailbox dma address to the port. */
8007 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04008008
James Smart2d843ed2012-09-29 11:29:06 -04008009 /* wait for bootstrap mbox register for low address write done */
8010 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
8011 if (rc)
8012 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04008013
8014 /*
8015 * Read the CQ to ensure the mailbox has completed.
8016 * If so, update the mailbox status so that the upper layers
8017 * can complete the request normally.
8018 */
8019 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
8020 sizeof(struct lpfc_mqe));
8021 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
8022 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
8023 sizeof(struct lpfc_mcqe));
8024 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04008025 /*
8026 * When the CQE status indicates a failure and the mailbox status
8027 * indicates success then copy the CQE status into the mailbox status
8028 * (and prefix it with x4000).
8029 */
James Smartda0436e2009-05-22 14:51:39 -04008030 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04008031 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
8032 bf_set(lpfc_mqe_status, mb,
8033 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04008034 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04008035 } else
8036 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04008037
8038 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008039 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04008040 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
8041 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04008042 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
8043 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8044 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008045 bf_get(lpfc_mqe_status, mb),
8046 mb->un.mb_words[0], mb->un.mb_words[1],
8047 mb->un.mb_words[2], mb->un.mb_words[3],
8048 mb->un.mb_words[4], mb->un.mb_words[5],
8049 mb->un.mb_words[6], mb->un.mb_words[7],
8050 mb->un.mb_words[8], mb->un.mb_words[9],
8051 mb->un.mb_words[10], mb->un.mb_words[11],
8052 mb->un.mb_words[12], mboxq->mcqe.word0,
8053 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
8054 mboxq->mcqe.trailer);
8055exit:
8056 /* We are holding the token, no needed for lock when release */
8057 spin_lock_irqsave(&phba->hbalock, iflag);
8058 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8059 phba->sli.mbox_active = NULL;
8060 spin_unlock_irqrestore(&phba->hbalock, iflag);
8061 return rc;
8062}
8063
8064/**
8065 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
8066 * @phba: Pointer to HBA context object.
8067 * @pmbox: Pointer to mailbox object.
8068 * @flag: Flag indicating how the mailbox need to be processed.
8069 *
8070 * This function is called by discovery code and HBA management code to submit
8071 * a mailbox command to firmware with SLI-4 interface spec.
8072 *
8073 * Return codes the caller owns the mailbox command after the return of the
8074 * function.
8075 **/
8076static int
8077lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
8078 uint32_t flag)
8079{
8080 struct lpfc_sli *psli = &phba->sli;
8081 unsigned long iflags;
8082 int rc;
8083
James Smartb76f2dc2011-07-22 18:37:42 -04008084 /* dump from issue mailbox command if setup */
8085 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
8086
James Smart8fa38512009-07-19 10:01:03 -04008087 rc = lpfc_mbox_dev_check(phba);
8088 if (unlikely(rc)) {
8089 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008090 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04008091 "cannot issue Data: x%x x%x\n",
8092 mboxq->vport ? mboxq->vport->vpi : 0,
8093 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008094 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8095 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04008096 psli->sli_flag, flag);
8097 goto out_not_finished;
8098 }
8099
James Smartda0436e2009-05-22 14:51:39 -04008100 /* Detect polling mode and jump to a handler */
8101 if (!phba->sli4_hba.intr_enable) {
8102 if (flag == MBX_POLL)
8103 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8104 else
8105 rc = -EIO;
8106 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04008107 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04008108 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04008109 "(x%x/x%x) failure: "
8110 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8111 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04008112 mboxq->vport ? mboxq->vport->vpi : 0,
8113 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008114 lpfc_sli_config_mbox_subsys_get(phba,
8115 mboxq),
8116 lpfc_sli_config_mbox_opcode_get(phba,
8117 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008118 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8119 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8120 bf_get(lpfc_mcqe_ext_status,
8121 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04008122 psli->sli_flag, flag);
8123 return rc;
8124 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04008125 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
8126 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04008127 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04008128 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008129 mboxq->vport ? mboxq->vport->vpi : 0,
8130 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008131 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8132 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008133 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008134 /* Try to block the asynchronous mailbox posting */
8135 rc = lpfc_sli4_async_mbox_block(phba);
8136 if (!rc) {
8137 /* Successfully blocked, now issue sync mbox cmd */
8138 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
8139 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04008140 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04008141 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04008142 "(%d):2597 Sync Mailbox command "
8143 "x%x (x%x/x%x) failure: "
8144 "mqe_sta: x%x mcqe_sta: x%x/x%x "
8145 "Data: x%x x%x\n,",
8146 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04008147 mboxq->u.mb.mbxCommand,
8148 lpfc_sli_config_mbox_subsys_get(phba,
8149 mboxq),
8150 lpfc_sli_config_mbox_opcode_get(phba,
8151 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04008152 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
8153 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
8154 bf_get(lpfc_mcqe_ext_status,
8155 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04008156 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04008157 /* Unblock the async mailbox posting afterward */
8158 lpfc_sli4_async_mbox_unblock(phba);
8159 }
8160 return rc;
James Smartda0436e2009-05-22 14:51:39 -04008161 }
8162
8163 /* Now, interrupt mode asynchrous mailbox command */
8164 rc = lpfc_mbox_cmd_check(phba, mboxq);
8165 if (rc) {
8166 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008167 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008168 "cannot issue Data: x%x x%x\n",
8169 mboxq->vport ? mboxq->vport->vpi : 0,
8170 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008171 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8172 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008173 psli->sli_flag, flag);
8174 goto out_not_finished;
8175 }
James Smartda0436e2009-05-22 14:51:39 -04008176
8177 /* Put the mailbox command to the driver internal FIFO */
8178 psli->slistat.mbox_busy++;
8179 spin_lock_irqsave(&phba->hbalock, iflags);
8180 lpfc_mbox_put(phba, mboxq);
8181 spin_unlock_irqrestore(&phba->hbalock, iflags);
8182 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
8183 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04008184 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04008185 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
8186 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04008187 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8188 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008189 phba->pport->port_state,
8190 psli->sli_flag, MBX_NOWAIT);
8191 /* Wake up worker thread to transport mailbox command from head */
8192 lpfc_worker_wake_up(phba);
8193
8194 return MBX_BUSY;
8195
8196out_not_finished:
8197 return MBX_NOT_FINISHED;
8198}
8199
8200/**
8201 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
8202 * @phba: Pointer to HBA context object.
8203 *
8204 * This function is called by worker thread to send a mailbox command to
8205 * SLI4 HBA firmware.
8206 *
8207 **/
8208int
8209lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
8210{
8211 struct lpfc_sli *psli = &phba->sli;
8212 LPFC_MBOXQ_t *mboxq;
8213 int rc = MBX_SUCCESS;
8214 unsigned long iflags;
8215 struct lpfc_mqe *mqe;
8216 uint32_t mbx_cmnd;
8217
8218 /* Check interrupt mode before post async mailbox command */
8219 if (unlikely(!phba->sli4_hba.intr_enable))
8220 return MBX_NOT_FINISHED;
8221
8222 /* Check for mailbox command service token */
8223 spin_lock_irqsave(&phba->hbalock, iflags);
8224 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
8225 spin_unlock_irqrestore(&phba->hbalock, iflags);
8226 return MBX_NOT_FINISHED;
8227 }
8228 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8229 spin_unlock_irqrestore(&phba->hbalock, iflags);
8230 return MBX_NOT_FINISHED;
8231 }
8232 if (unlikely(phba->sli.mbox_active)) {
8233 spin_unlock_irqrestore(&phba->hbalock, iflags);
8234 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
8235 "0384 There is pending active mailbox cmd\n");
8236 return MBX_NOT_FINISHED;
8237 }
8238 /* Take the mailbox command service token */
8239 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
8240
8241 /* Get the next mailbox command from head of queue */
8242 mboxq = lpfc_mbox_get(phba);
8243
8244 /* If no more mailbox command waiting for post, we're done */
8245 if (!mboxq) {
8246 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8247 spin_unlock_irqrestore(&phba->hbalock, iflags);
8248 return MBX_SUCCESS;
8249 }
8250 phba->sli.mbox_active = mboxq;
8251 spin_unlock_irqrestore(&phba->hbalock, iflags);
8252
8253 /* Check device readiness for posting mailbox command */
8254 rc = lpfc_mbox_dev_check(phba);
8255 if (unlikely(rc))
8256 /* Driver clean routine will clean up pending mailbox */
8257 goto out_not_finished;
8258
8259 /* Prepare the mbox command to be posted */
8260 mqe = &mboxq->u.mqe;
8261 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8262
8263 /* Start timer for the mbox_tmo and log some mailbox post messages */
8264 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008265 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008266
8267 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008268 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008269 "x%x x%x\n",
8270 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008271 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8272 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008273 phba->pport->port_state, psli->sli_flag);
8274
8275 if (mbx_cmnd != MBX_HEARTBEAT) {
8276 if (mboxq->vport) {
8277 lpfc_debugfs_disc_trc(mboxq->vport,
8278 LPFC_DISC_TRC_MBOX_VPORT,
8279 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8280 mbx_cmnd, mqe->un.mb_words[0],
8281 mqe->un.mb_words[1]);
8282 } else {
8283 lpfc_debugfs_disc_trc(phba->pport,
8284 LPFC_DISC_TRC_MBOX,
8285 "MBOX Send: cmd:x%x mb:x%x x%x",
8286 mbx_cmnd, mqe->un.mb_words[0],
8287 mqe->un.mb_words[1]);
8288 }
8289 }
8290 psli->slistat.mbox_cmd++;
8291
8292 /* Post the mailbox command to the port */
8293 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8294 if (rc != MBX_SUCCESS) {
8295 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008296 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008297 "cannot issue Data: x%x x%x\n",
8298 mboxq->vport ? mboxq->vport->vpi : 0,
8299 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008300 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8301 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008302 psli->sli_flag, MBX_NOWAIT);
8303 goto out_not_finished;
8304 }
8305
8306 return rc;
8307
8308out_not_finished:
8309 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008310 if (phba->sli.mbox_active) {
8311 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8312 __lpfc_mbox_cmpl_put(phba, mboxq);
8313 /* Release the token */
8314 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8315 phba->sli.mbox_active = NULL;
8316 }
James Smartda0436e2009-05-22 14:51:39 -04008317 spin_unlock_irqrestore(&phba->hbalock, iflags);
8318
8319 return MBX_NOT_FINISHED;
8320}
8321
8322/**
8323 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8324 * @phba: Pointer to HBA context object.
8325 * @pmbox: Pointer to mailbox object.
8326 * @flag: Flag indicating how the mailbox need to be processed.
8327 *
8328 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8329 * the API jump table function pointer from the lpfc_hba struct.
8330 *
8331 * Return codes the caller owns the mailbox command after the return of the
8332 * function.
8333 **/
8334int
8335lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8336{
8337 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8338}
8339
8340/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008341 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008342 * @phba: The hba struct for which this call is being executed.
8343 * @dev_grp: The HBA PCI-Device group number.
8344 *
8345 * This routine sets up the mbox interface API function jump table in @phba
8346 * struct.
8347 * Returns: 0 - success, -ENODEV - failure.
8348 **/
8349int
8350lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8351{
8352
8353 switch (dev_grp) {
8354 case LPFC_PCI_DEV_LP:
8355 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8356 phba->lpfc_sli_handle_slow_ring_event =
8357 lpfc_sli_handle_slow_ring_event_s3;
8358 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8359 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8360 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8361 break;
8362 case LPFC_PCI_DEV_OC:
8363 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8364 phba->lpfc_sli_handle_slow_ring_event =
8365 lpfc_sli_handle_slow_ring_event_s4;
8366 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8367 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8368 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8369 break;
8370 default:
8371 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8372 "1420 Invalid HBA PCI-device group: 0x%x\n",
8373 dev_grp);
8374 return -ENODEV;
8375 break;
8376 }
8377 return 0;
8378}
8379
8380/**
James Smart3621a712009-04-06 18:47:14 -04008381 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008382 * @phba: Pointer to HBA context object.
8383 * @pring: Pointer to driver SLI ring object.
8384 * @piocb: Pointer to address of newly added command iocb.
8385 *
8386 * This function is called with hbalock held to add a command
8387 * iocb to the txq when SLI layer cannot submit the command iocb
8388 * to the ring.
8389 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008390void
James Smart92d7f7b2007-06-17 19:56:38 -05008391__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008392 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008393{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008394 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008395 /* Insert the caller's iocb in the txq tail for later processing. */
8396 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008397}
8398
James Smarte59058c2008-08-24 21:49:00 -04008399/**
James Smart3621a712009-04-06 18:47:14 -04008400 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008401 * @phba: Pointer to HBA context object.
8402 * @pring: Pointer to driver SLI ring object.
8403 * @piocb: Pointer to address of newly added command iocb.
8404 *
8405 * This function is called with hbalock held before a new
8406 * iocb is submitted to the firmware. This function checks
8407 * txq to flush the iocbs in txq to Firmware before
8408 * submitting new iocbs to the Firmware.
8409 * If there are iocbs in the txq which need to be submitted
8410 * to firmware, lpfc_sli_next_iocb returns the first element
8411 * of the txq after dequeuing it from txq.
8412 * If there is no iocb in the txq then the function will return
8413 * *piocb and *piocb is set to NULL. Caller needs to check
8414 * *piocb to find if there are more commands in the txq.
8415 **/
dea31012005-04-17 16:05:31 -05008416static struct lpfc_iocbq *
8417lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008418 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008419{
8420 struct lpfc_iocbq * nextiocb;
8421
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008422 lockdep_assert_held(&phba->hbalock);
8423
dea31012005-04-17 16:05:31 -05008424 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8425 if (!nextiocb) {
8426 nextiocb = *piocb;
8427 *piocb = NULL;
8428 }
8429
8430 return nextiocb;
8431}
8432
James Smarte59058c2008-08-24 21:49:00 -04008433/**
James Smart3772a992009-05-22 14:50:54 -04008434 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008435 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008436 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008437 * @piocb: Pointer to command iocb.
8438 * @flag: Flag indicating if this command can be put into txq.
8439 *
James Smart3772a992009-05-22 14:50:54 -04008440 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8441 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8442 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8443 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8444 * this function allows only iocbs for posting buffers. This function finds
8445 * next available slot in the command ring and posts the command to the
8446 * available slot and writes the port attention register to request HBA start
8447 * processing new iocb. If there is no slot available in the ring and
8448 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8449 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008450 *
James Smart3772a992009-05-22 14:50:54 -04008451 * This function is called with hbalock held. The function will return success
8452 * after it successfully submit the iocb to firmware or after adding to the
8453 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008454 **/
James Smart98c9ea52007-10-27 13:37:33 -04008455static int
James Smart3772a992009-05-22 14:50:54 -04008456__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008457 struct lpfc_iocbq *piocb, uint32_t flag)
8458{
8459 struct lpfc_iocbq *nextiocb;
8460 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008461 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008462
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008463 lockdep_assert_held(&phba->hbalock);
8464
James Smart92d7f7b2007-06-17 19:56:38 -05008465 if (piocb->iocb_cmpl && (!piocb->vport) &&
8466 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8467 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8468 lpfc_printf_log(phba, KERN_ERR,
8469 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008470 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008471 piocb->iocb.ulpCommand);
8472 dump_stack();
8473 return IOCB_ERROR;
8474 }
8475
8476
Linas Vepstas8d63f372007-02-14 14:28:36 -06008477 /* If the PCI channel is in offline state, do not post iocbs. */
8478 if (unlikely(pci_channel_offline(phba->pcidev)))
8479 return IOCB_ERROR;
8480
James Smarta257bf92009-04-06 18:48:10 -04008481 /* If HBA has a deferred error attention, fail the iocb. */
8482 if (unlikely(phba->hba_flag & DEFER_ERATT))
8483 return IOCB_ERROR;
8484
dea31012005-04-17 16:05:31 -05008485 /*
8486 * We should never get an IOCB if we are in a < LINK_DOWN state
8487 */
James Smart2e0fef82007-06-17 19:56:36 -05008488 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008489 return IOCB_ERROR;
8490
8491 /*
8492 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008493 * outstanding event.
dea31012005-04-17 16:05:31 -05008494 */
James Smart0b727fe2007-10-27 13:37:25 -04008495 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008496 goto iocb_busy;
8497
James Smart2e0fef82007-06-17 19:56:36 -05008498 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008499 /*
James Smart2680eea2007-04-25 09:52:55 -04008500 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008501 * can be issued if the link is not up.
8502 */
8503 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008504 case CMD_GEN_REQUEST64_CR:
8505 case CMD_GEN_REQUEST64_CX:
8506 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8507 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008508 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008509 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8510 MENLO_TRANSPORT_TYPE))
8511
8512 goto iocb_busy;
8513 break;
dea31012005-04-17 16:05:31 -05008514 case CMD_QUE_RING_BUF_CN:
8515 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008516 /*
8517 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8518 * completion, iocb_cmpl MUST be 0.
8519 */
8520 if (piocb->iocb_cmpl)
8521 piocb->iocb_cmpl = NULL;
8522 /*FALLTHROUGH*/
8523 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008524 case CMD_CLOSE_XRI_CN:
8525 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008526 break;
8527 default:
8528 goto iocb_busy;
8529 }
8530
8531 /*
8532 * For FCP commands, we must be in a state where we can process link
8533 * attention events.
8534 */
James Smart895427b2017-02-12 13:52:30 -08008535 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008536 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008537 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008538 }
dea31012005-04-17 16:05:31 -05008539
dea31012005-04-17 16:05:31 -05008540 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8541 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8542 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8543
8544 if (iocb)
8545 lpfc_sli_update_ring(phba, pring);
8546 else
8547 lpfc_sli_update_full_ring(phba, pring);
8548
8549 if (!piocb)
8550 return IOCB_SUCCESS;
8551
8552 goto out_busy;
8553
8554 iocb_busy:
8555 pring->stats.iocb_cmd_delay++;
8556
8557 out_busy:
8558
8559 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008560 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008561 return IOCB_SUCCESS;
8562 }
8563
8564 return IOCB_BUSY;
8565}
8566
James Smart3772a992009-05-22 14:50:54 -04008567/**
James Smart4f774512009-05-22 14:52:35 -04008568 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8569 * @phba: Pointer to HBA context object.
8570 * @piocb: Pointer to command iocb.
8571 * @sglq: Pointer to the scatter gather queue object.
8572 *
8573 * This routine converts the bpl or bde that is in the IOCB
8574 * to a sgl list for the sli4 hardware. The physical address
8575 * of the bpl/bde is converted back to a virtual address.
8576 * If the IOCB contains a BPL then the list of BDE's is
8577 * converted to sli4_sge's. If the IOCB contains a single
8578 * BDE then it is converted to a single sli_sge.
8579 * The IOCB is still in cpu endianess so the contents of
8580 * the bpl can be used without byte swapping.
8581 *
8582 * Returns valid XRI = Success, NO_XRI = Failure.
8583**/
8584static uint16_t
8585lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8586 struct lpfc_sglq *sglq)
8587{
8588 uint16_t xritag = NO_XRI;
8589 struct ulp_bde64 *bpl = NULL;
8590 struct ulp_bde64 bde;
8591 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008592 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008593 IOCB_t *icmd;
8594 int numBdes = 0;
8595 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008596 uint32_t offset = 0; /* accumulated offset in the sg request list */
8597 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008598
8599 if (!piocbq || !sglq)
8600 return xritag;
8601
8602 sgl = (struct sli4_sge *)sglq->sgl;
8603 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008604 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8605 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008606 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8607 numBdes = icmd->un.genreq64.bdl.bdeSize /
8608 sizeof(struct ulp_bde64);
8609 /* The addrHigh and addrLow fields within the IOCB
8610 * have not been byteswapped yet so there is no
8611 * need to swap them back.
8612 */
James Smart1b511972011-12-13 13:23:09 -05008613 if (piocbq->context3)
8614 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8615 else
8616 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008617
James Smart1b511972011-12-13 13:23:09 -05008618 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008619 if (!bpl)
8620 return xritag;
8621
8622 for (i = 0; i < numBdes; i++) {
8623 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008624 sgl->addr_hi = bpl->addrHigh;
8625 sgl->addr_lo = bpl->addrLow;
8626
James Smart05580562011-05-24 11:40:48 -04008627 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008628 if ((i+1) == numBdes)
8629 bf_set(lpfc_sli4_sge_last, sgl, 1);
8630 else
8631 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008632 /* swap the size field back to the cpu so we
8633 * can assign it to the sgl.
8634 */
8635 bde.tus.w = le32_to_cpu(bpl->tus.w);
8636 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008637 /* The offsets in the sgl need to be accumulated
8638 * separately for the request and reply lists.
8639 * The request is always first, the reply follows.
8640 */
8641 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8642 /* add up the reply sg entries */
8643 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8644 inbound++;
8645 /* first inbound? reset the offset */
8646 if (inbound == 1)
8647 offset = 0;
8648 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008649 bf_set(lpfc_sli4_sge_type, sgl,
8650 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008651 offset += bde.tus.f.bdeSize;
8652 }
James Smart546fc852011-03-11 16:06:29 -05008653 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008654 bpl++;
8655 sgl++;
8656 }
8657 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8658 /* The addrHigh and addrLow fields of the BDE have not
8659 * been byteswapped yet so they need to be swapped
8660 * before putting them in the sgl.
8661 */
8662 sgl->addr_hi =
8663 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8664 sgl->addr_lo =
8665 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008666 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008667 bf_set(lpfc_sli4_sge_last, sgl, 1);
8668 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008669 sgl->sge_len =
8670 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008671 }
8672 return sglq->sli4_xritag;
8673}
8674
8675/**
James Smart4f774512009-05-22 14:52:35 -04008676 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8677 * @phba: Pointer to HBA context object.
8678 * @piocb: Pointer to command iocb.
8679 * @wqe: Pointer to the work queue entry.
8680 *
8681 * This routine converts the iocb command to its Work Queue Entry
8682 * equivalent. The wqe pointer should not have any fields set when
8683 * this routine is called because it will memcpy over them.
8684 * This routine does not set the CQ_ID or the WQEC bits in the
8685 * wqe.
8686 *
8687 * Returns: 0 = Success, IOCB_ERROR = Failure.
8688 **/
8689static int
8690lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8691 union lpfc_wqe *wqe)
8692{
James Smart5ffc2662009-11-18 15:39:44 -05008693 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008694 uint8_t ct = 0;
8695 uint32_t fip;
8696 uint32_t abort_tag;
8697 uint8_t command_type = ELS_COMMAND_NON_FIP;
8698 uint8_t cmnd;
8699 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008700 uint16_t abrt_iotag;
8701 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008702 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008703 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008704 int numBdes, i;
8705 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008706 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008707 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008708 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008709
James Smart45ed1192009-10-02 15:17:02 -04008710 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008711 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008712 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008713 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008714 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008715 command_type = ELS_COMMAND_FIP;
8716 else
8717 command_type = ELS_COMMAND_NON_FIP;
8718
James Smartb5c53952016-03-31 14:12:30 -07008719 if (phba->fcp_embed_io)
8720 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008721 /* Some of the fields are in the right position already */
8722 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartae9e28f2017-05-15 15:20:51 -07008723 if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
8724 /* The ct field has moved so reset */
8725 wqe->generic.wqe_com.word7 = 0;
8726 wqe->generic.wqe_com.word10 = 0;
8727 }
James Smartb5c53952016-03-31 14:12:30 -07008728
8729 abort_tag = (uint32_t) iocbq->iotag;
8730 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008731 /* words0-2 bpl convert bde */
8732 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008733 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8734 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008735 bpl = (struct ulp_bde64 *)
8736 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8737 if (!bpl)
8738 return IOCB_ERROR;
8739
8740 /* Should already be byte swapped. */
8741 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8742 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8743 /* swap the size field back to the cpu so we
8744 * can assign it to the sgl.
8745 */
8746 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008747 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8748 total_len = 0;
8749 for (i = 0; i < numBdes; i++) {
8750 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8751 total_len += bde.tus.f.bdeSize;
8752 }
James Smart4f774512009-05-22 14:52:35 -04008753 } else
James Smart5ffc2662009-11-18 15:39:44 -05008754 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008755
8756 iocbq->iocb.ulpIoTag = iocbq->iotag;
8757 cmnd = iocbq->iocb.ulpCommand;
8758
8759 switch (iocbq->iocb.ulpCommand) {
8760 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008761 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8762 ndlp = iocbq->context_un.ndlp;
8763 else
8764 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008765 if (!iocbq->iocb.ulpLe) {
8766 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8767 "2007 Only Limited Edition cmd Format"
8768 " supported 0x%x\n",
8769 iocbq->iocb.ulpCommand);
8770 return IOCB_ERROR;
8771 }
James Smartff78d8f2011-12-13 13:21:35 -05008772
James Smart5ffc2662009-11-18 15:39:44 -05008773 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008774 /* Els_reguest64 has a TMO */
8775 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8776 iocbq->iocb.ulpTimeout);
8777 /* Need a VF for word 4 set the vf bit*/
8778 bf_set(els_req64_vf, &wqe->els_req, 0);
8779 /* And a VFID for word 12 */
8780 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008781 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008782 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8783 iocbq->iocb.ulpContext);
8784 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8785 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008786 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008787 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008788 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8789 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008790 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8791 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008792 if_type = bf_get(lpfc_sli_intf_if_type,
8793 &phba->sli4_hba.sli_intf);
8794 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008795 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008796 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008797 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008798 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008799 *pcmd == ELS_CMD_PLOGI)) {
8800 bf_set(els_req64_sp, &wqe->els_req, 1);
8801 bf_set(els_req64_sid, &wqe->els_req,
8802 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008803 if ((*pcmd == ELS_CMD_FLOGI) &&
8804 !(phba->fc_topology ==
8805 LPFC_TOPOLOGY_LOOP))
8806 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008807 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8808 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008809 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008810 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008811 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8812 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8813 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8814 }
James Smartc8685952009-11-18 15:39:16 -05008815 }
James Smart6d368e52011-05-24 11:44:12 -04008816 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8817 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008818 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8819 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8820 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8821 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8822 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8823 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008824 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008825 break;
James Smart5ffc2662009-11-18 15:39:44 -05008826 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008827 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8828 iocbq->iocb.un.ulpWord[3]);
8829 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008830 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008831 /* The entire sequence is transmitted for this IOCB */
8832 xmit_len = total_len;
8833 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008834 if (phba->link_flag & LS_LOOPBACK_MODE)
8835 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008836 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008837 /* word3 iocb=io_tag32 wqe=reserved */
8838 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008839 /* word4 relative_offset memcpy */
8840 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008841 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8842 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8843 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8844 LPFC_WQE_IOD_WRITE);
8845 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8846 LPFC_WQE_LENLOC_WORD12);
8847 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008848 wqe->xmit_sequence.xmit_len = xmit_len;
8849 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008850 break;
James Smart4f774512009-05-22 14:52:35 -04008851 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008852 /* word3 iocb=iotag32 wqe=seq_payload_len */
8853 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008854 /* word4 iocb=rsvd wqe=rsvd */
8855 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8856 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008857 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008858 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008859 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8860 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8861 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8862 LPFC_WQE_LENLOC_WORD3);
8863 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008864 break;
James Smart4f774512009-05-22 14:52:35 -04008865 case CMD_FCP_IWRITE64_CR:
8866 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008867 /* word3 iocb=iotag wqe=payload_offset_len */
8868 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008869 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8870 xmit_len + sizeof(struct fcp_rsp));
8871 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8872 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008873 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8874 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8875 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8876 iocbq->iocb.ulpFCP2Rcvy);
8877 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8878 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008879 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8880 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8881 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008882 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008883 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008884 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8885 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008886 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8887 if (iocbq->priority) {
8888 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8889 (iocbq->priority << 1));
8890 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008891 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8892 (phba->cfg_XLanePriority << 1));
8893 }
8894 }
James Smartb5c53952016-03-31 14:12:30 -07008895 /* Note, word 10 is already initialized to 0 */
8896
8897 if (phba->fcp_embed_io) {
8898 struct lpfc_scsi_buf *lpfc_cmd;
8899 struct sli4_sge *sgl;
8900 union lpfc_wqe128 *wqe128;
8901 struct fcp_cmnd *fcp_cmnd;
8902 uint32_t *ptr;
8903
8904 /* 128 byte wqe support here */
8905 wqe128 = (union lpfc_wqe128 *)wqe;
8906
8907 lpfc_cmd = iocbq->context1;
8908 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8909 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8910
8911 /* Word 0-2 - FCP_CMND */
8912 wqe128->generic.bde.tus.f.bdeFlags =
8913 BUFF_TYPE_BDE_IMMED;
8914 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8915 wqe128->generic.bde.addrHigh = 0;
8916 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8917
8918 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8919
8920 /* Word 22-29 FCP CMND Payload */
8921 ptr = &wqe128->words[22];
8922 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8923 }
James Smart7851fe22011-07-22 18:36:52 -04008924 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008925 case CMD_FCP_IREAD64_CR:
8926 /* word3 iocb=iotag wqe=payload_offset_len */
8927 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008928 bf_set(payload_offset_len, &wqe->fcp_iread,
8929 xmit_len + sizeof(struct fcp_rsp));
8930 bf_set(cmd_buff_len, &wqe->fcp_iread,
8931 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008932 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8933 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8934 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8935 iocbq->iocb.ulpFCP2Rcvy);
8936 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008937 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008938 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8939 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8940 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008941 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008942 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008943 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8944 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008945 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8946 if (iocbq->priority) {
8947 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8948 (iocbq->priority << 1));
8949 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008950 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8951 (phba->cfg_XLanePriority << 1));
8952 }
8953 }
James Smartb5c53952016-03-31 14:12:30 -07008954 /* Note, word 10 is already initialized to 0 */
8955
8956 if (phba->fcp_embed_io) {
8957 struct lpfc_scsi_buf *lpfc_cmd;
8958 struct sli4_sge *sgl;
8959 union lpfc_wqe128 *wqe128;
8960 struct fcp_cmnd *fcp_cmnd;
8961 uint32_t *ptr;
8962
8963 /* 128 byte wqe support here */
8964 wqe128 = (union lpfc_wqe128 *)wqe;
8965
8966 lpfc_cmd = iocbq->context1;
8967 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8968 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8969
8970 /* Word 0-2 - FCP_CMND */
8971 wqe128->generic.bde.tus.f.bdeFlags =
8972 BUFF_TYPE_BDE_IMMED;
8973 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8974 wqe128->generic.bde.addrHigh = 0;
8975 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8976
8977 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8978
8979 /* Word 22-29 FCP CMND Payload */
8980 ptr = &wqe128->words[22];
8981 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8982 }
James Smart7851fe22011-07-22 18:36:52 -04008983 break;
James Smartf1126682009-06-10 17:22:44 -04008984 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008985 /* word3 iocb=iotag wqe=payload_offset_len */
8986 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8987 bf_set(payload_offset_len, &wqe->fcp_icmd,
8988 xmit_len + sizeof(struct fcp_rsp));
8989 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8990 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008991 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008992 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008993 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008994 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8995 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8996 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8997 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8998 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008999 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
9000 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05009001 if (iocbq->iocb_flag & LPFC_IO_OAS) {
9002 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07009003 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
9004 if (iocbq->priority) {
9005 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9006 (iocbq->priority << 1));
9007 } else {
James Smart1ba981f2014-02-20 09:56:45 -05009008 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
9009 (phba->cfg_XLanePriority << 1));
9010 }
9011 }
James Smartb5c53952016-03-31 14:12:30 -07009012 /* Note, word 10 is already initialized to 0 */
9013
9014 if (phba->fcp_embed_io) {
9015 struct lpfc_scsi_buf *lpfc_cmd;
9016 struct sli4_sge *sgl;
9017 union lpfc_wqe128 *wqe128;
9018 struct fcp_cmnd *fcp_cmnd;
9019 uint32_t *ptr;
9020
9021 /* 128 byte wqe support here */
9022 wqe128 = (union lpfc_wqe128 *)wqe;
9023
9024 lpfc_cmd = iocbq->context1;
9025 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
9026 fcp_cmnd = lpfc_cmd->fcp_cmnd;
9027
9028 /* Word 0-2 - FCP_CMND */
9029 wqe128->generic.bde.tus.f.bdeFlags =
9030 BUFF_TYPE_BDE_IMMED;
9031 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
9032 wqe128->generic.bde.addrHigh = 0;
9033 wqe128->generic.bde.addrLow = 88; /* Word 22 */
9034
9035 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
9036
9037 /* Word 22-29 FCP CMND Payload */
9038 ptr = &wqe128->words[22];
9039 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
9040 }
James Smart7851fe22011-07-22 18:36:52 -04009041 break;
James Smart4f774512009-05-22 14:52:35 -04009042 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05009043 /* For this command calculate the xmit length of the
9044 * request bde.
9045 */
9046 xmit_len = 0;
9047 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
9048 sizeof(struct ulp_bde64);
9049 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05009050 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05009051 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
9052 break;
James Smart63e801c2010-11-20 23:14:19 -05009053 xmit_len += bde.tus.f.bdeSize;
9054 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009055 /* word3 iocb=IO_TAG wqe=request_payload_len */
9056 wqe->gen_req.request_payload_len = xmit_len;
9057 /* word4 iocb=parameter wqe=relative_offset memcpy */
9058 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04009059 /* word6 context tag copied in memcpy */
9060 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
9061 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
9062 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9063 "2015 Invalid CT %x command 0x%x\n",
9064 ct, iocbq->iocb.ulpCommand);
9065 return IOCB_ERROR;
9066 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009067 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
9068 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
9069 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
9070 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
9071 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
9072 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
9073 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
9074 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04009075 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04009076 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009077 break;
James Smart4f774512009-05-22 14:52:35 -04009078 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04009079 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04009080 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04009081 /* word3 iocb=iotag32 wqe=response_payload_len */
9082 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04009083 /* word4 */
9084 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04009085 /* word5 iocb=rsvd wge=did */
9086 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04009087 iocbq->iocb.un.xseq64.xmit_els_remoteID);
9088
9089 if_type = bf_get(lpfc_sli_intf_if_type,
9090 &phba->sli4_hba.sli_intf);
9091 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
9092 if (iocbq->vport->fc_flag & FC_PT2PT) {
9093 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9094 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
9095 iocbq->vport->fc_myDID);
9096 if (iocbq->vport->fc_myDID == Fabric_DID) {
9097 bf_set(wqe_els_did,
9098 &wqe->xmit_els_rsp.wqe_dest, 0);
9099 }
9100 }
9101 }
James Smartf0d9bcc2010-10-22 11:07:09 -04009102 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
9103 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9104 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
9105 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04009106 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04009107 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04009108 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04009109 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009110 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
9111 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
9112 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
9113 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
9114 LPFC_WQE_LENLOC_WORD3);
9115 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04009116 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
9117 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05009118 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
9119 iocbq->context2)->virt);
9120 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04009121 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
9122 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05009123 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04009124 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
9125 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05009126 phba->vpi_ids[phba->pport->vpi]);
9127 }
James Smart4f774512009-05-22 14:52:35 -04009128 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04009129 break;
James Smart4f774512009-05-22 14:52:35 -04009130 case CMD_CLOSE_XRI_CN:
9131 case CMD_ABORT_XRI_CN:
9132 case CMD_ABORT_XRI_CX:
9133 /* words 0-2 memcpy should be 0 rserved */
9134 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04009135 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
9136 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
9137 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
9138 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
9139 } else
9140 fip = 0;
9141
9142 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04009143 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04009144 * The link is down, or the command was ELS_FIP
9145 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04009146 * on the wire.
9147 */
9148 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
9149 else
9150 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
9151 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04009152 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
9153 wqe->abort_cmd.rsrvd5 = 0;
9154 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04009155 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
9156 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04009157 /*
9158 * The abort handler will send us CMD_ABORT_XRI_CN or
9159 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
9160 */
James Smartf0d9bcc2010-10-22 11:07:09 -04009161 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
9162 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
9163 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
9164 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04009165 cmnd = CMD_ABORT_XRI_CX;
9166 command_type = OTHER_COMMAND;
9167 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04009168 break;
James Smart6669f9b2009-10-02 15:16:45 -04009169 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05009170 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05009171 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04009172 * we re-construct this WQE here based on information in
9173 * iocbq from scratch.
9174 */
9175 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05009176 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04009177 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009178 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
9179 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05009180 LPFC_ABTS_UNSOL_INT) {
9181 /* ABTS sent by initiator to CT exchange, the
9182 * RX_ID field will be filled with the newly
9183 * allocated responder XRI.
9184 */
9185 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
9186 iocbq->sli4_xritag);
9187 } else {
9188 /* ABTS sent by responder to CT exchange, the
9189 * RX_ID field will be filled with the responder
9190 * RX_ID from ABTS.
9191 */
9192 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05009193 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05009194 }
James Smart6669f9b2009-10-02 15:16:45 -04009195 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
9196 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05009197
9198 /* Use CT=VPI */
9199 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
9200 ndlp->nlp_DID);
9201 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
9202 iocbq->iocb.ulpContext);
9203 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04009204 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05009205 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04009206 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
9207 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
9208 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04009209 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
9210 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05009211 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
9212 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
9213 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
9214 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
9215 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
9216 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
9217 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
9218 }
9219
James Smart7851fe22011-07-22 18:36:52 -04009220 break;
James Smartae9e28f2017-05-15 15:20:51 -07009221 case CMD_SEND_FRAME:
9222 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9223 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9224 return 0;
James Smart4f774512009-05-22 14:52:35 -04009225 case CMD_XRI_ABORTED_CX:
9226 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04009227 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
9228 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
9229 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
9230 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
9231 default:
9232 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9233 "2014 Invalid command 0x%x\n",
9234 iocbq->iocb.ulpCommand);
9235 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04009236 break;
James Smart4f774512009-05-22 14:52:35 -04009237 }
James Smart6d368e52011-05-24 11:44:12 -04009238
James Smart8012cc32012-10-31 14:44:49 -04009239 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
9240 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
9241 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
9242 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
9243 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
9244 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
9245 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
9246 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04009247 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
9248 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
9249 wqe->generic.wqe_com.abort_tag = abort_tag;
9250 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
9251 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
9252 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
9253 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04009254 return 0;
9255}
9256
9257/**
9258 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
9259 * @phba: Pointer to HBA context object.
9260 * @ring_number: SLI ring number to issue iocb on.
9261 * @piocb: Pointer to command iocb.
9262 * @flag: Flag indicating if this command can be put into txq.
9263 *
9264 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9265 * an iocb command to an HBA with SLI-4 interface spec.
9266 *
9267 * This function is called with hbalock held. The function will return success
9268 * after it successfully submit the iocb to firmware or after adding to the
9269 * txq.
9270 **/
9271static int
9272__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9273 struct lpfc_iocbq *piocb, uint32_t flag)
9274{
9275 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07009276 union lpfc_wqe *wqe;
9277 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05009278 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009279 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009280
James Smart895427b2017-02-12 13:52:30 -08009281 /* Get the WQ */
9282 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9283 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9284 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9285 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9286 else
9287 wq = phba->sli4_hba.oas_wq;
9288 } else {
9289 wq = phba->sli4_hba.els_wq;
9290 }
9291
9292 /* Get corresponding ring */
9293 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009294
James Smartb5c53952016-03-31 14:12:30 -07009295 /*
9296 * The WQE can be either 64 or 128 bytes,
9297 * so allocate space on the stack assuming the largest.
9298 */
9299 wqe = (union lpfc_wqe *)&wqe128;
9300
James Smart895427b2017-02-12 13:52:30 -08009301 lockdep_assert_held(&phba->hbalock);
9302
James Smart4f774512009-05-22 14:52:35 -04009303 if (piocb->sli4_xritag == NO_XRI) {
9304 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009305 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009306 sglq = NULL;
9307 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009308 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009309 if (!(flag & SLI_IOCB_RET_IOCB)) {
9310 __lpfc_sli_ringtx_put(phba,
9311 pring, piocb);
9312 return IOCB_SUCCESS;
9313 } else {
9314 return IOCB_BUSY;
9315 }
9316 } else {
James Smart895427b2017-02-12 13:52:30 -08009317 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009318 if (!sglq) {
9319 if (!(flag & SLI_IOCB_RET_IOCB)) {
9320 __lpfc_sli_ringtx_put(phba,
9321 pring,
9322 piocb);
9323 return IOCB_SUCCESS;
9324 } else
9325 return IOCB_BUSY;
9326 }
9327 }
James Smart4f774512009-05-22 14:52:35 -04009328 }
James Smart2ea259e2017-02-12 13:52:27 -08009329 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009330 /* These IO's already have an XRI and a mapped sgl. */
9331 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009332 else {
James Smart6d368e52011-05-24 11:44:12 -04009333 /*
9334 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009335 * sglq is on the active list
9336 */
James Smartedccdc12013-01-03 15:43:45 -05009337 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009338 if (!sglq)
9339 return IOCB_ERROR;
9340 }
9341
9342 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009343 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009344 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009345 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009346 return IOCB_ERROR;
9347 }
9348
James Smartb5c53952016-03-31 14:12:30 -07009349 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04009350 return IOCB_ERROR;
9351
James Smart895427b2017-02-12 13:52:30 -08009352 if (lpfc_sli4_wq_put(wq, wqe))
9353 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009354 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9355
9356 return 0;
9357}
9358
9359/**
James Smart3772a992009-05-22 14:50:54 -04009360 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9361 *
9362 * This routine wraps the actual lockless version for issusing IOCB function
9363 * pointer from the lpfc_hba struct.
9364 *
9365 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009366 * IOCB_ERROR - Error
9367 * IOCB_SUCCESS - Success
9368 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009369 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009370int
James Smart3772a992009-05-22 14:50:54 -04009371__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9372 struct lpfc_iocbq *piocb, uint32_t flag)
9373{
9374 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9375}
9376
9377/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009378 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009379 * @phba: The hba struct for which this call is being executed.
9380 * @dev_grp: The HBA PCI-Device group number.
9381 *
9382 * This routine sets up the SLI interface API function jump table in @phba
9383 * struct.
9384 * Returns: 0 - success, -ENODEV - failure.
9385 **/
9386int
9387lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9388{
9389
9390 switch (dev_grp) {
9391 case LPFC_PCI_DEV_LP:
9392 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9393 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9394 break;
James Smart4f774512009-05-22 14:52:35 -04009395 case LPFC_PCI_DEV_OC:
9396 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9397 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9398 break;
James Smart3772a992009-05-22 14:50:54 -04009399 default:
9400 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9401 "1419 Invalid HBA PCI-device group: 0x%x\n",
9402 dev_grp);
9403 return -ENODEV;
9404 break;
9405 }
9406 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9407 return 0;
9408}
James Smart92d7f7b2007-06-17 19:56:38 -05009409
James Smarta1efe162015-05-21 13:55:20 -04009410/**
James Smart895427b2017-02-12 13:52:30 -08009411 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009412 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009413 * @piocb: Pointer to command iocb.
9414 *
James Smart895427b2017-02-12 13:52:30 -08009415 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9416 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009417 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009418 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009419 */
James Smart895427b2017-02-12 13:52:30 -08009420struct lpfc_sli_ring *
9421lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009422{
James Smart895427b2017-02-12 13:52:30 -08009423 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009424 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009425 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009426 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009427 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009428 /*
James Smart895427b2017-02-12 13:52:30 -08009429 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009430 * be setup based on what work queue we used.
9431 */
Dick Kennedy8e036a92017-09-29 17:34:35 -07009432 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smart895427b2017-02-12 13:52:30 -08009433 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009434 lpfc_sli4_scmd_to_wqidx_distr(phba,
9435 piocb->context1);
Dick Kennedy8e036a92017-09-29 17:34:35 -07009436 piocb->hba_wqidx = piocb->hba_wqidx %
9437 phba->cfg_fcp_io_channel;
9438 }
James Smart895427b2017-02-12 13:52:30 -08009439 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009440 } else {
9441 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009442 return NULL;
9443 piocb->hba_wqidx = 0;
9444 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009445 }
James Smart895427b2017-02-12 13:52:30 -08009446 } else {
9447 if (unlikely(!phba->sli4_hba.els_wq))
9448 return NULL;
9449 piocb->hba_wqidx = 0;
9450 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009451 }
James Smart9bd2bff52014-09-03 12:57:30 -04009452}
9453
James Smarte59058c2008-08-24 21:49:00 -04009454/**
James Smart3621a712009-04-06 18:47:14 -04009455 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009456 * @phba: Pointer to HBA context object.
9457 * @pring: Pointer to driver SLI ring object.
9458 * @piocb: Pointer to command iocb.
9459 * @flag: Flag indicating if this command can be put into txq.
9460 *
9461 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9462 * function. This function gets the hbalock and calls
9463 * __lpfc_sli_issue_iocb function and will return the error returned
9464 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9465 * functions which do not hold hbalock.
9466 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009467int
James Smart3772a992009-05-22 14:50:54 -04009468lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009469 struct lpfc_iocbq *piocb, uint32_t flag)
9470{
James Smart895427b2017-02-12 13:52:30 -08009471 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009472 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009473 struct lpfc_queue *fpeq;
9474 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009475 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009476 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009477
James Smart7e56aa22012-08-03 12:35:34 -04009478 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009479 pring = lpfc_sli4_calc_ring(phba, piocb);
9480 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009481 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009482
James Smart9bd2bff52014-09-03 12:57:30 -04009483 spin_lock_irqsave(&pring->ring_lock, iflags);
9484 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9485 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009486
James Smart9bd2bff52014-09-03 12:57:30 -04009487 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009488 idx = piocb->hba_wqidx;
9489 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009490
James Smart895427b2017-02-12 13:52:30 -08009491 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009492
James Smart9bd2bff52014-09-03 12:57:30 -04009493 /* Get associated EQ with this index */
9494 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009495
James Smart9bd2bff52014-09-03 12:57:30 -04009496 /* Turn off interrupts from this EQ */
9497 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009498
James Smart9bd2bff52014-09-03 12:57:30 -04009499 /*
9500 * Process all the events on FCP EQ
9501 */
9502 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9503 lpfc_sli4_hba_handle_eqe(phba,
9504 eqe, idx);
9505 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009506 }
James Smartba20c852012-08-03 12:36:52 -04009507
James Smart9bd2bff52014-09-03 12:57:30 -04009508 /* Always clear and re-arm the EQ */
9509 lpfc_sli4_eq_release(fpeq,
9510 LPFC_QUEUE_REARM);
9511 }
James Smart895427b2017-02-12 13:52:30 -08009512 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009513 }
James Smart7e56aa22012-08-03 12:35:34 -04009514 } else {
9515 /* For now, SLI2/3 will still use hbalock */
9516 spin_lock_irqsave(&phba->hbalock, iflags);
9517 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9518 spin_unlock_irqrestore(&phba->hbalock, iflags);
9519 }
James Smart92d7f7b2007-06-17 19:56:38 -05009520 return rc;
9521}
9522
James Smarte59058c2008-08-24 21:49:00 -04009523/**
James Smart3621a712009-04-06 18:47:14 -04009524 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009525 * @phba: Pointer to HBA context object.
9526 *
9527 * This function is called while driver attaches with the
9528 * HBA to setup the extra ring. The extra ring is used
9529 * only when driver needs to support target mode functionality
9530 * or IP over FC functionalities.
9531 *
James Smart895427b2017-02-12 13:52:30 -08009532 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009533 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009534static int
9535lpfc_extra_ring_setup( struct lpfc_hba *phba)
9536{
9537 struct lpfc_sli *psli;
9538 struct lpfc_sli_ring *pring;
9539
9540 psli = &phba->sli;
9541
9542 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009543
9544 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009545 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009546 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9547 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9548 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9549 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009550
James Smarta4bc3372006-12-02 13:34:16 -05009551 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009552 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009553
James Smart7e56aa22012-08-03 12:35:34 -04009554 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9555 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9556 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9557 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009558
9559 /* Setup default profile for this ring */
9560 pring->iotag_max = 4096;
9561 pring->num_mask = 1;
9562 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009563 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9564 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009565 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9566 return 0;
9567}
9568
James Smartcb69f7d2011-12-13 13:21:57 -05009569/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9570 * @phba: Pointer to HBA context object.
9571 * @iocbq: Pointer to iocb object.
9572 *
9573 * The async_event handler calls this routine when it receives
9574 * an ASYNC_STATUS_CN event from the port. The port generates
9575 * this event when an Abort Sequence request to an rport fails
9576 * twice in succession. The abort could be originated by the
9577 * driver or by the port. The ABTS could have been for an ELS
9578 * or FCP IO. The port only generates this event when an ABTS
9579 * fails to complete after one retry.
9580 */
9581static void
9582lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9583 struct lpfc_iocbq *iocbq)
9584{
9585 struct lpfc_nodelist *ndlp = NULL;
9586 uint16_t rpi = 0, vpi = 0;
9587 struct lpfc_vport *vport = NULL;
9588
9589 /* The rpi in the ulpContext is vport-sensitive. */
9590 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9591 rpi = iocbq->iocb.ulpContext;
9592
9593 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9594 "3092 Port generated ABTS async event "
9595 "on vpi %d rpi %d status 0x%x\n",
9596 vpi, rpi, iocbq->iocb.ulpStatus);
9597
9598 vport = lpfc_find_vport_by_vpid(phba, vpi);
9599 if (!vport)
9600 goto err_exit;
9601 ndlp = lpfc_findnode_rpi(vport, rpi);
9602 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9603 goto err_exit;
9604
9605 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9606 lpfc_sli_abts_recover_port(vport, ndlp);
9607 return;
9608
9609 err_exit:
9610 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9611 "3095 Event Context not found, no "
9612 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9613 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9614 vpi, rpi);
9615}
9616
9617/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9618 * @phba: pointer to HBA context object.
9619 * @ndlp: nodelist pointer for the impacted rport.
9620 * @axri: pointer to the wcqe containing the failed exchange.
9621 *
9622 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9623 * port. The port generates this event when an abort exchange request to an
9624 * rport fails twice in succession with no reply. The abort could be originated
9625 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9626 */
9627void
9628lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9629 struct lpfc_nodelist *ndlp,
9630 struct sli4_wcqe_xri_aborted *axri)
9631{
9632 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009633 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009634
James Smart6b5151f2012-01-18 16:24:06 -05009635 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009636 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9637 "3115 Node Context not found, driver "
9638 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009639 return;
9640 }
9641
James Smartcb69f7d2011-12-13 13:21:57 -05009642 vport = ndlp->vport;
9643 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9644 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009645 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009646 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009647 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009648 bf_get(lpfc_wcqe_xa_status, axri),
9649 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009650
James Smart5c1db2a2012-03-01 22:34:36 -05009651 /*
9652 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9653 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9654 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9655 */
James Smarte3d2b802012-08-14 14:25:43 -04009656 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009657 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9658 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009659 lpfc_sli_abts_recover_port(vport, ndlp);
9660}
9661
James Smarte59058c2008-08-24 21:49:00 -04009662/**
James Smart3621a712009-04-06 18:47:14 -04009663 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009664 * @phba: Pointer to HBA context object.
9665 * @pring: Pointer to driver SLI ring object.
9666 * @iocbq: Pointer to iocb object.
9667 *
9668 * This function is called by the slow ring event handler
9669 * function when there is an ASYNC event iocb in the ring.
9670 * This function is called with no lock held.
9671 * Currently this function handles only temperature related
9672 * ASYNC events. The function decodes the temperature sensor
9673 * event message and posts events for the management applications.
9674 **/
James Smart98c9ea52007-10-27 13:37:33 -04009675static void
James Smart57127f12007-10-27 13:37:05 -04009676lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9677 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9678{
9679 IOCB_t *icmd;
9680 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009681 struct temp_event temp_event_data;
9682 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009683 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009684
9685 icmd = &iocbq->iocb;
9686 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009687
James Smartcb69f7d2011-12-13 13:21:57 -05009688 switch (evt_code) {
9689 case ASYNC_TEMP_WARN:
9690 case ASYNC_TEMP_SAFE:
9691 temp_event_data.data = (uint32_t) icmd->ulpContext;
9692 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9693 if (evt_code == ASYNC_TEMP_WARN) {
9694 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9695 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9696 "0347 Adapter is very hot, please take "
9697 "corrective action. temperature : %d Celsius\n",
9698 (uint32_t) icmd->ulpContext);
9699 } else {
9700 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9701 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9702 "0340 Adapter temperature is OK now. "
9703 "temperature : %d Celsius\n",
9704 (uint32_t) icmd->ulpContext);
9705 }
9706
9707 /* Send temperature change event to applications */
9708 shost = lpfc_shost_from_vport(phba->pport);
9709 fc_host_post_vendor_event(shost, fc_get_event_number(),
9710 sizeof(temp_event_data), (char *) &temp_event_data,
9711 LPFC_NL_VENDOR_ID);
9712 break;
9713 case ASYNC_STATUS_CN:
9714 lpfc_sli_abts_err_handler(phba, iocbq);
9715 break;
9716 default:
James Smarta257bf92009-04-06 18:48:10 -04009717 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009718 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009719 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009720 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009721 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9722 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9723 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9724 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009725 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009726 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9727 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9728 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9729 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9730
James Smartcb69f7d2011-12-13 13:21:57 -05009731 break;
James Smart57127f12007-10-27 13:37:05 -04009732 }
James Smart57127f12007-10-27 13:37:05 -04009733}
9734
9735
James Smarte59058c2008-08-24 21:49:00 -04009736/**
James Smart895427b2017-02-12 13:52:30 -08009737 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009738 * @phba: Pointer to HBA context object.
9739 *
9740 * lpfc_sli_setup sets up rings of the SLI interface with
9741 * number of iocbs per ring and iotags. This function is
9742 * called while driver attach to the HBA and before the
9743 * interrupts are enabled. So there is no need for locking.
9744 *
9745 * This function always returns 0.
9746 **/
dea31012005-04-17 16:05:31 -05009747int
James Smart895427b2017-02-12 13:52:30 -08009748lpfc_sli4_setup(struct lpfc_hba *phba)
9749{
9750 struct lpfc_sli_ring *pring;
9751
9752 pring = phba->sli4_hba.els_wq->pring;
9753 pring->num_mask = LPFC_MAX_RING_MASK;
9754 pring->prt[0].profile = 0; /* Mask 0 */
9755 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9756 pring->prt[0].type = FC_TYPE_ELS;
9757 pring->prt[0].lpfc_sli_rcv_unsol_event =
9758 lpfc_els_unsol_event;
9759 pring->prt[1].profile = 0; /* Mask 1 */
9760 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9761 pring->prt[1].type = FC_TYPE_ELS;
9762 pring->prt[1].lpfc_sli_rcv_unsol_event =
9763 lpfc_els_unsol_event;
9764 pring->prt[2].profile = 0; /* Mask 2 */
9765 /* NameServer Inquiry */
9766 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9767 /* NameServer */
9768 pring->prt[2].type = FC_TYPE_CT;
9769 pring->prt[2].lpfc_sli_rcv_unsol_event =
9770 lpfc_ct_unsol_event;
9771 pring->prt[3].profile = 0; /* Mask 3 */
9772 /* NameServer response */
9773 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9774 /* NameServer */
9775 pring->prt[3].type = FC_TYPE_CT;
9776 pring->prt[3].lpfc_sli_rcv_unsol_event =
9777 lpfc_ct_unsol_event;
9778 return 0;
9779}
9780
9781/**
9782 * lpfc_sli_setup - SLI ring setup function
9783 * @phba: Pointer to HBA context object.
9784 *
9785 * lpfc_sli_setup sets up rings of the SLI interface with
9786 * number of iocbs per ring and iotags. This function is
9787 * called while driver attach to the HBA and before the
9788 * interrupts are enabled. So there is no need for locking.
9789 *
9790 * This function always returns 0. SLI3 only.
9791 **/
9792int
dea31012005-04-17 16:05:31 -05009793lpfc_sli_setup(struct lpfc_hba *phba)
9794{
James Smarted957682007-06-17 19:56:37 -05009795 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009796 struct lpfc_sli *psli = &phba->sli;
9797 struct lpfc_sli_ring *pring;
9798
James Smart2a76a282012-08-03 12:35:54 -04009799 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -05009800 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -05009801
James Bottomley604a3e32005-10-29 10:28:33 -05009802 psli->iocbq_lookup = NULL;
9803 psli->iocbq_lookup_len = 0;
9804 psli->last_iotag = 0;
9805
dea31012005-04-17 16:05:31 -05009806 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009807 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009808 switch (i) {
9809 case LPFC_FCP_RING: /* ring 0 - FCP */
9810 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009811 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9812 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9813 pring->sli.sli3.numCiocb +=
9814 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9815 pring->sli.sli3.numRiocb +=
9816 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9817 pring->sli.sli3.numCiocb +=
9818 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9819 pring->sli.sli3.numRiocb +=
9820 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9821 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009822 SLI3_IOCB_CMD_SIZE :
9823 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009824 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009825 SLI3_IOCB_RSP_SIZE :
9826 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009827 pring->iotag_ctr = 0;
9828 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009829 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009830 pring->fast_iotag = pring->iotag_max;
9831 pring->num_mask = 0;
9832 break;
James Smarta4bc3372006-12-02 13:34:16 -05009833 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009834 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009835 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9836 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9837 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009838 SLI3_IOCB_CMD_SIZE :
9839 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009840 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009841 SLI3_IOCB_RSP_SIZE :
9842 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009843 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009844 pring->num_mask = 0;
9845 break;
9846 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9847 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009848 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9849 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9850 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009851 SLI3_IOCB_CMD_SIZE :
9852 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009853 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009854 SLI3_IOCB_RSP_SIZE :
9855 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009856 pring->fast_iotag = 0;
9857 pring->iotag_ctr = 0;
9858 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009859 pring->lpfc_sli_rcv_async_status =
9860 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009861 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009862 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009863 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9864 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009865 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009866 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009867 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009868 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9869 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009870 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009871 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009872 pring->prt[2].profile = 0; /* Mask 2 */
9873 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009874 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009875 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009876 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009877 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009878 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009879 pring->prt[3].profile = 0; /* Mask 3 */
9880 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009881 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009882 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009883 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009884 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009885 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009886 break;
9887 }
James Smart7e56aa22012-08-03 12:35:34 -04009888 totiocbsize += (pring->sli.sli3.numCiocb *
9889 pring->sli.sli3.sizeCiocb) +
9890 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009891 }
James Smarted957682007-06-17 19:56:37 -05009892 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009893 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009894 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9895 "SLI2 SLIM Data: x%x x%lx\n",
9896 phba->brd_no, totiocbsize,
9897 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009898 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009899 if (phba->cfg_multi_ring_support == 2)
9900 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009901
9902 return 0;
9903}
9904
James Smarte59058c2008-08-24 21:49:00 -04009905/**
James Smart895427b2017-02-12 13:52:30 -08009906 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009907 * @phba: Pointer to HBA context object.
9908 *
James Smart895427b2017-02-12 13:52:30 -08009909 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -04009910 * ring. This function also initializes ring indices of each ring.
9911 * This function is called during the initialization of the SLI
9912 * interface of an HBA.
9913 * This function is called with no lock held and always returns
9914 * 1.
9915 **/
James Smart895427b2017-02-12 13:52:30 -08009916void
9917lpfc_sli4_queue_init(struct lpfc_hba *phba)
9918{
9919 struct lpfc_sli *psli;
9920 struct lpfc_sli_ring *pring;
9921 int i;
9922
9923 psli = &phba->sli;
9924 spin_lock_irq(&phba->hbalock);
9925 INIT_LIST_HEAD(&psli->mboxq);
9926 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9927 /* Initialize list headers for txq and txcmplq as double linked lists */
9928 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9929 pring = phba->sli4_hba.fcp_wq[i]->pring;
9930 pring->flag = 0;
9931 pring->ringno = LPFC_FCP_RING;
9932 INIT_LIST_HEAD(&pring->txq);
9933 INIT_LIST_HEAD(&pring->txcmplq);
9934 INIT_LIST_HEAD(&pring->iocb_continueq);
9935 spin_lock_init(&pring->ring_lock);
9936 }
9937 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9938 pring = phba->sli4_hba.nvme_wq[i]->pring;
9939 pring->flag = 0;
9940 pring->ringno = LPFC_FCP_RING;
9941 INIT_LIST_HEAD(&pring->txq);
9942 INIT_LIST_HEAD(&pring->txcmplq);
9943 INIT_LIST_HEAD(&pring->iocb_continueq);
9944 spin_lock_init(&pring->ring_lock);
9945 }
9946 pring = phba->sli4_hba.els_wq->pring;
9947 pring->flag = 0;
9948 pring->ringno = LPFC_ELS_RING;
9949 INIT_LIST_HEAD(&pring->txq);
9950 INIT_LIST_HEAD(&pring->txcmplq);
9951 INIT_LIST_HEAD(&pring->iocb_continueq);
9952 spin_lock_init(&pring->ring_lock);
9953
9954 if (phba->cfg_nvme_io_channel) {
9955 pring = phba->sli4_hba.nvmels_wq->pring;
9956 pring->flag = 0;
9957 pring->ringno = LPFC_ELS_RING;
9958 INIT_LIST_HEAD(&pring->txq);
9959 INIT_LIST_HEAD(&pring->txcmplq);
9960 INIT_LIST_HEAD(&pring->iocb_continueq);
9961 spin_lock_init(&pring->ring_lock);
9962 }
9963
9964 if (phba->cfg_fof) {
9965 pring = phba->sli4_hba.oas_wq->pring;
9966 pring->flag = 0;
9967 pring->ringno = LPFC_FCP_RING;
9968 INIT_LIST_HEAD(&pring->txq);
9969 INIT_LIST_HEAD(&pring->txcmplq);
9970 INIT_LIST_HEAD(&pring->iocb_continueq);
9971 spin_lock_init(&pring->ring_lock);
9972 }
9973
9974 spin_unlock_irq(&phba->hbalock);
9975}
9976
9977/**
9978 * lpfc_sli_queue_init - Queue initialization function
9979 * @phba: Pointer to HBA context object.
9980 *
9981 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9982 * ring. This function also initializes ring indices of each ring.
9983 * This function is called during the initialization of the SLI
9984 * interface of an HBA.
9985 * This function is called with no lock held and always returns
9986 * 1.
9987 **/
9988void
9989lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009990{
9991 struct lpfc_sli *psli;
9992 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009993 int i;
dea31012005-04-17 16:05:31 -05009994
9995 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009996 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009997 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009998 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009999 /* Initialize list headers for txq and txcmplq as double linked lists */
10000 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -080010001 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -050010002 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -040010003 pring->sli.sli3.next_cmdidx = 0;
10004 pring->sli.sli3.local_getidx = 0;
10005 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -050010006 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -050010007 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -050010008 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -080010009 pring->flag = 0;
10010 INIT_LIST_HEAD(&pring->txq);
10011 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -040010012 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -050010013 }
James Smart2e0fef82007-06-17 19:56:36 -050010014 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010015}
10016
James Smarte59058c2008-08-24 21:49:00 -040010017/**
James Smart04c68492009-05-22 14:52:52 -040010018 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
10019 * @phba: Pointer to HBA context object.
10020 *
10021 * This routine flushes the mailbox command subsystem. It will unconditionally
10022 * flush all the mailbox commands in the three possible stages in the mailbox
10023 * command sub-system: pending mailbox command queue; the outstanding mailbox
10024 * command; and completed mailbox command queue. It is caller's responsibility
10025 * to make sure that the driver is in the proper state to flush the mailbox
10026 * command sub-system. Namely, the posting of mailbox commands into the
10027 * pending mailbox command queue from the various clients must be stopped;
10028 * either the HBA is in a state that it will never works on the outstanding
10029 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
10030 * mailbox command has been completed.
10031 **/
10032static void
10033lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
10034{
10035 LIST_HEAD(completions);
10036 struct lpfc_sli *psli = &phba->sli;
10037 LPFC_MBOXQ_t *pmb;
10038 unsigned long iflag;
10039
10040 /* Flush all the mailbox commands in the mbox system */
10041 spin_lock_irqsave(&phba->hbalock, iflag);
10042 /* The pending mailbox command queue */
10043 list_splice_init(&phba->sli.mboxq, &completions);
10044 /* The outstanding active mailbox command */
10045 if (psli->mbox_active) {
10046 list_add_tail(&psli->mbox_active->list, &completions);
10047 psli->mbox_active = NULL;
10048 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10049 }
10050 /* The completed mailbox command queue */
10051 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
10052 spin_unlock_irqrestore(&phba->hbalock, iflag);
10053
10054 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
10055 while (!list_empty(&completions)) {
10056 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
10057 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
10058 if (pmb->mbox_cmpl)
10059 pmb->mbox_cmpl(phba, pmb);
10060 }
10061}
10062
10063/**
James Smart3621a712009-04-06 18:47:14 -040010064 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -040010065 * @vport: Pointer to virtual port object.
10066 *
10067 * lpfc_sli_host_down is called to clean up the resources
10068 * associated with a vport before destroying virtual
10069 * port data structures.
10070 * This function does following operations:
10071 * - Free discovery resources associated with this virtual
10072 * port.
10073 * - Free iocbs associated with this virtual port in
10074 * the txq.
10075 * - Send abort for all iocb commands associated with this
10076 * vport in txcmplq.
10077 *
10078 * This function is called with no lock held and always returns 1.
10079 **/
dea31012005-04-17 16:05:31 -050010080int
James Smart92d7f7b2007-06-17 19:56:38 -050010081lpfc_sli_host_down(struct lpfc_vport *vport)
10082{
James Smart858c9f62007-06-17 19:56:39 -050010083 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -050010084 struct lpfc_hba *phba = vport->phba;
10085 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010086 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -050010087 struct lpfc_sli_ring *pring;
10088 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -050010089 int i;
10090 unsigned long flags = 0;
10091 uint16_t prev_pring_flag;
10092
10093 lpfc_cleanup_discovery_resources(vport);
10094
10095 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010096
James Smart895427b2017-02-12 13:52:30 -080010097 /*
10098 * Error everything on the txq since these iocbs
10099 * have not been given to the FW yet.
10100 * Also issue ABTS for everything on the txcmplq
10101 */
10102 if (phba->sli_rev != LPFC_SLI_REV4) {
10103 for (i = 0; i < psli->num_rings; i++) {
10104 pring = &psli->sli3_ring[i];
10105 prev_pring_flag = pring->flag;
10106 /* Only slow rings */
10107 if (pring->ringno == LPFC_ELS_RING) {
10108 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10109 /* Set the lpfc data pending flag */
10110 set_bit(LPFC_DATA_READY, &phba->data_flags);
10111 }
10112 list_for_each_entry_safe(iocb, next_iocb,
10113 &pring->txq, list) {
10114 if (iocb->vport != vport)
10115 continue;
10116 list_move_tail(&iocb->list, &completions);
10117 }
10118 list_for_each_entry_safe(iocb, next_iocb,
10119 &pring->txcmplq, list) {
10120 if (iocb->vport != vport)
10121 continue;
10122 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10123 }
10124 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -050010125 }
James Smart895427b2017-02-12 13:52:30 -080010126 } else {
10127 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10128 pring = qp->pring;
10129 if (!pring)
10130 continue;
10131 if (pring == phba->sli4_hba.els_wq->pring) {
10132 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10133 /* Set the lpfc data pending flag */
10134 set_bit(LPFC_DATA_READY, &phba->data_flags);
10135 }
10136 prev_pring_flag = pring->flag;
10137 spin_lock_irq(&pring->ring_lock);
10138 list_for_each_entry_safe(iocb, next_iocb,
10139 &pring->txq, list) {
10140 if (iocb->vport != vport)
10141 continue;
10142 list_move_tail(&iocb->list, &completions);
10143 }
10144 spin_unlock_irq(&pring->ring_lock);
10145 list_for_each_entry_safe(iocb, next_iocb,
10146 &pring->txcmplq, list) {
10147 if (iocb->vport != vport)
10148 continue;
10149 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
10150 }
10151 pring->flag = prev_pring_flag;
10152 }
James Smart92d7f7b2007-06-17 19:56:38 -050010153 }
James Smart92d7f7b2007-06-17 19:56:38 -050010154 spin_unlock_irqrestore(&phba->hbalock, flags);
10155
James Smarta257bf92009-04-06 18:48:10 -040010156 /* Cancel all the IOCBs from the completions list */
10157 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10158 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -050010159 return 1;
10160}
10161
James Smarte59058c2008-08-24 21:49:00 -040010162/**
James Smart3621a712009-04-06 18:47:14 -040010163 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -040010164 * @phba: Pointer to HBA context object.
10165 *
10166 * This function cleans up all iocb, buffers, mailbox commands
10167 * while shutting down the HBA. This function is called with no
10168 * lock held and always returns 1.
10169 * This function does the following to cleanup driver resources:
10170 * - Free discovery resources for each virtual port
10171 * - Cleanup any pending fabric iocbs
10172 * - Iterate through the iocb txq and free each entry
10173 * in the list.
10174 * - Free up any buffer posted to the HBA
10175 * - Free mailbox commands in the mailbox queue.
10176 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010177int
James Smart2e0fef82007-06-17 19:56:36 -050010178lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -050010179{
James Smart2534ba72007-04-25 09:52:20 -040010180 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -050010181 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -080010182 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -050010183 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -050010184 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -050010185 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -040010186 int i;
10187
10188 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -040010189 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -050010190
dea31012005-04-17 16:05:31 -050010191 lpfc_hba_down_prep(phba);
10192
James Smart92d7f7b2007-06-17 19:56:38 -050010193 lpfc_fabric_abort_hba(phba);
10194
James Smart2e0fef82007-06-17 19:56:36 -050010195 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010196
James Smart895427b2017-02-12 13:52:30 -080010197 /*
10198 * Error everything on the txq since these iocbs
10199 * have not been given to the FW yet.
10200 */
10201 if (phba->sli_rev != LPFC_SLI_REV4) {
10202 for (i = 0; i < psli->num_rings; i++) {
10203 pring = &psli->sli3_ring[i];
10204 /* Only slow rings */
10205 if (pring->ringno == LPFC_ELS_RING) {
10206 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10207 /* Set the lpfc data pending flag */
10208 set_bit(LPFC_DATA_READY, &phba->data_flags);
10209 }
10210 list_splice_init(&pring->txq, &completions);
10211 }
10212 } else {
10213 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10214 pring = qp->pring;
10215 if (!pring)
10216 continue;
10217 spin_lock_irq(&pring->ring_lock);
10218 list_splice_init(&pring->txq, &completions);
10219 spin_unlock_irq(&pring->ring_lock);
10220 if (pring == phba->sli4_hba.els_wq->pring) {
10221 pring->flag |= LPFC_DEFERRED_RING_EVENT;
10222 /* Set the lpfc data pending flag */
10223 set_bit(LPFC_DATA_READY, &phba->data_flags);
10224 }
10225 }
dea31012005-04-17 16:05:31 -050010226 }
James Smart2e0fef82007-06-17 19:56:36 -050010227 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -050010228
James Smarta257bf92009-04-06 18:48:10 -040010229 /* Cancel all the IOCBs from the completions list */
10230 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10231 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -040010232
James Smart0ff10d42008-01-11 01:52:36 -050010233 spin_lock_irqsave(&phba->hbalock, flags);
10234 list_splice_init(&phba->elsbuf, &completions);
10235 phba->elsbuf_cnt = 0;
10236 phba->elsbuf_prev_cnt = 0;
10237 spin_unlock_irqrestore(&phba->hbalock, flags);
10238
10239 while (!list_empty(&completions)) {
10240 list_remove_head(&completions, buf_ptr,
10241 struct lpfc_dmabuf, list);
10242 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
10243 kfree(buf_ptr);
10244 }
10245
dea31012005-04-17 16:05:31 -050010246 /* Return any active mbox cmds */
10247 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -050010248
James Smartda0436e2009-05-22 14:51:39 -040010249 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010250 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -040010251 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -050010252
James Smartda0436e2009-05-22 14:51:39 -040010253 return 1;
10254}
James Smart92d7f7b2007-06-17 19:56:38 -050010255
James Smartda0436e2009-05-22 14:51:39 -040010256/**
James Smart3621a712009-04-06 18:47:14 -040010257 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -040010258 * @srcp: Source memory pointer.
10259 * @destp: Destination memory pointer.
10260 * @cnt: Number of words required to be copied.
10261 *
10262 * This function is used for copying data between driver memory
10263 * and the SLI memory. This function also changes the endianness
10264 * of each word if native endianness is different from SLI
10265 * endianness. This function can be called with or without
10266 * lock.
10267 **/
dea31012005-04-17 16:05:31 -050010268void
10269lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10270{
10271 uint32_t *src = srcp;
10272 uint32_t *dest = destp;
10273 uint32_t ldata;
10274 int i;
10275
10276 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10277 ldata = *src;
10278 ldata = le32_to_cpu(ldata);
10279 *dest = ldata;
10280 src++;
10281 dest++;
10282 }
10283}
10284
James Smarte59058c2008-08-24 21:49:00 -040010285
10286/**
James Smarta0c87cb2009-07-19 10:01:10 -040010287 * lpfc_sli_bemem_bcopy - SLI memory copy function
10288 * @srcp: Source memory pointer.
10289 * @destp: Destination memory pointer.
10290 * @cnt: Number of words required to be copied.
10291 *
10292 * This function is used for copying data between a data structure
10293 * with big endian representation to local endianness.
10294 * This function can be called with or without lock.
10295 **/
10296void
10297lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10298{
10299 uint32_t *src = srcp;
10300 uint32_t *dest = destp;
10301 uint32_t ldata;
10302 int i;
10303
10304 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10305 ldata = *src;
10306 ldata = be32_to_cpu(ldata);
10307 *dest = ldata;
10308 src++;
10309 dest++;
10310 }
10311}
10312
10313/**
James Smart3621a712009-04-06 18:47:14 -040010314 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010315 * @phba: Pointer to HBA context object.
10316 * @pring: Pointer to driver SLI ring object.
10317 * @mp: Pointer to driver buffer object.
10318 *
10319 * This function is called with no lock held.
10320 * It always return zero after adding the buffer to the postbufq
10321 * buffer list.
10322 **/
dea31012005-04-17 16:05:31 -050010323int
James Smart2e0fef82007-06-17 19:56:36 -050010324lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10325 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010326{
10327 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10328 later */
James Smart2e0fef82007-06-17 19:56:36 -050010329 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010330 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010331 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010332 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010333 return 0;
10334}
10335
James Smarte59058c2008-08-24 21:49:00 -040010336/**
James Smart3621a712009-04-06 18:47:14 -040010337 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010338 * @phba: Pointer to HBA context object.
10339 *
10340 * When HBQ is enabled, buffers are searched based on tags. This function
10341 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10342 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10343 * does not conflict with tags of buffer posted for unsolicited events.
10344 * The function returns the allocated tag. The function is called with
10345 * no locks held.
10346 **/
James Smart76bb24e2007-10-27 13:38:00 -040010347uint32_t
10348lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10349{
10350 spin_lock_irq(&phba->hbalock);
10351 phba->buffer_tag_count++;
10352 /*
10353 * Always set the QUE_BUFTAG_BIT to distiguish between
10354 * a tag assigned by HBQ.
10355 */
10356 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10357 spin_unlock_irq(&phba->hbalock);
10358 return phba->buffer_tag_count;
10359}
10360
James Smarte59058c2008-08-24 21:49:00 -040010361/**
James Smart3621a712009-04-06 18:47:14 -040010362 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010363 * @phba: Pointer to HBA context object.
10364 * @pring: Pointer to driver SLI ring object.
10365 * @tag: Buffer tag.
10366 *
10367 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10368 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10369 * iocb is posted to the response ring with the tag of the buffer.
10370 * This function searches the pring->postbufq list using the tag
10371 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10372 * iocb. If the buffer is found then lpfc_dmabuf object of the
10373 * buffer is returned to the caller else NULL is returned.
10374 * This function is called with no lock held.
10375 **/
James Smart76bb24e2007-10-27 13:38:00 -040010376struct lpfc_dmabuf *
10377lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10378 uint32_t tag)
10379{
10380 struct lpfc_dmabuf *mp, *next_mp;
10381 struct list_head *slp = &pring->postbufq;
10382
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010383 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010384 spin_lock_irq(&phba->hbalock);
10385 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10386 if (mp->buffer_tag == tag) {
10387 list_del_init(&mp->list);
10388 pring->postbufq_cnt--;
10389 spin_unlock_irq(&phba->hbalock);
10390 return mp;
10391 }
10392 }
10393
10394 spin_unlock_irq(&phba->hbalock);
10395 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010396 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010397 "ring %d Data x%lx x%p x%p x%x\n",
10398 pring->ringno, (unsigned long) tag,
10399 slp->next, slp->prev, pring->postbufq_cnt);
10400
10401 return NULL;
10402}
dea31012005-04-17 16:05:31 -050010403
James Smarte59058c2008-08-24 21:49:00 -040010404/**
James Smart3621a712009-04-06 18:47:14 -040010405 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010406 * @phba: Pointer to HBA context object.
10407 * @pring: Pointer to driver SLI ring object.
10408 * @phys: DMA address of the buffer.
10409 *
10410 * This function searches the buffer list using the dma_address
10411 * of unsolicited event to find the driver's lpfc_dmabuf object
10412 * corresponding to the dma_address. The function returns the
10413 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10414 * This function is called by the ct and els unsolicited event
10415 * handlers to get the buffer associated with the unsolicited
10416 * event.
10417 *
10418 * This function is called with no lock held.
10419 **/
dea31012005-04-17 16:05:31 -050010420struct lpfc_dmabuf *
10421lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10422 dma_addr_t phys)
10423{
10424 struct lpfc_dmabuf *mp, *next_mp;
10425 struct list_head *slp = &pring->postbufq;
10426
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010427 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010428 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010429 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10430 if (mp->phys == phys) {
10431 list_del_init(&mp->list);
10432 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010433 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010434 return mp;
10435 }
10436 }
10437
James Smart2e0fef82007-06-17 19:56:36 -050010438 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010439 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010440 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010441 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010442 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010443 slp->next, slp->prev, pring->postbufq_cnt);
10444 return NULL;
10445}
10446
James Smarte59058c2008-08-24 21:49:00 -040010447/**
James Smart3621a712009-04-06 18:47:14 -040010448 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010449 * @phba: Pointer to HBA context object.
10450 * @cmdiocb: Pointer to driver command iocb object.
10451 * @rspiocb: Pointer to driver response iocb object.
10452 *
10453 * This function is the completion handler for the abort iocbs for
10454 * ELS commands. This function is called from the ELS ring event
10455 * handler with no lock held. This function frees memory resources
10456 * associated with the abort iocb.
10457 **/
dea31012005-04-17 16:05:31 -050010458static void
James Smart2e0fef82007-06-17 19:56:36 -050010459lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10460 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010461{
James Smart2e0fef82007-06-17 19:56:36 -050010462 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010463 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010464 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010465
10466 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010467
10468 /*
10469 * Assume that the port already completed and returned, or
10470 * will return the iocb. Just Log the message.
10471 */
James Smart2680eea2007-04-25 09:52:55 -040010472 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10473 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10474
James Smart2e0fef82007-06-17 19:56:36 -050010475 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010476 if (phba->sli_rev < LPFC_SLI_REV4) {
10477 if (abort_iotag != 0 &&
10478 abort_iotag <= phba->sli.last_iotag)
10479 abort_iocb =
10480 phba->sli.iocbq_lookup[abort_iotag];
10481 } else
10482 /* For sli4 the abort_tag is the XRI,
10483 * so the abort routine puts the iotag of the iocb
10484 * being aborted in the context field of the abort
10485 * IOCB.
10486 */
10487 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010488
James Smart2a9bf3d2010-06-07 15:24:45 -040010489 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10490 "0327 Cannot abort els iocb %p "
10491 "with tag %x context %x, abort status %x, "
10492 "abort code %x\n",
10493 abort_iocb, abort_iotag, abort_context,
10494 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010495
James Smartff78d8f2011-12-13 13:21:35 -050010496 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010497 }
James Bottomley604a3e32005-10-29 10:28:33 -050010498 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010499 return;
10500}
10501
James Smarte59058c2008-08-24 21:49:00 -040010502/**
James Smart3621a712009-04-06 18:47:14 -040010503 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010504 * @phba: Pointer to HBA context object.
10505 * @cmdiocb: Pointer to driver command iocb object.
10506 * @rspiocb: Pointer to driver response iocb object.
10507 *
10508 * The function is called from SLI ring event handler with no
10509 * lock held. This function is the completion handler for ELS commands
10510 * which are aborted. The function frees memory resources used for
10511 * the aborted ELS commands.
10512 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010513static void
10514lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10515 struct lpfc_iocbq *rspiocb)
10516{
10517 IOCB_t *irsp = &rspiocb->iocb;
10518
10519 /* ELS cmd tag <ulpIoTag> completes */
10520 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010521 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010522 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010523 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010524 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010525 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10526 lpfc_ct_free_iocb(phba, cmdiocb);
10527 else
10528 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010529 return;
10530}
10531
James Smarte59058c2008-08-24 21:49:00 -040010532/**
James Smart5af5eee2010-10-22 11:06:38 -040010533 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010534 * @phba: Pointer to HBA context object.
10535 * @pring: Pointer to driver SLI ring object.
10536 * @cmdiocb: Pointer to driver command iocb object.
10537 *
James Smart5af5eee2010-10-22 11:06:38 -040010538 * This function issues an abort iocb for the provided command iocb down to
10539 * the port. Other than the case the outstanding command iocb is an abort
10540 * request, this function issues abort out unconditionally. This function is
10541 * called with hbalock held. The function returns 0 when it fails due to
10542 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010543 **/
James Smart5af5eee2010-10-22 11:06:38 -040010544static int
10545lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010546 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010547{
James Smart2e0fef82007-06-17 19:56:36 -050010548 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010549 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010550 IOCB_t *icmd = NULL;
10551 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010552 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010553 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010554
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010555 lockdep_assert_held(&phba->hbalock);
10556
James Smart92d7f7b2007-06-17 19:56:38 -050010557 /*
10558 * There are certain command types we don't want to abort. And we
10559 * don't want to abort commands that are already in the process of
10560 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010561 */
10562 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010563 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010564 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10565 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010566 return 0;
10567
dea31012005-04-17 16:05:31 -050010568 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010569 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010570 if (abtsiocbp == NULL)
10571 return 0;
dea31012005-04-17 16:05:31 -050010572
James Smart07951072007-04-25 09:51:38 -040010573 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010574 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010575 */
10576 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10577
dea31012005-04-17 16:05:31 -050010578 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010579 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10580 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010581 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010582 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010583 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10584 }
James Smartda0436e2009-05-22 14:51:39 -040010585 else
10586 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010587 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010588 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010589
James Smart5ffc2662009-11-18 15:39:44 -050010590 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010591 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010592 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10593 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010594 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10595 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010596
James Smart2e0fef82007-06-17 19:56:36 -050010597 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010598 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10599 else
10600 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10601
10602 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010603 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010604
James Smarte8b62012007-08-02 11:10:09 -040010605 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10606 "0339 Abort xri x%x, original iotag x%x, "
10607 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010608 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010609 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010610 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010611
10612 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010613 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10614 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010615 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010616 /* Note: both hbalock and ring_lock need to be set here */
10617 spin_lock_irqsave(&pring->ring_lock, iflags);
10618 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10619 abtsiocbp, 0);
10620 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10621 } else {
10622 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10623 abtsiocbp, 0);
10624 }
James Smart07951072007-04-25 09:51:38 -040010625
James Smartd7c255b2008-08-24 21:50:00 -040010626 if (retval)
10627 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010628
10629 /*
10630 * Caller to this routine should check for IOCB_ERROR
10631 * and handle it properly. This routine no longer removes
10632 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10633 */
10634 return retval;
10635}
10636
10637/**
10638 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10639 * @phba: Pointer to HBA context object.
10640 * @pring: Pointer to driver SLI ring object.
10641 * @cmdiocb: Pointer to driver command iocb object.
10642 *
10643 * This function issues an abort iocb for the provided command iocb. In case
10644 * of unloading, the abort iocb will not be issued to commands on the ELS
10645 * ring. Instead, the callback function shall be changed to those commands
10646 * so that nothing happens when them finishes. This function is called with
10647 * hbalock held. The function returns 0 when the command iocb is an abort
10648 * request.
10649 **/
10650int
10651lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10652 struct lpfc_iocbq *cmdiocb)
10653{
10654 struct lpfc_vport *vport = cmdiocb->vport;
10655 int retval = IOCB_ERROR;
10656 IOCB_t *icmd = NULL;
10657
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010658 lockdep_assert_held(&phba->hbalock);
10659
James Smart5af5eee2010-10-22 11:06:38 -040010660 /*
10661 * There are certain command types we don't want to abort. And we
10662 * don't want to abort commands that are already in the process of
10663 * being aborted.
10664 */
10665 icmd = &cmdiocb->iocb;
10666 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10667 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10668 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10669 return 0;
10670
Dick Kennedy1234a6d2017-09-29 17:34:29 -070010671 if (!pring) {
10672 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10673 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10674 else
10675 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10676 goto abort_iotag_exit;
10677 }
10678
James Smart5af5eee2010-10-22 11:06:38 -040010679 /*
10680 * If we're unloading, don't abort iocb on the ELS ring, but change
10681 * the callback so that nothing happens when it finishes.
10682 */
10683 if ((vport->load_flag & FC_UNLOADING) &&
10684 (pring->ringno == LPFC_ELS_RING)) {
10685 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10686 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10687 else
10688 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10689 goto abort_iotag_exit;
10690 }
10691
10692 /* Now, we try to issue the abort to the cmdiocb out */
10693 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10694
James Smart07951072007-04-25 09:51:38 -040010695abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010696 /*
10697 * Caller to this routine should check for IOCB_ERROR
10698 * and handle it properly. This routine no longer removes
10699 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010700 */
James Smart2e0fef82007-06-17 19:56:36 -050010701 return retval;
dea31012005-04-17 16:05:31 -050010702}
10703
James Smarte59058c2008-08-24 21:49:00 -040010704/**
James Smart895427b2017-02-12 13:52:30 -080010705 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10706 * @phba: Pointer to HBA context object.
10707 * @pring: Pointer to driver SLI ring object.
10708 * @cmdiocb: Pointer to driver command iocb object.
10709 *
10710 * This function issues an abort iocb for the provided command iocb down to
10711 * the port. Other than the case the outstanding command iocb is an abort
10712 * request, this function issues abort out unconditionally. This function is
10713 * called with hbalock held. The function returns 0 when it fails due to
10714 * memory allocation failure or when the command iocb is an abort request.
10715 **/
10716static int
10717lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10718 struct lpfc_iocbq *cmdiocb)
10719{
10720 struct lpfc_vport *vport = cmdiocb->vport;
10721 struct lpfc_iocbq *abtsiocbp;
10722 union lpfc_wqe *abts_wqe;
10723 int retval;
10724
10725 /*
10726 * There are certain command types we don't want to abort. And we
10727 * don't want to abort commands that are already in the process of
10728 * being aborted.
10729 */
10730 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10731 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10732 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10733 return 0;
10734
10735 /* issue ABTS for this io based on iotag */
10736 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10737 if (abtsiocbp == NULL)
10738 return 0;
10739
10740 /* This signals the response to set the correct status
10741 * before calling the completion handler
10742 */
10743 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10744
10745 /* Complete prepping the abort wqe and issue to the FW. */
10746 abts_wqe = &abtsiocbp->wqe;
10747 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10748 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10749
10750 /* Explicitly set reserved fields to zero.*/
10751 abts_wqe->abort_cmd.rsrvd4 = 0;
10752 abts_wqe->abort_cmd.rsrvd5 = 0;
10753
10754 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10755 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10756 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10757
10758 /* word 7 */
10759 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10760 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10761 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10762 cmdiocb->iocb.ulpClass);
10763
10764 /* word 8 - tell the FW to abort the IO associated with this
10765 * outstanding exchange ID.
10766 */
10767 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10768
10769 /* word 9 - this is the iotag for the abts_wqe completion. */
10770 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10771 abtsiocbp->iotag);
10772
10773 /* word 10 */
10774 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10775 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10776 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10777
10778 /* word 11 */
10779 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10780 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10781 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10782
10783 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10784 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10785 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080010786 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080010787 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
Dick Kennedycd22d602017-08-23 16:55:35 -070010788 if (retval) {
James Smart895427b2017-02-12 13:52:30 -080010789 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10790 "6147 Failed abts issue_wqe with status x%x "
10791 "for oxid x%x\n",
10792 retval, cmdiocb->sli4_xritag);
10793 lpfc_sli_release_iocbq(phba, abtsiocbp);
10794 return retval;
10795 }
10796
10797 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10798 "6148 Drv Abort NVME Request Issued for "
10799 "ox_id x%x on reqtag x%x\n",
10800 cmdiocb->sli4_xritag,
10801 abtsiocbp->iotag);
10802
10803 return retval;
10804}
10805
10806/**
James Smart5af5eee2010-10-22 11:06:38 -040010807 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10808 * @phba: pointer to lpfc HBA data structure.
10809 *
10810 * This routine will abort all pending and outstanding iocbs to an HBA.
10811 **/
10812void
10813lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10814{
10815 struct lpfc_sli *psli = &phba->sli;
10816 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080010817 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010818 int i;
10819
James Smart895427b2017-02-12 13:52:30 -080010820 if (phba->sli_rev != LPFC_SLI_REV4) {
10821 for (i = 0; i < psli->num_rings; i++) {
10822 pring = &psli->sli3_ring[i];
10823 lpfc_sli_abort_iocb_ring(phba, pring);
10824 }
10825 return;
10826 }
10827 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10828 pring = qp->pring;
10829 if (!pring)
10830 continue;
James Smartdb55fba2014-04-04 13:52:02 -040010831 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010832 }
10833}
10834
10835/**
James Smart3621a712009-04-06 18:47:14 -040010836 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010837 * @iocbq: Pointer to driver iocb object.
10838 * @vport: Pointer to driver virtual port object.
10839 * @tgt_id: SCSI ID of the target.
10840 * @lun_id: LUN ID of the scsi device.
10841 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10842 *
James Smart3621a712009-04-06 18:47:14 -040010843 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010844 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10845 * 0 if the filtering criteria is met for the given iocb and will return
10846 * 1 if the filtering criteria is not met.
10847 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10848 * given iocb is for the SCSI device specified by vport, tgt_id and
10849 * lun_id parameter.
10850 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10851 * given iocb is for the SCSI target specified by vport and tgt_id
10852 * parameters.
10853 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10854 * given iocb is for the SCSI host associated with the given vport.
10855 * This function is called with no locks held.
10856 **/
dea31012005-04-17 16:05:31 -050010857static int
James Smart51ef4c22007-08-02 11:10:31 -040010858lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10859 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010860 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010861{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010862 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010863 int rc = 1;
10864
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010865 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10866 return rc;
10867
James Smart51ef4c22007-08-02 11:10:31 -040010868 if (iocbq->vport != vport)
10869 return rc;
10870
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010871 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010872
James Smart495a7142008-06-14 22:52:59 -040010873 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010874 return rc;
10875
10876 switch (ctx_cmd) {
10877 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010878 if ((lpfc_cmd->rdata->pnode) &&
10879 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10880 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010881 rc = 0;
10882 break;
10883 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010884 if ((lpfc_cmd->rdata->pnode) &&
10885 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010886 rc = 0;
10887 break;
dea31012005-04-17 16:05:31 -050010888 case LPFC_CTX_HOST:
10889 rc = 0;
10890 break;
10891 default:
10892 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010893 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010894 break;
10895 }
10896
10897 return rc;
10898}
10899
James Smarte59058c2008-08-24 21:49:00 -040010900/**
James Smart3621a712009-04-06 18:47:14 -040010901 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010902 * @vport: Pointer to virtual port.
10903 * @tgt_id: SCSI ID of the target.
10904 * @lun_id: LUN ID of the scsi device.
10905 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10906 *
10907 * This function returns number of FCP commands pending for the vport.
10908 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10909 * commands pending on the vport associated with SCSI device specified
10910 * by tgt_id and lun_id parameters.
10911 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10912 * commands pending on the vport associated with SCSI target specified
10913 * by tgt_id parameter.
10914 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10915 * commands pending on the vport.
10916 * This function returns the number of iocbs which satisfy the filter.
10917 * This function is called without any lock held.
10918 **/
dea31012005-04-17 16:05:31 -050010919int
James Smart51ef4c22007-08-02 11:10:31 -040010920lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10921 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010922{
James Smart51ef4c22007-08-02 11:10:31 -040010923 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010924 struct lpfc_iocbq *iocbq;
10925 int sum, i;
dea31012005-04-17 16:05:31 -050010926
Johannes Thumshirn31979002016-07-18 16:06:03 +020010927 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010928 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10929 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010930
James Smart51ef4c22007-08-02 11:10:31 -040010931 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10932 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010933 sum++;
dea31012005-04-17 16:05:31 -050010934 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010935 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010936
dea31012005-04-17 16:05:31 -050010937 return sum;
10938}
10939
James Smarte59058c2008-08-24 21:49:00 -040010940/**
James Smart3621a712009-04-06 18:47:14 -040010941 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010942 * @phba: Pointer to HBA context object
10943 * @cmdiocb: Pointer to command iocb object.
10944 * @rspiocb: Pointer to response iocb object.
10945 *
10946 * This function is called when an aborted FCP iocb completes. This
10947 * function is called by the ring event handler with no lock held.
10948 * This function frees the iocb.
10949 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010950void
James Smart2e0fef82007-06-17 19:56:36 -050010951lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10952 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010953{
James Smartcb69f7d2011-12-13 13:21:57 -050010954 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010955 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010956 "original iotag x%x, abort cmd iotag x%x "
10957 "status 0x%x, reason 0x%x\n",
10958 cmdiocb->iocb.un.acxri.abortContextTag,
10959 cmdiocb->iocb.un.acxri.abortIoTag,
10960 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10961 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010962 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010963 return;
10964}
10965
James Smarte59058c2008-08-24 21:49:00 -040010966/**
James Smart3621a712009-04-06 18:47:14 -040010967 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010968 * @vport: Pointer to virtual port.
10969 * @pring: Pointer to driver SLI ring object.
10970 * @tgt_id: SCSI ID of the target.
10971 * @lun_id: LUN ID of the scsi device.
10972 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10973 *
10974 * This function sends an abort command for every SCSI command
10975 * associated with the given virtual port pending on the ring
10976 * filtered by lpfc_sli_validate_fcp_iocb function.
10977 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10978 * FCP iocbs associated with lun specified by tgt_id and lun_id
10979 * parameters
10980 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10981 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10982 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10983 * FCP iocbs associated with virtual port.
10984 * This function returns number of iocbs it failed to abort.
10985 * This function is called with no locks held.
10986 **/
dea31012005-04-17 16:05:31 -050010987int
James Smart51ef4c22007-08-02 11:10:31 -040010988lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10989 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010990{
James Smart51ef4c22007-08-02 11:10:31 -040010991 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010992 struct lpfc_iocbq *iocbq;
10993 struct lpfc_iocbq *abtsiocb;
James Smartecbb2272017-06-01 21:07:04 -070010994 struct lpfc_sli_ring *pring_s4;
dea31012005-04-17 16:05:31 -050010995 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010996 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010997 int i;
dea31012005-04-17 16:05:31 -050010998
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010999 for (i = 1; i <= phba->sli.last_iotag; i++) {
11000 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050011001
James Smart51ef4c22007-08-02 11:10:31 -040011002 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050011003 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050011004 continue;
11005
James Smartafbd8d82013-09-06 12:22:13 -040011006 /*
11007 * If the iocbq is already being aborted, don't take a second
11008 * action, but do count it.
11009 */
11010 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11011 continue;
11012
dea31012005-04-17 16:05:31 -050011013 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011014 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050011015 if (abtsiocb == NULL) {
11016 errcnt++;
11017 continue;
11018 }
dea31012005-04-17 16:05:31 -050011019
James Smartafbd8d82013-09-06 12:22:13 -040011020 /* indicate the IO is being aborted by the driver. */
11021 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11022
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040011023 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050011024 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11025 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040011026 if (phba->sli_rev == LPFC_SLI_REV4)
11027 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
11028 else
11029 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050011030 abtsiocb->iocb.ulpLe = 1;
11031 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040011032 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050011033
James Smart5ffc2662009-11-18 15:39:44 -050011034 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011035 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050011036 if (iocbq->iocb_flag & LPFC_IO_FCP)
11037 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011038 if (iocbq->iocb_flag & LPFC_IO_FOF)
11039 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050011040
James Smart2e0fef82007-06-17 19:56:36 -050011041 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050011042 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11043 else
11044 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11045
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040011046 /* Setup callback routine and issue the command. */
11047 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartecbb2272017-06-01 21:07:04 -070011048 if (phba->sli_rev == LPFC_SLI_REV4) {
11049 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11050 if (!pring_s4)
11051 continue;
11052 ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11053 abtsiocb, 0);
11054 } else
11055 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
11056 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050011057 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050011058 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050011059 errcnt++;
11060 continue;
11061 }
11062 }
11063
11064 return errcnt;
11065}
11066
James Smarte59058c2008-08-24 21:49:00 -040011067/**
James Smart98912dda2014-04-04 13:52:31 -040011068 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
11069 * @vport: Pointer to virtual port.
11070 * @pring: Pointer to driver SLI ring object.
11071 * @tgt_id: SCSI ID of the target.
11072 * @lun_id: LUN ID of the scsi device.
11073 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
11074 *
11075 * This function sends an abort command for every SCSI command
11076 * associated with the given virtual port pending on the ring
11077 * filtered by lpfc_sli_validate_fcp_iocb function.
11078 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
11079 * FCP iocbs associated with lun specified by tgt_id and lun_id
11080 * parameters
11081 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
11082 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
11083 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
11084 * FCP iocbs associated with virtual port.
11085 * This function returns number of iocbs it aborted .
11086 * This function is called with no locks held right after a taskmgmt
11087 * command is sent.
11088 **/
11089int
11090lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
11091 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
11092{
11093 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040011094 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040011095 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040011096 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040011097 struct lpfc_iocbq *iocbq;
11098 IOCB_t *icmd;
11099 int sum, i, ret_val;
11100 unsigned long iflags;
11101 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040011102
11103 spin_lock_irq(&phba->hbalock);
11104
11105 /* all I/Os are in process of being flushed */
11106 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
11107 spin_unlock_irq(&phba->hbalock);
11108 return 0;
11109 }
11110 sum = 0;
11111
11112 for (i = 1; i <= phba->sli.last_iotag; i++) {
11113 iocbq = phba->sli.iocbq_lookup[i];
11114
11115 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
11116 cmd) != 0)
11117 continue;
11118
11119 /*
11120 * If the iocbq is already being aborted, don't take a second
11121 * action, but do count it.
11122 */
11123 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
11124 continue;
11125
11126 /* issue ABTS for this IOCB based on iotag */
11127 abtsiocbq = __lpfc_sli_get_iocbq(phba);
11128 if (abtsiocbq == NULL)
11129 continue;
11130
11131 icmd = &iocbq->iocb;
11132 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
11133 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
11134 if (phba->sli_rev == LPFC_SLI_REV4)
11135 abtsiocbq->iocb.un.acxri.abortIoTag =
11136 iocbq->sli4_xritag;
11137 else
11138 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
11139 abtsiocbq->iocb.ulpLe = 1;
11140 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
11141 abtsiocbq->vport = vport;
11142
11143 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080011144 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040011145 if (iocbq->iocb_flag & LPFC_IO_FCP)
11146 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040011147 if (iocbq->iocb_flag & LPFC_IO_FOF)
11148 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040011149
James Smart8c50d252014-09-03 12:58:16 -040011150 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
11151 ndlp = lpfc_cmd->rdata->pnode;
11152
11153 if (lpfc_is_link_up(phba) &&
11154 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040011155 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
11156 else
11157 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
11158
11159 /* Setup callback routine and issue the command. */
11160 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
11161
11162 /*
11163 * Indicate the IO is being aborted by the driver and set
11164 * the caller's flag into the aborted IO.
11165 */
11166 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
11167
11168 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080011169 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
11170 if (pring_s4 == NULL)
11171 continue;
James Smart98912dda2014-04-04 13:52:31 -040011172 /* Note: both hbalock and ring_lock must be set here */
11173 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
11174 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
11175 abtsiocbq, 0);
11176 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
11177 } else {
11178 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
11179 abtsiocbq, 0);
11180 }
11181
11182
11183 if (ret_val == IOCB_ERROR)
11184 __lpfc_sli_release_iocbq(phba, abtsiocbq);
11185 else
11186 sum++;
11187 }
11188 spin_unlock_irq(&phba->hbalock);
11189 return sum;
11190}
11191
11192/**
James Smart3621a712009-04-06 18:47:14 -040011193 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040011194 * @phba: Pointer to HBA context object.
11195 * @cmdiocbq: Pointer to command iocb.
11196 * @rspiocbq: Pointer to response iocb.
11197 *
11198 * This function is the completion handler for iocbs issued using
11199 * lpfc_sli_issue_iocb_wait function. This function is called by the
11200 * ring event handler function without any lock held. This function
11201 * can be called from both worker thread context and interrupt
11202 * context. This function also can be called from other thread which
11203 * cleans up the SLI layer objects.
11204 * This function copy the contents of the response iocb to the
11205 * response iocb memory object provided by the caller of
11206 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11207 * sleeps for the iocb completion.
11208 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011209static void
11210lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
11211 struct lpfc_iocbq *cmdiocbq,
11212 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050011213{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011214 wait_queue_head_t *pdone_q;
11215 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050011216 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050011217
James Smart2e0fef82007-06-17 19:56:36 -050011218 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040011219 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
11220
11221 /*
11222 * A time out has occurred for the iocb. If a time out
11223 * completion handler has been supplied, call it. Otherwise,
11224 * just free the iocbq.
11225 */
11226
11227 spin_unlock_irqrestore(&phba->hbalock, iflags);
11228 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
11229 cmdiocbq->wait_iocb_cmpl = NULL;
11230 if (cmdiocbq->iocb_cmpl)
11231 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
11232 else
11233 lpfc_sli_release_iocbq(phba, cmdiocbq);
11234 return;
11235 }
11236
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011237 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
11238 if (cmdiocbq->context2 && rspiocbq)
11239 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
11240 &rspiocbq->iocb, sizeof(IOCB_t));
11241
James Smart0f65ff62010-02-26 14:14:23 -050011242 /* Set the exchange busy flag for task management commands */
11243 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
11244 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
11245 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
11246 cur_iocbq);
11247 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
11248 }
11249
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011250 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011251 if (pdone_q)
11252 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050011253 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050011254 return;
11255}
11256
James Smarte59058c2008-08-24 21:49:00 -040011257/**
James Smartd11e31d2009-06-10 17:23:06 -040011258 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
11259 * @phba: Pointer to HBA context object..
11260 * @piocbq: Pointer to command iocb.
11261 * @flag: Flag to test.
11262 *
11263 * This routine grabs the hbalock and then test the iocb_flag to
11264 * see if the passed in flag is set.
11265 * Returns:
11266 * 1 if flag is set.
11267 * 0 if flag is not set.
11268 **/
11269static int
11270lpfc_chk_iocb_flg(struct lpfc_hba *phba,
11271 struct lpfc_iocbq *piocbq, uint32_t flag)
11272{
11273 unsigned long iflags;
11274 int ret;
11275
11276 spin_lock_irqsave(&phba->hbalock, iflags);
11277 ret = piocbq->iocb_flag & flag;
11278 spin_unlock_irqrestore(&phba->hbalock, iflags);
11279 return ret;
11280
11281}
11282
11283/**
James Smart3621a712009-04-06 18:47:14 -040011284 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011285 * @phba: Pointer to HBA context object..
11286 * @pring: Pointer to sli ring.
11287 * @piocb: Pointer to command iocb.
11288 * @prspiocbq: Pointer to response iocb.
11289 * @timeout: Timeout in number of seconds.
11290 *
11291 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011292 * iocb to complete. The iocb_cmpl field of the shall be used
11293 * to handle iocbs which time out. If the field is NULL, the
11294 * function shall free the iocbq structure. If more clean up is
11295 * needed, the caller is expected to provide a completion function
11296 * that will provide the needed clean up. If the iocb command is
11297 * not completed within timeout seconds, the function will either
11298 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11299 * completion function set in the iocb_cmpl field and then return
11300 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11301 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011302 * The function waits for the iocb completion using an
11303 * non-interruptible wait.
11304 * This function will sleep while waiting for iocb completion.
11305 * So, this function should not be called from any context which
11306 * does not allow sleeping. Due to the same reason, this function
11307 * cannot be called with interrupt disabled.
11308 * This function assumes that the iocb completions occur while
11309 * this function sleep. So, this function cannot be called from
11310 * the thread which process iocb completion for this ring.
11311 * This function clears the iocb_flag of the iocb object before
11312 * issuing the iocb and the iocb completion handler sets this
11313 * flag and wakes this thread when the iocb completes.
11314 * The contents of the response iocb will be copied to prspiocbq
11315 * by the completion handler when the command completes.
11316 * This function returns IOCB_SUCCESS when success.
11317 * This function is called with no lock held.
11318 **/
dea31012005-04-17 16:05:31 -050011319int
James Smart2e0fef82007-06-17 19:56:36 -050011320lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011321 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011322 struct lpfc_iocbq *piocb,
11323 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011324 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011325{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011326 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011327 long timeleft, timeout_req = 0;
11328 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011329 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011330 struct lpfc_iocbq *iocb;
11331 int txq_cnt = 0;
11332 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011333 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011334 unsigned long iflags;
11335 bool iocb_completed = true;
11336
James Smart895427b2017-02-12 13:52:30 -080011337 if (phba->sli_rev >= LPFC_SLI_REV4)
11338 pring = lpfc_sli4_calc_ring(phba, piocb);
11339 else
11340 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011341 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011342 * If the caller has provided a response iocbq buffer, then context2
11343 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011344 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011345 if (prspiocbq) {
11346 if (piocb->context2)
11347 return IOCB_ERROR;
11348 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011349 }
11350
James Smart5a0916b2013-07-15 18:31:42 -040011351 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011352 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11353 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011354 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011355
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011356 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011357 if (lpfc_readl(phba->HCregaddr, &creg_val))
11358 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011359 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11360 writel(creg_val, phba->HCregaddr);
11361 readl(phba->HCregaddr); /* flush */
11362 }
11363
James Smart2a9bf3d2010-06-07 15:24:45 -040011364 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11365 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011366 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011367 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011368 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011369 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011370 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011371 spin_lock_irqsave(&phba->hbalock, iflags);
11372 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011373
James Smart5a0916b2013-07-15 18:31:42 -040011374 /*
11375 * IOCB timed out. Inform the wake iocb wait
11376 * completion function and set local status
11377 */
11378
11379 iocb_completed = false;
11380 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11381 }
11382 spin_unlock_irqrestore(&phba->hbalock, iflags);
11383 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011384 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011385 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011386 /* Note: we are not indicating if the IOCB has a success
11387 * status or not - that's for the caller to check.
11388 * IOCB_SUCCESS means just that the command was sent and
11389 * completed. Not that it completed successfully.
11390 * */
James Smart7054a602007-04-25 09:52:34 -040011391 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011392 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011393 "0338 IOCB wait timeout error - no "
11394 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011395 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011396 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011397 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011398 "0330 IOCB wake NOT set, "
11399 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011400 timeout, (timeleft / jiffies));
11401 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011402 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011403 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011404 if (phba->cfg_log_verbose & LOG_SLI) {
11405 list_for_each_entry(iocb, &pring->txq, list) {
11406 txq_cnt++;
11407 }
11408 list_for_each_entry(iocb, &pring->txcmplq, list) {
11409 txcmplq_cnt++;
11410 }
11411 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11412 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11413 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11414 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011415 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011416 } else {
11417 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011418 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011419 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011420 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011421 }
11422
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011423 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011424 if (lpfc_readl(phba->HCregaddr, &creg_val))
11425 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011426 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11427 writel(creg_val, phba->HCregaddr);
11428 readl(phba->HCregaddr); /* flush */
11429 }
11430
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011431 if (prspiocbq)
11432 piocb->context2 = NULL;
11433
11434 piocb->context_un.wait_queue = NULL;
11435 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011436 return retval;
11437}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011438
James Smarte59058c2008-08-24 21:49:00 -040011439/**
James Smart3621a712009-04-06 18:47:14 -040011440 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011441 * @phba: Pointer to HBA context object.
11442 * @pmboxq: Pointer to driver mailbox object.
11443 * @timeout: Timeout in number of seconds.
11444 *
11445 * This function issues the mailbox to firmware and waits for the
11446 * mailbox command to complete. If the mailbox command is not
11447 * completed within timeout seconds, it returns MBX_TIMEOUT.
11448 * The function waits for the mailbox completion using an
11449 * interruptible wait. If the thread is woken up due to a
11450 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11451 * should not free the mailbox resources, if this function returns
11452 * MBX_TIMEOUT.
11453 * This function will sleep while waiting for mailbox completion.
11454 * So, this function should not be called from any context which
11455 * does not allow sleeping. Due to the same reason, this function
11456 * cannot be called with interrupt disabled.
11457 * This function assumes that the mailbox completion occurs while
11458 * this function sleep. So, this function cannot be called from
11459 * the worker thread which processes mailbox completion.
11460 * This function is called in the context of HBA management
11461 * applications.
11462 * This function returns MBX_SUCCESS when successful.
11463 * This function is called with no lock held.
11464 **/
dea31012005-04-17 16:05:31 -050011465int
James Smart2e0fef82007-06-17 19:56:36 -050011466lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011467 uint32_t timeout)
11468{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011469 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a22013-05-31 17:05:27 -040011470 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050011471 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011472 unsigned long flag;
dea31012005-04-17 16:05:31 -050011473
James Smartb230b8a22013-05-31 17:05:27 -040011474 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040011475 if (pmboxq->context1)
James Smartb230b8a22013-05-31 17:05:27 -040011476 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050011477
James Smart495a7142008-06-14 22:52:59 -040011478 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011479 /* setup wake call as IOCB callback */
11480 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11481 /* setup context field to pass wait_queue pointer to wake function */
11482 pmboxq->context1 = &done_q;
11483
dea31012005-04-17 16:05:31 -050011484 /* now issue the command */
11485 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011486 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040011487 wait_event_interruptible_timeout(done_q,
11488 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040011489 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011490
James Smart858c9f62007-06-17 19:56:39 -050011491 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a22013-05-31 17:05:27 -040011492 /* restore the possible extended buffer for free resource */
11493 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040011494 /*
11495 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11496 * else do not free the resources.
11497 */
James Smartd7c47992010-06-08 18:31:54 -040011498 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011499 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011500 } else {
James Smart7054a602007-04-25 09:52:34 -040011501 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011502 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11503 }
11504 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a22013-05-31 17:05:27 -040011505 } else {
11506 /* restore the possible extended buffer for free resource */
11507 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050011508 }
11509
dea31012005-04-17 16:05:31 -050011510 return retval;
11511}
11512
James Smarte59058c2008-08-24 21:49:00 -040011513/**
James Smart3772a992009-05-22 14:50:54 -040011514 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011515 * @phba: Pointer to HBA context.
11516 *
James Smart3772a992009-05-22 14:50:54 -040011517 * This function is called to shutdown the driver's mailbox sub-system.
11518 * It first marks the mailbox sub-system is in a block state to prevent
11519 * the asynchronous mailbox command from issued off the pending mailbox
11520 * command queue. If the mailbox command sub-system shutdown is due to
11521 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11522 * the mailbox sub-system flush routine to forcefully bring down the
11523 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11524 * as with offline or HBA function reset), this routine will wait for the
11525 * outstanding mailbox command to complete before invoking the mailbox
11526 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011527 **/
James Smart3772a992009-05-22 14:50:54 -040011528void
James Smart618a5232012-06-12 13:54:36 -040011529lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011530{
James Smart3772a992009-05-22 14:50:54 -040011531 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011532 unsigned long timeout;
11533
James Smart618a5232012-06-12 13:54:36 -040011534 if (mbx_action == LPFC_MBX_NO_WAIT) {
11535 /* delay 100ms for port state */
11536 msleep(100);
11537 lpfc_sli_mbox_sys_flush(phba);
11538 return;
11539 }
James Smarta183a152011-10-10 21:32:43 -040011540 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011541
James Smart3772a992009-05-22 14:50:54 -040011542 spin_lock_irq(&phba->hbalock);
11543 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011544
11545 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011546 /* Determine how long we might wait for the active mailbox
11547 * command to be gracefully completed by firmware.
11548 */
James Smarta183a152011-10-10 21:32:43 -040011549 if (phba->sli.mbox_active)
11550 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11551 phba->sli.mbox_active) *
11552 1000) + jiffies;
11553 spin_unlock_irq(&phba->hbalock);
11554
James Smart3772a992009-05-22 14:50:54 -040011555 while (phba->sli.mbox_active) {
11556 /* Check active mailbox complete status every 2ms */
11557 msleep(2);
11558 if (time_after(jiffies, timeout))
11559 /* Timeout, let the mailbox flush routine to
11560 * forcefully release active mailbox command
11561 */
11562 break;
11563 }
James Smartd7069f02012-03-01 22:36:29 -050011564 } else
11565 spin_unlock_irq(&phba->hbalock);
11566
James Smart3772a992009-05-22 14:50:54 -040011567 lpfc_sli_mbox_sys_flush(phba);
11568}
11569
11570/**
11571 * lpfc_sli_eratt_read - read sli-3 error attention events
11572 * @phba: Pointer to HBA context.
11573 *
11574 * This function is called to read the SLI3 device error attention registers
11575 * for possible error attention events. The caller must hold the hostlock
11576 * with spin_lock_irq().
11577 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011578 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011579 * Register and returns 0 otherwise.
11580 **/
11581static int
11582lpfc_sli_eratt_read(struct lpfc_hba *phba)
11583{
James Smarted957682007-06-17 19:56:37 -050011584 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011585
James Smart3772a992009-05-22 14:50:54 -040011586 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011587 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11588 goto unplug_err;
11589
James Smart3772a992009-05-22 14:50:54 -040011590 if (ha_copy & HA_ERATT) {
11591 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011592 if (lpfc_sli_read_hs(phba))
11593 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011594
James Smart3772a992009-05-22 14:50:54 -040011595 /* Check if there is a deferred error condition is active */
11596 if ((HS_FFER1 & phba->work_hs) &&
11597 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011598 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011599 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011600 /* Clear all interrupt enable conditions */
11601 writel(0, phba->HCregaddr);
11602 readl(phba->HCregaddr);
11603 }
11604
11605 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011606 phba->work_ha |= HA_ERATT;
11607 /* Indicate polling handles this ERATT */
11608 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011609 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011610 }
James Smart3772a992009-05-22 14:50:54 -040011611 return 0;
James Smart9940b972011-03-11 16:06:12 -050011612
11613unplug_err:
11614 /* Set the driver HS work bitmap */
11615 phba->work_hs |= UNPLUG_ERR;
11616 /* Set the driver HA work bitmap */
11617 phba->work_ha |= HA_ERATT;
11618 /* Indicate polling handles this ERATT */
11619 phba->hba_flag |= HBA_ERATT_HANDLED;
11620 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011621}
11622
James Smarte59058c2008-08-24 21:49:00 -040011623/**
James Smartda0436e2009-05-22 14:51:39 -040011624 * lpfc_sli4_eratt_read - read sli-4 error attention events
11625 * @phba: Pointer to HBA context.
11626 *
11627 * This function is called to read the SLI4 device error attention registers
11628 * for possible error attention events. The caller must hold the hostlock
11629 * with spin_lock_irq().
11630 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011631 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011632 * Register and returns 0 otherwise.
11633 **/
11634static int
11635lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11636{
11637 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011638 uint32_t if_type, portsmphr;
11639 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011640
James Smart2fcee4b2010-12-15 17:57:46 -050011641 /*
11642 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011643 * registers for error attention. This can be changed later.
11644 */
James Smart2fcee4b2010-12-15 17:57:46 -050011645 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11646 switch (if_type) {
11647 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011648 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11649 &uerr_sta_lo) ||
11650 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11651 &uerr_sta_hi)) {
11652 phba->work_hs |= UNPLUG_ERR;
11653 phba->work_ha |= HA_ERATT;
11654 phba->hba_flag |= HBA_ERATT_HANDLED;
11655 return 1;
11656 }
James Smart2fcee4b2010-12-15 17:57:46 -050011657 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11658 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11659 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11660 "1423 HBA Unrecoverable error: "
11661 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11662 "ue_mask_lo_reg=0x%x, "
11663 "ue_mask_hi_reg=0x%x\n",
11664 uerr_sta_lo, uerr_sta_hi,
11665 phba->sli4_hba.ue_mask_lo,
11666 phba->sli4_hba.ue_mask_hi);
11667 phba->work_status[0] = uerr_sta_lo;
11668 phba->work_status[1] = uerr_sta_hi;
11669 phba->work_ha |= HA_ERATT;
11670 phba->hba_flag |= HBA_ERATT_HANDLED;
11671 return 1;
11672 }
11673 break;
11674 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050011675 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11676 &portstat_reg.word0) ||
11677 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11678 &portsmphr)){
11679 phba->work_hs |= UNPLUG_ERR;
11680 phba->work_ha |= HA_ERATT;
11681 phba->hba_flag |= HBA_ERATT_HANDLED;
11682 return 1;
11683 }
James Smart2fcee4b2010-12-15 17:57:46 -050011684 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11685 phba->work_status[0] =
11686 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11687 phba->work_status[1] =
11688 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11689 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011690 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011691 "port status reg 0x%x, "
11692 "port smphr reg 0x%x, "
11693 "error 1=0x%x, error 2=0x%x\n",
11694 portstat_reg.word0,
11695 portsmphr,
11696 phba->work_status[0],
11697 phba->work_status[1]);
11698 phba->work_ha |= HA_ERATT;
11699 phba->hba_flag |= HBA_ERATT_HANDLED;
11700 return 1;
11701 }
11702 break;
11703 case LPFC_SLI_INTF_IF_TYPE_1:
11704 default:
James Smarta747c9c2009-11-18 15:41:10 -050011705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011706 "2886 HBA Error Attention on unsupported "
11707 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011708 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011709 }
James Smart2fcee4b2010-12-15 17:57:46 -050011710
James Smartda0436e2009-05-22 14:51:39 -040011711 return 0;
11712}
11713
11714/**
James Smart3621a712009-04-06 18:47:14 -040011715 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011716 * @phba: Pointer to HBA context.
11717 *
James Smart3772a992009-05-22 14:50:54 -040011718 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011719 * error attention register bit for error attention events.
11720 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011721 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011722 * Register and returns 0 otherwise.
11723 **/
11724int
11725lpfc_sli_check_eratt(struct lpfc_hba *phba)
11726{
11727 uint32_t ha_copy;
11728
11729 /* If somebody is waiting to handle an eratt, don't process it
11730 * here. The brdkill function will do this.
11731 */
11732 if (phba->link_flag & LS_IGNORE_ERATT)
11733 return 0;
11734
11735 /* Check if interrupt handler handles this ERATT */
11736 spin_lock_irq(&phba->hbalock);
11737 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11738 /* Interrupt handler has handled ERATT */
11739 spin_unlock_irq(&phba->hbalock);
11740 return 0;
11741 }
11742
James Smarta257bf92009-04-06 18:48:10 -040011743 /*
11744 * If there is deferred error attention, do not check for error
11745 * attention
11746 */
11747 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11748 spin_unlock_irq(&phba->hbalock);
11749 return 0;
11750 }
11751
James Smart3772a992009-05-22 14:50:54 -040011752 /* If PCI channel is offline, don't process it */
11753 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011754 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011755 return 0;
11756 }
11757
11758 switch (phba->sli_rev) {
11759 case LPFC_SLI_REV2:
11760 case LPFC_SLI_REV3:
11761 /* Read chip Host Attention (HA) register */
11762 ha_copy = lpfc_sli_eratt_read(phba);
11763 break;
James Smartda0436e2009-05-22 14:51:39 -040011764 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011765 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011766 ha_copy = lpfc_sli4_eratt_read(phba);
11767 break;
James Smart3772a992009-05-22 14:50:54 -040011768 default:
11769 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11770 "0299 Invalid SLI revision (%d)\n",
11771 phba->sli_rev);
11772 ha_copy = 0;
11773 break;
James Smart93996272008-08-24 21:50:30 -040011774 }
11775 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011776
11777 return ha_copy;
11778}
11779
11780/**
11781 * lpfc_intr_state_check - Check device state for interrupt handling
11782 * @phba: Pointer to HBA context.
11783 *
11784 * This inline routine checks whether a device or its PCI slot is in a state
11785 * that the interrupt should be handled.
11786 *
11787 * This function returns 0 if the device or the PCI slot is in a state that
11788 * interrupt should be handled, otherwise -EIO.
11789 */
11790static inline int
11791lpfc_intr_state_check(struct lpfc_hba *phba)
11792{
11793 /* If the pci channel is offline, ignore all the interrupts */
11794 if (unlikely(pci_channel_offline(phba->pcidev)))
11795 return -EIO;
11796
11797 /* Update device level interrupt statistics */
11798 phba->sli.slistat.sli_intr++;
11799
11800 /* Ignore all interrupts during initialization. */
11801 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11802 return -EIO;
11803
James Smart93996272008-08-24 21:50:30 -040011804 return 0;
11805}
11806
11807/**
James Smart3772a992009-05-22 14:50:54 -040011808 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011809 * @irq: Interrupt number.
11810 * @dev_id: The device context pointer.
11811 *
James Smart93996272008-08-24 21:50:30 -040011812 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011813 * service routine when device with SLI-3 interface spec is enabled with
11814 * MSI-X multi-message interrupt mode and there are slow-path events in
11815 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11816 * interrupt mode, this function is called as part of the device-level
11817 * interrupt handler. When the PCI slot is in error recovery or the HBA
11818 * is undergoing initialization, the interrupt handler will not process
11819 * the interrupt. The link attention and ELS ring attention events are
11820 * handled by the worker thread. The interrupt handler signals the worker
11821 * thread and returns for these events. This function is called without
11822 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011823 * structures.
11824 *
11825 * This function returns IRQ_HANDLED when interrupt is handled else it
11826 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011827 **/
dea31012005-04-17 16:05:31 -050011828irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011829lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011830{
James Smart2e0fef82007-06-17 19:56:36 -050011831 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011832 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011833 uint32_t work_ha_copy;
11834 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011835 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011836 uint32_t control;
11837
James Smart92d7f7b2007-06-17 19:56:38 -050011838 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011839 struct lpfc_vport *vport;
11840 struct lpfc_nodelist *ndlp;
11841 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011842 LPFC_MBOXQ_t *pmb;
11843 int rc;
11844
dea31012005-04-17 16:05:31 -050011845 /*
11846 * Get the driver's phba structure from the dev_id and
11847 * assume the HBA is not interrupting.
11848 */
James Smart93996272008-08-24 21:50:30 -040011849 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011850
11851 if (unlikely(!phba))
11852 return IRQ_NONE;
11853
dea31012005-04-17 16:05:31 -050011854 /*
James Smart93996272008-08-24 21:50:30 -040011855 * Stuff needs to be attented to when this function is invoked as an
11856 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011857 */
James Smart93996272008-08-24 21:50:30 -040011858 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011859 /* Check device state for handling interrupt */
11860 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011861 return IRQ_NONE;
11862 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011863 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011864 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11865 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011866 /* If somebody is waiting to handle an eratt don't process it
11867 * here. The brdkill function will do this.
11868 */
11869 if (phba->link_flag & LS_IGNORE_ERATT)
11870 ha_copy &= ~HA_ERATT;
11871 /* Check the need for handling ERATT in interrupt handler */
11872 if (ha_copy & HA_ERATT) {
11873 if (phba->hba_flag & HBA_ERATT_HANDLED)
11874 /* ERATT polling has handled ERATT */
11875 ha_copy &= ~HA_ERATT;
11876 else
11877 /* Indicate interrupt handler handles ERATT */
11878 phba->hba_flag |= HBA_ERATT_HANDLED;
11879 }
James Smarta257bf92009-04-06 18:48:10 -040011880
11881 /*
11882 * If there is deferred error attention, do not check for any
11883 * interrupt.
11884 */
11885 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011886 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011887 return IRQ_NONE;
11888 }
11889
James Smart93996272008-08-24 21:50:30 -040011890 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011891 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11892 goto unplug_error;
11893
James Smarta747c9c2009-11-18 15:41:10 -050011894 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11895 HC_LAINT_ENA | HC_ERINT_ENA),
11896 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011897 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11898 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011899 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011900 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011901 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011902 } else
11903 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011904
dea31012005-04-17 16:05:31 -050011905 work_ha_copy = ha_copy & phba->work_ha_mask;
11906
James Smart93996272008-08-24 21:50:30 -040011907 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011908 if (work_ha_copy & HA_LATT) {
11909 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11910 /*
11911 * Turn off Link Attention interrupts
11912 * until CLEAR_LA done
11913 */
James Smart5b75da22008-12-04 22:39:35 -050011914 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011915 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011916 if (lpfc_readl(phba->HCregaddr, &control))
11917 goto unplug_error;
dea31012005-04-17 16:05:31 -050011918 control &= ~HC_LAINT_ENA;
11919 writel(control, phba->HCregaddr);
11920 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011921 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011922 }
11923 else
11924 work_ha_copy &= ~HA_LATT;
11925 }
11926
James Smart93996272008-08-24 21:50:30 -040011927 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011928 /*
11929 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11930 * the only slow ring.
11931 */
11932 status = (work_ha_copy &
11933 (HA_RXMASK << (4*LPFC_ELS_RING)));
11934 status >>= (4*LPFC_ELS_RING);
11935 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011936 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011937 if (lpfc_readl(phba->HCregaddr, &control))
11938 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011939
11940 lpfc_debugfs_slow_ring_trc(phba,
11941 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11942 control, status,
11943 (uint32_t)phba->sli.slistat.sli_intr);
11944
James Smart858c9f62007-06-17 19:56:39 -050011945 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011946 lpfc_debugfs_slow_ring_trc(phba,
11947 "ISR Disable ring:"
11948 "pwork:x%x hawork:x%x wait:x%x",
11949 phba->work_ha, work_ha_copy,
11950 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011951 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011952
James Smart858c9f62007-06-17 19:56:39 -050011953 control &=
11954 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011955 writel(control, phba->HCregaddr);
11956 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011957 }
James Smarta58cbd52007-08-02 11:09:43 -040011958 else {
11959 lpfc_debugfs_slow_ring_trc(phba,
11960 "ISR slow ring: pwork:"
11961 "x%x hawork:x%x wait:x%x",
11962 phba->work_ha, work_ha_copy,
11963 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011964 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011965 }
James Smart5b75da22008-12-04 22:39:35 -050011966 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011967 }
11968 }
James Smart5b75da22008-12-04 22:39:35 -050011969 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011970 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011971 if (lpfc_sli_read_hs(phba))
11972 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011973 /*
11974 * Check if there is a deferred error condition
11975 * is active
11976 */
11977 if ((HS_FFER1 & phba->work_hs) &&
11978 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011979 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11980 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011981 phba->hba_flag |= DEFER_ERATT;
11982 /* Clear all interrupt enable conditions */
11983 writel(0, phba->HCregaddr);
11984 readl(phba->HCregaddr);
11985 }
11986 }
11987
James Smart93996272008-08-24 21:50:30 -040011988 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011989 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011990 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011991 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011992 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011993
11994 /* First check out the status word */
11995 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11996 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011997 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011998 /*
11999 * Stray Mailbox Interrupt, mbxCommand <cmd>
12000 * mbxStatus <status>
12001 */
James Smart09372822008-01-11 01:52:54 -050012002 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050012003 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040012004 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050012005 "Interrupt mbxCommand x%x "
12006 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040012007 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050012008 pmbox->mbxCommand,
12009 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050012010 /* clear mailbox attention bit */
12011 work_ha_copy &= ~HA_MBATT;
12012 } else {
James Smart97eab632008-04-07 10:16:05 -040012013 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050012014 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050012015 phba->last_completion_time = jiffies;
12016 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050012017 if (pmb->mbox_cmpl) {
12018 lpfc_sli_pcimem_bcopy(mbox, pmbox,
12019 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040012020 if (pmb->out_ext_byte_len &&
12021 pmb->context2)
12022 lpfc_sli_pcimem_bcopy(
12023 phba->mbox_ext,
12024 pmb->context2,
12025 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050012026 }
James Smart09372822008-01-11 01:52:54 -050012027 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12028 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12029
12030 lpfc_debugfs_disc_trc(vport,
12031 LPFC_DISC_TRC_MBOX_VPORT,
12032 "MBOX dflt rpi: : "
12033 "status:x%x rpi:x%x",
12034 (uint32_t)pmbox->mbxStatus,
12035 pmbox->un.varWords[0], 0);
12036
12037 if (!pmbox->mbxStatus) {
12038 mp = (struct lpfc_dmabuf *)
12039 (pmb->context1);
12040 ndlp = (struct lpfc_nodelist *)
12041 pmb->context2;
12042
12043 /* Reg_LOGIN of dflt RPI was
12044 * successful. new lets get
12045 * rid of the RPI using the
12046 * same mbox buffer.
12047 */
12048 lpfc_unreg_login(phba,
12049 vport->vpi,
12050 pmbox->un.varWords[0],
12051 pmb);
12052 pmb->mbox_cmpl =
12053 lpfc_mbx_cmpl_dflt_rpi;
12054 pmb->context1 = mp;
12055 pmb->context2 = ndlp;
12056 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040012057 rc = lpfc_sli_issue_mbox(phba,
12058 pmb,
12059 MBX_NOWAIT);
12060 if (rc != MBX_BUSY)
12061 lpfc_printf_log(phba,
12062 KERN_ERR,
12063 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040012064 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040012065 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040012066 if (rc != MBX_NOT_FINISHED)
12067 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050012068 }
12069 }
James Smart5b75da22008-12-04 22:39:35 -050012070 spin_lock_irqsave(
12071 &phba->pport->work_port_lock,
12072 iflag);
James Smart09372822008-01-11 01:52:54 -050012073 phba->pport->work_port_events &=
12074 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050012075 spin_unlock_irqrestore(
12076 &phba->pport->work_port_lock,
12077 iflag);
James Smart09372822008-01-11 01:52:54 -050012078 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050012079 }
James Smart97eab632008-04-07 10:16:05 -040012080 } else
James Smart5b75da22008-12-04 22:39:35 -050012081 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012082
James Smart92d7f7b2007-06-17 19:56:38 -050012083 if ((work_ha_copy & HA_MBATT) &&
12084 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050012085send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050012086 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040012087 do {
12088 rc = lpfc_sli_issue_mbox(phba, NULL,
12089 MBX_NOWAIT);
12090 } while (rc == MBX_NOT_FINISHED);
12091 if (rc != MBX_SUCCESS)
12092 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12093 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040012094 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050012095 }
12096
James Smart5b75da22008-12-04 22:39:35 -050012097 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050012098 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050012099 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040012100 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050012101 }
James Smart93996272008-08-24 21:50:30 -040012102 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050012103unplug_error:
12104 spin_unlock_irqrestore(&phba->hbalock, iflag);
12105 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050012106
James Smart3772a992009-05-22 14:50:54 -040012107} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040012108
12109/**
James Smart3772a992009-05-22 14:50:54 -040012110 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040012111 * @irq: Interrupt number.
12112 * @dev_id: The device context pointer.
12113 *
12114 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040012115 * service routine when device with SLI-3 interface spec is enabled with
12116 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12117 * ring event in the HBA. However, when the device is enabled with either
12118 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12119 * device-level interrupt handler. When the PCI slot is in error recovery
12120 * or the HBA is undergoing initialization, the interrupt handler will not
12121 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12122 * the intrrupt context. This function is called without any lock held.
12123 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012124 *
12125 * This function returns IRQ_HANDLED when interrupt is handled else it
12126 * returns IRQ_NONE.
12127 **/
12128irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012129lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012130{
12131 struct lpfc_hba *phba;
12132 uint32_t ha_copy;
12133 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050012134 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080012135 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040012136
12137 /* Get the driver's phba structure from the dev_id and
12138 * assume the HBA is not interrupting.
12139 */
12140 phba = (struct lpfc_hba *) dev_id;
12141
12142 if (unlikely(!phba))
12143 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050012144
12145 /*
James Smart93996272008-08-24 21:50:30 -040012146 * Stuff needs to be attented to when this function is invoked as an
12147 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050012148 */
James Smart93996272008-08-24 21:50:30 -040012149 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040012150 /* Check device state for handling interrupt */
12151 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012152 return IRQ_NONE;
12153 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050012154 if (lpfc_readl(phba->HAregaddr, &ha_copy))
12155 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040012156 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050012157 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012158 /*
12159 * If there is deferred error attention, do not check for
12160 * any interrupt.
12161 */
12162 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040012163 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040012164 return IRQ_NONE;
12165 }
James Smart93996272008-08-24 21:50:30 -040012166 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
12167 phba->HAregaddr);
12168 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050012169 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040012170 } else
12171 ha_copy = phba->ha_copy;
12172
12173 /*
12174 * Process all events on FCP ring. Take the optimized path for FCP IO.
12175 */
12176 ha_copy &= ~(phba->work_ha_mask);
12177
12178 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050012179 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080012180 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050012181 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080012182 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050012183
12184 if (phba->cfg_multi_ring_support == 2) {
12185 /*
James Smart93996272008-08-24 21:50:30 -040012186 * Process all events on extra ring. Take the optimized path
12187 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050012188 */
James Smart93996272008-08-24 21:50:30 -040012189 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050012190 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050012191 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050012192 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080012193 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050012194 status);
12195 }
12196 }
dea31012005-04-17 16:05:31 -050012197 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040012198} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050012199
James Smart93996272008-08-24 21:50:30 -040012200/**
James Smart3772a992009-05-22 14:50:54 -040012201 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040012202 * @irq: Interrupt number.
12203 * @dev_id: The device context pointer.
12204 *
James Smart3772a992009-05-22 14:50:54 -040012205 * This function is the HBA device-level interrupt handler to device with
12206 * SLI-3 interface spec, called from the PCI layer when either MSI or
12207 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
12208 * requires driver attention. This function invokes the slow-path interrupt
12209 * attention handling function and fast-path interrupt attention handling
12210 * function in turn to process the relevant HBA attention events. This
12211 * function is called without any lock held. It gets the hbalock to access
12212 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040012213 *
12214 * This function returns IRQ_HANDLED when interrupt is handled, else it
12215 * returns IRQ_NONE.
12216 **/
12217irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040012218lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040012219{
12220 struct lpfc_hba *phba;
12221 irqreturn_t sp_irq_rc, fp_irq_rc;
12222 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050012223 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040012224
12225 /*
12226 * Get the driver's phba structure from the dev_id and
12227 * assume the HBA is not interrupting.
12228 */
12229 phba = (struct lpfc_hba *) dev_id;
12230
12231 if (unlikely(!phba))
12232 return IRQ_NONE;
12233
James Smart3772a992009-05-22 14:50:54 -040012234 /* Check device state for handling interrupt */
12235 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040012236 return IRQ_NONE;
12237
12238 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050012239 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
12240 spin_unlock(&phba->hbalock);
12241 return IRQ_HANDLED;
12242 }
12243
James Smart93996272008-08-24 21:50:30 -040012244 if (unlikely(!phba->ha_copy)) {
12245 spin_unlock(&phba->hbalock);
12246 return IRQ_NONE;
12247 } else if (phba->ha_copy & HA_ERATT) {
12248 if (phba->hba_flag & HBA_ERATT_HANDLED)
12249 /* ERATT polling has handled ERATT */
12250 phba->ha_copy &= ~HA_ERATT;
12251 else
12252 /* Indicate interrupt handler handles ERATT */
12253 phba->hba_flag |= HBA_ERATT_HANDLED;
12254 }
12255
James Smarta257bf92009-04-06 18:48:10 -040012256 /*
12257 * If there is deferred error attention, do not check for any interrupt.
12258 */
12259 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020012260 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040012261 return IRQ_NONE;
12262 }
12263
James Smart93996272008-08-24 21:50:30 -040012264 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050012265 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
12266 spin_unlock(&phba->hbalock);
12267 return IRQ_HANDLED;
12268 }
James Smarta747c9c2009-11-18 15:41:10 -050012269 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
12270 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
12271 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012272 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050012273 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040012274 readl(phba->HAregaddr); /* flush */
12275 spin_unlock(&phba->hbalock);
12276
12277 /*
12278 * Invokes slow-path host attention interrupt handling as appropriate.
12279 */
12280
12281 /* status of events with mailbox and link attention */
12282 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12283
12284 /* status of events with ELS ring */
12285 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12286 status2 >>= (4*LPFC_ELS_RING);
12287
12288 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012289 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012290 else
12291 sp_irq_rc = IRQ_NONE;
12292
12293 /*
12294 * Invoke fast-path host attention interrupt handling as appropriate.
12295 */
12296
12297 /* status of events with FCP ring */
12298 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12299 status1 >>= (4*LPFC_FCP_RING);
12300
12301 /* status of events with extra ring */
12302 if (phba->cfg_multi_ring_support == 2) {
12303 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12304 status2 >>= (4*LPFC_EXTRA_RING);
12305 } else
12306 status2 = 0;
12307
12308 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012309 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012310 else
12311 fp_irq_rc = IRQ_NONE;
12312
12313 /* Return device-level interrupt handling status */
12314 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012315} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012316
12317/**
12318 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12319 * @phba: pointer to lpfc hba data structure.
12320 *
12321 * This routine is invoked by the worker thread to process all the pending
12322 * SLI4 FCP abort XRI events.
12323 **/
12324void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12325{
12326 struct lpfc_cq_event *cq_event;
12327
12328 /* First, declare the fcp xri abort event has been handled */
12329 spin_lock_irq(&phba->hbalock);
12330 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12331 spin_unlock_irq(&phba->hbalock);
12332 /* Now, handle all the fcp xri abort events */
12333 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12334 /* Get the first event from the head of the event queue */
12335 spin_lock_irq(&phba->hbalock);
12336 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12337 cq_event, struct lpfc_cq_event, list);
12338 spin_unlock_irq(&phba->hbalock);
12339 /* Notify aborted XRI for FCP work queue */
12340 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12341 /* Free the event processed back to the free pool */
12342 lpfc_sli4_cq_event_release(phba, cq_event);
12343 }
12344}
12345
12346/**
12347 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12348 * @phba: pointer to lpfc hba data structure.
12349 *
12350 * This routine is invoked by the worker thread to process all the pending
12351 * SLI4 els abort xri events.
12352 **/
12353void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12354{
12355 struct lpfc_cq_event *cq_event;
12356
12357 /* First, declare the els xri abort event has been handled */
12358 spin_lock_irq(&phba->hbalock);
12359 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12360 spin_unlock_irq(&phba->hbalock);
12361 /* Now, handle all the els xri abort events */
12362 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12363 /* Get the first event from the head of the event queue */
12364 spin_lock_irq(&phba->hbalock);
12365 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12366 cq_event, struct lpfc_cq_event, list);
12367 spin_unlock_irq(&phba->hbalock);
12368 /* Notify aborted XRI for ELS work queue */
12369 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12370 /* Free the event processed back to the free pool */
12371 lpfc_sli4_cq_event_release(phba, cq_event);
12372 }
12373}
12374
James Smart341af102010-01-26 23:07:37 -050012375/**
12376 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12377 * @phba: pointer to lpfc hba data structure
12378 * @pIocbIn: pointer to the rspiocbq
12379 * @pIocbOut: pointer to the cmdiocbq
12380 * @wcqe: pointer to the complete wcqe
12381 *
12382 * This routine transfers the fields of a command iocbq to a response iocbq
12383 * by copying all the IOCB fields from command iocbq and transferring the
12384 * completion status information from the complete wcqe.
12385 **/
James Smart4f774512009-05-22 14:52:35 -040012386static void
James Smart341af102010-01-26 23:07:37 -050012387lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12388 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012389 struct lpfc_iocbq *pIocbOut,
12390 struct lpfc_wcqe_complete *wcqe)
12391{
James Smartaf227412013-10-10 12:23:10 -040012392 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012393 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012394 uint32_t status, max_response;
12395 struct lpfc_dmabuf *dmabuf;
12396 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012397 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12398
12399 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12400 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012401 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012402 status = bf_get(lpfc_wcqe_c_status, wcqe);
12403 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012404 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12405 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12406 pIocbIn->iocb.un.fcpi.fcpi_parm =
12407 pIocbOut->iocb.un.fcpi.fcpi_parm -
12408 wcqe->total_data_placed;
12409 else
12410 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012411 else {
James Smart4f774512009-05-22 14:52:35 -040012412 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012413 switch (pIocbOut->iocb.ulpCommand) {
12414 case CMD_ELS_REQUEST64_CR:
12415 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12416 bpl = (struct ulp_bde64 *)dmabuf->virt;
12417 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12418 max_response = bde.tus.f.bdeSize;
12419 break;
12420 case CMD_GEN_REQUEST64_CR:
12421 max_response = 0;
12422 if (!pIocbOut->context3)
12423 break;
12424 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12425 sizeof(struct ulp_bde64);
12426 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12427 bpl = (struct ulp_bde64 *)dmabuf->virt;
12428 for (i = 0; i < numBdes; i++) {
12429 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12430 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12431 max_response += bde.tus.f.bdeSize;
12432 }
12433 break;
12434 default:
12435 max_response = wcqe->total_data_placed;
12436 break;
12437 }
12438 if (max_response < wcqe->total_data_placed)
12439 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12440 else
12441 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12442 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012443 }
James Smart341af102010-01-26 23:07:37 -050012444
James Smartacd68592012-01-18 16:25:09 -050012445 /* Convert BG errors for completion status */
12446 if (status == CQE_STATUS_DI_ERROR) {
12447 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12448
12449 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12450 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12451 else
12452 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12453
12454 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12455 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12456 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12457 BGS_GUARD_ERR_MASK;
12458 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12459 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12460 BGS_APPTAG_ERR_MASK;
12461 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12462 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12463 BGS_REFTAG_ERR_MASK;
12464
12465 /* Check to see if there was any good data before the error */
12466 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12467 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12468 BGS_HI_WATER_MARK_PRESENT_MASK;
12469 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12470 wcqe->total_data_placed;
12471 }
12472
12473 /*
12474 * Set ALL the error bits to indicate we don't know what
12475 * type of error it is.
12476 */
12477 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12478 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12479 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12480 BGS_GUARD_ERR_MASK);
12481 }
12482
James Smart341af102010-01-26 23:07:37 -050012483 /* Pick up HBA exchange busy condition */
12484 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12485 spin_lock_irqsave(&phba->hbalock, iflags);
12486 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12487 spin_unlock_irqrestore(&phba->hbalock, iflags);
12488 }
James Smart4f774512009-05-22 14:52:35 -040012489}
12490
12491/**
James Smart45ed1192009-10-02 15:17:02 -040012492 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12493 * @phba: Pointer to HBA context object.
12494 * @wcqe: Pointer to work-queue completion queue entry.
12495 *
12496 * This routine handles an ELS work-queue completion event and construct
12497 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12498 * discovery engine to handle.
12499 *
12500 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12501 **/
12502static struct lpfc_iocbq *
12503lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12504 struct lpfc_iocbq *irspiocbq)
12505{
James Smart895427b2017-02-12 13:52:30 -080012506 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012507 struct lpfc_iocbq *cmdiocbq;
12508 struct lpfc_wcqe_complete *wcqe;
12509 unsigned long iflags;
12510
James Smart895427b2017-02-12 13:52:30 -080012511 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070012512 if (unlikely(!pring))
12513 return NULL;
James Smart895427b2017-02-12 13:52:30 -080012514
James Smart45ed1192009-10-02 15:17:02 -040012515 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012516 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012517 pring->stats.iocb_event++;
12518 /* Look up the ELS command IOCB and create pseudo response IOCB */
12519 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12520 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart45ed1192009-10-02 15:17:02 -040012521 if (unlikely(!cmdiocbq)) {
Dick Kennedy401bb412017-09-29 17:34:28 -070012522 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012523 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12524 "0386 ELS complete with no corresponding "
Dick Kennedy401bb412017-09-29 17:34:28 -070012525 "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
12526 wcqe->word0, wcqe->total_data_placed,
12527 wcqe->parameter, wcqe->word3);
James Smart45ed1192009-10-02 15:17:02 -040012528 lpfc_sli_release_iocbq(phba, irspiocbq);
12529 return NULL;
12530 }
12531
Dick Kennedy401bb412017-09-29 17:34:28 -070012532 /* Put the iocb back on the txcmplq */
12533 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
12534 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12535
James Smart45ed1192009-10-02 15:17:02 -040012536 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012537 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012538
12539 return irspiocbq;
12540}
12541
James Smart8a5ca102017-11-20 16:00:30 -080012542inline struct lpfc_cq_event *
12543lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
12544{
12545 struct lpfc_cq_event *cq_event;
12546
12547 /* Allocate a new internal CQ_EVENT entry */
12548 cq_event = lpfc_sli4_cq_event_alloc(phba);
12549 if (!cq_event) {
12550 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12551 "0602 Failed to alloc CQ_EVENT entry\n");
12552 return NULL;
12553 }
12554
12555 /* Move the CQE into the event */
12556 memcpy(&cq_event->cqe, entry, size);
12557 return cq_event;
12558}
12559
James Smart45ed1192009-10-02 15:17:02 -040012560/**
James Smart04c68492009-05-22 14:52:52 -040012561 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12562 * @phba: Pointer to HBA context object.
12563 * @cqe: Pointer to mailbox completion queue entry.
12564 *
12565 * This routine process a mailbox completion queue entry with asynchrous
12566 * event.
12567 *
12568 * Return: true if work posted to worker thread, otherwise false.
12569 **/
12570static bool
12571lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12572{
12573 struct lpfc_cq_event *cq_event;
12574 unsigned long iflags;
12575
12576 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12577 "0392 Async Event: word0:x%x, word1:x%x, "
12578 "word2:x%x, word3:x%x\n", mcqe->word0,
12579 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12580
James Smart8a5ca102017-11-20 16:00:30 -080012581 cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
12582 if (!cq_event)
James Smart04c68492009-05-22 14:52:52 -040012583 return false;
James Smart04c68492009-05-22 14:52:52 -040012584 spin_lock_irqsave(&phba->hbalock, iflags);
12585 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12586 /* Set the async event flag */
12587 phba->hba_flag |= ASYNC_EVENT;
12588 spin_unlock_irqrestore(&phba->hbalock, iflags);
12589
12590 return true;
12591}
12592
12593/**
12594 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12595 * @phba: Pointer to HBA context object.
12596 * @cqe: Pointer to mailbox completion queue entry.
12597 *
12598 * This routine process a mailbox completion queue entry with mailbox
12599 * completion event.
12600 *
12601 * Return: true if work posted to worker thread, otherwise false.
12602 **/
12603static bool
12604lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12605{
12606 uint32_t mcqe_status;
12607 MAILBOX_t *mbox, *pmbox;
12608 struct lpfc_mqe *mqe;
12609 struct lpfc_vport *vport;
12610 struct lpfc_nodelist *ndlp;
12611 struct lpfc_dmabuf *mp;
12612 unsigned long iflags;
12613 LPFC_MBOXQ_t *pmb;
12614 bool workposted = false;
12615 int rc;
12616
12617 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12618 if (!bf_get(lpfc_trailer_completed, mcqe))
12619 goto out_no_mqe_complete;
12620
12621 /* Get the reference to the active mbox command */
12622 spin_lock_irqsave(&phba->hbalock, iflags);
12623 pmb = phba->sli.mbox_active;
12624 if (unlikely(!pmb)) {
12625 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12626 "1832 No pending MBOX command to handle\n");
12627 spin_unlock_irqrestore(&phba->hbalock, iflags);
12628 goto out_no_mqe_complete;
12629 }
12630 spin_unlock_irqrestore(&phba->hbalock, iflags);
12631 mqe = &pmb->u.mqe;
12632 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12633 mbox = phba->mbox;
12634 vport = pmb->vport;
12635
12636 /* Reset heartbeat timer */
12637 phba->last_completion_time = jiffies;
12638 del_timer(&phba->sli.mbox_tmo);
12639
12640 /* Move mbox data to caller's mailbox region, do endian swapping */
12641 if (pmb->mbox_cmpl && mbox)
12642 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012643
James Smart73d91e52011-10-10 21:32:10 -040012644 /*
12645 * For mcqe errors, conditionally move a modified error code to
12646 * the mbox so that the error will not be missed.
12647 */
12648 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12649 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12650 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12651 bf_set(lpfc_mqe_status, mqe,
12652 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12653 }
James Smart04c68492009-05-22 14:52:52 -040012654 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12655 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12656 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12657 "MBOX dflt rpi: status:x%x rpi:x%x",
12658 mcqe_status,
12659 pmbox->un.varWords[0], 0);
12660 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12661 mp = (struct lpfc_dmabuf *)(pmb->context1);
12662 ndlp = (struct lpfc_nodelist *)pmb->context2;
12663 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12664 * RID of the PPI using the same mbox buffer.
12665 */
12666 lpfc_unreg_login(phba, vport->vpi,
12667 pmbox->un.varWords[0], pmb);
12668 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12669 pmb->context1 = mp;
12670 pmb->context2 = ndlp;
12671 pmb->vport = vport;
12672 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12673 if (rc != MBX_BUSY)
12674 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12675 LOG_SLI, "0385 rc should "
12676 "have been MBX_BUSY\n");
12677 if (rc != MBX_NOT_FINISHED)
12678 goto send_current_mbox;
12679 }
12680 }
12681 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12682 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12683 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12684
12685 /* There is mailbox completion work to do */
12686 spin_lock_irqsave(&phba->hbalock, iflags);
12687 __lpfc_mbox_cmpl_put(phba, pmb);
12688 phba->work_ha |= HA_MBATT;
12689 spin_unlock_irqrestore(&phba->hbalock, iflags);
12690 workposted = true;
12691
12692send_current_mbox:
12693 spin_lock_irqsave(&phba->hbalock, iflags);
12694 /* Release the mailbox command posting token */
12695 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12696 /* Setting active mailbox pointer need to be in sync to flag clear */
12697 phba->sli.mbox_active = NULL;
12698 spin_unlock_irqrestore(&phba->hbalock, iflags);
12699 /* Wake up worker thread to post the next pending mailbox command */
12700 lpfc_worker_wake_up(phba);
12701out_no_mqe_complete:
12702 if (bf_get(lpfc_trailer_consumed, mcqe))
12703 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12704 return workposted;
12705}
12706
12707/**
12708 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12709 * @phba: Pointer to HBA context object.
12710 * @cqe: Pointer to mailbox completion queue entry.
12711 *
12712 * This routine process a mailbox completion queue entry, it invokes the
12713 * proper mailbox complete handling or asynchrous event handling routine
12714 * according to the MCQE's async bit.
12715 *
12716 * Return: true if work posted to worker thread, otherwise false.
12717 **/
12718static bool
12719lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12720{
12721 struct lpfc_mcqe mcqe;
12722 bool workposted;
12723
12724 /* Copy the mailbox MCQE and convert endian order as needed */
12725 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12726
12727 /* Invoke the proper event handling routine */
12728 if (!bf_get(lpfc_trailer_async, &mcqe))
12729 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12730 else
12731 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12732 return workposted;
12733}
12734
12735/**
James Smart4f774512009-05-22 14:52:35 -040012736 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12737 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012738 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012739 * @wcqe: Pointer to work-queue completion queue entry.
12740 *
12741 * This routine handles an ELS work-queue completion event.
12742 *
12743 * Return: true if work posted to worker thread, otherwise false.
12744 **/
12745static bool
James Smart2a76a282012-08-03 12:35:54 -040012746lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012747 struct lpfc_wcqe_complete *wcqe)
12748{
James Smart4f774512009-05-22 14:52:35 -040012749 struct lpfc_iocbq *irspiocbq;
12750 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012751 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012752 int txq_cnt = 0;
12753 int txcmplq_cnt = 0;
12754 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012755
James Smart45ed1192009-10-02 15:17:02 -040012756 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012757 irspiocbq = lpfc_sli_get_iocbq(phba);
12758 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012759 if (!list_empty(&pring->txq))
12760 txq_cnt++;
12761 if (!list_empty(&pring->txcmplq))
12762 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012763 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012764 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12765 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012766 txq_cnt, phba->iocb_cnt,
12767 fcp_txcmplq_cnt,
12768 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012769 return false;
James Smart4f774512009-05-22 14:52:35 -040012770 }
James Smart4f774512009-05-22 14:52:35 -040012771
James Smart45ed1192009-10-02 15:17:02 -040012772 /* Save off the slow-path queue event for work thread to process */
12773 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012774 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012775 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012776 &phba->sli4_hba.sp_queue_event);
12777 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012778 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012779
James Smart45ed1192009-10-02 15:17:02 -040012780 return true;
James Smart4f774512009-05-22 14:52:35 -040012781}
12782
12783/**
12784 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12785 * @phba: Pointer to HBA context object.
12786 * @wcqe: Pointer to work-queue completion queue entry.
12787 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080012788 * This routine handles slow-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040012789 * proper WQ release routine to the slow-path WQ.
12790 **/
12791static void
12792lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12793 struct lpfc_wcqe_release *wcqe)
12794{
James Smart2e90f4b2011-12-13 13:22:37 -050012795 /* sanity check on queue memory */
12796 if (unlikely(!phba->sli4_hba.els_wq))
12797 return;
James Smart4f774512009-05-22 14:52:35 -040012798 /* Check for the slow-path ELS work queue */
12799 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12800 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12801 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12802 else
12803 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12804 "2579 Slow-path wqe consume event carries "
12805 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12806 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12807 phba->sli4_hba.els_wq->queue_id);
12808}
12809
12810/**
12811 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12812 * @phba: Pointer to HBA context object.
12813 * @cq: Pointer to a WQ completion queue.
12814 * @wcqe: Pointer to work-queue completion queue entry.
12815 *
12816 * This routine handles an XRI abort event.
12817 *
12818 * Return: true if work posted to worker thread, otherwise false.
12819 **/
12820static bool
12821lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12822 struct lpfc_queue *cq,
12823 struct sli4_wcqe_xri_aborted *wcqe)
12824{
12825 bool workposted = false;
12826 struct lpfc_cq_event *cq_event;
12827 unsigned long iflags;
12828
James Smart4f774512009-05-22 14:52:35 -040012829 switch (cq->subtype) {
12830 case LPFC_FCP:
James Smart8a5ca102017-11-20 16:00:30 -080012831 cq_event = lpfc_cq_event_setup(
12832 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12833 if (!cq_event)
12834 return false;
James Smart4f774512009-05-22 14:52:35 -040012835 spin_lock_irqsave(&phba->hbalock, iflags);
12836 list_add_tail(&cq_event->list,
12837 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12838 /* Set the fcp xri abort event flag */
12839 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12840 spin_unlock_irqrestore(&phba->hbalock, iflags);
12841 workposted = true;
12842 break;
James Smart422c4cb2017-11-20 16:00:32 -080012843 case LPFC_NVME_LS: /* NVME LS uses ELS resources */
James Smart4f774512009-05-22 14:52:35 -040012844 case LPFC_ELS:
James Smart8a5ca102017-11-20 16:00:30 -080012845 cq_event = lpfc_cq_event_setup(
12846 phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12847 if (!cq_event)
12848 return false;
James Smart4f774512009-05-22 14:52:35 -040012849 spin_lock_irqsave(&phba->hbalock, iflags);
12850 list_add_tail(&cq_event->list,
12851 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12852 /* Set the els xri abort event flag */
12853 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12854 spin_unlock_irqrestore(&phba->hbalock, iflags);
12855 workposted = true;
12856 break;
James Smart318083a2017-03-04 09:30:30 -080012857 case LPFC_NVME:
James Smart8a5ca102017-11-20 16:00:30 -080012858 /* Notify aborted XRI for NVME work queue */
12859 if (phba->nvmet_support)
12860 lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
12861 else
12862 lpfc_sli4_nvme_xri_aborted(phba, wcqe);
12863
12864 workposted = false;
James Smart318083a2017-03-04 09:30:30 -080012865 break;
James Smart4f774512009-05-22 14:52:35 -040012866 default:
12867 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart318083a2017-03-04 09:30:30 -080012868 "0603 Invalid CQ subtype %d: "
12869 "%08x %08x %08x %08x\n",
12870 cq->subtype, wcqe->word0, wcqe->parameter,
12871 wcqe->word2, wcqe->word3);
James Smart4f774512009-05-22 14:52:35 -040012872 workposted = false;
12873 break;
12874 }
12875 return workposted;
12876}
12877
12878/**
James Smart4d9ab992009-10-02 15:16:39 -040012879 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012880 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012881 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012882 *
James Smart4d9ab992009-10-02 15:16:39 -040012883 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012884 *
12885 * Return: true if work posted to worker thread, otherwise false.
12886 **/
12887static bool
James Smart4d9ab992009-10-02 15:16:39 -040012888lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12889{
12890 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080012891 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040012892 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12893 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
James Smart547077a2017-05-15 15:20:40 -070012894 struct lpfc_nvmet_tgtport *tgtp;
James Smart4d9ab992009-10-02 15:16:39 -040012895 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012896 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012897 unsigned long iflags;
12898
James Smart2e90f4b2011-12-13 13:22:37 -050012899 /* sanity check on queue memory */
12900 if (unlikely(!hrq) || unlikely(!drq))
12901 return workposted;
12902
James Smart7851fe22011-07-22 18:36:52 -040012903 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12904 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12905 else
12906 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12907 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012908 goto out;
12909
12910 status = bf_get(lpfc_rcqe_status, rcqe);
12911 switch (status) {
12912 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12913 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12914 "2537 Receive Frame Truncated!!\n");
12915 case FC_STATUS_RQ_SUCCESS:
12916 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080012917 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012918 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12919 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012920 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012921 spin_unlock_irqrestore(&phba->hbalock, iflags);
12922 goto out;
12923 }
James Smartb84daac2012-08-03 12:35:13 -040012924 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070012925 hrq->RQ_buf_posted--;
James Smart4d9ab992009-10-02 15:16:39 -040012926 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080012927
12928 /* If a NVME LS event (type 0x28), treat it as Fast path */
12929 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12930
James Smart4d9ab992009-10-02 15:16:39 -040012931 /* save off the frame for the word thread to process */
12932 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012933 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012934 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012935 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012936 spin_unlock_irqrestore(&phba->hbalock, iflags);
12937 workposted = true;
12938 break;
James Smart4d9ab992009-10-02 15:16:39 -040012939 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070012940 if (phba->nvmet_support) {
12941 tgtp = phba->targetport->private;
12942 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
12943 "6402 RQE Error x%x, posted %d err_cnt "
12944 "%d: %x %x %x\n",
12945 status, hrq->RQ_buf_posted,
12946 hrq->RQ_no_posted_buf,
12947 atomic_read(&tgtp->rcv_fcp_cmd_in),
12948 atomic_read(&tgtp->rcv_fcp_cmd_out),
12949 atomic_read(&tgtp->xmt_fcp_release));
12950 }
12951 /* fallthrough */
12952
12953 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smartb84daac2012-08-03 12:35:13 -040012954 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012955 /* Post more buffers if possible */
12956 spin_lock_irqsave(&phba->hbalock, iflags);
12957 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12958 spin_unlock_irqrestore(&phba->hbalock, iflags);
12959 workposted = true;
12960 break;
12961 }
12962out:
12963 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012964}
12965
12966/**
12967 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12968 * @phba: Pointer to HBA context object.
12969 * @cq: Pointer to the completion queue.
12970 * @wcqe: Pointer to a completion queue entry.
12971 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012972 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012973 * entry.
12974 *
12975 * Return: true if work posted to worker thread, otherwise false.
12976 **/
12977static bool
12978lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012979 struct lpfc_cqe *cqe)
12980{
James Smart45ed1192009-10-02 15:17:02 -040012981 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012982 bool workposted = false;
12983
12984 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012985 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012986
12987 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012988 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012989 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012990 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012991 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012992 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012993 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012994 break;
12995 case CQE_CODE_RELEASE_WQE:
12996 /* Process the WQ release event */
12997 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012998 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012999 break;
13000 case CQE_CODE_XRI_ABORTED:
13001 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013002 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013003 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040013004 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040013005 break;
James Smart4d9ab992009-10-02 15:16:39 -040013006 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040013007 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040013008 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040013009 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040013010 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040013011 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040013012 break;
James Smart4f774512009-05-22 14:52:35 -040013013 default:
13014 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13015 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040013016 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040013017 break;
13018 }
13019 return workposted;
13020}
13021
13022/**
James Smart4f774512009-05-22 14:52:35 -040013023 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
13024 * @phba: Pointer to HBA context object.
13025 * @eqe: Pointer to fast-path event queue entry.
13026 *
13027 * This routine process a event queue entry from the slow-path event queue.
13028 * It will check the MajorCode and MinorCode to determine this is for a
13029 * completion event on a completion queue, if not, an error shall be logged
13030 * and just return. Otherwise, it will get to the corresponding completion
13031 * queue and process all the entries on that completion queue, rearm the
13032 * completion queue, and then return.
13033 *
13034 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013035static void
James Smart67d12732012-08-03 12:36:13 -040013036lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13037 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040013038{
James Smart67d12732012-08-03 12:36:13 -040013039 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040013040 uint16_t cqid;
13041
James Smart4f774512009-05-22 14:52:35 -040013042 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040013043 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040013044
James Smart4f774512009-05-22 14:52:35 -040013045 list_for_each_entry(childq, &speq->child_list, list) {
13046 if (childq->queue_id == cqid) {
13047 cq = childq;
13048 break;
13049 }
13050 }
13051 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040013052 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13053 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13054 "0365 Slow-path CQ identifier "
13055 "(%d) does not exist\n", cqid);
Dick Kennedyf485c182017-09-29 17:34:34 -070013056 return;
James Smart4f774512009-05-22 14:52:35 -040013057 }
13058
James Smart895427b2017-02-12 13:52:30 -080013059 /* Save EQ associated with this CQ */
13060 cq->assoc_qp = speq;
13061
Dick Kennedyf485c182017-09-29 17:34:34 -070013062 if (!queue_work(phba->wq, &cq->spwork))
13063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13064 "0390 Cannot schedule soft IRQ "
13065 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13066 cqid, cq->queue_id, smp_processor_id());
13067}
13068
13069/**
13070 * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
13071 * @phba: Pointer to HBA context object.
13072 *
13073 * This routine process a event queue entry from the slow-path event queue.
13074 * It will check the MajorCode and MinorCode to determine this is for a
13075 * completion event on a completion queue, if not, an error shall be logged
13076 * and just return. Otherwise, it will get to the corresponding completion
13077 * queue and process all the entries on that completion queue, rearm the
13078 * completion queue, and then return.
13079 *
13080 **/
13081static void
13082lpfc_sli4_sp_process_cq(struct work_struct *work)
13083{
13084 struct lpfc_queue *cq =
13085 container_of(work, struct lpfc_queue, spwork);
13086 struct lpfc_hba *phba = cq->phba;
13087 struct lpfc_cqe *cqe;
13088 bool workposted = false;
13089 int ccount = 0;
13090
James Smart4f774512009-05-22 14:52:35 -040013091 /* Process all the entries to the CQ */
13092 switch (cq->type) {
13093 case LPFC_MCQ:
13094 while ((cqe = lpfc_sli4_cq_get(cq))) {
13095 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013096 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013097 break;
James Smartb84daac2012-08-03 12:35:13 -040013098 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040013099 }
13100 break;
13101 case LPFC_WCQ:
13102 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013103 if (cq->subtype == LPFC_FCP ||
13104 cq->subtype == LPFC_NVME) {
13105#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13106 if (phba->ktime_on)
13107 cq->isr_timestamp = ktime_get_ns();
13108 else
13109 cq->isr_timestamp = 0;
13110#endif
James Smart895427b2017-02-12 13:52:30 -080013111 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040013112 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013113 } else {
James Smart05580562011-05-24 11:40:48 -040013114 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
13115 cqe);
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013116 }
Dick Kennedyf485c182017-09-29 17:34:34 -070013117 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013118 break;
James Smart4f774512009-05-22 14:52:35 -040013119 }
James Smartb84daac2012-08-03 12:35:13 -040013120
13121 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013122 if (ccount > cq->CQ_max_cqe)
13123 cq->CQ_max_cqe = ccount;
James Smart4f774512009-05-22 14:52:35 -040013124 break;
13125 default:
13126 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13127 "0370 Invalid completion queue type (%d)\n",
13128 cq->type);
Dick Kennedyf485c182017-09-29 17:34:34 -070013129 return;
James Smart4f774512009-05-22 14:52:35 -040013130 }
13131
13132 /* Catch the no cq entry condition, log an error */
Dick Kennedyf485c182017-09-29 17:34:34 -070013133 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013134 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13135 "0371 No entry from the CQ: identifier "
13136 "(x%x), type (%d)\n", cq->queue_id, cq->type);
13137
13138 /* In any case, flash and re-arm the RCQ */
13139 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13140
13141 /* wake up worker thread if there are works to be done */
13142 if (workposted)
13143 lpfc_worker_wake_up(phba);
13144}
13145
13146/**
13147 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040013148 * @phba: Pointer to HBA context object.
13149 * @cq: Pointer to associated CQ
13150 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040013151 *
13152 * This routine process a fast-path work queue completion entry from fast-path
13153 * event queue for FCP command response completion.
13154 **/
13155static void
James Smart2a76a282012-08-03 12:35:54 -040013156lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013157 struct lpfc_wcqe_complete *wcqe)
13158{
James Smart2a76a282012-08-03 12:35:54 -040013159 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040013160 struct lpfc_iocbq *cmdiocbq;
13161 struct lpfc_iocbq irspiocbq;
13162 unsigned long iflags;
13163
James Smart4f774512009-05-22 14:52:35 -040013164 /* Check for response status */
13165 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
13166 /* If resource errors reported from HBA, reduce queue
13167 * depth of the SCSI device.
13168 */
James Smarte3d2b802012-08-14 14:25:43 -040013169 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
13170 IOSTAT_LOCAL_REJECT)) &&
13171 ((wcqe->parameter & IOERR_PARAM_MASK) ==
13172 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040013173 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040013174
James Smart4f774512009-05-22 14:52:35 -040013175 /* Log the error status */
13176 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13177 "0373 FCP complete error: status=x%x, "
13178 "hw_status=x%x, total_data_specified=%d, "
13179 "parameter=x%x, word3=x%x\n",
13180 bf_get(lpfc_wcqe_c_status, wcqe),
13181 bf_get(lpfc_wcqe_c_hw_status, wcqe),
13182 wcqe->total_data_placed, wcqe->parameter,
13183 wcqe->word3);
13184 }
13185
13186 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040013187 spin_lock_irqsave(&pring->ring_lock, iflags);
13188 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040013189 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
13190 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040013191 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040013192 if (unlikely(!cmdiocbq)) {
13193 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13194 "0374 FCP complete with no corresponding "
13195 "cmdiocb: iotag (%d)\n",
13196 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13197 return;
13198 }
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013199#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13200 cmdiocbq->isr_timestamp = cq->isr_timestamp;
13201#endif
James Smart895427b2017-02-12 13:52:30 -080013202 if (cmdiocbq->iocb_cmpl == NULL) {
13203 if (cmdiocbq->wqe_cmpl) {
13204 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13205 spin_lock_irqsave(&phba->hbalock, iflags);
13206 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13207 spin_unlock_irqrestore(&phba->hbalock, iflags);
13208 }
13209
13210 /* Pass the cmd_iocb and the wcqe to the upper layer */
13211 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
13212 return;
13213 }
James Smart4f774512009-05-22 14:52:35 -040013214 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13215 "0375 FCP cmdiocb not callback function "
13216 "iotag: (%d)\n",
13217 bf_get(lpfc_wcqe_c_request_tag, wcqe));
13218 return;
13219 }
13220
13221 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050013222 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013223
James Smart0f65ff62010-02-26 14:14:23 -050013224 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
13225 spin_lock_irqsave(&phba->hbalock, iflags);
13226 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
13227 spin_unlock_irqrestore(&phba->hbalock, iflags);
13228 }
13229
James Smart4f774512009-05-22 14:52:35 -040013230 /* Pass the cmd_iocb and the rsp state to the upper layer */
13231 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
13232}
13233
13234/**
13235 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
13236 * @phba: Pointer to HBA context object.
13237 * @cq: Pointer to completion queue.
13238 * @wcqe: Pointer to work-queue completion queue entry.
13239 *
Masahiro Yamada3f8b6fb2017-02-27 14:29:25 -080013240 * This routine handles an fast-path WQ entry consumed event by invoking the
James Smart4f774512009-05-22 14:52:35 -040013241 * proper WQ release routine to the slow-path WQ.
13242 **/
13243static void
13244lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13245 struct lpfc_wcqe_release *wcqe)
13246{
13247 struct lpfc_queue *childwq;
13248 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080013249 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040013250
13251 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080013252 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040013253 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080013254 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040013255 lpfc_sli4_wq_release(childwq,
13256 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
James Smart6e8e1c12018-01-30 15:58:49 -080013257 if (childwq->q_flag & HBA_NVMET_WQFULL)
13258 lpfc_nvmet_wqfull_process(phba, childwq);
James Smart4f774512009-05-22 14:52:35 -040013259 wqid_matched = true;
13260 break;
13261 }
13262 }
13263 /* Report warning log message if no match found */
13264 if (wqid_matched != true)
13265 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13266 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080013267 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040013268}
13269
13270/**
James Smart2d7dbc42017-02-12 13:52:35 -080013271 * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
13272 * @phba: Pointer to HBA context object.
13273 * @rcqe: Pointer to receive-queue completion queue entry.
13274 *
13275 * This routine process a receive-queue completion queue entry.
13276 *
13277 * Return: true if work posted to worker thread, otherwise false.
13278 **/
13279static bool
13280lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
13281 struct lpfc_rcqe *rcqe)
13282{
13283 bool workposted = false;
13284 struct lpfc_queue *hrq;
13285 struct lpfc_queue *drq;
13286 struct rqb_dmabuf *dma_buf;
13287 struct fc_frame_header *fc_hdr;
James Smart547077a2017-05-15 15:20:40 -070013288 struct lpfc_nvmet_tgtport *tgtp;
James Smart2d7dbc42017-02-12 13:52:35 -080013289 uint32_t status, rq_id;
13290 unsigned long iflags;
13291 uint32_t fctl, idx;
13292
13293 if ((phba->nvmet_support == 0) ||
13294 (phba->sli4_hba.nvmet_cqset == NULL))
13295 return workposted;
13296
13297 idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
13298 hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
13299 drq = phba->sli4_hba.nvmet_mrq_data[idx];
13300
13301 /* sanity check on queue memory */
13302 if (unlikely(!hrq) || unlikely(!drq))
13303 return workposted;
13304
13305 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
13306 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
13307 else
13308 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
13309
13310 if ((phba->nvmet_support == 0) ||
13311 (rq_id != hrq->queue_id))
13312 return workposted;
13313
13314 status = bf_get(lpfc_rcqe_status, rcqe);
13315 switch (status) {
13316 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
13317 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13318 "6126 Receive Frame Truncated!!\n");
James Smart78e1d202017-06-01 21:07:09 -070013319 /* Drop thru */
James Smart2d7dbc42017-02-12 13:52:35 -080013320 case FC_STATUS_RQ_SUCCESS:
James Smart2d7dbc42017-02-12 13:52:35 -080013321 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartcbc5de12017-12-08 17:18:04 -080013322 lpfc_sli4_rq_release(hrq, drq);
James Smart2d7dbc42017-02-12 13:52:35 -080013323 dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
13324 if (!dma_buf) {
13325 hrq->RQ_no_buf_found++;
13326 spin_unlock_irqrestore(&phba->hbalock, iflags);
13327 goto out;
13328 }
13329 spin_unlock_irqrestore(&phba->hbalock, iflags);
13330 hrq->RQ_rcv_buf++;
James Smart547077a2017-05-15 15:20:40 -070013331 hrq->RQ_buf_posted--;
James Smart2d7dbc42017-02-12 13:52:35 -080013332 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
13333
13334 /* Just some basic sanity checks on FCP Command frame */
13335 fctl = (fc_hdr->fh_f_ctl[0] << 16 |
13336 fc_hdr->fh_f_ctl[1] << 8 |
13337 fc_hdr->fh_f_ctl[2]);
13338 if (((fctl &
13339 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
13340 (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
13341 (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
13342 goto drop;
13343
13344 if (fc_hdr->fh_type == FC_TYPE_FCP) {
13345 dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
James Smartd613b6a2017-02-12 13:52:37 -080013346 lpfc_nvmet_unsol_fcp_event(
Dick Kennedy66d7ce92017-08-23 16:55:42 -070013347 phba, idx, dma_buf,
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013348 cq->isr_timestamp);
James Smart2d7dbc42017-02-12 13:52:35 -080013349 return false;
13350 }
13351drop:
13352 lpfc_in_buf_free(phba, &dma_buf->dbuf);
13353 break;
James Smart2d7dbc42017-02-12 13:52:35 -080013354 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smart547077a2017-05-15 15:20:40 -070013355 if (phba->nvmet_support) {
13356 tgtp = phba->targetport->private;
13357 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
13358 "6401 RQE Error x%x, posted %d err_cnt "
13359 "%d: %x %x %x\n",
13360 status, hrq->RQ_buf_posted,
13361 hrq->RQ_no_posted_buf,
13362 atomic_read(&tgtp->rcv_fcp_cmd_in),
13363 atomic_read(&tgtp->rcv_fcp_cmd_out),
13364 atomic_read(&tgtp->xmt_fcp_release));
13365 }
13366 /* fallthrough */
13367
13368 case FC_STATUS_INSUFF_BUF_NEED_BUF:
James Smart2d7dbc42017-02-12 13:52:35 -080013369 hrq->RQ_no_posted_buf++;
13370 /* Post more buffers if possible */
James Smart2d7dbc42017-02-12 13:52:35 -080013371 break;
13372 }
13373out:
13374 return workposted;
13375}
13376
13377/**
James Smart895427b2017-02-12 13:52:30 -080013378 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040013379 * @cq: Pointer to the completion queue.
13380 * @eqe: Pointer to fast-path completion queue entry.
13381 *
13382 * This routine process a fast-path work queue completion entry from fast-path
13383 * event queue for FCP command response completion.
13384 **/
13385static int
James Smart895427b2017-02-12 13:52:30 -080013386lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040013387 struct lpfc_cqe *cqe)
13388{
13389 struct lpfc_wcqe_release wcqe;
13390 bool workposted = false;
13391
13392 /* Copy the work queue CQE and convert endian order if needed */
13393 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
13394
13395 /* Check and process for different type of WCQE and dispatch */
13396 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
13397 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080013398 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040013399 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040013400 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040013401 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080013402 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
13403 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
13404 (struct lpfc_wcqe_complete *)&wcqe);
13405 if (cq->subtype == LPFC_NVME_LS)
13406 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040013407 (struct lpfc_wcqe_complete *)&wcqe);
13408 break;
13409 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040013410 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040013411 /* Process the WQ release event */
13412 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
13413 (struct lpfc_wcqe_release *)&wcqe);
13414 break;
13415 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040013416 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040013417 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040013418 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040013419 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
13420 (struct sli4_wcqe_xri_aborted *)&wcqe);
13421 break;
James Smart895427b2017-02-12 13:52:30 -080013422 case CQE_CODE_RECEIVE_V1:
13423 case CQE_CODE_RECEIVE:
13424 phba->last_completion_time = jiffies;
James Smart2d7dbc42017-02-12 13:52:35 -080013425 if (cq->subtype == LPFC_NVMET) {
13426 workposted = lpfc_sli4_nvmet_handle_rcqe(
13427 phba, cq, (struct lpfc_rcqe *)&wcqe);
13428 }
James Smart895427b2017-02-12 13:52:30 -080013429 break;
James Smart4f774512009-05-22 14:52:35 -040013430 default:
13431 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080013432 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040013433 bf_get(lpfc_wcqe_c_code, &wcqe));
13434 break;
13435 }
13436 return workposted;
13437}
13438
13439/**
James Smart67d12732012-08-03 12:36:13 -040013440 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040013441 * @phba: Pointer to HBA context object.
13442 * @eqe: Pointer to fast-path event queue entry.
13443 *
13444 * This routine process a event queue entry from the fast-path event queue.
13445 * It will check the MajorCode and MinorCode to determine this is for a
13446 * completion event on a completion queue, if not, an error shall be logged
13447 * and just return. Otherwise, it will get to the corresponding completion
13448 * queue and process all the entries on the completion queue, rearm the
13449 * completion queue, and then return.
13450 **/
Dick Kennedyf485c182017-09-29 17:34:34 -070013451static void
James Smart67d12732012-08-03 12:36:13 -040013452lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
13453 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040013454{
James Smart895427b2017-02-12 13:52:30 -080013455 struct lpfc_queue *cq = NULL;
James Smart2d7dbc42017-02-12 13:52:35 -080013456 uint16_t cqid, id;
James Smart4f774512009-05-22 14:52:35 -040013457
James Smartcb5172e2010-03-15 11:25:07 -040013458 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040013459 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040013460 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040013461 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040013462 bf_get_le32(lpfc_eqe_major_code, eqe),
13463 bf_get_le32(lpfc_eqe_minor_code, eqe));
Dick Kennedyf485c182017-09-29 17:34:34 -070013464 return;
James Smart4f774512009-05-22 14:52:35 -040013465 }
13466
James Smart67d12732012-08-03 12:36:13 -040013467 /* Get the reference to the corresponding CQ */
13468 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13469
James Smart2d7dbc42017-02-12 13:52:35 -080013470 if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
13471 id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
13472 if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
13473 /* Process NVMET unsol rcv */
13474 cq = phba->sli4_hba.nvmet_cqset[cqid - id];
13475 goto process_cq;
13476 }
13477 }
13478
James Smart895427b2017-02-12 13:52:30 -080013479 if (phba->sli4_hba.nvme_cq_map &&
13480 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
James Smartf358dd02017-02-12 13:52:34 -080013481 /* Process NVME / NVMET command completion */
James Smart895427b2017-02-12 13:52:30 -080013482 cq = phba->sli4_hba.nvme_cq[qidx];
13483 goto process_cq;
13484 }
13485
13486 if (phba->sli4_hba.fcp_cq_map &&
13487 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13488 /* Process FCP command completion */
13489 cq = phba->sli4_hba.fcp_cq[qidx];
13490 goto process_cq;
13491 }
13492
13493 if (phba->sli4_hba.nvmels_cq &&
13494 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13495 /* Process NVME unsol rcv */
13496 cq = phba->sli4_hba.nvmels_cq;
13497 }
13498
13499 /* Otherwise this is a Slow path event */
13500 if (cq == NULL) {
Dick Kennedyf485c182017-09-29 17:34:34 -070013501 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
13502 return;
James Smart67d12732012-08-03 12:36:13 -040013503 }
13504
James Smart895427b2017-02-12 13:52:30 -080013505process_cq:
James Smart4f774512009-05-22 14:52:35 -040013506 if (unlikely(cqid != cq->queue_id)) {
13507 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13508 "0368 Miss-matched fast-path completion "
13509 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13510 cqid, cq->queue_id);
Dick Kennedyf485c182017-09-29 17:34:34 -070013511 return;
James Smart4f774512009-05-22 14:52:35 -040013512 }
13513
James Smart895427b2017-02-12 13:52:30 -080013514 /* Save EQ associated with this CQ */
13515 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13516
Dick Kennedyf485c182017-09-29 17:34:34 -070013517 if (!queue_work(phba->wq, &cq->irqwork))
13518 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13519 "0363 Cannot schedule soft IRQ "
13520 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13521 cqid, cq->queue_id, smp_processor_id());
13522}
13523
13524/**
13525 * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
13526 * @phba: Pointer to HBA context object.
13527 * @eqe: Pointer to fast-path event queue entry.
13528 *
13529 * This routine process a event queue entry from the fast-path event queue.
13530 * It will check the MajorCode and MinorCode to determine this is for a
13531 * completion event on a completion queue, if not, an error shall be logged
13532 * and just return. Otherwise, it will get to the corresponding completion
13533 * queue and process all the entries on the completion queue, rearm the
13534 * completion queue, and then return.
13535 **/
13536static void
13537lpfc_sli4_hba_process_cq(struct work_struct *work)
13538{
13539 struct lpfc_queue *cq =
13540 container_of(work, struct lpfc_queue, irqwork);
13541 struct lpfc_hba *phba = cq->phba;
13542 struct lpfc_cqe *cqe;
13543 bool workposted = false;
13544 int ccount = 0;
13545
James Smart4f774512009-05-22 14:52:35 -040013546 /* Process all the entries to the CQ */
13547 while ((cqe = lpfc_sli4_cq_get(cq))) {
Dick Kennedyc8a4ce02017-09-29 17:34:33 -070013548#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13549 if (phba->ktime_on)
13550 cq->isr_timestamp = ktime_get_ns();
13551 else
13552 cq->isr_timestamp = 0;
13553#endif
James Smart895427b2017-02-12 13:52:30 -080013554 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
Dick Kennedyf485c182017-09-29 17:34:34 -070013555 if (!(++ccount % cq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013556 break;
James Smart4f774512009-05-22 14:52:35 -040013557 }
13558
James Smartb84daac2012-08-03 12:35:13 -040013559 /* Track the max number of CQEs processed in 1 EQ */
Dick Kennedyf485c182017-09-29 17:34:34 -070013560 if (ccount > cq->CQ_max_cqe)
13561 cq->CQ_max_cqe = ccount;
13562 cq->assoc_qp->EQ_cqe_cnt += ccount;
James Smartb84daac2012-08-03 12:35:13 -040013563
James Smart4f774512009-05-22 14:52:35 -040013564 /* Catch the no cq entry condition */
Dick Kennedyf485c182017-09-29 17:34:34 -070013565 if (unlikely(ccount == 0))
James Smart4f774512009-05-22 14:52:35 -040013566 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13567 "0369 No entry from fast-path completion "
13568 "queue fcpcqid=%d\n", cq->queue_id);
13569
13570 /* In any case, flash and re-arm the CQ */
13571 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13572
13573 /* wake up worker thread if there are works to be done */
13574 if (workposted)
13575 lpfc_worker_wake_up(phba);
13576}
13577
13578static void
13579lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13580{
13581 struct lpfc_eqe *eqe;
13582
13583 /* walk all the EQ entries and drop on the floor */
13584 while ((eqe = lpfc_sli4_eq_get(eq)))
13585 ;
13586
13587 /* Clear and re-arm the EQ */
13588 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13589}
13590
James Smart1ba981f2014-02-20 09:56:45 -050013591
13592/**
13593 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13594 * entry
13595 * @phba: Pointer to HBA context object.
13596 * @eqe: Pointer to fast-path event queue entry.
13597 *
13598 * This routine process a event queue entry from the Flash Optimized Fabric
13599 * event queue. It will check the MajorCode and MinorCode to determine this
13600 * is for a completion event on a completion queue, if not, an error shall be
13601 * logged and just return. Otherwise, it will get to the corresponding
13602 * completion queue and process all the entries on the completion queue, rearm
13603 * the completion queue, and then return.
13604 **/
13605static void
13606lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13607{
13608 struct lpfc_queue *cq;
James Smart1ba981f2014-02-20 09:56:45 -050013609 uint16_t cqid;
James Smart1ba981f2014-02-20 09:56:45 -050013610
13611 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13612 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13613 "9147 Not a valid completion "
13614 "event: majorcode=x%x, minorcode=x%x\n",
13615 bf_get_le32(lpfc_eqe_major_code, eqe),
13616 bf_get_le32(lpfc_eqe_minor_code, eqe));
13617 return;
13618 }
13619
13620 /* Get the reference to the corresponding CQ */
13621 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13622
13623 /* Next check for OAS */
13624 cq = phba->sli4_hba.oas_cq;
13625 if (unlikely(!cq)) {
13626 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13627 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13628 "9148 OAS completion queue "
13629 "does not exist\n");
13630 return;
13631 }
13632
13633 if (unlikely(cqid != cq->queue_id)) {
13634 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13635 "9149 Miss-matched fast-path compl "
13636 "queue id: eqcqid=%d, fcpcqid=%d\n",
13637 cqid, cq->queue_id);
13638 return;
13639 }
13640
James Smartd41b65b2017-06-15 22:56:41 -070013641 /* Save EQ associated with this CQ */
13642 cq->assoc_qp = phba->sli4_hba.fof_eq;
13643
Dick Kennedyf485c182017-09-29 17:34:34 -070013644 /* CQ work will be processed on CPU affinitized to this IRQ */
13645 if (!queue_work(phba->wq, &cq->irqwork))
James Smart1ba981f2014-02-20 09:56:45 -050013646 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
Dick Kennedyf485c182017-09-29 17:34:34 -070013647 "0367 Cannot schedule soft IRQ "
13648 "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
13649 cqid, cq->queue_id, smp_processor_id());
James Smart1ba981f2014-02-20 09:56:45 -050013650}
13651
13652/**
13653 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13654 * @irq: Interrupt number.
13655 * @dev_id: The device context pointer.
13656 *
13657 * This function is directly called from the PCI layer as an interrupt
13658 * service routine when device with SLI-4 interface spec is enabled with
13659 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13660 * IOCB ring event in the HBA. However, when the device is enabled with either
13661 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13662 * device-level interrupt handler. When the PCI slot is in error recovery
13663 * or the HBA is undergoing initialization, the interrupt handler will not
13664 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13665 * the intrrupt context. This function is called without any lock held.
13666 * It gets the hbalock to access and update SLI data structures. Note that,
13667 * the EQ to CQ are one-to-one map such that the EQ index is
13668 * equal to that of CQ index.
13669 *
13670 * This function returns IRQ_HANDLED when interrupt is handled else it
13671 * returns IRQ_NONE.
13672 **/
13673irqreturn_t
13674lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13675{
13676 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013677 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013678 struct lpfc_queue *eq;
13679 struct lpfc_eqe *eqe;
13680 unsigned long iflag;
13681 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013682
13683 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013684 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13685 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013686
13687 if (unlikely(!phba))
13688 return IRQ_NONE;
13689
13690 /* Get to the EQ struct associated with this vector */
13691 eq = phba->sli4_hba.fof_eq;
13692 if (unlikely(!eq))
13693 return IRQ_NONE;
13694
13695 /* Check device state for handling interrupt */
13696 if (unlikely(lpfc_intr_state_check(phba))) {
James Smart1ba981f2014-02-20 09:56:45 -050013697 /* Check again for link_state with lock held */
13698 spin_lock_irqsave(&phba->hbalock, iflag);
13699 if (phba->link_state < LPFC_LINK_DOWN)
13700 /* Flush, clear interrupt, and rearm the EQ */
13701 lpfc_sli4_eq_flush(phba, eq);
13702 spin_unlock_irqrestore(&phba->hbalock, iflag);
13703 return IRQ_NONE;
13704 }
13705
13706 /*
13707 * Process all the event on FCP fast-path EQ
13708 */
13709 while ((eqe = lpfc_sli4_eq_get(eq))) {
13710 lpfc_sli4_fof_handle_eqe(phba, eqe);
13711 if (!(++ecount % eq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013712 break;
James Smart1ba981f2014-02-20 09:56:45 -050013713 eq->EQ_processed++;
13714 }
13715
13716 /* Track the max number of EQEs processed in 1 intr */
13717 if (ecount > eq->EQ_max_eqe)
13718 eq->EQ_max_eqe = ecount;
13719
13720
13721 if (unlikely(ecount == 0)) {
13722 eq->EQ_no_entry++;
13723
13724 if (phba->intr_type == MSIX)
13725 /* MSI-X treated interrupt served as no EQ share INT */
13726 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13727 "9145 MSI-X interrupt with no EQE\n");
13728 else {
13729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13730 "9146 ISR interrupt with no EQE\n");
13731 /* Non MSI-X treated on interrupt as EQ share INT */
13732 return IRQ_NONE;
13733 }
13734 }
13735 /* Always clear and re-arm the fast-path EQ */
13736 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13737 return IRQ_HANDLED;
13738}
13739
James Smart4f774512009-05-22 14:52:35 -040013740/**
James Smart67d12732012-08-03 12:36:13 -040013741 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013742 * @irq: Interrupt number.
13743 * @dev_id: The device context pointer.
13744 *
13745 * This function is directly called from the PCI layer as an interrupt
13746 * service routine when device with SLI-4 interface spec is enabled with
13747 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13748 * ring event in the HBA. However, when the device is enabled with either
13749 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13750 * device-level interrupt handler. When the PCI slot is in error recovery
13751 * or the HBA is undergoing initialization, the interrupt handler will not
13752 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13753 * the intrrupt context. This function is called without any lock held.
13754 * It gets the hbalock to access and update SLI data structures. Note that,
13755 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13756 * equal to that of FCP CQ index.
13757 *
James Smart67d12732012-08-03 12:36:13 -040013758 * The link attention and ELS ring attention events are handled
13759 * by the worker thread. The interrupt handler signals the worker thread
13760 * and returns for these events. This function is called without any lock
13761 * held. It gets the hbalock to access and update SLI data structures.
13762 *
James Smart4f774512009-05-22 14:52:35 -040013763 * This function returns IRQ_HANDLED when interrupt is handled else it
13764 * returns IRQ_NONE.
13765 **/
13766irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013767lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013768{
13769 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013770 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013771 struct lpfc_queue *fpeq;
13772 struct lpfc_eqe *eqe;
13773 unsigned long iflag;
13774 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013775 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013776
13777 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013778 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13779 phba = hba_eq_hdl->phba;
13780 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040013781
13782 if (unlikely(!phba))
13783 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040013784 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040013785 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013786
13787 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080013788 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050013789 if (unlikely(!fpeq))
13790 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013791
James Smartba20c852012-08-03 12:36:52 -040013792 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013793 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartba20c852012-08-03 12:36:52 -040013794 lpfc_sli4_eq_clr_intr(fpeq);
13795 else {
James Smart895427b2017-02-12 13:52:30 -080013796 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013797 return IRQ_NONE;
13798 }
13799 }
13800
James Smart4f774512009-05-22 14:52:35 -040013801 /* Check device state for handling interrupt */
13802 if (unlikely(lpfc_intr_state_check(phba))) {
13803 /* Check again for link_state with lock held */
13804 spin_lock_irqsave(&phba->hbalock, iflag);
13805 if (phba->link_state < LPFC_LINK_DOWN)
13806 /* Flush, clear interrupt, and rearm the EQ */
13807 lpfc_sli4_eq_flush(phba, fpeq);
13808 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040013809 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013810 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040013811 return IRQ_NONE;
13812 }
13813
13814 /*
13815 * Process all the event on FCP fast-path EQ
13816 */
13817 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
Dick Kennedyf485c182017-09-29 17:34:34 -070013818 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
13819 if (!(++ecount % fpeq->entry_repost))
James Smart7869da12017-05-15 15:20:43 -070013820 break;
James Smartb84daac2012-08-03 12:35:13 -040013821 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040013822 }
13823
James Smartb84daac2012-08-03 12:35:13 -040013824 /* Track the max number of EQEs processed in 1 intr */
13825 if (ecount > fpeq->EQ_max_eqe)
13826 fpeq->EQ_max_eqe = ecount;
13827
James Smart4f774512009-05-22 14:52:35 -040013828 /* Always clear and re-arm the fast-path EQ */
13829 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13830
13831 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040013832 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040013833
13834 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013835 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013836 return IRQ_NONE;
13837 }
13838
James Smart4f774512009-05-22 14:52:35 -040013839 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 "0358 MSI-X interrupt with no EQE\n");
13843 else
13844 /* Non MSI-X treated on interrupt as EQ share INT */
13845 return IRQ_NONE;
13846 }
13847
James Smartba20c852012-08-03 12:36:52 -040013848 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013849 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13850
James Smart4f774512009-05-22 14:52:35 -040013851 return IRQ_HANDLED;
13852} /* lpfc_sli4_fp_intr_handler */
13853
13854/**
13855 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13856 * @irq: Interrupt number.
13857 * @dev_id: The device context pointer.
13858 *
13859 * This function is the device-level interrupt handler to device with SLI-4
13860 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13861 * interrupt mode is enabled and there is an event in the HBA which requires
13862 * driver attention. This function invokes the slow-path interrupt attention
13863 * handling function and fast-path interrupt attention handling function in
13864 * turn to process the relevant HBA attention events. This function is called
13865 * without any lock held. It gets the hbalock to access and update SLI data
13866 * structures.
13867 *
13868 * This function returns IRQ_HANDLED when interrupt is handled, else it
13869 * returns IRQ_NONE.
13870 **/
13871irqreturn_t
13872lpfc_sli4_intr_handler(int irq, void *dev_id)
13873{
13874 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040013875 irqreturn_t hba_irq_rc;
13876 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080013877 int qidx;
James Smart4f774512009-05-22 14:52:35 -040013878
13879 /* Get the driver's phba structure from the dev_id */
13880 phba = (struct lpfc_hba *)dev_id;
13881
13882 if (unlikely(!phba))
13883 return IRQ_NONE;
13884
13885 /*
James Smart4f774512009-05-22 14:52:35 -040013886 * Invoke fast-path host attention interrupt handling as appropriate.
13887 */
James Smart895427b2017-02-12 13:52:30 -080013888 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040013889 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013890 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013891 if (hba_irq_rc == IRQ_HANDLED)
13892 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040013893 }
13894
James Smart1ba981f2014-02-20 09:56:45 -050013895 if (phba->cfg_fof) {
13896 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013897 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050013898 if (hba_irq_rc == IRQ_HANDLED)
13899 hba_handled |= true;
13900 }
13901
James Smart67d12732012-08-03 12:36:13 -040013902 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013903} /* lpfc_sli4_intr_handler */
13904
13905/**
13906 * lpfc_sli4_queue_free - free a queue structure and associated memory
13907 * @queue: The queue structure to free.
13908 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040013909 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040013910 * the host resident queue. This function must be called after destroying the
13911 * queue on the HBA.
13912 **/
13913void
13914lpfc_sli4_queue_free(struct lpfc_queue *queue)
13915{
13916 struct lpfc_dmabuf *dmabuf;
13917
13918 if (!queue)
13919 return;
13920
13921 while (!list_empty(&queue->page_list)) {
13922 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13923 list);
James Smart81b96ed2017-11-20 16:00:29 -080013924 dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
James Smart4f774512009-05-22 14:52:35 -040013925 dmabuf->virt, dmabuf->phys);
13926 kfree(dmabuf);
13927 }
James Smart895427b2017-02-12 13:52:30 -080013928 if (queue->rqbp) {
13929 lpfc_free_rq_buffer(queue->phba, queue);
13930 kfree(queue->rqbp);
13931 }
James Smartd1f525a2017-04-21 16:04:55 -070013932
13933 if (!list_empty(&queue->wq_list))
13934 list_del(&queue->wq_list);
13935
James Smart4f774512009-05-22 14:52:35 -040013936 kfree(queue);
13937 return;
13938}
13939
13940/**
13941 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13942 * @phba: The HBA that this queue is being created on.
James Smart81b96ed2017-11-20 16:00:29 -080013943 * @page_size: The size of a queue page
James Smart4f774512009-05-22 14:52:35 -040013944 * @entry_size: The size of each queue entry for this queue.
13945 * @entry count: The number of entries that this queue will handle.
13946 *
13947 * This function allocates a queue structure and the DMAable memory used for
13948 * the host resident queue. This function must be called before creating the
13949 * queue on the HBA.
13950 **/
13951struct lpfc_queue *
James Smart81b96ed2017-11-20 16:00:29 -080013952lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
13953 uint32_t entry_size, uint32_t entry_count)
James Smart4f774512009-05-22 14:52:35 -040013954{
13955 struct lpfc_queue *queue;
13956 struct lpfc_dmabuf *dmabuf;
13957 int x, total_qe_count;
13958 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040013959 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040013960
James Smartcb5172e2010-03-15 11:25:07 -040013961 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080013962 hw_page_size = page_size;
James Smartcb5172e2010-03-15 11:25:07 -040013963
James Smart4f774512009-05-22 14:52:35 -040013964 queue = kzalloc(sizeof(struct lpfc_queue) +
13965 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13966 if (!queue)
13967 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040013968 queue->page_count = (ALIGN(entry_size * entry_count,
13969 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080013970
13971 /* If needed, Adjust page count to match the max the adapter supports */
13972 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13973 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13974
James Smart4f774512009-05-22 14:52:35 -040013975 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080013976 INIT_LIST_HEAD(&queue->wq_list);
James Smart6e8e1c12018-01-30 15:58:49 -080013977 INIT_LIST_HEAD(&queue->wqfull_list);
James Smart4f774512009-05-22 14:52:35 -040013978 INIT_LIST_HEAD(&queue->page_list);
13979 INIT_LIST_HEAD(&queue->child_list);
James Smart81b96ed2017-11-20 16:00:29 -080013980
13981 /* Set queue parameters now. If the system cannot provide memory
13982 * resources, the free routine needs to know what was allocated.
13983 */
13984 queue->entry_size = entry_size;
13985 queue->entry_count = entry_count;
13986 queue->page_size = hw_page_size;
13987 queue->phba = phba;
13988
James Smart4f774512009-05-22 14:52:35 -040013989 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13990 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13991 if (!dmabuf)
13992 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040013993 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13994 hw_page_size, &dmabuf->phys,
13995 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013996 if (!dmabuf->virt) {
13997 kfree(dmabuf);
13998 goto out_fail;
13999 }
14000 dmabuf->buffer_tag = x;
14001 list_add_tail(&dmabuf->list, &queue->page_list);
14002 /* initialize queue's entry array */
14003 dma_pointer = dmabuf->virt;
14004 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040014005 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040014006 total_qe_count++, dma_pointer += entry_size) {
14007 queue->qe[total_qe_count].address = dma_pointer;
14008 }
14009 }
Dick Kennedyf485c182017-09-29 17:34:34 -070014010 INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
14011 INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
James Smart4f774512009-05-22 14:52:35 -040014012
James Smart64eb4dc2017-05-15 15:20:49 -070014013 /* entry_repost will be set during q creation */
14014
James Smart4f774512009-05-22 14:52:35 -040014015 return queue;
14016out_fail:
14017 lpfc_sli4_queue_free(queue);
14018 return NULL;
14019}
14020
14021/**
James Smart962bc512013-01-03 15:44:00 -050014022 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
14023 * @phba: HBA structure that indicates port to create a queue on.
14024 * @pci_barset: PCI BAR set flag.
14025 *
14026 * This function shall perform iomap of the specified PCI BAR address to host
14027 * memory address if not already done so and return it. The returned host
14028 * memory address can be NULL.
14029 */
14030static void __iomem *
14031lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
14032{
James Smart962bc512013-01-03 15:44:00 -050014033 if (!phba->pcidev)
14034 return NULL;
James Smart962bc512013-01-03 15:44:00 -050014035
14036 switch (pci_barset) {
14037 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050014038 return phba->pci_bar0_memmap_p;
14039 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050014040 return phba->pci_bar2_memmap_p;
14041 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050014042 return phba->pci_bar4_memmap_p;
14043 default:
14044 break;
14045 }
14046 return NULL;
14047}
14048
14049/**
James Smart895427b2017-02-12 13:52:30 -080014050 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040014051 * @phba: HBA structure that indicates port to create a queue on.
14052 * @startq: The starting FCP EQ to modify
14053 *
14054 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
James Smart43140ca2017-03-04 09:30:34 -080014055 * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
14056 * updated in one mailbox command.
James Smart173edbb2012-06-12 13:54:50 -040014057 *
14058 * The @phba struct is used to send mailbox command to HBA. The @startq
14059 * is used to get the starting FCP EQ to change.
14060 * This function is asynchronous and will wait for the mailbox
14061 * command to finish before continuing.
14062 *
14063 * On success this function will return a zero. If unable to allocate enough
14064 * memory this function will return -ENOMEM. If the queue create mailbox command
14065 * fails this function will return -ENXIO.
14066 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014067int
James Smart0cf07f842017-06-01 21:07:10 -070014068lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
14069 uint32_t numq, uint32_t imax)
James Smart173edbb2012-06-12 13:54:50 -040014070{
14071 struct lpfc_mbx_modify_eq_delay *eq_delay;
14072 LPFC_MBOXQ_t *mbox;
14073 struct lpfc_queue *eq;
14074 int cnt, rc, length, status = 0;
14075 uint32_t shdr_status, shdr_add_status;
James Smart0cf07f842017-06-01 21:07:10 -070014076 uint32_t result, val;
James Smart895427b2017-02-12 13:52:30 -080014077 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040014078 union lpfc_sli4_cfg_shdr *shdr;
14079 uint16_t dmult;
14080
James Smart895427b2017-02-12 13:52:30 -080014081 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040014082 return 0;
14083
14084 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14085 if (!mbox)
14086 return -ENOMEM;
14087 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
14088 sizeof(struct lpfc_sli4_cfg_mhdr));
14089 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14090 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
14091 length, LPFC_SLI4_MBX_EMBED);
14092 eq_delay = &mbox->u.mqe.un.eq_delay;
14093
14094 /* Calculate delay multiper from maximum interrupt per second */
James Smart0cf07f842017-06-01 21:07:10 -070014095 result = imax / phba->io_channel_irqs;
James Smart895427b2017-02-12 13:52:30 -080014096 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040014097 dmult = 0;
14098 else
14099 dmult = LPFC_DMULT_CONST/result - 1;
James Smart0cf07f842017-06-01 21:07:10 -070014100 if (dmult > LPFC_DMULT_MAX)
14101 dmult = LPFC_DMULT_MAX;
James Smart173edbb2012-06-12 13:54:50 -040014102
14103 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080014104 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
14105 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040014106 if (!eq)
14107 continue;
James Smart0cf07f842017-06-01 21:07:10 -070014108 eq->q_mode = imax;
James Smart173edbb2012-06-12 13:54:50 -040014109 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
14110 eq_delay->u.request.eq[cnt].phase = 0;
14111 eq_delay->u.request.eq[cnt].delay_multi = dmult;
14112 cnt++;
James Smart0cf07f842017-06-01 21:07:10 -070014113
14114 /* q_mode is only used for auto_imax */
14115 if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
14116 /* Use EQ Delay Register method for q_mode */
14117
14118 /* Convert for EQ Delay register */
14119 val = phba->cfg_fcp_imax;
14120 if (val) {
14121 /* First, interrupts per sec per EQ */
14122 val = phba->cfg_fcp_imax /
14123 phba->io_channel_irqs;
14124
14125 /* us delay between each interrupt */
14126 val = LPFC_SEC_TO_USEC / val;
14127 }
14128 eq->q_mode = val;
14129 } else {
14130 eq->q_mode = imax;
14131 }
14132
14133 if (cnt >= numq)
James Smart173edbb2012-06-12 13:54:50 -040014134 break;
14135 }
14136 eq_delay->u.request.num_eq = cnt;
14137
14138 mbox->vport = phba->pport;
14139 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14140 mbox->context1 = NULL;
14141 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14142 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
14143 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14144 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14145 if (shdr_status || shdr_add_status || rc) {
14146 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14147 "2512 MODIFY_EQ_DELAY mailbox failed with "
14148 "status x%x add_status x%x, mbx status x%x\n",
14149 shdr_status, shdr_add_status, rc);
14150 status = -ENXIO;
14151 }
14152 mempool_free(mbox, phba->mbox_mem_pool);
14153 return status;
14154}
14155
14156/**
James Smart4f774512009-05-22 14:52:35 -040014157 * lpfc_eq_create - Create an Event Queue on the HBA
14158 * @phba: HBA structure that indicates port to create a queue on.
14159 * @eq: The queue structure to use to create the event queue.
14160 * @imax: The maximum interrupt per second limit.
14161 *
14162 * This function creates an event queue, as detailed in @eq, on a port,
14163 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
14164 *
14165 * The @phba struct is used to send mailbox command to HBA. The @eq struct
14166 * is used to get the entry count and entry size that are necessary to
14167 * determine the number of pages to allocate and use for this queue. This
14168 * function will send the EQ_CREATE mailbox command to the HBA to setup the
14169 * event queue. This function is asynchronous and will wait for the mailbox
14170 * command to finish before continuing.
14171 *
14172 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014173 * memory this function will return -ENOMEM. If the queue create mailbox command
14174 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014175 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014176int
James Smartee020062012-09-29 11:28:52 -040014177lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040014178{
14179 struct lpfc_mbx_eq_create *eq_create;
14180 LPFC_MBOXQ_t *mbox;
14181 int rc, length, status = 0;
14182 struct lpfc_dmabuf *dmabuf;
14183 uint32_t shdr_status, shdr_add_status;
14184 union lpfc_sli4_cfg_shdr *shdr;
14185 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040014186 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14187
James Smart2e90f4b2011-12-13 13:22:37 -050014188 /* sanity check on queue memory */
14189 if (!eq)
14190 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014191 if (!phba->sli4_hba.pc_sli4_params.supported)
14192 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014193
14194 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14195 if (!mbox)
14196 return -ENOMEM;
14197 length = (sizeof(struct lpfc_mbx_eq_create) -
14198 sizeof(struct lpfc_sli4_cfg_mhdr));
14199 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14200 LPFC_MBOX_OPCODE_EQ_CREATE,
14201 length, LPFC_SLI4_MBX_EMBED);
14202 eq_create = &mbox->u.mqe.un.eq_create;
14203 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
14204 eq->page_count);
14205 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
14206 LPFC_EQE_SIZE);
14207 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040014208 /* don't setup delay multiplier using EQ_CREATE */
14209 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040014210 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
14211 dmult);
14212 switch (eq->entry_count) {
14213 default:
14214 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14215 "0360 Unsupported EQ count. (%d)\n",
14216 eq->entry_count);
14217 if (eq->entry_count < 256)
14218 return -EINVAL;
14219 /* otherwise default to smallest count (drop through) */
14220 case 256:
14221 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14222 LPFC_EQ_CNT_256);
14223 break;
14224 case 512:
14225 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14226 LPFC_EQ_CNT_512);
14227 break;
14228 case 1024:
14229 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14230 LPFC_EQ_CNT_1024);
14231 break;
14232 case 2048:
14233 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14234 LPFC_EQ_CNT_2048);
14235 break;
14236 case 4096:
14237 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
14238 LPFC_EQ_CNT_4096);
14239 break;
14240 }
14241 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014242 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014243 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14244 putPaddrLow(dmabuf->phys);
14245 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14246 putPaddrHigh(dmabuf->phys);
14247 }
14248 mbox->vport = phba->pport;
14249 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14250 mbox->context1 = NULL;
14251 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14252 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
14253 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14254 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14255 if (shdr_status || shdr_add_status || rc) {
14256 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14257 "2500 EQ_CREATE mailbox failed with "
14258 "status x%x add_status x%x, mbx status x%x\n",
14259 shdr_status, shdr_add_status, rc);
14260 status = -ENXIO;
14261 }
14262 eq->type = LPFC_EQ;
14263 eq->subtype = LPFC_NONE;
14264 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
14265 if (eq->queue_id == 0xFFFF)
14266 status = -ENXIO;
14267 eq->host_index = 0;
14268 eq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014269 eq->entry_repost = LPFC_EQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014270
James Smart8fa38512009-07-19 10:01:03 -040014271 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014272 return status;
14273}
14274
14275/**
14276 * lpfc_cq_create - Create a Completion Queue on the HBA
14277 * @phba: HBA structure that indicates port to create a queue on.
14278 * @cq: The queue structure to use to create the completion queue.
14279 * @eq: The event queue to bind this completion queue to.
14280 *
14281 * This function creates a completion queue, as detailed in @wq, on a port,
14282 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
14283 *
14284 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14285 * is used to get the entry count and entry size that are necessary to
14286 * determine the number of pages to allocate and use for this queue. The @eq
14287 * is used to indicate which event queue to bind this completion queue to. This
14288 * function will send the CQ_CREATE mailbox command to the HBA to setup the
14289 * completion queue. This function is asynchronous and will wait for the mailbox
14290 * command to finish before continuing.
14291 *
14292 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014293 * memory this function will return -ENOMEM. If the queue create mailbox command
14294 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014295 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014296int
James Smart4f774512009-05-22 14:52:35 -040014297lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
14298 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
14299{
14300 struct lpfc_mbx_cq_create *cq_create;
14301 struct lpfc_dmabuf *dmabuf;
14302 LPFC_MBOXQ_t *mbox;
14303 int rc, length, status = 0;
14304 uint32_t shdr_status, shdr_add_status;
14305 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014306 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14307
James Smart2e90f4b2011-12-13 13:22:37 -050014308 /* sanity check on queue memory */
14309 if (!cq || !eq)
14310 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014311 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014312 hw_page_size = cq->page_size;
James Smart49198b32010-04-06 15:04:33 -040014313
James Smart4f774512009-05-22 14:52:35 -040014314 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14315 if (!mbox)
14316 return -ENOMEM;
14317 length = (sizeof(struct lpfc_mbx_cq_create) -
14318 sizeof(struct lpfc_sli4_cfg_mhdr));
14319 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14320 LPFC_MBOX_OPCODE_CQ_CREATE,
14321 length, LPFC_SLI4_MBX_EMBED);
14322 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014323 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014324 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
14325 cq->page_count);
14326 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
14327 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014328 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14329 phba->sli4_hba.pc_sli4_params.cqv);
14330 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smart81b96ed2017-11-20 16:00:29 -080014331 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
14332 (cq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014333 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
14334 eq->queue_id);
14335 } else {
14336 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
14337 eq->queue_id);
14338 }
James Smart4f774512009-05-22 14:52:35 -040014339 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014340 case 2048:
14341 case 4096:
14342 if (phba->sli4_hba.pc_sli4_params.cqv ==
14343 LPFC_Q_CREATE_VERSION_2) {
14344 cq_create->u.request.context.lpfc_cq_context_count =
14345 cq->entry_count;
14346 bf_set(lpfc_cq_context_count,
14347 &cq_create->u.request.context,
14348 LPFC_CQ_CNT_WORD7);
14349 break;
14350 }
14351 /* Fall Thru */
James Smart4f774512009-05-22 14:52:35 -040014352 default:
14353 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080014354 "0361 Unsupported CQ count: "
James Smart64eb4dc2017-05-15 15:20:49 -070014355 "entry cnt %d sz %d pg cnt %d\n",
James Smart2ea259e2017-02-12 13:52:27 -080014356 cq->entry_count, cq->entry_size,
James Smart64eb4dc2017-05-15 15:20:49 -070014357 cq->page_count);
James Smart4f4c1862012-06-12 13:54:02 -040014358 if (cq->entry_count < 256) {
14359 status = -EINVAL;
14360 goto out;
14361 }
James Smart4f774512009-05-22 14:52:35 -040014362 /* otherwise default to smallest count (drop through) */
14363 case 256:
14364 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14365 LPFC_CQ_CNT_256);
14366 break;
14367 case 512:
14368 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14369 LPFC_CQ_CNT_512);
14370 break;
14371 case 1024:
14372 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
14373 LPFC_CQ_CNT_1024);
14374 break;
14375 }
14376 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart81b96ed2017-11-20 16:00:29 -080014377 memset(dmabuf->virt, 0, cq->page_size);
James Smart4f774512009-05-22 14:52:35 -040014378 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14379 putPaddrLow(dmabuf->phys);
14380 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14381 putPaddrHigh(dmabuf->phys);
14382 }
14383 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14384
14385 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014386 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14387 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14388 if (shdr_status || shdr_add_status || rc) {
14389 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14390 "2501 CQ_CREATE mailbox failed with "
14391 "status x%x add_status x%x, mbx status x%x\n",
14392 shdr_status, shdr_add_status, rc);
14393 status = -ENXIO;
14394 goto out;
14395 }
14396 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
14397 if (cq->queue_id == 0xFFFF) {
14398 status = -ENXIO;
14399 goto out;
14400 }
14401 /* link the cq onto the parent eq child list */
14402 list_add_tail(&cq->list, &eq->child_list);
14403 /* Set up completion queue's type and subtype */
14404 cq->type = type;
14405 cq->subtype = subtype;
14406 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050014407 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014408 cq->host_index = 0;
14409 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014410 cq->entry_repost = LPFC_CQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040014411
James Smart8fa38512009-07-19 10:01:03 -040014412out:
14413 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014414 return status;
14415}
14416
14417/**
James Smart2d7dbc42017-02-12 13:52:35 -080014418 * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
14419 * @phba: HBA structure that indicates port to create a queue on.
14420 * @cqp: The queue structure array to use to create the completion queues.
14421 * @eqp: The event queue array to bind these completion queues to.
14422 *
14423 * This function creates a set of completion queue, s to support MRQ
14424 * as detailed in @cqp, on a port,
14425 * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
14426 *
14427 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14428 * is used to get the entry count and entry size that are necessary to
14429 * determine the number of pages to allocate and use for this queue. The @eq
14430 * is used to indicate which event queue to bind this completion queue to. This
14431 * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
14432 * completion queue. This function is asynchronous and will wait for the mailbox
14433 * command to finish before continuing.
14434 *
14435 * On success this function will return a zero. If unable to allocate enough
14436 * memory this function will return -ENOMEM. If the queue create mailbox command
14437 * fails this function will return -ENXIO.
14438 **/
14439int
14440lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
14441 struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
14442{
14443 struct lpfc_queue *cq;
14444 struct lpfc_queue *eq;
14445 struct lpfc_mbx_cq_create_set *cq_set;
14446 struct lpfc_dmabuf *dmabuf;
14447 LPFC_MBOXQ_t *mbox;
14448 int rc, length, alloclen, status = 0;
14449 int cnt, idx, numcq, page_idx = 0;
14450 uint32_t shdr_status, shdr_add_status;
14451 union lpfc_sli4_cfg_shdr *shdr;
14452 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
14453
14454 /* sanity check on queue memory */
14455 numcq = phba->cfg_nvmet_mrq;
14456 if (!cqp || !eqp || !numcq)
14457 return -ENODEV;
James Smart2d7dbc42017-02-12 13:52:35 -080014458
14459 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14460 if (!mbox)
14461 return -ENOMEM;
14462
14463 length = sizeof(struct lpfc_mbx_cq_create_set);
14464 length += ((numcq * cqp[0]->page_count) *
14465 sizeof(struct dma_address));
14466 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14467 LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
14468 LPFC_SLI4_MBX_NEMBED);
14469 if (alloclen < length) {
14470 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14471 "3098 Allocated DMA memory size (%d) is "
14472 "less than the requested DMA memory size "
14473 "(%d)\n", alloclen, length);
14474 status = -ENOMEM;
14475 goto out;
14476 }
14477 cq_set = mbox->sge_array->addr[0];
14478 shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
14479 bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
14480
14481 for (idx = 0; idx < numcq; idx++) {
14482 cq = cqp[idx];
14483 eq = eqp[idx];
14484 if (!cq || !eq) {
14485 status = -ENOMEM;
14486 goto out;
14487 }
James Smart81b96ed2017-11-20 16:00:29 -080014488 if (!phba->sli4_hba.pc_sli4_params.supported)
14489 hw_page_size = cq->page_size;
James Smart2d7dbc42017-02-12 13:52:35 -080014490
14491 switch (idx) {
14492 case 0:
14493 bf_set(lpfc_mbx_cq_create_set_page_size,
14494 &cq_set->u.request,
14495 (hw_page_size / SLI4_PAGE_SIZE));
14496 bf_set(lpfc_mbx_cq_create_set_num_pages,
14497 &cq_set->u.request, cq->page_count);
14498 bf_set(lpfc_mbx_cq_create_set_evt,
14499 &cq_set->u.request, 1);
14500 bf_set(lpfc_mbx_cq_create_set_valid,
14501 &cq_set->u.request, 1);
14502 bf_set(lpfc_mbx_cq_create_set_cqe_size,
14503 &cq_set->u.request, 0);
14504 bf_set(lpfc_mbx_cq_create_set_num_cq,
14505 &cq_set->u.request, numcq);
14506 switch (cq->entry_count) {
James Smart81b96ed2017-11-20 16:00:29 -080014507 case 2048:
14508 case 4096:
14509 if (phba->sli4_hba.pc_sli4_params.cqv ==
14510 LPFC_Q_CREATE_VERSION_2) {
14511 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14512 &cq_set->u.request,
14513 cq->entry_count);
14514 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14515 &cq_set->u.request,
14516 LPFC_CQ_CNT_WORD7);
14517 break;
14518 }
14519 /* Fall Thru */
James Smart2d7dbc42017-02-12 13:52:35 -080014520 default:
14521 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14522 "3118 Bad CQ count. (%d)\n",
14523 cq->entry_count);
14524 if (cq->entry_count < 256) {
14525 status = -EINVAL;
14526 goto out;
14527 }
14528 /* otherwise default to smallest (drop thru) */
14529 case 256:
14530 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14531 &cq_set->u.request, LPFC_CQ_CNT_256);
14532 break;
14533 case 512:
14534 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14535 &cq_set->u.request, LPFC_CQ_CNT_512);
14536 break;
14537 case 1024:
14538 bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
14539 &cq_set->u.request, LPFC_CQ_CNT_1024);
14540 break;
14541 }
14542 bf_set(lpfc_mbx_cq_create_set_eq_id0,
14543 &cq_set->u.request, eq->queue_id);
14544 break;
14545 case 1:
14546 bf_set(lpfc_mbx_cq_create_set_eq_id1,
14547 &cq_set->u.request, eq->queue_id);
14548 break;
14549 case 2:
14550 bf_set(lpfc_mbx_cq_create_set_eq_id2,
14551 &cq_set->u.request, eq->queue_id);
14552 break;
14553 case 3:
14554 bf_set(lpfc_mbx_cq_create_set_eq_id3,
14555 &cq_set->u.request, eq->queue_id);
14556 break;
14557 case 4:
14558 bf_set(lpfc_mbx_cq_create_set_eq_id4,
14559 &cq_set->u.request, eq->queue_id);
14560 break;
14561 case 5:
14562 bf_set(lpfc_mbx_cq_create_set_eq_id5,
14563 &cq_set->u.request, eq->queue_id);
14564 break;
14565 case 6:
14566 bf_set(lpfc_mbx_cq_create_set_eq_id6,
14567 &cq_set->u.request, eq->queue_id);
14568 break;
14569 case 7:
14570 bf_set(lpfc_mbx_cq_create_set_eq_id7,
14571 &cq_set->u.request, eq->queue_id);
14572 break;
14573 case 8:
14574 bf_set(lpfc_mbx_cq_create_set_eq_id8,
14575 &cq_set->u.request, eq->queue_id);
14576 break;
14577 case 9:
14578 bf_set(lpfc_mbx_cq_create_set_eq_id9,
14579 &cq_set->u.request, eq->queue_id);
14580 break;
14581 case 10:
14582 bf_set(lpfc_mbx_cq_create_set_eq_id10,
14583 &cq_set->u.request, eq->queue_id);
14584 break;
14585 case 11:
14586 bf_set(lpfc_mbx_cq_create_set_eq_id11,
14587 &cq_set->u.request, eq->queue_id);
14588 break;
14589 case 12:
14590 bf_set(lpfc_mbx_cq_create_set_eq_id12,
14591 &cq_set->u.request, eq->queue_id);
14592 break;
14593 case 13:
14594 bf_set(lpfc_mbx_cq_create_set_eq_id13,
14595 &cq_set->u.request, eq->queue_id);
14596 break;
14597 case 14:
14598 bf_set(lpfc_mbx_cq_create_set_eq_id14,
14599 &cq_set->u.request, eq->queue_id);
14600 break;
14601 case 15:
14602 bf_set(lpfc_mbx_cq_create_set_eq_id15,
14603 &cq_set->u.request, eq->queue_id);
14604 break;
14605 }
14606
14607 /* link the cq onto the parent eq child list */
14608 list_add_tail(&cq->list, &eq->child_list);
14609 /* Set up completion queue's type and subtype */
14610 cq->type = type;
14611 cq->subtype = subtype;
14612 cq->assoc_qid = eq->queue_id;
14613 cq->host_index = 0;
14614 cq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014615 cq->entry_repost = LPFC_CQ_REPOST;
James Smart81b96ed2017-11-20 16:00:29 -080014616 cq->chann = idx;
James Smart2d7dbc42017-02-12 13:52:35 -080014617
14618 rc = 0;
14619 list_for_each_entry(dmabuf, &cq->page_list, list) {
14620 memset(dmabuf->virt, 0, hw_page_size);
14621 cnt = page_idx + dmabuf->buffer_tag;
14622 cq_set->u.request.page[cnt].addr_lo =
14623 putPaddrLow(dmabuf->phys);
14624 cq_set->u.request.page[cnt].addr_hi =
14625 putPaddrHigh(dmabuf->phys);
14626 rc++;
14627 }
14628 page_idx += rc;
14629 }
14630
14631 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14632
14633 /* The IOCTL status is embedded in the mailbox subheader. */
14634 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14635 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14636 if (shdr_status || shdr_add_status || rc) {
14637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14638 "3119 CQ_CREATE_SET mailbox failed with "
14639 "status x%x add_status x%x, mbx status x%x\n",
14640 shdr_status, shdr_add_status, rc);
14641 status = -ENXIO;
14642 goto out;
14643 }
14644 rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
14645 if (rc == 0xFFFF) {
14646 status = -ENXIO;
14647 goto out;
14648 }
14649
14650 for (idx = 0; idx < numcq; idx++) {
14651 cq = cqp[idx];
14652 cq->queue_id = rc + idx;
14653 }
14654
14655out:
14656 lpfc_sli4_mbox_cmd_free(phba, mbox);
14657 return status;
14658}
14659
14660/**
James Smartb19a0612010-04-06 14:48:51 -040014661 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040014662 * @phba: HBA structure that indicates port to create a queue on.
14663 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040014664 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
14665 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040014666 *
James Smartb19a0612010-04-06 14:48:51 -040014667 * This function provides failback (fb) functionality when the
14668 * mq_create_ext fails on older FW generations. It's purpose is identical
14669 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040014670 *
James Smartb19a0612010-04-06 14:48:51 -040014671 * This routine cannot fail as all attributes were previously accessed and
14672 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040014673 **/
James Smartb19a0612010-04-06 14:48:51 -040014674static void
14675lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
14676 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040014677{
14678 struct lpfc_mbx_mq_create *mq_create;
14679 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040014680 int length;
James Smart04c68492009-05-22 14:52:52 -040014681
James Smart04c68492009-05-22 14:52:52 -040014682 length = (sizeof(struct lpfc_mbx_mq_create) -
14683 sizeof(struct lpfc_sli4_cfg_mhdr));
14684 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14685 LPFC_MBOX_OPCODE_MQ_CREATE,
14686 length, LPFC_SLI4_MBX_EMBED);
14687 mq_create = &mbox->u.mqe.un.mq_create;
14688 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040014689 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040014690 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040014691 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040014692 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
14693 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040014694 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014695 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14696 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040014697 break;
14698 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014699 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14700 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040014701 break;
14702 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014703 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14704 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040014705 break;
14706 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014707 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
14708 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040014709 break;
14710 }
14711 list_for_each_entry(dmabuf, &mq->page_list, list) {
14712 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040014713 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040014714 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040014715 putPaddrHigh(dmabuf->phys);
14716 }
14717}
14718
14719/**
14720 * lpfc_mq_create - Create a mailbox Queue on the HBA
14721 * @phba: HBA structure that indicates port to create a queue on.
14722 * @mq: The queue structure to use to create the mailbox queue.
14723 * @cq: The completion queue to associate with this cq.
14724 * @subtype: The queue's subtype.
14725 *
14726 * This function creates a mailbox queue, as detailed in @mq, on a port,
14727 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
14728 *
14729 * The @phba struct is used to send mailbox command to HBA. The @cq struct
14730 * is used to get the entry count and entry size that are necessary to
14731 * determine the number of pages to allocate and use for this queue. This
14732 * function will send the MQ_CREATE mailbox command to the HBA to setup the
14733 * mailbox queue. This function is asynchronous and will wait for the mailbox
14734 * command to finish before continuing.
14735 *
14736 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014737 * memory this function will return -ENOMEM. If the queue create mailbox command
14738 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040014739 **/
14740int32_t
14741lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
14742 struct lpfc_queue *cq, uint32_t subtype)
14743{
14744 struct lpfc_mbx_mq_create *mq_create;
14745 struct lpfc_mbx_mq_create_ext *mq_create_ext;
14746 struct lpfc_dmabuf *dmabuf;
14747 LPFC_MBOXQ_t *mbox;
14748 int rc, length, status = 0;
14749 uint32_t shdr_status, shdr_add_status;
14750 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014751 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040014752
James Smart2e90f4b2011-12-13 13:22:37 -050014753 /* sanity check on queue memory */
14754 if (!mq || !cq)
14755 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014756 if (!phba->sli4_hba.pc_sli4_params.supported)
14757 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040014758
14759 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14760 if (!mbox)
14761 return -ENOMEM;
14762 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
14763 sizeof(struct lpfc_sli4_cfg_mhdr));
14764 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14765 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
14766 length, LPFC_SLI4_MBX_EMBED);
14767
14768 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050014769 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050014770 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14771 &mq_create_ext->u.request, mq->page_count);
14772 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14773 &mq_create_ext->u.request, 1);
14774 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040014775 &mq_create_ext->u.request, 1);
14776 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14777 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050014778 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14779 &mq_create_ext->u.request, 1);
14780 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14781 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040014782 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014783 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14784 phba->sli4_hba.pc_sli4_params.mqv);
14785 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14786 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14787 cq->queue_id);
14788 else
14789 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14790 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040014791 switch (mq->entry_count) {
14792 default:
14793 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14794 "0362 Unsupported MQ count. (%d)\n",
14795 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014796 if (mq->entry_count < 16) {
14797 status = -EINVAL;
14798 goto out;
14799 }
James Smartb19a0612010-04-06 14:48:51 -040014800 /* otherwise default to smallest count (drop through) */
14801 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014802 bf_set(lpfc_mq_context_ring_size,
14803 &mq_create_ext->u.request.context,
14804 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040014805 break;
14806 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014807 bf_set(lpfc_mq_context_ring_size,
14808 &mq_create_ext->u.request.context,
14809 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040014810 break;
14811 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014812 bf_set(lpfc_mq_context_ring_size,
14813 &mq_create_ext->u.request.context,
14814 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040014815 break;
14816 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014817 bf_set(lpfc_mq_context_ring_size,
14818 &mq_create_ext->u.request.context,
14819 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040014820 break;
14821 }
14822 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014823 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040014824 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14825 putPaddrLow(dmabuf->phys);
14826 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040014827 putPaddrHigh(dmabuf->phys);
14828 }
14829 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040014830 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14831 &mq_create_ext->u.response);
14832 if (rc != MBX_SUCCESS) {
14833 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14834 "2795 MQ_CREATE_EXT failed with "
14835 "status x%x. Failback to MQ_CREATE.\n",
14836 rc);
14837 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14838 mq_create = &mbox->u.mqe.un.mq_create;
14839 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14840 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14841 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14842 &mq_create->u.response);
14843 }
14844
James Smart04c68492009-05-22 14:52:52 -040014845 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040014846 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14847 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14848 if (shdr_status || shdr_add_status || rc) {
14849 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14850 "2502 MQ_CREATE mailbox failed with "
14851 "status x%x add_status x%x, mbx status x%x\n",
14852 shdr_status, shdr_add_status, rc);
14853 status = -ENXIO;
14854 goto out;
14855 }
James Smart04c68492009-05-22 14:52:52 -040014856 if (mq->queue_id == 0xFFFF) {
14857 status = -ENXIO;
14858 goto out;
14859 }
14860 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050014861 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040014862 mq->subtype = subtype;
14863 mq->host_index = 0;
14864 mq->hba_index = 0;
James Smart64eb4dc2017-05-15 15:20:49 -070014865 mq->entry_repost = LPFC_MQ_REPOST;
James Smart04c68492009-05-22 14:52:52 -040014866
14867 /* link the mq onto the parent cq child list */
14868 list_add_tail(&mq->list, &cq->child_list);
14869out:
James Smart8fa38512009-07-19 10:01:03 -040014870 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014871 return status;
14872}
14873
14874/**
James Smart4f774512009-05-22 14:52:35 -040014875 * lpfc_wq_create - Create a Work Queue on the HBA
14876 * @phba: HBA structure that indicates port to create a queue on.
14877 * @wq: The queue structure to use to create the work queue.
14878 * @cq: The completion queue to bind this work queue to.
14879 * @subtype: The subtype of the work queue indicating its functionality.
14880 *
14881 * This function creates a work queue, as detailed in @wq, on a port, described
14882 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14883 *
14884 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14885 * is used to get the entry count and entry size that are necessary to
14886 * determine the number of pages to allocate and use for this queue. The @cq
14887 * is used to indicate which completion queue to bind this work queue to. This
14888 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14889 * work queue. This function is asynchronous and will wait for the mailbox
14890 * command to finish before continuing.
14891 *
14892 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014893 * memory this function will return -ENOMEM. If the queue create mailbox command
14894 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014895 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014896int
James Smart4f774512009-05-22 14:52:35 -040014897lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14898 struct lpfc_queue *cq, uint32_t subtype)
14899{
14900 struct lpfc_mbx_wq_create *wq_create;
14901 struct lpfc_dmabuf *dmabuf;
14902 LPFC_MBOXQ_t *mbox;
14903 int rc, length, status = 0;
14904 uint32_t shdr_status, shdr_add_status;
14905 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014906 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050014907 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050014908 void __iomem *bar_memmap_p;
14909 uint32_t db_offset;
14910 uint16_t pci_barset;
James Smart81b96ed2017-11-20 16:00:29 -080014911 uint8_t wq_create_version;
James Smart49198b32010-04-06 15:04:33 -040014912
James Smart2e90f4b2011-12-13 13:22:37 -050014913 /* sanity check on queue memory */
14914 if (!wq || !cq)
14915 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014916 if (!phba->sli4_hba.pc_sli4_params.supported)
James Smart81b96ed2017-11-20 16:00:29 -080014917 hw_page_size = wq->page_size;
James Smart4f774512009-05-22 14:52:35 -040014918
14919 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14920 if (!mbox)
14921 return -ENOMEM;
14922 length = (sizeof(struct lpfc_mbx_wq_create) -
14923 sizeof(struct lpfc_sli4_cfg_mhdr));
14924 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14925 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14926 length, LPFC_SLI4_MBX_EMBED);
14927 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014928 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014929 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14930 wq->page_count);
14931 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14932 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040014933
14934 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050014935 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14936 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050014937
James Smartc176ffa2018-01-30 15:58:46 -080014938 if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
14939 (wq->page_size > SLI4_PAGE_SIZE))
James Smart81b96ed2017-11-20 16:00:29 -080014940 wq_create_version = LPFC_Q_CREATE_VERSION_1;
14941 else
14942 wq_create_version = LPFC_Q_CREATE_VERSION_0;
14943
14944 switch (wq_create_version) {
James Smart0c651872013-07-15 18:33:23 -040014945 case LPFC_Q_CREATE_VERSION_0:
14946 switch (wq->entry_size) {
14947 default:
14948 case 64:
14949 /* Nothing to do, version 0 ONLY supports 64 byte */
14950 page = wq_create->u.request.page;
14951 break;
14952 case 128:
14953 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14954 LPFC_WQ_SZ128_SUPPORT)) {
14955 status = -ERANGE;
14956 goto out;
14957 }
14958 /* If we get here the HBA MUST also support V1 and
14959 * we MUST use it
14960 */
14961 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14962 LPFC_Q_CREATE_VERSION_1);
14963
14964 bf_set(lpfc_mbx_wq_create_wqe_count,
14965 &wq_create->u.request_1, wq->entry_count);
14966 bf_set(lpfc_mbx_wq_create_wqe_size,
14967 &wq_create->u.request_1,
14968 LPFC_WQ_WQE_SIZE_128);
14969 bf_set(lpfc_mbx_wq_create_page_size,
14970 &wq_create->u.request_1,
James Smart8ea73db2017-02-12 13:52:25 -080014971 LPFC_WQ_PAGE_SIZE_4096);
James Smart0c651872013-07-15 18:33:23 -040014972 page = wq_create->u.request_1.page;
14973 break;
14974 }
14975 break;
14976 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050014977 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14978 wq->entry_count);
James Smart3f247de2017-04-21 16:04:56 -070014979 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14980 LPFC_Q_CREATE_VERSION_1);
14981
James Smart5a6f1332011-03-11 16:05:35 -050014982 switch (wq->entry_size) {
14983 default:
14984 case 64:
14985 bf_set(lpfc_mbx_wq_create_wqe_size,
14986 &wq_create->u.request_1,
14987 LPFC_WQ_WQE_SIZE_64);
14988 break;
14989 case 128:
James Smart0c651872013-07-15 18:33:23 -040014990 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14991 LPFC_WQ_SZ128_SUPPORT)) {
14992 status = -ERANGE;
14993 goto out;
14994 }
James Smart5a6f1332011-03-11 16:05:35 -050014995 bf_set(lpfc_mbx_wq_create_wqe_size,
14996 &wq_create->u.request_1,
14997 LPFC_WQ_WQE_SIZE_128);
14998 break;
14999 }
James Smart8ea73db2017-02-12 13:52:25 -080015000 bf_set(lpfc_mbx_wq_create_page_size,
15001 &wq_create->u.request_1,
James Smart81b96ed2017-11-20 16:00:29 -080015002 (wq->page_size / SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015003 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040015004 break;
15005 default:
15006 status = -ERANGE;
15007 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050015008 }
James Smart0c651872013-07-15 18:33:23 -040015009
James Smart4f774512009-05-22 14:52:35 -040015010 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015011 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050015012 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
15013 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040015014 }
James Smart962bc512013-01-03 15:44:00 -050015015
15016 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15017 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
15018
James Smart4f774512009-05-22 14:52:35 -040015019 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15020 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015021 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15022 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15023 if (shdr_status || shdr_add_status || rc) {
15024 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15025 "2503 WQ_CREATE mailbox failed with "
15026 "status x%x add_status x%x, mbx status x%x\n",
15027 shdr_status, shdr_add_status, rc);
15028 status = -ENXIO;
15029 goto out;
15030 }
15031 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
15032 if (wq->queue_id == 0xFFFF) {
15033 status = -ENXIO;
15034 goto out;
15035 }
James Smart962bc512013-01-03 15:44:00 -050015036 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15037 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
15038 &wq_create->u.response);
15039 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
15040 (wq->db_format != LPFC_DB_RING_FORMAT)) {
15041 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15042 "3265 WQ[%d] doorbell format not "
15043 "supported: x%x\n", wq->queue_id,
15044 wq->db_format);
15045 status = -EINVAL;
15046 goto out;
15047 }
15048 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
15049 &wq_create->u.response);
15050 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15051 if (!bar_memmap_p) {
15052 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15053 "3263 WQ[%d] failed to memmap pci "
15054 "barset:x%x\n", wq->queue_id,
15055 pci_barset);
15056 status = -ENOMEM;
15057 goto out;
15058 }
15059 db_offset = wq_create->u.response.doorbell_offset;
15060 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
15061 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
15062 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15063 "3252 WQ[%d] doorbell offset not "
15064 "supported: x%x\n", wq->queue_id,
15065 db_offset);
15066 status = -EINVAL;
15067 goto out;
15068 }
15069 wq->db_regaddr = bar_memmap_p + db_offset;
15070 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015071 "3264 WQ[%d]: barset:x%x, offset:x%x, "
15072 "format:x%x\n", wq->queue_id, pci_barset,
15073 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015074 } else {
15075 wq->db_format = LPFC_DB_LIST_FORMAT;
15076 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
15077 }
James Smart895427b2017-02-12 13:52:30 -080015078 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
15079 if (wq->pring == NULL) {
15080 status = -ENOMEM;
15081 goto out;
15082 }
James Smart4f774512009-05-22 14:52:35 -040015083 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050015084 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015085 wq->subtype = subtype;
15086 wq->host_index = 0;
15087 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050015088 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040015089
15090 /* link the wq onto the parent cq child list */
15091 list_add_tail(&wq->list, &cq->child_list);
15092out:
James Smart8fa38512009-07-19 10:01:03 -040015093 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015094 return status;
15095}
15096
15097/**
15098 * lpfc_rq_create - Create a Receive Queue on the HBA
15099 * @phba: HBA structure that indicates port to create a queue on.
15100 * @hrq: The queue structure to use to create the header receive queue.
15101 * @drq: The queue structure to use to create the data receive queue.
15102 * @cq: The completion queue to bind this work queue to.
15103 *
15104 * This function creates a receive buffer queue pair , as detailed in @hrq and
15105 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15106 * to the HBA.
15107 *
15108 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15109 * struct is used to get the entry count that is necessary to determine the
15110 * number of pages to use for this queue. The @cq is used to indicate which
15111 * completion queue to bind received buffers that are posted to these queues to.
15112 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15113 * receive queue pair. This function is asynchronous and will wait for the
15114 * mailbox command to finish before continuing.
15115 *
15116 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040015117 * memory this function will return -ENOMEM. If the queue create mailbox command
15118 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015119 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015120int
James Smart4f774512009-05-22 14:52:35 -040015121lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15122 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
15123{
15124 struct lpfc_mbx_rq_create *rq_create;
15125 struct lpfc_dmabuf *dmabuf;
15126 LPFC_MBOXQ_t *mbox;
15127 int rc, length, status = 0;
15128 uint32_t shdr_status, shdr_add_status;
15129 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040015130 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050015131 void __iomem *bar_memmap_p;
15132 uint32_t db_offset;
15133 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040015134
James Smart2e90f4b2011-12-13 13:22:37 -050015135 /* sanity check on queue memory */
15136 if (!hrq || !drq || !cq)
15137 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040015138 if (!phba->sli4_hba.pc_sli4_params.supported)
15139 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040015140
15141 if (hrq->entry_count != drq->entry_count)
15142 return -EINVAL;
15143 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15144 if (!mbox)
15145 return -ENOMEM;
15146 length = (sizeof(struct lpfc_mbx_rq_create) -
15147 sizeof(struct lpfc_sli4_cfg_mhdr));
15148 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15149 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15150 length, LPFC_SLI4_MBX_EMBED);
15151 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050015152 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15153 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15154 phba->sli4_hba.pc_sli4_params.rqv);
15155 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15156 bf_set(lpfc_rq_context_rqe_count_1,
15157 &rq_create->u.request.context,
15158 hrq->entry_count);
15159 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015160 bf_set(lpfc_rq_context_rqe_size,
15161 &rq_create->u.request.context,
15162 LPFC_RQE_SIZE_8);
15163 bf_set(lpfc_rq_context_page_size,
15164 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080015165 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050015166 } else {
15167 switch (hrq->entry_count) {
15168 default:
15169 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15170 "2535 Unsupported RQ count. (%d)\n",
15171 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015172 if (hrq->entry_count < 512) {
15173 status = -EINVAL;
15174 goto out;
15175 }
James Smart5a6f1332011-03-11 16:05:35 -050015176 /* otherwise default to smallest count (drop through) */
15177 case 512:
15178 bf_set(lpfc_rq_context_rqe_count,
15179 &rq_create->u.request.context,
15180 LPFC_RQ_RING_SIZE_512);
15181 break;
15182 case 1024:
15183 bf_set(lpfc_rq_context_rqe_count,
15184 &rq_create->u.request.context,
15185 LPFC_RQ_RING_SIZE_1024);
15186 break;
15187 case 2048:
15188 bf_set(lpfc_rq_context_rqe_count,
15189 &rq_create->u.request.context,
15190 LPFC_RQ_RING_SIZE_2048);
15191 break;
15192 case 4096:
15193 bf_set(lpfc_rq_context_rqe_count,
15194 &rq_create->u.request.context,
15195 LPFC_RQ_RING_SIZE_4096);
15196 break;
15197 }
15198 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
15199 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015200 }
15201 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15202 cq->queue_id);
15203 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15204 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015205 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040015206 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040015207 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15208 putPaddrLow(dmabuf->phys);
15209 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15210 putPaddrHigh(dmabuf->phys);
15211 }
James Smart962bc512013-01-03 15:44:00 -050015212 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15213 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
15214
James Smart4f774512009-05-22 14:52:35 -040015215 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15216 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040015217 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15218 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15219 if (shdr_status || shdr_add_status || rc) {
15220 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15221 "2504 RQ_CREATE mailbox failed with "
15222 "status x%x add_status x%x, mbx status x%x\n",
15223 shdr_status, shdr_add_status, rc);
15224 status = -ENXIO;
15225 goto out;
15226 }
15227 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15228 if (hrq->queue_id == 0xFFFF) {
15229 status = -ENXIO;
15230 goto out;
15231 }
James Smart962bc512013-01-03 15:44:00 -050015232
15233 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
15234 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
15235 &rq_create->u.response);
15236 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
15237 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
15238 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15239 "3262 RQ [%d] doorbell format not "
15240 "supported: x%x\n", hrq->queue_id,
15241 hrq->db_format);
15242 status = -EINVAL;
15243 goto out;
15244 }
15245
15246 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
15247 &rq_create->u.response);
15248 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
15249 if (!bar_memmap_p) {
15250 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15251 "3269 RQ[%d] failed to memmap pci "
15252 "barset:x%x\n", hrq->queue_id,
15253 pci_barset);
15254 status = -ENOMEM;
15255 goto out;
15256 }
15257
15258 db_offset = rq_create->u.response.doorbell_offset;
15259 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
15260 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
15261 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15262 "3270 RQ[%d] doorbell offset not "
15263 "supported: x%x\n", hrq->queue_id,
15264 db_offset);
15265 status = -EINVAL;
15266 goto out;
15267 }
15268 hrq->db_regaddr = bar_memmap_p + db_offset;
15269 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040015270 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
15271 "format:x%x\n", hrq->queue_id, pci_barset,
15272 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050015273 } else {
15274 hrq->db_format = LPFC_DB_RING_FORMAT;
15275 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15276 }
James Smart4f774512009-05-22 14:52:35 -040015277 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015278 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015279 hrq->subtype = subtype;
15280 hrq->host_index = 0;
15281 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015282 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015283
15284 /* now create the data queue */
15285 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15286 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
15287 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050015288 bf_set(lpfc_mbox_hdr_version, &shdr->request,
15289 phba->sli4_hba.pc_sli4_params.rqv);
15290 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
15291 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040015292 &rq_create->u.request.context, hrq->entry_count);
James Smart3c603be2017-05-15 15:20:44 -070015293 if (subtype == LPFC_NVMET)
15294 rq_create->u.request.context.buffer_size =
15295 LPFC_NVMET_DATA_BUF_SIZE;
15296 else
15297 rq_create->u.request.context.buffer_size =
15298 LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040015299 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
15300 LPFC_RQE_SIZE_8);
15301 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
15302 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050015303 } else {
15304 switch (drq->entry_count) {
15305 default:
15306 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15307 "2536 Unsupported RQ count. (%d)\n",
15308 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040015309 if (drq->entry_count < 512) {
15310 status = -EINVAL;
15311 goto out;
15312 }
James Smart5a6f1332011-03-11 16:05:35 -050015313 /* otherwise default to smallest count (drop through) */
15314 case 512:
15315 bf_set(lpfc_rq_context_rqe_count,
15316 &rq_create->u.request.context,
15317 LPFC_RQ_RING_SIZE_512);
15318 break;
15319 case 1024:
15320 bf_set(lpfc_rq_context_rqe_count,
15321 &rq_create->u.request.context,
15322 LPFC_RQ_RING_SIZE_1024);
15323 break;
15324 case 2048:
15325 bf_set(lpfc_rq_context_rqe_count,
15326 &rq_create->u.request.context,
15327 LPFC_RQ_RING_SIZE_2048);
15328 break;
15329 case 4096:
15330 bf_set(lpfc_rq_context_rqe_count,
15331 &rq_create->u.request.context,
15332 LPFC_RQ_RING_SIZE_4096);
15333 break;
15334 }
James Smart3c603be2017-05-15 15:20:44 -070015335 if (subtype == LPFC_NVMET)
15336 bf_set(lpfc_rq_context_buf_size,
15337 &rq_create->u.request.context,
15338 LPFC_NVMET_DATA_BUF_SIZE);
15339 else
15340 bf_set(lpfc_rq_context_buf_size,
15341 &rq_create->u.request.context,
15342 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040015343 }
15344 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
15345 cq->queue_id);
15346 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
15347 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040015348 list_for_each_entry(dmabuf, &drq->page_list, list) {
15349 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
15350 putPaddrLow(dmabuf->phys);
15351 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
15352 putPaddrHigh(dmabuf->phys);
15353 }
James Smart962bc512013-01-03 15:44:00 -050015354 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
15355 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040015356 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15357 /* The IOCTL status is embedded in the mailbox subheader. */
15358 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
15359 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15360 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15361 if (shdr_status || shdr_add_status || rc) {
15362 status = -ENXIO;
15363 goto out;
15364 }
15365 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15366 if (drq->queue_id == 0xFFFF) {
15367 status = -ENXIO;
15368 goto out;
15369 }
15370 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050015371 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040015372 drq->subtype = subtype;
15373 drq->host_index = 0;
15374 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015375 drq->entry_repost = LPFC_RQ_REPOST;
James Smart4f774512009-05-22 14:52:35 -040015376
15377 /* link the header and data RQs onto the parent cq child list */
15378 list_add_tail(&hrq->list, &cq->child_list);
15379 list_add_tail(&drq->list, &cq->child_list);
15380
15381out:
James Smart8fa38512009-07-19 10:01:03 -040015382 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015383 return status;
15384}
15385
15386/**
James Smart2d7dbc42017-02-12 13:52:35 -080015387 * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
15388 * @phba: HBA structure that indicates port to create a queue on.
15389 * @hrqp: The queue structure array to use to create the header receive queues.
15390 * @drqp: The queue structure array to use to create the data receive queues.
15391 * @cqp: The completion queue array to bind these receive queues to.
15392 *
15393 * This function creates a receive buffer queue pair , as detailed in @hrq and
15394 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
15395 * to the HBA.
15396 *
15397 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
15398 * struct is used to get the entry count that is necessary to determine the
15399 * number of pages to use for this queue. The @cq is used to indicate which
15400 * completion queue to bind received buffers that are posted to these queues to.
15401 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
15402 * receive queue pair. This function is asynchronous and will wait for the
15403 * mailbox command to finish before continuing.
15404 *
15405 * On success this function will return a zero. If unable to allocate enough
15406 * memory this function will return -ENOMEM. If the queue create mailbox command
15407 * fails this function will return -ENXIO.
15408 **/
15409int
15410lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
15411 struct lpfc_queue **drqp, struct lpfc_queue **cqp,
15412 uint32_t subtype)
15413{
15414 struct lpfc_queue *hrq, *drq, *cq;
15415 struct lpfc_mbx_rq_create_v2 *rq_create;
15416 struct lpfc_dmabuf *dmabuf;
15417 LPFC_MBOXQ_t *mbox;
15418 int rc, length, alloclen, status = 0;
15419 int cnt, idx, numrq, page_idx = 0;
15420 uint32_t shdr_status, shdr_add_status;
15421 union lpfc_sli4_cfg_shdr *shdr;
15422 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
15423
15424 numrq = phba->cfg_nvmet_mrq;
15425 /* sanity check on array memory */
15426 if (!hrqp || !drqp || !cqp || !numrq)
15427 return -ENODEV;
15428 if (!phba->sli4_hba.pc_sli4_params.supported)
15429 hw_page_size = SLI4_PAGE_SIZE;
15430
15431 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15432 if (!mbox)
15433 return -ENOMEM;
15434
15435 length = sizeof(struct lpfc_mbx_rq_create_v2);
15436 length += ((2 * numrq * hrqp[0]->page_count) *
15437 sizeof(struct dma_address));
15438
15439 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15440 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
15441 LPFC_SLI4_MBX_NEMBED);
15442 if (alloclen < length) {
15443 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15444 "3099 Allocated DMA memory size (%d) is "
15445 "less than the requested DMA memory size "
15446 "(%d)\n", alloclen, length);
15447 status = -ENOMEM;
15448 goto out;
15449 }
15450
15451
15452
15453 rq_create = mbox->sge_array->addr[0];
15454 shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
15455
15456 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
15457 cnt = 0;
15458
15459 for (idx = 0; idx < numrq; idx++) {
15460 hrq = hrqp[idx];
15461 drq = drqp[idx];
15462 cq = cqp[idx];
15463
James Smart2d7dbc42017-02-12 13:52:35 -080015464 /* sanity check on queue memory */
15465 if (!hrq || !drq || !cq) {
15466 status = -ENODEV;
15467 goto out;
15468 }
15469
James Smart7aabe842017-03-04 09:30:22 -080015470 if (hrq->entry_count != drq->entry_count) {
15471 status = -EINVAL;
15472 goto out;
15473 }
15474
James Smart2d7dbc42017-02-12 13:52:35 -080015475 if (idx == 0) {
15476 bf_set(lpfc_mbx_rq_create_num_pages,
15477 &rq_create->u.request,
15478 hrq->page_count);
15479 bf_set(lpfc_mbx_rq_create_rq_cnt,
15480 &rq_create->u.request, (numrq * 2));
15481 bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
15482 1);
15483 bf_set(lpfc_rq_context_base_cq,
15484 &rq_create->u.request.context,
15485 cq->queue_id);
15486 bf_set(lpfc_rq_context_data_size,
15487 &rq_create->u.request.context,
James Smart3c603be2017-05-15 15:20:44 -070015488 LPFC_NVMET_DATA_BUF_SIZE);
James Smart2d7dbc42017-02-12 13:52:35 -080015489 bf_set(lpfc_rq_context_hdr_size,
15490 &rq_create->u.request.context,
15491 LPFC_HDR_BUF_SIZE);
15492 bf_set(lpfc_rq_context_rqe_count_1,
15493 &rq_create->u.request.context,
15494 hrq->entry_count);
15495 bf_set(lpfc_rq_context_rqe_size,
15496 &rq_create->u.request.context,
15497 LPFC_RQE_SIZE_8);
15498 bf_set(lpfc_rq_context_page_size,
15499 &rq_create->u.request.context,
15500 (PAGE_SIZE/SLI4_PAGE_SIZE));
15501 }
15502 rc = 0;
15503 list_for_each_entry(dmabuf, &hrq->page_list, list) {
15504 memset(dmabuf->virt, 0, hw_page_size);
15505 cnt = page_idx + dmabuf->buffer_tag;
15506 rq_create->u.request.page[cnt].addr_lo =
15507 putPaddrLow(dmabuf->phys);
15508 rq_create->u.request.page[cnt].addr_hi =
15509 putPaddrHigh(dmabuf->phys);
15510 rc++;
15511 }
15512 page_idx += rc;
15513
15514 rc = 0;
15515 list_for_each_entry(dmabuf, &drq->page_list, list) {
15516 memset(dmabuf->virt, 0, hw_page_size);
15517 cnt = page_idx + dmabuf->buffer_tag;
15518 rq_create->u.request.page[cnt].addr_lo =
15519 putPaddrLow(dmabuf->phys);
15520 rq_create->u.request.page[cnt].addr_hi =
15521 putPaddrHigh(dmabuf->phys);
15522 rc++;
15523 }
15524 page_idx += rc;
15525
15526 hrq->db_format = LPFC_DB_RING_FORMAT;
15527 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15528 hrq->type = LPFC_HRQ;
15529 hrq->assoc_qid = cq->queue_id;
15530 hrq->subtype = subtype;
15531 hrq->host_index = 0;
15532 hrq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015533 hrq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015534
15535 drq->db_format = LPFC_DB_RING_FORMAT;
15536 drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
15537 drq->type = LPFC_DRQ;
15538 drq->assoc_qid = cq->queue_id;
15539 drq->subtype = subtype;
15540 drq->host_index = 0;
15541 drq->hba_index = 0;
James Smart61f3d4b2017-05-15 15:20:41 -070015542 drq->entry_repost = LPFC_RQ_REPOST;
James Smart2d7dbc42017-02-12 13:52:35 -080015543
15544 list_add_tail(&hrq->list, &cq->child_list);
15545 list_add_tail(&drq->list, &cq->child_list);
15546 }
15547
15548 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15549 /* The IOCTL status is embedded in the mailbox subheader. */
15550 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15551 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15552 if (shdr_status || shdr_add_status || rc) {
15553 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15554 "3120 RQ_CREATE mailbox failed with "
15555 "status x%x add_status x%x, mbx status x%x\n",
15556 shdr_status, shdr_add_status, rc);
15557 status = -ENXIO;
15558 goto out;
15559 }
15560 rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
15561 if (rc == 0xFFFF) {
15562 status = -ENXIO;
15563 goto out;
15564 }
15565
15566 /* Initialize all RQs with associated queue id */
15567 for (idx = 0; idx < numrq; idx++) {
15568 hrq = hrqp[idx];
15569 hrq->queue_id = rc + (2 * idx);
15570 drq = drqp[idx];
15571 drq->queue_id = rc + (2 * idx) + 1;
15572 }
15573
15574out:
15575 lpfc_sli4_mbox_cmd_free(phba, mbox);
15576 return status;
15577}
15578
15579/**
James Smart4f774512009-05-22 14:52:35 -040015580 * lpfc_eq_destroy - Destroy an event Queue on the HBA
15581 * @eq: The queue structure associated with the queue to destroy.
15582 *
15583 * This function destroys a queue, as detailed in @eq by sending an mailbox
15584 * command, specific to the type of queue, to the HBA.
15585 *
15586 * The @eq struct is used to get the queue ID of the queue to destroy.
15587 *
15588 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015589 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015590 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015591int
James Smart4f774512009-05-22 14:52:35 -040015592lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
15593{
15594 LPFC_MBOXQ_t *mbox;
15595 int rc, length, status = 0;
15596 uint32_t shdr_status, shdr_add_status;
15597 union lpfc_sli4_cfg_shdr *shdr;
15598
James Smart2e90f4b2011-12-13 13:22:37 -050015599 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015600 if (!eq)
15601 return -ENODEV;
15602 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
15603 if (!mbox)
15604 return -ENOMEM;
15605 length = (sizeof(struct lpfc_mbx_eq_destroy) -
15606 sizeof(struct lpfc_sli4_cfg_mhdr));
15607 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15608 LPFC_MBOX_OPCODE_EQ_DESTROY,
15609 length, LPFC_SLI4_MBX_EMBED);
15610 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
15611 eq->queue_id);
15612 mbox->vport = eq->phba->pport;
15613 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15614
15615 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
15616 /* The IOCTL status is embedded in the mailbox subheader. */
15617 shdr = (union lpfc_sli4_cfg_shdr *)
15618 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
15619 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15620 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15621 if (shdr_status || shdr_add_status || rc) {
15622 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15623 "2505 EQ_DESTROY mailbox failed with "
15624 "status x%x add_status x%x, mbx status x%x\n",
15625 shdr_status, shdr_add_status, rc);
15626 status = -ENXIO;
15627 }
15628
15629 /* Remove eq from any list */
15630 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040015631 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015632 return status;
15633}
15634
15635/**
15636 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
15637 * @cq: The queue structure associated with the queue to destroy.
15638 *
15639 * This function destroys a queue, as detailed in @cq by sending an mailbox
15640 * command, specific to the type of queue, to the HBA.
15641 *
15642 * The @cq struct is used to get the queue ID of the queue to destroy.
15643 *
15644 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015645 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015646 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015647int
James Smart4f774512009-05-22 14:52:35 -040015648lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
15649{
15650 LPFC_MBOXQ_t *mbox;
15651 int rc, length, status = 0;
15652 uint32_t shdr_status, shdr_add_status;
15653 union lpfc_sli4_cfg_shdr *shdr;
15654
James Smart2e90f4b2011-12-13 13:22:37 -050015655 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015656 if (!cq)
15657 return -ENODEV;
15658 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
15659 if (!mbox)
15660 return -ENOMEM;
15661 length = (sizeof(struct lpfc_mbx_cq_destroy) -
15662 sizeof(struct lpfc_sli4_cfg_mhdr));
15663 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15664 LPFC_MBOX_OPCODE_CQ_DESTROY,
15665 length, LPFC_SLI4_MBX_EMBED);
15666 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
15667 cq->queue_id);
15668 mbox->vport = cq->phba->pport;
15669 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15670 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
15671 /* The IOCTL status is embedded in the mailbox subheader. */
15672 shdr = (union lpfc_sli4_cfg_shdr *)
15673 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
15674 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15675 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15676 if (shdr_status || shdr_add_status || rc) {
15677 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15678 "2506 CQ_DESTROY mailbox failed with "
15679 "status x%x add_status x%x, mbx status x%x\n",
15680 shdr_status, shdr_add_status, rc);
15681 status = -ENXIO;
15682 }
15683 /* Remove cq from any list */
15684 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040015685 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015686 return status;
15687}
15688
15689/**
James Smart04c68492009-05-22 14:52:52 -040015690 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
15691 * @qm: The queue structure associated with the queue to destroy.
15692 *
15693 * This function destroys a queue, as detailed in @mq by sending an mailbox
15694 * command, specific to the type of queue, to the HBA.
15695 *
15696 * The @mq struct is used to get the queue ID of the queue to destroy.
15697 *
15698 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015699 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040015700 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015701int
James Smart04c68492009-05-22 14:52:52 -040015702lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
15703{
15704 LPFC_MBOXQ_t *mbox;
15705 int rc, length, status = 0;
15706 uint32_t shdr_status, shdr_add_status;
15707 union lpfc_sli4_cfg_shdr *shdr;
15708
James Smart2e90f4b2011-12-13 13:22:37 -050015709 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040015710 if (!mq)
15711 return -ENODEV;
15712 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
15713 if (!mbox)
15714 return -ENOMEM;
15715 length = (sizeof(struct lpfc_mbx_mq_destroy) -
15716 sizeof(struct lpfc_sli4_cfg_mhdr));
15717 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15718 LPFC_MBOX_OPCODE_MQ_DESTROY,
15719 length, LPFC_SLI4_MBX_EMBED);
15720 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
15721 mq->queue_id);
15722 mbox->vport = mq->phba->pport;
15723 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15724 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
15725 /* The IOCTL status is embedded in the mailbox subheader. */
15726 shdr = (union lpfc_sli4_cfg_shdr *)
15727 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
15728 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15729 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15730 if (shdr_status || shdr_add_status || rc) {
15731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15732 "2507 MQ_DESTROY mailbox failed with "
15733 "status x%x add_status x%x, mbx status x%x\n",
15734 shdr_status, shdr_add_status, rc);
15735 status = -ENXIO;
15736 }
15737 /* Remove mq from any list */
15738 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040015739 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040015740 return status;
15741}
15742
15743/**
James Smart4f774512009-05-22 14:52:35 -040015744 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
15745 * @wq: The queue structure associated with the queue to destroy.
15746 *
15747 * This function destroys a queue, as detailed in @wq by sending an mailbox
15748 * command, specific to the type of queue, to the HBA.
15749 *
15750 * The @wq struct is used to get the queue ID of the queue to destroy.
15751 *
15752 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015753 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015754 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015755int
James Smart4f774512009-05-22 14:52:35 -040015756lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
15757{
15758 LPFC_MBOXQ_t *mbox;
15759 int rc, length, status = 0;
15760 uint32_t shdr_status, shdr_add_status;
15761 union lpfc_sli4_cfg_shdr *shdr;
15762
James Smart2e90f4b2011-12-13 13:22:37 -050015763 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015764 if (!wq)
15765 return -ENODEV;
15766 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
15767 if (!mbox)
15768 return -ENOMEM;
15769 length = (sizeof(struct lpfc_mbx_wq_destroy) -
15770 sizeof(struct lpfc_sli4_cfg_mhdr));
15771 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15772 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
15773 length, LPFC_SLI4_MBX_EMBED);
15774 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
15775 wq->queue_id);
15776 mbox->vport = wq->phba->pport;
15777 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15778 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
15779 shdr = (union lpfc_sli4_cfg_shdr *)
15780 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
15781 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15782 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15783 if (shdr_status || shdr_add_status || rc) {
15784 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15785 "2508 WQ_DESTROY mailbox failed with "
15786 "status x%x add_status x%x, mbx status x%x\n",
15787 shdr_status, shdr_add_status, rc);
15788 status = -ENXIO;
15789 }
15790 /* Remove wq from any list */
15791 list_del_init(&wq->list);
James Smartd1f525a2017-04-21 16:04:55 -070015792 kfree(wq->pring);
15793 wq->pring = NULL;
James Smart8fa38512009-07-19 10:01:03 -040015794 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015795 return status;
15796}
15797
15798/**
15799 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
15800 * @rq: The queue structure associated with the queue to destroy.
15801 *
15802 * This function destroys a queue, as detailed in @rq by sending an mailbox
15803 * command, specific to the type of queue, to the HBA.
15804 *
15805 * The @rq struct is used to get the queue ID of the queue to destroy.
15806 *
15807 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040015808 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040015809 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040015810int
James Smart4f774512009-05-22 14:52:35 -040015811lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
15812 struct lpfc_queue *drq)
15813{
15814 LPFC_MBOXQ_t *mbox;
15815 int rc, length, status = 0;
15816 uint32_t shdr_status, shdr_add_status;
15817 union lpfc_sli4_cfg_shdr *shdr;
15818
James Smart2e90f4b2011-12-13 13:22:37 -050015819 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040015820 if (!hrq || !drq)
15821 return -ENODEV;
15822 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
15823 if (!mbox)
15824 return -ENOMEM;
15825 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050015826 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040015827 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15828 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
15829 length, LPFC_SLI4_MBX_EMBED);
15830 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15831 hrq->queue_id);
15832 mbox->vport = hrq->phba->pport;
15833 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15834 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
15835 /* The IOCTL status is embedded in the mailbox subheader. */
15836 shdr = (union lpfc_sli4_cfg_shdr *)
15837 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15838 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15839 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15840 if (shdr_status || shdr_add_status || rc) {
15841 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15842 "2509 RQ_DESTROY mailbox failed with "
15843 "status x%x add_status x%x, mbx status x%x\n",
15844 shdr_status, shdr_add_status, rc);
15845 if (rc != MBX_TIMEOUT)
15846 mempool_free(mbox, hrq->phba->mbox_mem_pool);
15847 return -ENXIO;
15848 }
15849 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
15850 drq->queue_id);
15851 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
15852 shdr = (union lpfc_sli4_cfg_shdr *)
15853 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
15854 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15855 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15856 if (shdr_status || shdr_add_status || rc) {
15857 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15858 "2510 RQ_DESTROY mailbox failed with "
15859 "status x%x add_status x%x, mbx status x%x\n",
15860 shdr_status, shdr_add_status, rc);
15861 status = -ENXIO;
15862 }
15863 list_del_init(&hrq->list);
15864 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040015865 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040015866 return status;
15867}
15868
15869/**
15870 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
15871 * @phba: The virtual port for which this call being executed.
15872 * @pdma_phys_addr0: Physical address of the 1st SGL page.
15873 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
15874 * @xritag: the xritag that ties this io to the SGL pages.
15875 *
15876 * This routine will post the sgl pages for the IO that has the xritag
15877 * that is in the iocbq structure. The xritag is assigned during iocbq
15878 * creation and persists for as long as the driver is loaded.
15879 * if the caller has fewer than 256 scatter gather segments to map then
15880 * pdma_phys_addr1 should be 0.
15881 * If the caller needs to map more than 256 scatter gather segment then
15882 * pdma_phys_addr1 should be a valid physical address.
15883 * physical address for SGLs must be 64 byte aligned.
15884 * If you are going to map 2 SGL's then the first one must have 256 entries
15885 * the second sgl can have between 1 and 256 entries.
15886 *
15887 * Return codes:
15888 * 0 - Success
15889 * -ENXIO, -ENOMEM - Failure
15890 **/
15891int
15892lpfc_sli4_post_sgl(struct lpfc_hba *phba,
15893 dma_addr_t pdma_phys_addr0,
15894 dma_addr_t pdma_phys_addr1,
15895 uint16_t xritag)
15896{
15897 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
15898 LPFC_MBOXQ_t *mbox;
15899 int rc;
15900 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040015901 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040015902 union lpfc_sli4_cfg_shdr *shdr;
15903
15904 if (xritag == NO_XRI) {
15905 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15906 "0364 Invalid param:\n");
15907 return -EINVAL;
15908 }
15909
15910 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15911 if (!mbox)
15912 return -ENOMEM;
15913
15914 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15915 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
15916 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050015917 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040015918
15919 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
15920 &mbox->u.mqe.un.post_sgl_pages;
15921 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
15922 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
15923
15924 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
15925 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
15926 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
15927 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
15928
15929 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
15930 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
15931 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
15932 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
15933 if (!phba->sli4_hba.intr_enable)
15934 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040015935 else {
James Smarta183a152011-10-10 21:32:43 -040015936 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040015937 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15938 }
James Smart4f774512009-05-22 14:52:35 -040015939 /* The IOCTL status is embedded in the mailbox subheader. */
15940 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
15941 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15942 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15943 if (rc != MBX_TIMEOUT)
15944 mempool_free(mbox, phba->mbox_mem_pool);
15945 if (shdr_status || shdr_add_status || rc) {
15946 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15947 "2511 POST_SGL mailbox failed with "
15948 "status x%x add_status x%x, mbx status x%x\n",
15949 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040015950 }
15951 return 0;
15952}
James Smart4f774512009-05-22 14:52:35 -040015953
15954/**
James Smart88a2cfb2011-07-22 18:36:33 -040015955 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040015956 * @phba: pointer to lpfc hba data structure.
15957 *
15958 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040015959 * HBA consistent with the SLI-4 interface spec. This routine
15960 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15961 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040015962 *
James Smart88a2cfb2011-07-22 18:36:33 -040015963 * Returns
15964 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15965 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15966 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015967static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015968lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15969{
15970 unsigned long xri;
15971
15972 /*
15973 * Fetch the next logical xri. Because this index is logical,
15974 * the driver starts at 0 each time.
15975 */
15976 spin_lock_irq(&phba->hbalock);
15977 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15978 phba->sli4_hba.max_cfg_param.max_xri, 0);
15979 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15980 spin_unlock_irq(&phba->hbalock);
15981 return NO_XRI;
15982 } else {
15983 set_bit(xri, phba->sli4_hba.xri_bmask);
15984 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040015985 }
James Smart6d368e52011-05-24 11:44:12 -040015986 spin_unlock_irq(&phba->hbalock);
15987 return xri;
15988}
15989
15990/**
15991 * lpfc_sli4_free_xri - Release an xri for reuse.
15992 * @phba: pointer to lpfc hba data structure.
15993 *
15994 * This routine is invoked to release an xri to the pool of
15995 * available rpis maintained by the driver.
15996 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015997static void
James Smart6d368e52011-05-24 11:44:12 -040015998__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15999{
16000 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040016001 phba->sli4_hba.max_cfg_param.xri_used--;
16002 }
16003}
16004
16005/**
16006 * lpfc_sli4_free_xri - Release an xri for reuse.
16007 * @phba: pointer to lpfc hba data structure.
16008 *
16009 * This routine is invoked to release an xri to the pool of
16010 * available rpis maintained by the driver.
16011 **/
16012void
16013lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
16014{
16015 spin_lock_irq(&phba->hbalock);
16016 __lpfc_sli4_free_xri(phba, xri);
16017 spin_unlock_irq(&phba->hbalock);
16018}
16019
16020/**
James Smart4f774512009-05-22 14:52:35 -040016021 * lpfc_sli4_next_xritag - Get an xritag for the io
16022 * @phba: Pointer to HBA context object.
16023 *
16024 * This function gets an xritag for the iocb. If there is no unused xritag
16025 * it will return 0xffff.
16026 * The function returns the allocated xritag if successful, else returns zero.
16027 * Zero is not a valid xritag.
16028 * The caller is not required to hold any lock.
16029 **/
16030uint16_t
16031lpfc_sli4_next_xritag(struct lpfc_hba *phba)
16032{
James Smart6d368e52011-05-24 11:44:12 -040016033 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040016034
James Smart6d368e52011-05-24 11:44:12 -040016035 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040016036 if (xri_index == NO_XRI)
16037 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
16038 "2004 Failed to allocate XRI.last XRITAG is %d"
16039 " Max XRI is %d, Used XRI is %d\n",
16040 xri_index,
16041 phba->sli4_hba.max_cfg_param.max_xri,
16042 phba->sli4_hba.max_cfg_param.xri_used);
16043 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040016044}
16045
16046/**
James Smart895427b2017-02-12 13:52:30 -080016047 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040016048 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040016049 * @post_sgl_list: pointer to els sgl entry list.
16050 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040016051 *
16052 * This routine is invoked to post a block of driver's sgl pages to the
16053 * HBA using non-embedded mailbox command. No Lock is held. This routine
16054 * is only called when the driver is loading and after all IO has been
16055 * stopped.
16056 **/
James Smart8a9d2e82012-05-09 21:16:12 -040016057static int
James Smart895427b2017-02-12 13:52:30 -080016058lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040016059 struct list_head *post_sgl_list,
16060 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040016061{
James Smart8a9d2e82012-05-09 21:16:12 -040016062 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040016063 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16064 struct sgl_page_pairs *sgl_pg_pairs;
16065 void *viraddr;
16066 LPFC_MBOXQ_t *mbox;
16067 uint32_t reqlen, alloclen, pg_pairs;
16068 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040016069 uint16_t xritag_start = 0;
16070 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040016071 uint32_t shdr_status, shdr_add_status;
16072 union lpfc_sli4_cfg_shdr *shdr;
16073
James Smart895427b2017-02-12 13:52:30 -080016074 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016075 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016076 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080016077 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040016078 "2559 Block sgl registration required DMA "
16079 "size (%d) great than a page\n", reqlen);
16080 return -ENOMEM;
16081 }
James Smart895427b2017-02-12 13:52:30 -080016082
James Smart4f774512009-05-22 14:52:35 -040016083 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040016084 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040016085 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040016086
16087 /* Allocate DMA memory and set up the non-embedded mailbox command */
16088 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16089 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16090 LPFC_SLI4_MBX_NEMBED);
16091
16092 if (alloclen < reqlen) {
16093 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16094 "0285 Allocated DMA memory size (%d) is "
16095 "less than the requested DMA memory "
16096 "size (%d)\n", alloclen, reqlen);
16097 lpfc_sli4_mbox_cmd_free(phba, mbox);
16098 return -ENOMEM;
16099 }
James Smart4f774512009-05-22 14:52:35 -040016100 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040016101 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040016102 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16103 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16104
James Smart8a9d2e82012-05-09 21:16:12 -040016105 pg_pairs = 0;
16106 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040016107 /* Set up the sge entry */
16108 sgl_pg_pairs->sgl_pg0_addr_lo =
16109 cpu_to_le32(putPaddrLow(sglq_entry->phys));
16110 sgl_pg_pairs->sgl_pg0_addr_hi =
16111 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
16112 sgl_pg_pairs->sgl_pg1_addr_lo =
16113 cpu_to_le32(putPaddrLow(0));
16114 sgl_pg_pairs->sgl_pg1_addr_hi =
16115 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040016116
James Smart4f774512009-05-22 14:52:35 -040016117 /* Keep the first xritag on the list */
16118 if (pg_pairs == 0)
16119 xritag_start = sglq_entry->sli4_xritag;
16120 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040016121 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040016122 }
James Smart6d368e52011-05-24 11:44:12 -040016123
16124 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040016125 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080016126 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040016127 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080016128
James Smart4f774512009-05-22 14:52:35 -040016129 if (!phba->sli4_hba.intr_enable)
16130 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16131 else {
James Smarta183a152011-10-10 21:32:43 -040016132 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016133 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16134 }
16135 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16136 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16137 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16138 if (rc != MBX_TIMEOUT)
16139 lpfc_sli4_mbox_cmd_free(phba, mbox);
16140 if (shdr_status || shdr_add_status || rc) {
16141 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16142 "2513 POST_SGL_BLOCK mailbox command failed "
16143 "status x%x add_status x%x mbx status x%x\n",
16144 shdr_status, shdr_add_status, rc);
16145 rc = -ENXIO;
16146 }
16147 return rc;
16148}
16149
16150/**
16151 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
16152 * @phba: pointer to lpfc hba data structure.
16153 * @sblist: pointer to scsi buffer list.
16154 * @count: number of scsi buffers on the list.
16155 *
16156 * This routine is invoked to post a block of @count scsi sgl pages from a
16157 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
16158 * No Lock is held.
16159 *
16160 **/
16161int
James Smart8a9d2e82012-05-09 21:16:12 -040016162lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
16163 struct list_head *sblist,
16164 int count)
James Smart4f774512009-05-22 14:52:35 -040016165{
16166 struct lpfc_scsi_buf *psb;
16167 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
16168 struct sgl_page_pairs *sgl_pg_pairs;
16169 void *viraddr;
16170 LPFC_MBOXQ_t *mbox;
16171 uint32_t reqlen, alloclen, pg_pairs;
16172 uint32_t mbox_tmo;
16173 uint16_t xritag_start = 0;
16174 int rc = 0;
16175 uint32_t shdr_status, shdr_add_status;
16176 dma_addr_t pdma_phys_bpl1;
16177 union lpfc_sli4_cfg_shdr *shdr;
16178
16179 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040016180 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040016181 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040016182 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040016183 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
16184 "0217 Block sgl registration required DMA "
16185 "size (%d) great than a page\n", reqlen);
16186 return -ENOMEM;
16187 }
16188 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16189 if (!mbox) {
16190 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16191 "0283 Failed to allocate mbox cmd memory\n");
16192 return -ENOMEM;
16193 }
16194
16195 /* Allocate DMA memory and set up the non-embedded mailbox command */
16196 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16197 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
16198 LPFC_SLI4_MBX_NEMBED);
16199
16200 if (alloclen < reqlen) {
16201 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16202 "2561 Allocated DMA memory size (%d) is "
16203 "less than the requested DMA memory "
16204 "size (%d)\n", alloclen, reqlen);
16205 lpfc_sli4_mbox_cmd_free(phba, mbox);
16206 return -ENOMEM;
16207 }
James Smart6d368e52011-05-24 11:44:12 -040016208
James Smart4f774512009-05-22 14:52:35 -040016209 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040016210 viraddr = mbox->sge_array->addr[0];
16211
16212 /* Set up the SGL pages in the non-embedded DMA pages */
16213 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
16214 sgl_pg_pairs = &sgl->sgl_pg_pairs;
16215
16216 pg_pairs = 0;
16217 list_for_each_entry(psb, sblist, list) {
16218 /* Set up the sge entry */
16219 sgl_pg_pairs->sgl_pg0_addr_lo =
16220 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
16221 sgl_pg_pairs->sgl_pg0_addr_hi =
16222 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
16223 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
16224 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
16225 else
16226 pdma_phys_bpl1 = 0;
16227 sgl_pg_pairs->sgl_pg1_addr_lo =
16228 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
16229 sgl_pg_pairs->sgl_pg1_addr_hi =
16230 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
16231 /* Keep the first xritag on the list */
16232 if (pg_pairs == 0)
16233 xritag_start = psb->cur_iocbq.sli4_xritag;
16234 sgl_pg_pairs++;
16235 pg_pairs++;
16236 }
16237 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
16238 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
16239 /* Perform endian conversion if necessary */
16240 sgl->word0 = cpu_to_le32(sgl->word0);
16241
16242 if (!phba->sli4_hba.intr_enable)
16243 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16244 else {
James Smarta183a152011-10-10 21:32:43 -040016245 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040016246 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16247 }
16248 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
16249 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16250 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16251 if (rc != MBX_TIMEOUT)
16252 lpfc_sli4_mbox_cmd_free(phba, mbox);
16253 if (shdr_status || shdr_add_status || rc) {
16254 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16255 "2564 POST_SGL_BLOCK mailbox command failed "
16256 "status x%x add_status x%x mbx status x%x\n",
16257 shdr_status, shdr_add_status, rc);
16258 rc = -ENXIO;
16259 }
16260 return rc;
16261}
16262
16263/**
16264 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
16265 * @phba: pointer to lpfc_hba struct that the frame was received on
16266 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16267 *
16268 * This function checks the fields in the @fc_hdr to see if the FC frame is a
16269 * valid type of frame that the LPFC driver will handle. This function will
16270 * return a zero if the frame is a valid frame or a non zero value when the
16271 * frame does not pass the check.
16272 **/
16273static int
16274lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
16275{
Tomas Henzl474ffb72010-12-22 16:52:40 +010016276 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040016277 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050016278 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040016279
James Smartae9e28f2017-05-15 15:20:51 -070016280#define FC_RCTL_MDS_DIAGS 0xF4
16281
James Smart4f774512009-05-22 14:52:35 -040016282 switch (fc_hdr->fh_r_ctl) {
16283 case FC_RCTL_DD_UNCAT: /* uncategorized information */
16284 case FC_RCTL_DD_SOL_DATA: /* solicited data */
16285 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
16286 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
16287 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
16288 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
16289 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
16290 case FC_RCTL_DD_CMD_STATUS: /* command status */
16291 case FC_RCTL_ELS_REQ: /* extended link services request */
16292 case FC_RCTL_ELS_REP: /* extended link services reply */
16293 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
16294 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
16295 case FC_RCTL_BA_NOP: /* basic link service NOP */
16296 case FC_RCTL_BA_ABTS: /* basic link service abort */
16297 case FC_RCTL_BA_RMC: /* remove connection */
16298 case FC_RCTL_BA_ACC: /* basic accept */
16299 case FC_RCTL_BA_RJT: /* basic reject */
16300 case FC_RCTL_BA_PRMT:
16301 case FC_RCTL_ACK_1: /* acknowledge_1 */
16302 case FC_RCTL_ACK_0: /* acknowledge_0 */
16303 case FC_RCTL_P_RJT: /* port reject */
16304 case FC_RCTL_F_RJT: /* fabric reject */
16305 case FC_RCTL_P_BSY: /* port busy */
16306 case FC_RCTL_F_BSY: /* fabric busy to data frame */
16307 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
16308 case FC_RCTL_LCR: /* link credit reset */
James Smartae9e28f2017-05-15 15:20:51 -070016309 case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
James Smart4f774512009-05-22 14:52:35 -040016310 case FC_RCTL_END: /* end */
16311 break;
16312 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
16313 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16314 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
16315 return lpfc_fc_frame_check(phba, fc_hdr);
16316 default:
16317 goto drop;
16318 }
James Smartae9e28f2017-05-15 15:20:51 -070016319
16320#define FC_TYPE_VENDOR_UNIQUE 0xFF
16321
James Smart4f774512009-05-22 14:52:35 -040016322 switch (fc_hdr->fh_type) {
16323 case FC_TYPE_BLS:
16324 case FC_TYPE_ELS:
16325 case FC_TYPE_FCP:
16326 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080016327 case FC_TYPE_NVME:
James Smartae9e28f2017-05-15 15:20:51 -070016328 case FC_TYPE_VENDOR_UNIQUE:
James Smart4f774512009-05-22 14:52:35 -040016329 break;
16330 case FC_TYPE_IP:
16331 case FC_TYPE_ILS:
16332 default:
16333 goto drop;
16334 }
James Smart546fc852011-03-11 16:06:29 -050016335
James Smart4f774512009-05-22 14:52:35 -040016336 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016337 "2538 Received frame rctl:x%x, type:x%x, "
James Smart88f43a02013-04-17 20:19:44 -040016338 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart78e1d202017-06-01 21:07:09 -070016339 fc_hdr->fh_r_ctl, fc_hdr->fh_type,
16340 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
16341 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
16342 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
16343 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040016344 return 0;
16345drop:
16346 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
James Smart78e1d202017-06-01 21:07:09 -070016347 "2539 Dropped frame rctl:x%x type:x%x\n",
16348 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040016349 return 1;
16350}
16351
16352/**
16353 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
16354 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16355 *
16356 * This function processes the FC header to retrieve the VFI from the VF
16357 * header, if one exists. This function will return the VFI if one exists
16358 * or 0 if no VSAN Header exists.
16359 **/
16360static uint32_t
16361lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
16362{
16363 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
16364
16365 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
16366 return 0;
16367 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
16368}
16369
16370/**
16371 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
16372 * @phba: Pointer to the HBA structure to search for the vport on
16373 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
16374 * @fcfi: The FC Fabric ID that the frame came from
16375 *
16376 * This function searches the @phba for a vport that matches the content of the
16377 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
16378 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
16379 * returns the matching vport pointer or NULL if unable to match frame to a
16380 * vport.
16381 **/
16382static struct lpfc_vport *
16383lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080016384 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040016385{
16386 struct lpfc_vport **vports;
16387 struct lpfc_vport *vport = NULL;
16388 int i;
James Smart939723a2012-05-09 21:19:03 -040016389
James Smartbf086112011-08-21 21:48:13 -040016390 if (did == Fabric_DID)
16391 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040016392 if ((phba->pport->fc_flag & FC_PT2PT) &&
16393 !(phba->link_state == LPFC_HBA_READY))
16394 return phba->pport;
16395
James Smart4f774512009-05-22 14:52:35 -040016396 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080016397 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040016398 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16399 if (phba->fcf.fcfi == fcfi &&
16400 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
16401 vports[i]->fc_myDID == did) {
16402 vport = vports[i];
16403 break;
16404 }
16405 }
James Smart895427b2017-02-12 13:52:30 -080016406 }
James Smart4f774512009-05-22 14:52:35 -040016407 lpfc_destroy_vport_work_array(phba, vports);
16408 return vport;
16409}
16410
16411/**
James Smart45ed1192009-10-02 15:17:02 -040016412 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
16413 * @vport: The vport to work on.
16414 *
16415 * This function updates the receive sequence time stamp for this vport. The
16416 * receive sequence time stamp indicates the time that the last frame of the
16417 * the sequence that has been idle for the longest amount of time was received.
16418 * the driver uses this time stamp to indicate if any received sequences have
16419 * timed out.
16420 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016421static void
James Smart45ed1192009-10-02 15:17:02 -040016422lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
16423{
16424 struct lpfc_dmabuf *h_buf;
16425 struct hbq_dmabuf *dmabuf = NULL;
16426
16427 /* get the oldest sequence on the rcv list */
16428 h_buf = list_get_first(&vport->rcv_buffer_list,
16429 struct lpfc_dmabuf, list);
16430 if (!h_buf)
16431 return;
16432 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16433 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
16434}
16435
16436/**
16437 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
16438 * @vport: The vport that the received sequences were sent to.
16439 *
16440 * This function cleans up all outstanding received sequences. This is called
16441 * by the driver when a link event or user action invalidates all the received
16442 * sequences.
16443 **/
16444void
16445lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
16446{
16447 struct lpfc_dmabuf *h_buf, *hnext;
16448 struct lpfc_dmabuf *d_buf, *dnext;
16449 struct hbq_dmabuf *dmabuf = NULL;
16450
16451 /* start with the oldest sequence on the rcv list */
16452 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16453 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16454 list_del_init(&dmabuf->hbuf.list);
16455 list_for_each_entry_safe(d_buf, dnext,
16456 &dmabuf->dbuf.list, list) {
16457 list_del_init(&d_buf->list);
16458 lpfc_in_buf_free(vport->phba, d_buf);
16459 }
16460 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16461 }
16462}
16463
16464/**
16465 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
16466 * @vport: The vport that the received sequences were sent to.
16467 *
16468 * This function determines whether any received sequences have timed out by
16469 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
16470 * indicates that there is at least one timed out sequence this routine will
16471 * go through the received sequences one at a time from most inactive to most
16472 * active to determine which ones need to be cleaned up. Once it has determined
16473 * that a sequence needs to be cleaned up it will simply free up the resources
16474 * without sending an abort.
16475 **/
16476void
16477lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
16478{
16479 struct lpfc_dmabuf *h_buf, *hnext;
16480 struct lpfc_dmabuf *d_buf, *dnext;
16481 struct hbq_dmabuf *dmabuf = NULL;
16482 unsigned long timeout;
16483 int abort_count = 0;
16484
16485 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16486 vport->rcv_buffer_time_stamp);
16487 if (list_empty(&vport->rcv_buffer_list) ||
16488 time_before(jiffies, timeout))
16489 return;
16490 /* start with the oldest sequence on the rcv list */
16491 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
16492 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16493 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
16494 dmabuf->time_stamp);
16495 if (time_before(jiffies, timeout))
16496 break;
16497 abort_count++;
16498 list_del_init(&dmabuf->hbuf.list);
16499 list_for_each_entry_safe(d_buf, dnext,
16500 &dmabuf->dbuf.list, list) {
16501 list_del_init(&d_buf->list);
16502 lpfc_in_buf_free(vport->phba, d_buf);
16503 }
16504 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
16505 }
16506 if (abort_count)
16507 lpfc_update_rcv_time_stamp(vport);
16508}
16509
16510/**
James Smart4f774512009-05-22 14:52:35 -040016511 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
16512 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
16513 *
16514 * This function searches through the existing incomplete sequences that have
16515 * been sent to this @vport. If the frame matches one of the incomplete
16516 * sequences then the dbuf in the @dmabuf is added to the list of frames that
16517 * make up that sequence. If no sequence is found that matches this frame then
16518 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
16519 * This function returns a pointer to the first dmabuf in the sequence list that
16520 * the frame was linked to.
16521 **/
16522static struct hbq_dmabuf *
16523lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16524{
16525 struct fc_frame_header *new_hdr;
16526 struct fc_frame_header *temp_hdr;
16527 struct lpfc_dmabuf *d_buf;
16528 struct lpfc_dmabuf *h_buf;
16529 struct hbq_dmabuf *seq_dmabuf = NULL;
16530 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050016531 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040016532
James Smart4d9ab992009-10-02 15:16:39 -040016533 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016534 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040016535 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050016536
James Smart4f774512009-05-22 14:52:35 -040016537 /* Use the hdr_buf to find the sequence that this frame belongs to */
16538 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16539 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16540 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16541 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16542 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16543 continue;
16544 /* found a pending sequence that matches this frame */
16545 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16546 break;
16547 }
16548 if (!seq_dmabuf) {
16549 /*
16550 * This indicates first frame received for this sequence.
16551 * Queue the buffer on the vport's rcv_buffer_list.
16552 */
16553 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040016554 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016555 return dmabuf;
16556 }
16557 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050016558 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
16559 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040016560 list_del_init(&seq_dmabuf->hbuf.list);
16561 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
16562 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016563 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016564 return dmabuf;
16565 }
James Smart45ed1192009-10-02 15:17:02 -040016566 /* move this sequence to the tail to indicate a young sequence */
16567 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
16568 seq_dmabuf->time_stamp = jiffies;
16569 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050016570 if (list_empty(&seq_dmabuf->dbuf.list)) {
16571 temp_hdr = dmabuf->hbuf.virt;
16572 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
16573 return seq_dmabuf;
16574 }
James Smart4f774512009-05-22 14:52:35 -040016575 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050016576 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
16577 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040016578 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16579 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
16580 /*
16581 * If the frame's sequence count is greater than the frame on
16582 * the list then insert the frame right after this frame
16583 */
James Smarteeead812009-12-21 17:01:23 -050016584 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
16585 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040016586 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050016587 found = 1;
16588 break;
James Smart4f774512009-05-22 14:52:35 -040016589 }
James Smart4360ca92015-12-16 18:12:04 -050016590
16591 if (&d_buf->list == &seq_dmabuf->dbuf.list)
16592 break;
16593 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040016594 }
James Smart4360ca92015-12-16 18:12:04 -050016595
16596 if (found)
16597 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016598 return NULL;
16599}
16600
16601/**
James Smart6669f9b2009-10-02 15:16:45 -040016602 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
16603 * @vport: pointer to a vitural port
16604 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16605 *
16606 * This function tries to abort from the partially assembed sequence, described
16607 * by the information from basic abbort @dmabuf. It checks to see whether such
16608 * partially assembled sequence held by the driver. If so, it shall free up all
16609 * the frames from the partially assembled sequence.
16610 *
16611 * Return
16612 * true -- if there is matching partially assembled sequence present and all
16613 * the frames freed with the sequence;
16614 * false -- if there is no matching partially assembled sequence present so
16615 * nothing got aborted in the lower layer driver
16616 **/
16617static bool
16618lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
16619 struct hbq_dmabuf *dmabuf)
16620{
16621 struct fc_frame_header *new_hdr;
16622 struct fc_frame_header *temp_hdr;
16623 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
16624 struct hbq_dmabuf *seq_dmabuf = NULL;
16625
16626 /* Use the hdr_buf to find the sequence that matches this frame */
16627 INIT_LIST_HEAD(&dmabuf->dbuf.list);
16628 INIT_LIST_HEAD(&dmabuf->hbuf.list);
16629 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16630 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
16631 temp_hdr = (struct fc_frame_header *)h_buf->virt;
16632 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
16633 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
16634 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
16635 continue;
16636 /* found a pending sequence that matches this frame */
16637 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
16638 break;
16639 }
16640
16641 /* Free up all the frames from the partially assembled sequence */
16642 if (seq_dmabuf) {
16643 list_for_each_entry_safe(d_buf, n_buf,
16644 &seq_dmabuf->dbuf.list, list) {
16645 list_del_init(&d_buf->list);
16646 lpfc_in_buf_free(vport->phba, d_buf);
16647 }
16648 return true;
16649 }
16650 return false;
16651}
16652
16653/**
James Smart6dd9e312013-01-03 15:43:37 -050016654 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
16655 * @vport: pointer to a vitural port
16656 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16657 *
16658 * This function tries to abort from the assembed sequence from upper level
16659 * protocol, described by the information from basic abbort @dmabuf. It
16660 * checks to see whether such pending context exists at upper level protocol.
16661 * If so, it shall clean up the pending context.
16662 *
16663 * Return
16664 * true -- if there is matching pending context of the sequence cleaned
16665 * at ulp;
16666 * false -- if there is no matching pending context of the sequence present
16667 * at ulp.
16668 **/
16669static bool
16670lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
16671{
16672 struct lpfc_hba *phba = vport->phba;
16673 int handled;
16674
16675 /* Accepting abort at ulp with SLI4 only */
16676 if (phba->sli_rev < LPFC_SLI_REV4)
16677 return false;
16678
16679 /* Register all caring upper level protocols to attend abort */
16680 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
16681 if (handled)
16682 return true;
16683
16684 return false;
16685}
16686
16687/**
James Smart546fc852011-03-11 16:06:29 -050016688 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040016689 * @phba: Pointer to HBA context object.
16690 * @cmd_iocbq: pointer to the command iocbq structure.
16691 * @rsp_iocbq: pointer to the response iocbq structure.
16692 *
James Smart546fc852011-03-11 16:06:29 -050016693 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040016694 * event. It properly releases the memory allocated to the sequence abort
16695 * accept iocb.
16696 **/
16697static void
James Smart546fc852011-03-11 16:06:29 -050016698lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040016699 struct lpfc_iocbq *cmd_iocbq,
16700 struct lpfc_iocbq *rsp_iocbq)
16701{
James Smart6dd9e312013-01-03 15:43:37 -050016702 struct lpfc_nodelist *ndlp;
16703
16704 if (cmd_iocbq) {
16705 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
16706 lpfc_nlp_put(ndlp);
16707 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016708 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050016709 }
James Smart6b5151f2012-01-18 16:24:06 -050016710
16711 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
16712 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
16713 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16714 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
16715 rsp_iocbq->iocb.ulpStatus,
16716 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040016717}
16718
16719/**
James Smart6d368e52011-05-24 11:44:12 -040016720 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
16721 * @phba: Pointer to HBA context object.
16722 * @xri: xri id in transaction.
16723 *
16724 * This function validates the xri maps to the known range of XRIs allocated an
16725 * used by the driver.
16726 **/
James Smart7851fe22011-07-22 18:36:52 -040016727uint16_t
James Smart6d368e52011-05-24 11:44:12 -040016728lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
16729 uint16_t xri)
16730{
James Smarta2fc4aef2014-09-03 12:57:55 -040016731 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040016732
16733 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
16734 if (xri == phba->sli4_hba.xri_ids[i])
16735 return i;
16736 }
16737 return NO_XRI;
16738}
16739
James Smart6d368e52011-05-24 11:44:12 -040016740/**
James Smart546fc852011-03-11 16:06:29 -050016741 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016742 * @phba: Pointer to HBA context object.
16743 * @fc_hdr: pointer to a FC frame header.
16744 *
James Smart546fc852011-03-11 16:06:29 -050016745 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040016746 * event after aborting the sequence handling.
16747 **/
James Smart86c67372017-04-21 16:05:04 -070016748void
James Smart6dd9e312013-01-03 15:43:37 -050016749lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
16750 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040016751{
James Smart6dd9e312013-01-03 15:43:37 -050016752 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040016753 struct lpfc_iocbq *ctiocb = NULL;
16754 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040016755 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050016756 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040016757 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050016758 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040016759
16760 if (!lpfc_is_link_up(phba))
16761 return;
16762
16763 sid = sli4_sid_from_fc_hdr(fc_hdr);
16764 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050016765 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040016766
James Smart6dd9e312013-01-03 15:43:37 -050016767 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040016768 if (!ndlp) {
James Smart9d3d3402017-04-21 16:05:00 -070016769 ndlp = lpfc_nlp_init(vport, sid);
James Smart6dd9e312013-01-03 15:43:37 -050016770 if (!ndlp) {
16771 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16772 "1268 Failed to allocate ndlp for "
16773 "oxid:x%x SID:x%x\n", oxid, sid);
16774 return;
16775 }
James Smart6dd9e312013-01-03 15:43:37 -050016776 /* Put ndlp onto pport node list */
16777 lpfc_enqueue_node(vport, ndlp);
16778 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
16779 /* re-setup ndlp without removing from node list */
16780 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
16781 if (!ndlp) {
16782 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
16783 "3275 Failed to active ndlp found "
16784 "for oxid:x%x SID:x%x\n", oxid, sid);
16785 return;
16786 }
James Smart6669f9b2009-10-02 15:16:45 -040016787 }
16788
James Smart546fc852011-03-11 16:06:29 -050016789 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040016790 ctiocb = lpfc_sli_get_iocbq(phba);
16791 if (!ctiocb)
16792 return;
16793
James Smart5ffc2662009-11-18 15:39:44 -050016794 /* Extract the F_CTL field from FC_HDR */
16795 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
16796
James Smart6669f9b2009-10-02 15:16:45 -040016797 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040016798 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050016799 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040016800 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
16801 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
16802 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
16803
16804 /* Fill in the rest of iocb fields */
16805 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
16806 icmd->ulpBdeCount = 0;
16807 icmd->ulpLe = 1;
16808 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040016809 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050016810 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040016811
James Smart6669f9b2009-10-02 15:16:45 -040016812 ctiocb->iocb_cmpl = NULL;
16813 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050016814 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040016815 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050016816 ctiocb->sli4_xritag = NO_XRI;
16817
James Smartee0f4fe2012-05-09 21:19:14 -040016818 if (fctl & FC_FC_EX_CTX)
16819 /* Exchange responder sent the abort so we
16820 * own the oxid.
16821 */
16822 xri = oxid;
16823 else
16824 xri = rxid;
16825 lxri = lpfc_sli4_xri_inrange(phba, xri);
16826 if (lxri != NO_XRI)
16827 lpfc_set_rrq_active(phba, ndlp, lxri,
16828 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050016829 /* For BA_ABTS from exchange responder, if the logical xri with
16830 * the oxid maps to the FCP XRI range, the port no longer has
16831 * that exchange context, send a BLS_RJT. Override the IOCB for
16832 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050016833 */
James Smart6dd9e312013-01-03 15:43:37 -050016834 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080016835 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050016836 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16837 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16838 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16839 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16840 }
16841
16842 /* If BA_ABTS failed to abort a partially assembled receive sequence,
16843 * the driver no longer has that exchange, send a BLS_RJT. Override
16844 * the IOCB for a BA_RJT.
16845 */
16846 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050016847 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
16848 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
16849 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
16850 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
16851 }
James Smart6669f9b2009-10-02 15:16:45 -040016852
James Smart5ffc2662009-11-18 15:39:44 -050016853 if (fctl & FC_FC_EX_CTX) {
16854 /* ABTS sent by responder to CT exchange, construction
16855 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
16856 * field and RX_ID from ABTS for RX_ID field.
16857 */
James Smart546fc852011-03-11 16:06:29 -050016858 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050016859 } else {
16860 /* ABTS sent by initiator to CT exchange, construction
16861 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050016862 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050016863 */
James Smart546fc852011-03-11 16:06:29 -050016864 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050016865 }
James Smartf09c3ac2012-03-01 22:33:29 -050016866 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050016867 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050016868
James Smart546fc852011-03-11 16:06:29 -050016869 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050016870 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
16871 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
16872 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050016873
16874 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16875 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050016876 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
16877 "2925 Failed to issue CT ABTS RSP x%x on "
16878 "xri x%x, Data x%x\n",
16879 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
16880 phba->link_state);
16881 lpfc_nlp_put(ndlp);
16882 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050016883 lpfc_sli_release_iocbq(phba, ctiocb);
16884 }
James Smart6669f9b2009-10-02 15:16:45 -040016885}
16886
16887/**
16888 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
16889 * @vport: Pointer to the vport on which this sequence was received
16890 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16891 *
16892 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
16893 * receive sequence is only partially assembed by the driver, it shall abort
16894 * the partially assembled frames for the sequence. Otherwise, if the
16895 * unsolicited receive sequence has been completely assembled and passed to
16896 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
16897 * unsolicited sequence has been aborted. After that, it will issue a basic
16898 * accept to accept the abort.
16899 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016900static void
James Smart6669f9b2009-10-02 15:16:45 -040016901lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
16902 struct hbq_dmabuf *dmabuf)
16903{
16904 struct lpfc_hba *phba = vport->phba;
16905 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050016906 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050016907 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040016908
James Smart6669f9b2009-10-02 15:16:45 -040016909 /* Make a copy of fc_hdr before the dmabuf being released */
16910 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050016911 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040016912
James Smart5ffc2662009-11-18 15:39:44 -050016913 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050016914 /* ABTS by responder to exchange, no cleanup needed */
16915 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050016916 } else {
James Smart6dd9e312013-01-03 15:43:37 -050016917 /* ABTS by initiator to exchange, need to do cleanup */
16918 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
16919 if (aborted == false)
16920 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050016921 }
James Smart6dd9e312013-01-03 15:43:37 -050016922 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16923
James Smart86c67372017-04-21 16:05:04 -070016924 if (phba->nvmet_support) {
16925 lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
16926 return;
16927 }
16928
James Smart6dd9e312013-01-03 15:43:37 -050016929 /* Respond with BA_ACC or BA_RJT accordingly */
16930 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040016931}
16932
16933/**
James Smart4f774512009-05-22 14:52:35 -040016934 * lpfc_seq_complete - Indicates if a sequence is complete
16935 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16936 *
16937 * This function checks the sequence, starting with the frame described by
16938 * @dmabuf, to see if all the frames associated with this sequence are present.
16939 * the frames associated with this sequence are linked to the @dmabuf using the
16940 * dbuf list. This function looks for two major things. 1) That the first frame
16941 * has a sequence count of zero. 2) There is a frame with last frame of sequence
16942 * set. 3) That there are no holes in the sequence count. The function will
16943 * return 1 when the sequence is complete, otherwise it will return 0.
16944 **/
16945static int
16946lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
16947{
16948 struct fc_frame_header *hdr;
16949 struct lpfc_dmabuf *d_buf;
16950 struct hbq_dmabuf *seq_dmabuf;
16951 uint32_t fctl;
16952 int seq_count = 0;
16953
16954 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
16955 /* make sure first fame of sequence has a sequence count of zero */
16956 if (hdr->fh_seq_cnt != seq_count)
16957 return 0;
16958 fctl = (hdr->fh_f_ctl[0] << 16 |
16959 hdr->fh_f_ctl[1] << 8 |
16960 hdr->fh_f_ctl[2]);
16961 /* If last frame of sequence we can return success. */
16962 if (fctl & FC_FC_END_SEQ)
16963 return 1;
16964 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
16965 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16966 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16967 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050016968 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040016969 return 0;
16970 fctl = (hdr->fh_f_ctl[0] << 16 |
16971 hdr->fh_f_ctl[1] << 8 |
16972 hdr->fh_f_ctl[2]);
16973 /* If last frame of sequence we can return success. */
16974 if (fctl & FC_FC_END_SEQ)
16975 return 1;
16976 }
16977 return 0;
16978}
16979
16980/**
16981 * lpfc_prep_seq - Prep sequence for ULP processing
16982 * @vport: Pointer to the vport on which this sequence was received
16983 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16984 *
16985 * This function takes a sequence, described by a list of frames, and creates
16986 * a list of iocbq structures to describe the sequence. This iocbq list will be
16987 * used to issue to the generic unsolicited sequence handler. This routine
16988 * returns a pointer to the first iocbq in the list. If the function is unable
16989 * to allocate an iocbq then it throw out the received frames that were not
16990 * able to be described and return a pointer to the first iocbq. If unable to
16991 * allocate any iocbqs (including the first) this function will return NULL.
16992 **/
16993static struct lpfc_iocbq *
16994lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16995{
James Smart7851fe22011-07-22 18:36:52 -040016996 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040016997 struct lpfc_dmabuf *d_buf, *n_buf;
16998 struct lpfc_iocbq *first_iocbq, *iocbq;
16999 struct fc_frame_header *fc_hdr;
17000 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040017001 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050017002 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040017003
17004 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17005 /* remove from receive buffer list */
17006 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040017007 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040017008 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040017009 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040017010 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017011 /* Get an iocbq struct to fill in. */
17012 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
17013 if (first_iocbq) {
17014 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040017015 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040017016 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080017017 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040017018
17019 /* Check FC Header to see what TYPE of frame we are rcv'ing */
17020 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
17021 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
17022 first_iocbq->iocb.un.rcvels.parmRo =
17023 sli4_did_from_fc_hdr(fc_hdr);
17024 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
17025 } else
17026 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040017027 first_iocbq->iocb.ulpContext = NO_XRI;
17028 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
17029 be16_to_cpu(fc_hdr->fh_ox_id);
17030 /* iocbq is prepped for internal consumption. Physical vpi. */
17031 first_iocbq->iocb.unsli3.rcvsli3.vpi =
17032 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040017033 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040017034 tot_len = bf_get(lpfc_rcqe_length,
17035 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
17036
James Smart4f774512009-05-22 14:52:35 -040017037 first_iocbq->context2 = &seq_dmabuf->dbuf;
17038 first_iocbq->context3 = NULL;
17039 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040017040 if (tot_len > LPFC_DATA_BUF_SIZE)
17041 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040017042 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040017043 else
17044 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
17045
James Smart4f774512009-05-22 14:52:35 -040017046 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040017047
James Smart7851fe22011-07-22 18:36:52 -040017048 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040017049 }
17050 iocbq = first_iocbq;
17051 /*
17052 * Each IOCBq can have two Buffers assigned, so go through the list
17053 * of buffers for this sequence and save two buffers in each IOCBq
17054 */
17055 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
17056 if (!iocbq) {
17057 lpfc_in_buf_free(vport->phba, d_buf);
17058 continue;
17059 }
17060 if (!iocbq->context3) {
17061 iocbq->context3 = d_buf;
17062 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040017063 /* We need to get the size out of the right CQE */
17064 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17065 len = bf_get(lpfc_rcqe_length,
17066 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017067 pbde = (struct ulp_bde64 *)
17068 &iocbq->iocb.unsli3.sli3Words[4];
17069 if (len > LPFC_DATA_BUF_SIZE)
17070 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
17071 else
17072 pbde->tus.f.bdeSize = len;
17073
James Smart7851fe22011-07-22 18:36:52 -040017074 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
17075 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040017076 } else {
17077 iocbq = lpfc_sli_get_iocbq(vport->phba);
17078 if (!iocbq) {
17079 if (first_iocbq) {
17080 first_iocbq->iocb.ulpStatus =
17081 IOSTAT_FCP_RSP_ERROR;
17082 first_iocbq->iocb.un.ulpWord[4] =
17083 IOERR_NO_RESOURCES;
17084 }
17085 lpfc_in_buf_free(vport->phba, d_buf);
17086 continue;
17087 }
James Smart7851fe22011-07-22 18:36:52 -040017088 /* We need to get the size out of the right CQE */
17089 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
17090 len = bf_get(lpfc_rcqe_length,
17091 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040017092 iocbq->context2 = d_buf;
17093 iocbq->context3 = NULL;
17094 iocbq->iocb.ulpBdeCount = 1;
17095 if (len > LPFC_DATA_BUF_SIZE)
17096 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
17097 LPFC_DATA_BUF_SIZE;
17098 else
17099 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
17100
James Smart7851fe22011-07-22 18:36:52 -040017101 tot_len += len;
17102 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
17103
James Smart4f774512009-05-22 14:52:35 -040017104 iocbq->iocb.un.rcvels.remoteID = sid;
17105 list_add_tail(&iocbq->list, &first_iocbq->list);
17106 }
17107 }
17108 return first_iocbq;
17109}
17110
James Smart6669f9b2009-10-02 15:16:45 -040017111static void
17112lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
17113 struct hbq_dmabuf *seq_dmabuf)
17114{
17115 struct fc_frame_header *fc_hdr;
17116 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
17117 struct lpfc_hba *phba = vport->phba;
17118
17119 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
17120 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
17121 if (!iocbq) {
17122 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17123 "2707 Ring %d handler: Failed to allocate "
17124 "iocb Rctl x%x Type x%x received\n",
17125 LPFC_ELS_RING,
17126 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17127 return;
17128 }
17129 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080017130 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040017131 iocbq, fc_hdr->fh_r_ctl,
17132 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040017133 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040017134 "2540 Ring %d handler: unexpected Rctl "
17135 "x%x Type x%x received\n",
17136 LPFC_ELS_RING,
17137 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
17138
17139 /* Free iocb created in lpfc_prep_seq */
17140 list_for_each_entry_safe(curr_iocb, next_iocb,
17141 &iocbq->list, list) {
17142 list_del_init(&curr_iocb->list);
17143 lpfc_sli_release_iocbq(phba, curr_iocb);
17144 }
17145 lpfc_sli_release_iocbq(phba, iocbq);
17146}
17147
James Smartae9e28f2017-05-15 15:20:51 -070017148static void
17149lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
17150 struct lpfc_iocbq *rspiocb)
17151{
17152 struct lpfc_dmabuf *pcmd = cmdiocb->context2;
17153
17154 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017155 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017156 kfree(pcmd);
17157 lpfc_sli_release_iocbq(phba, cmdiocb);
17158}
17159
17160static void
17161lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
17162 struct hbq_dmabuf *dmabuf)
17163{
17164 struct fc_frame_header *fc_hdr;
17165 struct lpfc_hba *phba = vport->phba;
17166 struct lpfc_iocbq *iocbq = NULL;
17167 union lpfc_wqe *wqe;
17168 struct lpfc_dmabuf *pcmd = NULL;
17169 uint32_t frame_len;
17170 int rc;
17171
17172 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
17173 frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
17174
17175 /* Send the received frame back */
17176 iocbq = lpfc_sli_get_iocbq(phba);
17177 if (!iocbq)
17178 goto exit;
17179
17180 /* Allocate buffer for command payload */
17181 pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17182 if (pcmd)
Romain Perier771db5c2017-07-06 10:13:05 +020017183 pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
James Smartae9e28f2017-05-15 15:20:51 -070017184 &pcmd->phys);
17185 if (!pcmd || !pcmd->virt)
17186 goto exit;
17187
17188 INIT_LIST_HEAD(&pcmd->list);
17189
17190 /* copyin the payload */
17191 memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
17192
17193 /* fill in BDE's for command */
17194 iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
17195 iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
17196 iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
17197 iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
17198
17199 iocbq->context2 = pcmd;
17200 iocbq->vport = vport;
17201 iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
17202 iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
17203
17204 /*
17205 * Setup rest of the iocb as though it were a WQE
17206 * Build the SEND_FRAME WQE
17207 */
17208 wqe = (union lpfc_wqe *)&iocbq->iocb;
17209
17210 wqe->send_frame.frame_len = frame_len;
17211 wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
17212 wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
17213 wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
17214 wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
17215 wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
17216 wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
17217
17218 iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
17219 iocbq->iocb.ulpLe = 1;
17220 iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
17221 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
17222 if (rc == IOCB_ERROR)
17223 goto exit;
17224
17225 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17226 return;
17227
17228exit:
17229 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
17230 "2023 Unable to process MDS loopback frame\n");
17231 if (pcmd && pcmd->virt)
Romain Perier771db5c2017-07-06 10:13:05 +020017232 dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
James Smartae9e28f2017-05-15 15:20:51 -070017233 kfree(pcmd);
Dick Kennedy401bb412017-09-29 17:34:28 -070017234 if (iocbq)
17235 lpfc_sli_release_iocbq(phba, iocbq);
James Smartae9e28f2017-05-15 15:20:51 -070017236 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17237}
17238
James Smart4f774512009-05-22 14:52:35 -040017239/**
17240 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
17241 * @phba: Pointer to HBA context object.
17242 *
17243 * This function is called with no lock held. This function processes all
17244 * the received buffers and gives it to upper layers when a received buffer
17245 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080017246 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040017247 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
17248 * appropriate receive function when the final frame in a sequence is received.
17249 **/
James Smart4d9ab992009-10-02 15:16:39 -040017250void
17251lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
17252 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040017253{
James Smart4d9ab992009-10-02 15:16:39 -040017254 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040017255 struct fc_frame_header *fc_hdr;
17256 struct lpfc_vport *vport;
17257 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040017258 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040017259
James Smart4f774512009-05-22 14:52:35 -040017260 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040017261 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080017262
James Smart4d9ab992009-10-02 15:16:39 -040017263 /* check to see if this a valid type of frame */
17264 if (lpfc_fc_frame_check(phba, fc_hdr)) {
17265 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17266 return;
17267 }
James Smart2ea259e2017-02-12 13:52:27 -080017268
James Smart7851fe22011-07-22 18:36:52 -040017269 if ((bf_get(lpfc_cqe_code,
17270 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
17271 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
17272 &dmabuf->cq_event.cqe.rcqe_cmpl);
17273 else
17274 fcfi = bf_get(lpfc_rcqe_fcf_id,
17275 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040017276
James Smartae9e28f2017-05-15 15:20:51 -070017277 if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
17278 vport = phba->pport;
17279 /* Handle MDS Loopback frames */
17280 lpfc_sli4_handle_mds_loopback(vport, dmabuf);
17281 return;
17282 }
17283
James Smart895427b2017-02-12 13:52:30 -080017284 /* d_id this frame is directed to */
17285 did = sli4_did_from_fc_hdr(fc_hdr);
17286
17287 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040017288 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040017289 /* throw out the frame */
17290 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17291 return;
17292 }
James Smart939723a2012-05-09 21:19:03 -040017293
James Smart939723a2012-05-09 21:19:03 -040017294 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
17295 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
17296 (did != Fabric_DID)) {
17297 /*
17298 * Throw out the frame if we are not pt2pt.
17299 * The pt2pt protocol allows for discovery frames
17300 * to be received without a registered VPI.
17301 */
17302 if (!(vport->fc_flag & FC_PT2PT) ||
17303 (phba->link_state == LPFC_HBA_READY)) {
17304 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17305 return;
17306 }
17307 }
17308
James Smart6669f9b2009-10-02 15:16:45 -040017309 /* Handle the basic abort sequence (BA_ABTS) event */
17310 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
17311 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
17312 return;
17313 }
17314
James Smart4d9ab992009-10-02 15:16:39 -040017315 /* Link this frame */
17316 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
17317 if (!seq_dmabuf) {
17318 /* unable to add frame to vport - throw it out */
17319 lpfc_in_buf_free(phba, &dmabuf->dbuf);
17320 return;
17321 }
17322 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050017323 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040017324 return;
James Smartdef9c7a2009-12-21 17:02:28 -050017325
James Smart6669f9b2009-10-02 15:16:45 -040017326 /* Send the complete sequence to the upper layer protocol */
17327 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040017328}
James Smart6fb120a2009-05-22 14:52:59 -040017329
17330/**
17331 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
17332 * @phba: pointer to lpfc hba data structure.
17333 *
17334 * This routine is invoked to post rpi header templates to the
17335 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017336 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17337 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017338 *
17339 * This routine does not require any locks. It's usage is expected
17340 * to be driver load or reset recovery when the driver is
17341 * sequential.
17342 *
17343 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017344 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017345 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017346 * When this error occurs, the driver is not guaranteed
17347 * to have any rpi regions posted to the device and
17348 * must either attempt to repost the regions or take a
17349 * fatal error.
17350 **/
17351int
17352lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
17353{
17354 struct lpfc_rpi_hdr *rpi_page;
17355 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040017356 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017357
James Smart6d368e52011-05-24 11:44:12 -040017358 /* SLI4 ports that support extents do not require RPI headers. */
17359 if (!phba->sli4_hba.rpi_hdrs_in_use)
17360 goto exit;
17361 if (phba->sli4_hba.extents_in_use)
17362 return -EIO;
17363
James Smart6fb120a2009-05-22 14:52:59 -040017364 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040017365 /*
17366 * Assign the rpi headers a physical rpi only if the driver
17367 * has not initialized those resources. A port reset only
17368 * needs the headers posted.
17369 */
17370 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
17371 LPFC_RPI_RSRC_RDY)
17372 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
17373
James Smart6fb120a2009-05-22 14:52:59 -040017374 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
17375 if (rc != MBX_SUCCESS) {
17376 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17377 "2008 Error %d posting all rpi "
17378 "headers\n", rc);
17379 rc = -EIO;
17380 break;
17381 }
17382 }
17383
James Smart6d368e52011-05-24 11:44:12 -040017384 exit:
17385 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
17386 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040017387 return rc;
17388}
17389
17390/**
17391 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
17392 * @phba: pointer to lpfc hba data structure.
17393 * @rpi_page: pointer to the rpi memory region.
17394 *
17395 * This routine is invoked to post a single rpi header to the
17396 * HBA consistent with the SLI-4 interface spec. This memory region
17397 * maps up to 64 rpi context regions.
17398 *
17399 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017400 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040017401 * -ENOMEM - No available memory
17402 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040017403 **/
17404int
17405lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
17406{
17407 LPFC_MBOXQ_t *mboxq;
17408 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
17409 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017410 uint32_t shdr_status, shdr_add_status;
17411 union lpfc_sli4_cfg_shdr *shdr;
17412
James Smart6d368e52011-05-24 11:44:12 -040017413 /* SLI4 ports that support extents do not require RPI headers. */
17414 if (!phba->sli4_hba.rpi_hdrs_in_use)
17415 return rc;
17416 if (phba->sli4_hba.extents_in_use)
17417 return -EIO;
17418
James Smart6fb120a2009-05-22 14:52:59 -040017419 /* The port is notified of the header region via a mailbox command. */
17420 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17421 if (!mboxq) {
17422 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17423 "2001 Unable to allocate memory for issuing "
17424 "SLI_CONFIG_SPECIAL mailbox command\n");
17425 return -ENOMEM;
17426 }
17427
17428 /* Post all rpi memory regions to the port. */
17429 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040017430 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17431 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
17432 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050017433 sizeof(struct lpfc_sli4_cfg_mhdr),
17434 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040017435
17436
17437 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040017438 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
17439 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040017440 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
17441 hdr_tmpl, rpi_page->page_count);
17442
James Smart6fb120a2009-05-22 14:52:59 -040017443 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
17444 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040017445 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040017446 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
17447 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17448 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17449 if (rc != MBX_TIMEOUT)
17450 mempool_free(mboxq, phba->mbox_mem_pool);
17451 if (shdr_status || shdr_add_status || rc) {
17452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17453 "2514 POST_RPI_HDR mailbox failed with "
17454 "status x%x add_status x%x, mbx status x%x\n",
17455 shdr_status, shdr_add_status, rc);
17456 rc = -ENXIO;
James Smart845d9e82017-05-15 15:20:38 -070017457 } else {
17458 /*
17459 * The next_rpi stores the next logical module-64 rpi value used
17460 * to post physical rpis in subsequent rpi postings.
17461 */
17462 spin_lock_irq(&phba->hbalock);
17463 phba->sli4_hba.next_rpi = rpi_page->next_rpi;
17464 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040017465 }
17466 return rc;
17467}
17468
17469/**
17470 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
17471 * @phba: pointer to lpfc hba data structure.
17472 *
17473 * This routine is invoked to post rpi header templates to the
17474 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040017475 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
17476 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040017477 *
17478 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020017479 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040017480 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
17481 **/
17482int
17483lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
17484{
James Smart6d368e52011-05-24 11:44:12 -040017485 unsigned long rpi;
17486 uint16_t max_rpi, rpi_limit;
17487 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017488 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040017489 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040017490
James Smart6fb120a2009-05-22 14:52:59 -040017491 /*
James Smart6d368e52011-05-24 11:44:12 -040017492 * Fetch the next logical rpi. Because this index is logical,
17493 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040017494 */
James Smart4902b382013-10-10 12:20:35 -040017495 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040017496 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
17497 rpi_limit = phba->sli4_hba.next_rpi;
17498
James Smart6d368e52011-05-24 11:44:12 -040017499 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
17500 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040017501 rpi = LPFC_RPI_ALLOC_ERROR;
17502 else {
17503 set_bit(rpi, phba->sli4_hba.rpi_bmask);
17504 phba->sli4_hba.max_cfg_param.rpi_used++;
17505 phba->sli4_hba.rpi_count++;
17506 }
James Smartbe6bb942015-04-07 15:07:22 -040017507 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
17508 "0001 rpi:%x max:%x lim:%x\n",
17509 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040017510
17511 /*
17512 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040017513 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040017514 */
17515 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
17516 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040017517 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017518 return rpi;
17519 }
17520
17521 /*
James Smart6d368e52011-05-24 11:44:12 -040017522 * RPI header postings are not required for SLI4 ports capable of
17523 * extents.
17524 */
17525 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040017526 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040017527 return rpi;
17528 }
17529
17530 /*
James Smart6fb120a2009-05-22 14:52:59 -040017531 * If the driver is running low on rpi resources, allocate another
17532 * page now. Note that the next_rpi value is used because
17533 * it represents how many are actually in use whereas max_rpi notes
17534 * how many are supported max by the device.
17535 */
James Smart6d368e52011-05-24 11:44:12 -040017536 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040017537 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040017538 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
17539 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
17540 if (!rpi_hdr) {
17541 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17542 "2002 Error Could not grow rpi "
17543 "count\n");
17544 } else {
James Smart6d368e52011-05-24 11:44:12 -040017545 lrpi = rpi_hdr->start_rpi;
17546 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040017547 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
17548 }
17549 }
17550
17551 return rpi;
17552}
17553
17554/**
17555 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17556 * @phba: pointer to lpfc hba data structure.
17557 *
17558 * This routine is invoked to release an rpi to the pool of
17559 * available rpis maintained by the driver.
17560 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017561static void
James Smartd7c47992010-06-08 18:31:54 -040017562__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17563{
17564 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
17565 phba->sli4_hba.rpi_count--;
17566 phba->sli4_hba.max_cfg_param.rpi_used--;
17567 }
17568}
17569
17570/**
17571 * lpfc_sli4_free_rpi - Release an rpi for reuse.
17572 * @phba: pointer to lpfc hba data structure.
17573 *
17574 * This routine is invoked to release an rpi to the pool of
17575 * available rpis maintained by the driver.
17576 **/
17577void
James Smart6fb120a2009-05-22 14:52:59 -040017578lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
17579{
17580 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040017581 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040017582 spin_unlock_irq(&phba->hbalock);
17583}
17584
17585/**
17586 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
17587 * @phba: pointer to lpfc hba data structure.
17588 *
17589 * This routine is invoked to remove the memory region that
17590 * provided rpi via a bitmask.
17591 **/
17592void
17593lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
17594{
17595 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040017596 kfree(phba->sli4_hba.rpi_ids);
17597 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040017598}
17599
17600/**
17601 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
17602 * @phba: pointer to lpfc hba data structure.
17603 *
17604 * This routine is invoked to remove the memory region that
17605 * provided rpi via a bitmask.
17606 **/
17607int
James Smart6b5151f2012-01-18 16:24:06 -050017608lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
17609 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040017610{
17611 LPFC_MBOXQ_t *mboxq;
17612 struct lpfc_hba *phba = ndlp->phba;
17613 int rc;
17614
17615 /* The port is notified of the header region via a mailbox command. */
17616 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17617 if (!mboxq)
17618 return -ENOMEM;
17619
17620 /* Post all rpi memory regions to the port. */
17621 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050017622 if (cmpl) {
17623 mboxq->mbox_cmpl = cmpl;
17624 mboxq->context1 = arg;
17625 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050017626 } else
17627 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050017628 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040017629 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17630 if (rc == MBX_NOT_FINISHED) {
17631 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17632 "2010 Resume RPI Mailbox failed "
17633 "status %d, mbxStatus x%x\n", rc,
17634 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
17635 mempool_free(mboxq, phba->mbox_mem_pool);
17636 return -EIO;
17637 }
17638 return 0;
17639}
17640
17641/**
17642 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050017643 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040017644 *
James Smart76a95d72010-11-20 23:11:48 -050017645 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040017646 *
17647 * Returns:
17648 * 0 success
17649 * -Evalue otherwise
17650 **/
17651int
James Smart76a95d72010-11-20 23:11:48 -050017652lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040017653{
17654 LPFC_MBOXQ_t *mboxq;
17655 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040017656 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040017657 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050017658 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040017659 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17660 if (!mboxq)
17661 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050017662 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040017663 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040017664 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040017665 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050017666 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040017667 "2022 INIT VPI Mailbox failed "
17668 "status %d, mbxStatus x%x\n", rc,
17669 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040017670 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040017671 }
James Smart6a9c52c2009-10-02 15:16:51 -040017672 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050017673 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040017674
17675 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040017676}
17677
17678/**
17679 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
17680 * @phba: pointer to lpfc hba data structure.
17681 * @mboxq: Pointer to mailbox object.
17682 *
17683 * This routine is invoked to manually add a single FCF record. The caller
17684 * must pass a completely initialized FCF_Record. This routine takes
17685 * care of the nonembedded mailbox operations.
17686 **/
17687static void
17688lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
17689{
17690 void *virt_addr;
17691 union lpfc_sli4_cfg_shdr *shdr;
17692 uint32_t shdr_status, shdr_add_status;
17693
17694 virt_addr = mboxq->sge_array->addr[0];
17695 /* The IOCTL status is embedded in the mailbox subheader. */
17696 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
17697 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17698 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17699
17700 if ((shdr_status || shdr_add_status) &&
17701 (shdr_status != STATUS_FCF_IN_USE))
17702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17703 "2558 ADD_FCF_RECORD mailbox failed with "
17704 "status x%x add_status x%x\n",
17705 shdr_status, shdr_add_status);
17706
17707 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17708}
17709
17710/**
17711 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
17712 * @phba: pointer to lpfc hba data structure.
17713 * @fcf_record: pointer to the initialized fcf record to add.
17714 *
17715 * This routine is invoked to manually add a single FCF record. The caller
17716 * must pass a completely initialized FCF_Record. This routine takes
17717 * care of the nonembedded mailbox operations.
17718 **/
17719int
17720lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
17721{
17722 int rc = 0;
17723 LPFC_MBOXQ_t *mboxq;
17724 uint8_t *bytep;
17725 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040017726 struct lpfc_mbx_sge sge;
17727 uint32_t alloc_len, req_len;
17728 uint32_t fcfindex;
17729
17730 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17731 if (!mboxq) {
17732 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17733 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
17734 return -ENOMEM;
17735 }
17736
17737 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
17738 sizeof(uint32_t);
17739
17740 /* Allocate DMA memory and set up the non-embedded mailbox command */
17741 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
17742 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
17743 req_len, LPFC_SLI4_MBX_NEMBED);
17744 if (alloc_len < req_len) {
17745 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17746 "2523 Allocated DMA memory size (x%x) is "
17747 "less than the requested DMA memory "
17748 "size (x%x)\n", alloc_len, req_len);
17749 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17750 return -ENOMEM;
17751 }
17752
17753 /*
17754 * Get the first SGE entry from the non-embedded DMA memory. This
17755 * routine only uses a single SGE.
17756 */
17757 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040017758 virt_addr = mboxq->sge_array->addr[0];
17759 /*
17760 * Configure the FCF record for FCFI 0. This is the driver's
17761 * hardcoded default and gets used in nonFIP mode.
17762 */
17763 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
17764 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
17765 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
17766
17767 /*
17768 * Copy the fcf_index and the FCF Record Data. The data starts after
17769 * the FCoE header plus word10. The data copy needs to be endian
17770 * correct.
17771 */
17772 bytep += sizeof(uint32_t);
17773 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
17774 mboxq->vport = phba->pport;
17775 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
17776 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17777 if (rc == MBX_NOT_FINISHED) {
17778 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17779 "2515 ADD_FCF_RECORD mailbox failed with "
17780 "status 0x%x\n", rc);
17781 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17782 rc = -EIO;
17783 } else
17784 rc = 0;
17785
17786 return rc;
17787}
17788
17789/**
17790 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
17791 * @phba: pointer to lpfc hba data structure.
17792 * @fcf_record: pointer to the fcf record to write the default data.
17793 * @fcf_index: FCF table entry index.
17794 *
17795 * This routine is invoked to build the driver's default FCF record. The
17796 * values used are hardcoded. This routine handles memory initialization.
17797 *
17798 **/
17799void
17800lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
17801 struct fcf_record *fcf_record,
17802 uint16_t fcf_index)
17803{
17804 memset(fcf_record, 0, sizeof(struct fcf_record));
17805 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
17806 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
17807 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
17808 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
17809 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
17810 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
17811 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
17812 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
17813 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
17814 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
17815 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
17816 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
17817 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040017818 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040017819 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
17820 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
17821 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
17822 /* Set the VLAN bit map */
17823 if (phba->valid_vlan) {
17824 fcf_record->vlan_bitmap[phba->vlan_id / 8]
17825 = 1 << (phba->vlan_id % 8);
17826 }
17827}
17828
17829/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017830 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040017831 * @phba: pointer to lpfc hba data structure.
17832 * @fcf_index: FCF table entry offset.
17833 *
James Smart0c9ab6f2010-02-26 14:15:57 -050017834 * This routine is invoked to scan the entire FCF table by reading FCF
17835 * record and processing it one at a time starting from the @fcf_index
17836 * for initial FCF discovery or fast FCF failover rediscovery.
17837 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017838 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017839 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040017840 **/
17841int
James Smart0c9ab6f2010-02-26 14:15:57 -050017842lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040017843{
17844 int rc = 0, error;
17845 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040017846
James Smart32b97932009-07-19 10:01:21 -040017847 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050017848 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040017849 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17850 if (!mboxq) {
17851 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17852 "2000 Failed to allocate mbox for "
17853 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040017854 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050017855 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017856 }
James Smartecfd03c2010-02-12 14:41:27 -050017857 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050017858 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050017859 if (rc) {
17860 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050017861 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040017862 }
James Smartecfd03c2010-02-12 14:41:27 -050017863 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040017864 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050017865 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040017866
17867 spin_lock_irq(&phba->hbalock);
17868 phba->hba_flag |= FCF_TS_INPROG;
17869 spin_unlock_irq(&phba->hbalock);
17870
James Smart6fb120a2009-05-22 14:52:59 -040017871 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050017872 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040017873 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050017874 else {
James Smart38b92ef2010-08-04 16:11:39 -040017875 /* Reset eligible FCF count for new scan */
17876 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040017877 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040017878 error = 0;
James Smart32b97932009-07-19 10:01:21 -040017879 }
James Smart0c9ab6f2010-02-26 14:15:57 -050017880fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040017881 if (error) {
17882 if (mboxq)
17883 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040017884 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040017885 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040017886 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040017887 spin_unlock_irq(&phba->hbalock);
17888 }
James Smart6fb120a2009-05-22 14:52:59 -040017889 return error;
17890}
James Smarta0c87cb2009-07-19 10:01:10 -040017891
17892/**
James Smarta93ff372010-10-22 11:06:08 -040017893 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050017894 * @phba: pointer to lpfc hba data structure.
17895 * @fcf_index: FCF table entry offset.
17896 *
17897 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040017898 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050017899 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017900 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017901 * otherwise.
17902 **/
17903int
17904lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17905{
17906 int rc = 0, error;
17907 LPFC_MBOXQ_t *mboxq;
17908
17909 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17910 if (!mboxq) {
17911 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17912 "2763 Failed to allocate mbox for "
17913 "READ_FCF cmd\n");
17914 error = -ENOMEM;
17915 goto fail_fcf_read;
17916 }
17917 /* Construct the read FCF record mailbox command */
17918 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17919 if (rc) {
17920 error = -EINVAL;
17921 goto fail_fcf_read;
17922 }
17923 /* Issue the mailbox command asynchronously */
17924 mboxq->vport = phba->pport;
17925 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
17926 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17927 if (rc == MBX_NOT_FINISHED)
17928 error = -EIO;
17929 else
17930 error = 0;
17931
17932fail_fcf_read:
17933 if (error && mboxq)
17934 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17935 return error;
17936}
17937
17938/**
17939 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
17940 * @phba: pointer to lpfc hba data structure.
17941 * @fcf_index: FCF table entry offset.
17942 *
17943 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040017944 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050017945 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030017946 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050017947 * otherwise.
17948 **/
17949int
17950lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
17951{
17952 int rc = 0, error;
17953 LPFC_MBOXQ_t *mboxq;
17954
17955 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17956 if (!mboxq) {
17957 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
17958 "2758 Failed to allocate mbox for "
17959 "READ_FCF cmd\n");
17960 error = -ENOMEM;
17961 goto fail_fcf_read;
17962 }
17963 /* Construct the read FCF record mailbox command */
17964 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
17965 if (rc) {
17966 error = -EINVAL;
17967 goto fail_fcf_read;
17968 }
17969 /* Issue the mailbox command asynchronously */
17970 mboxq->vport = phba->pport;
17971 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
17972 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
17973 if (rc == MBX_NOT_FINISHED)
17974 error = -EIO;
17975 else
17976 error = 0;
17977
17978fail_fcf_read:
17979 if (error && mboxq)
17980 lpfc_sli4_mbox_cmd_free(phba, mboxq);
17981 return error;
17982}
17983
17984/**
James Smartf5cb5302015-12-16 18:11:52 -050017985 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040017986 * phba pointer to the lpfc_hba struct for this port.
17987 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
17988 * routine when the rr_bmask is empty. The FCF indecies are put into the
17989 * rr_bmask based on their priority level. Starting from the highest priority
17990 * to the lowest. The most likely FCF candidate will be in the highest
17991 * priority group. When this routine is called it searches the fcf_pri list for
17992 * next lowest priority group and repopulates the rr_bmask with only those
17993 * fcf_indexes.
17994 * returns:
17995 * 1=success 0=failure
17996 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017997static int
James Smart7d791df2011-07-22 18:37:52 -040017998lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
17999{
18000 uint16_t next_fcf_pri;
18001 uint16_t last_index;
18002 struct lpfc_fcf_pri *fcf_pri;
18003 int rc;
18004 int ret = 0;
18005
18006 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
18007 LPFC_SLI4_FCF_TBL_INDX_MAX);
18008 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
18009 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050018010
18011 /* Verify the priority list has 2 or more entries */
18012 spin_lock_irq(&phba->hbalock);
18013 if (list_empty(&phba->fcf.fcf_pri_list) ||
18014 list_is_singular(&phba->fcf.fcf_pri_list)) {
18015 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040018016 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
18017 "3061 Last IDX %d\n", last_index);
18018 return 0; /* Empty rr list */
18019 }
James Smart25626692013-03-01 16:36:54 -050018020 spin_unlock_irq(&phba->hbalock);
18021
James Smart7d791df2011-07-22 18:37:52 -040018022 next_fcf_pri = 0;
18023 /*
18024 * Clear the rr_bmask and set all of the bits that are at this
18025 * priority.
18026 */
18027 memset(phba->fcf.fcf_rr_bmask, 0,
18028 sizeof(*phba->fcf.fcf_rr_bmask));
18029 spin_lock_irq(&phba->hbalock);
18030 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18031 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
18032 continue;
18033 /*
18034 * the 1st priority that has not FLOGI failed
18035 * will be the highest.
18036 */
18037 if (!next_fcf_pri)
18038 next_fcf_pri = fcf_pri->fcf_rec.priority;
18039 spin_unlock_irq(&phba->hbalock);
18040 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18041 rc = lpfc_sli4_fcf_rr_index_set(phba,
18042 fcf_pri->fcf_rec.fcf_index);
18043 if (rc)
18044 return 0;
18045 }
18046 spin_lock_irq(&phba->hbalock);
18047 }
18048 /*
18049 * if next_fcf_pri was not set above and the list is not empty then
18050 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070018051 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040018052 */
18053 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
18054 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
18055 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
18056 /*
18057 * the 1st priority that has not FLOGI failed
18058 * will be the highest.
18059 */
18060 if (!next_fcf_pri)
18061 next_fcf_pri = fcf_pri->fcf_rec.priority;
18062 spin_unlock_irq(&phba->hbalock);
18063 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
18064 rc = lpfc_sli4_fcf_rr_index_set(phba,
18065 fcf_pri->fcf_rec.fcf_index);
18066 if (rc)
18067 return 0;
18068 }
18069 spin_lock_irq(&phba->hbalock);
18070 }
18071 } else
18072 ret = 1;
18073 spin_unlock_irq(&phba->hbalock);
18074
18075 return ret;
18076}
18077/**
James Smart0c9ab6f2010-02-26 14:15:57 -050018078 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
18079 * @phba: pointer to lpfc hba data structure.
18080 *
18081 * This routine is to get the next eligible FCF record index in a round
18082 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040018083 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050018084 * shall be returned, otherwise, the next eligible FCF record's index
18085 * shall be returned.
18086 **/
18087uint16_t
18088lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
18089{
18090 uint16_t next_fcf_index;
18091
James Smart421c6622013-01-03 15:44:16 -050018092initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040018093 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050018094 next_fcf_index = phba->fcf.current_rec.fcf_indx;
18095
James Smart7d791df2011-07-22 18:37:52 -040018096next_priority:
James Smart421c6622013-01-03 15:44:16 -050018097 /* Determine the next fcf index to check */
18098 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050018099 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18100 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040018101 next_fcf_index);
18102
James Smart0c9ab6f2010-02-26 14:15:57 -050018103 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040018104 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18105 /*
18106 * If we have wrapped then we need to clear the bits that
18107 * have been tested so that we can detect when we should
18108 * change the priority level.
18109 */
James Smart0c9ab6f2010-02-26 14:15:57 -050018110 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
18111 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040018112 }
18113
James Smart0c9ab6f2010-02-26 14:15:57 -050018114
James Smart3804dc82010-07-14 15:31:37 -040018115 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040018116 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
18117 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
18118 /*
18119 * If next fcf index is not found check if there are lower
18120 * Priority level fcf's in the fcf_priority list.
18121 * Set up the rr_bmask with all of the avaiable fcf bits
18122 * at that level and continue the selection process.
18123 */
18124 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050018125 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040018126 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18127 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040018128 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
18129 return LPFC_FCOE_FCF_NEXT_NONE;
18130 else {
18131 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
18132 "3063 Only FCF available idx %d, flag %x\n",
18133 next_fcf_index,
18134 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
18135 return next_fcf_index;
18136 }
James Smart3804dc82010-07-14 15:31:37 -040018137 }
18138
James Smart7d791df2011-07-22 18:37:52 -040018139 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
18140 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050018141 LPFC_FCF_FLOGI_FAILED) {
18142 if (list_is_singular(&phba->fcf.fcf_pri_list))
18143 return LPFC_FCOE_FCF_NEXT_NONE;
18144
James Smart7d791df2011-07-22 18:37:52 -040018145 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050018146 }
James Smart7d791df2011-07-22 18:37:52 -040018147
James Smart3804dc82010-07-14 15:31:37 -040018148 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018149 "2845 Get next roundrobin failover FCF (x%x)\n",
18150 next_fcf_index);
18151
James Smart0c9ab6f2010-02-26 14:15:57 -050018152 return next_fcf_index;
18153}
18154
18155/**
18156 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
18157 * @phba: pointer to lpfc hba data structure.
18158 *
18159 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018160 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018161 * does not go beyond the range of the driver allocated bmask dimension
18162 * before setting the bit.
18163 *
18164 * Returns 0 if the index bit successfully set, otherwise, it returns
18165 * -EINVAL.
18166 **/
18167int
18168lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
18169{
18170 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18171 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018172 "2610 FCF (x%x) reached driver's book "
18173 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018174 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18175 return -EINVAL;
18176 }
18177 /* Set the eligible FCF record index bmask */
18178 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
18179
James Smart3804dc82010-07-14 15:31:37 -040018180 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018181 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040018182 "bmask\n", fcf_index);
18183
James Smart0c9ab6f2010-02-26 14:15:57 -050018184 return 0;
18185}
18186
18187/**
James Smart3804dc82010-07-14 15:31:37 -040018188 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050018189 * @phba: pointer to lpfc hba data structure.
18190 *
18191 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040018192 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050018193 * does not go beyond the range of the driver allocated bmask dimension
18194 * before clearing the bit.
18195 **/
18196void
18197lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
18198{
James Smart9a803a72013-09-06 12:17:56 -040018199 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050018200 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
18201 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018202 "2762 FCF (x%x) reached driver's book "
18203 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050018204 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
18205 return;
18206 }
18207 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040018208 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040018209 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
18210 list) {
James Smart7d791df2011-07-22 18:37:52 -040018211 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
18212 list_del_init(&fcf_pri->list);
18213 break;
18214 }
18215 }
18216 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018217 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040018218
18219 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018220 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040018221 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050018222}
18223
18224/**
James Smartecfd03c2010-02-12 14:41:27 -050018225 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
18226 * @phba: pointer to lpfc hba data structure.
18227 *
18228 * This routine is the completion routine for the rediscover FCF table mailbox
18229 * command. If the mailbox command returned failure, it will try to stop the
18230 * FCF rediscover wait timer.
18231 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040018232static void
James Smartecfd03c2010-02-12 14:41:27 -050018233lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
18234{
18235 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18236 uint32_t shdr_status, shdr_add_status;
18237
18238 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18239
18240 shdr_status = bf_get(lpfc_mbox_hdr_status,
18241 &redisc_fcf->header.cfg_shdr.response);
18242 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
18243 &redisc_fcf->header.cfg_shdr.response);
18244 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050018245 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050018246 "2746 Requesting for FCF rediscovery failed "
18247 "status x%x add_status x%x\n",
18248 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050018249 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050018250 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018251 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018252 spin_unlock_irq(&phba->hbalock);
18253 /*
18254 * CVL event triggered FCF rediscover request failed,
18255 * last resort to re-try current registered FCF entry.
18256 */
18257 lpfc_retry_pport_discovery(phba);
18258 } else {
18259 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050018260 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050018261 spin_unlock_irq(&phba->hbalock);
18262 /*
18263 * DEAD FCF event triggered FCF rediscover request
18264 * failed, last resort to fail over as a link down
18265 * to FCF registration.
18266 */
18267 lpfc_sli4_fcf_dead_failthrough(phba);
18268 }
James Smart0c9ab6f2010-02-26 14:15:57 -050018269 } else {
18270 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040018271 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050018272 /*
18273 * Start FCF rediscovery wait timer for pending FCF
18274 * before rescan FCF record table.
18275 */
18276 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050018277 }
James Smartecfd03c2010-02-12 14:41:27 -050018278
18279 mempool_free(mbox, phba->mbox_mem_pool);
18280}
18281
18282/**
James Smart3804dc82010-07-14 15:31:37 -040018283 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050018284 * @phba: pointer to lpfc hba data structure.
18285 *
18286 * This routine is invoked to request for rediscovery of the entire FCF table
18287 * by the port.
18288 **/
18289int
18290lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
18291{
18292 LPFC_MBOXQ_t *mbox;
18293 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
18294 int rc, length;
18295
James Smart0c9ab6f2010-02-26 14:15:57 -050018296 /* Cancel retry delay timers to all vports before FCF rediscover */
18297 lpfc_cancel_all_vport_retry_delay_timer(phba);
18298
James Smartecfd03c2010-02-12 14:41:27 -050018299 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18300 if (!mbox) {
18301 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18302 "2745 Failed to allocate mbox for "
18303 "requesting FCF rediscover.\n");
18304 return -ENOMEM;
18305 }
18306
18307 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
18308 sizeof(struct lpfc_sli4_cfg_mhdr));
18309 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
18310 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
18311 length, LPFC_SLI4_MBX_EMBED);
18312
18313 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
18314 /* Set count to 0 for invalidating the entire FCF database */
18315 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
18316
18317 /* Issue the mailbox command asynchronously */
18318 mbox->vport = phba->pport;
18319 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
18320 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
18321
18322 if (rc == MBX_NOT_FINISHED) {
18323 mempool_free(mbox, phba->mbox_mem_pool);
18324 return -EIO;
18325 }
18326 return 0;
18327}
18328
18329/**
James Smartfc2b9892010-02-26 14:15:29 -050018330 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
18331 * @phba: pointer to lpfc hba data structure.
18332 *
18333 * This function is the failover routine as a last resort to the FCF DEAD
18334 * event when driver failed to perform fast FCF failover.
18335 **/
18336void
18337lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
18338{
18339 uint32_t link_state;
18340
18341 /*
18342 * Last resort as FCF DEAD event failover will treat this as
18343 * a link down, but save the link state because we don't want
18344 * it to be changed to Link Down unless it is already down.
18345 */
18346 link_state = phba->link_state;
18347 lpfc_linkdown(phba);
18348 phba->link_state = link_state;
18349
18350 /* Unregister FCF if no devices connected to it */
18351 lpfc_unregister_unused_fcf(phba);
18352}
18353
18354/**
James Smart026abb82011-12-13 13:20:45 -050018355 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018356 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050018357 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040018358 *
James Smart026abb82011-12-13 13:20:45 -050018359 * This function gets SLI3 port configure region 23 data through memory dump
18360 * mailbox command. When it successfully retrieves data, the size of the data
18361 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040018362 **/
James Smart026abb82011-12-13 13:20:45 -050018363static uint32_t
18364lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040018365{
18366 LPFC_MBOXQ_t *pmb = NULL;
18367 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050018368 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018369 int rc;
18370
James Smart026abb82011-12-13 13:20:45 -050018371 if (!rgn23_data)
18372 return 0;
18373
James Smarta0c87cb2009-07-19 10:01:10 -040018374 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18375 if (!pmb) {
18376 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018377 "2600 failed to allocate mailbox memory\n");
18378 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040018379 }
18380 mb = &pmb->u.mb;
18381
James Smarta0c87cb2009-07-19 10:01:10 -040018382 do {
18383 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
18384 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
18385
18386 if (rc != MBX_SUCCESS) {
18387 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050018388 "2601 failed to read config "
18389 "region 23, rc 0x%x Status 0x%x\n",
18390 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040018391 mb->un.varDmp.word_cnt = 0;
18392 }
18393 /*
18394 * dump mem may return a zero when finished or we got a
18395 * mailbox error, either way we are done.
18396 */
18397 if (mb->un.varDmp.word_cnt == 0)
18398 break;
18399 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
18400 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
18401
18402 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050018403 rgn23_data + offset,
18404 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040018405 offset += mb->un.varDmp.word_cnt;
18406 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
18407
James Smart026abb82011-12-13 13:20:45 -050018408 mempool_free(pmb, phba->mbox_mem_pool);
18409 return offset;
18410}
18411
18412/**
18413 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
18414 * @phba: pointer to lpfc hba data structure.
18415 * @rgn23_data: pointer to configure region 23 data.
18416 *
18417 * This function gets SLI4 port configure region 23 data through memory dump
18418 * mailbox command. When it successfully retrieves data, the size of the data
18419 * will be returned, otherwise, 0 will be returned.
18420 **/
18421static uint32_t
18422lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
18423{
18424 LPFC_MBOXQ_t *mboxq = NULL;
18425 struct lpfc_dmabuf *mp = NULL;
18426 struct lpfc_mqe *mqe;
18427 uint32_t data_length = 0;
18428 int rc;
18429
18430 if (!rgn23_data)
18431 return 0;
18432
18433 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18434 if (!mboxq) {
18435 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18436 "3105 failed to allocate mailbox memory\n");
18437 return 0;
18438 }
18439
18440 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
18441 goto out;
18442 mqe = &mboxq->u.mqe;
18443 mp = (struct lpfc_dmabuf *) mboxq->context1;
18444 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
18445 if (rc)
18446 goto out;
18447 data_length = mqe->un.mb_words[5];
18448 if (data_length == 0)
18449 goto out;
18450 if (data_length > DMP_RGN23_SIZE) {
18451 data_length = 0;
18452 goto out;
18453 }
18454 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
18455out:
18456 mempool_free(mboxq, phba->mbox_mem_pool);
18457 if (mp) {
18458 lpfc_mbuf_free(phba, mp->virt, mp->phys);
18459 kfree(mp);
18460 }
18461 return data_length;
18462}
18463
18464/**
18465 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
18466 * @phba: pointer to lpfc hba data structure.
18467 *
18468 * This function read region 23 and parse TLV for port status to
18469 * decide if the user disaled the port. If the TLV indicates the
18470 * port is disabled, the hba_flag is set accordingly.
18471 **/
18472void
18473lpfc_sli_read_link_ste(struct lpfc_hba *phba)
18474{
18475 uint8_t *rgn23_data = NULL;
18476 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
18477 uint32_t offset = 0;
18478
18479 /* Get adapter Region 23 data */
18480 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
18481 if (!rgn23_data)
18482 goto out;
18483
18484 if (phba->sli_rev < LPFC_SLI_REV4)
18485 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
18486 else {
18487 if_type = bf_get(lpfc_sli_intf_if_type,
18488 &phba->sli4_hba.sli_intf);
18489 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
18490 goto out;
18491 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
18492 }
James Smarta0c87cb2009-07-19 10:01:10 -040018493
18494 if (!data_size)
18495 goto out;
18496
18497 /* Check the region signature first */
18498 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
18499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18500 "2619 Config region 23 has bad signature\n");
18501 goto out;
18502 }
18503 offset += 4;
18504
18505 /* Check the data structure version */
18506 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
18507 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18508 "2620 Config region 23 has bad version\n");
18509 goto out;
18510 }
18511 offset += 4;
18512
18513 /* Parse TLV entries in the region */
18514 while (offset < data_size) {
18515 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
18516 break;
18517 /*
18518 * If the TLV is not driver specific TLV or driver id is
18519 * not linux driver id, skip the record.
18520 */
18521 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
18522 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
18523 (rgn23_data[offset + 3] != 0)) {
18524 offset += rgn23_data[offset + 1] * 4 + 4;
18525 continue;
18526 }
18527
18528 /* Driver found a driver specific TLV in the config region */
18529 sub_tlv_len = rgn23_data[offset + 1] * 4;
18530 offset += 4;
18531 tlv_offset = 0;
18532
18533 /*
18534 * Search for configured port state sub-TLV.
18535 */
18536 while ((offset < data_size) &&
18537 (tlv_offset < sub_tlv_len)) {
18538 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
18539 offset += 4;
18540 tlv_offset += 4;
18541 break;
18542 }
18543 if (rgn23_data[offset] != PORT_STE_TYPE) {
18544 offset += rgn23_data[offset + 1] * 4 + 4;
18545 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
18546 continue;
18547 }
18548
18549 /* This HBA contains PORT_STE configured */
18550 if (!rgn23_data[offset + 2])
18551 phba->hba_flag |= LINK_DISABLED;
18552
18553 goto out;
18554 }
18555 }
James Smart026abb82011-12-13 13:20:45 -050018556
James Smarta0c87cb2009-07-19 10:01:10 -040018557out:
James Smarta0c87cb2009-07-19 10:01:10 -040018558 kfree(rgn23_data);
18559 return;
18560}
James Smart695a8142010-01-26 23:08:03 -050018561
18562/**
James Smart52d52442011-05-24 11:42:45 -040018563 * lpfc_wr_object - write an object to the firmware
18564 * @phba: HBA structure that indicates port to create a queue on.
18565 * @dmabuf_list: list of dmabufs to write to the port.
18566 * @size: the total byte value of the objects to write to the port.
18567 * @offset: the current offset to be used to start the transfer.
18568 *
18569 * This routine will create a wr_object mailbox command to send to the port.
18570 * the mailbox command will be constructed using the dma buffers described in
18571 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
18572 * BDEs that the imbedded mailbox can support. The @offset variable will be
18573 * used to indicate the starting offset of the transfer and will also return
18574 * the offset after the write object mailbox has completed. @size is used to
18575 * determine the end of the object and whether the eof bit should be set.
18576 *
18577 * Return 0 is successful and offset will contain the the new offset to use
18578 * for the next write.
18579 * Return negative value for error cases.
18580 **/
18581int
18582lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
18583 uint32_t size, uint32_t *offset)
18584{
18585 struct lpfc_mbx_wr_object *wr_object;
18586 LPFC_MBOXQ_t *mbox;
18587 int rc = 0, i = 0;
18588 uint32_t shdr_status, shdr_add_status;
18589 uint32_t mbox_tmo;
18590 union lpfc_sli4_cfg_shdr *shdr;
18591 struct lpfc_dmabuf *dmabuf;
18592 uint32_t written = 0;
18593
18594 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18595 if (!mbox)
18596 return -ENOMEM;
18597
18598 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
18599 LPFC_MBOX_OPCODE_WRITE_OBJECT,
18600 sizeof(struct lpfc_mbx_wr_object) -
18601 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
18602
18603 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
18604 wr_object->u.request.write_offset = *offset;
18605 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
18606 wr_object->u.request.object_name[0] =
18607 cpu_to_le32(wr_object->u.request.object_name[0]);
18608 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
18609 list_for_each_entry(dmabuf, dmabuf_list, list) {
18610 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
18611 break;
18612 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
18613 wr_object->u.request.bde[i].addrHigh =
18614 putPaddrHigh(dmabuf->phys);
18615 if (written + SLI4_PAGE_SIZE >= size) {
18616 wr_object->u.request.bde[i].tus.f.bdeSize =
18617 (size - written);
18618 written += (size - written);
18619 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
18620 } else {
18621 wr_object->u.request.bde[i].tus.f.bdeSize =
18622 SLI4_PAGE_SIZE;
18623 written += SLI4_PAGE_SIZE;
18624 }
18625 i++;
18626 }
18627 wr_object->u.request.bde_count = i;
18628 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
18629 if (!phba->sli4_hba.intr_enable)
18630 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
18631 else {
James Smarta183a152011-10-10 21:32:43 -040018632 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040018633 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
18634 }
18635 /* The IOCTL status is embedded in the mailbox subheader. */
18636 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
18637 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
18638 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
18639 if (rc != MBX_TIMEOUT)
18640 mempool_free(mbox, phba->mbox_mem_pool);
18641 if (shdr_status || shdr_add_status || rc) {
18642 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
18643 "3025 Write Object mailbox failed with "
18644 "status x%x add_status x%x, mbx status x%x\n",
18645 shdr_status, shdr_add_status, rc);
18646 rc = -ENXIO;
18647 } else
18648 *offset += wr_object->u.response.actual_write_length;
18649 return rc;
18650}
18651
18652/**
James Smart695a8142010-01-26 23:08:03 -050018653 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
18654 * @vport: pointer to vport data structure.
18655 *
18656 * This function iterate through the mailboxq and clean up all REG_LOGIN
18657 * and REG_VPI mailbox commands associated with the vport. This function
18658 * is called when driver want to restart discovery of the vport due to
18659 * a Clear Virtual Link event.
18660 **/
18661void
18662lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
18663{
18664 struct lpfc_hba *phba = vport->phba;
18665 LPFC_MBOXQ_t *mb, *nextmb;
18666 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040018667 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040018668 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040018669 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040018670 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050018671 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050018672
James Smartd439d282010-09-29 11:18:45 -040018673 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050018674 spin_lock_irq(&phba->hbalock);
18675 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
18676 if (mb->vport != vport)
18677 continue;
18678
18679 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18680 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18681 continue;
18682
James Smartd439d282010-09-29 11:18:45 -040018683 list_del(&mb->list);
18684 list_add_tail(&mb->list, &mbox_cmd_list);
18685 }
18686 /* Clean up active mailbox command with the vport */
18687 mb = phba->sli.mbox_active;
18688 if (mb && (mb->vport == vport)) {
18689 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
18690 (mb->u.mb.mbxCommand == MBX_REG_VPI))
18691 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18692 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18693 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
18694 /* Put reference count for delayed processing */
18695 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
18696 /* Unregister the RPI when mailbox complete */
18697 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18698 }
18699 }
James Smart63e801c2010-11-20 23:14:19 -050018700 /* Cleanup any mailbox completions which are not yet processed */
18701 do {
18702 restart_loop = 0;
18703 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
18704 /*
18705 * If this mailox is already processed or it is
18706 * for another vport ignore it.
18707 */
18708 if ((mb->vport != vport) ||
18709 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
18710 continue;
18711
18712 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
18713 (mb->u.mb.mbxCommand != MBX_REG_VPI))
18714 continue;
18715
18716 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
18717 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18718 ndlp = (struct lpfc_nodelist *)mb->context2;
18719 /* Unregister the RPI when mailbox complete */
18720 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
18721 restart_loop = 1;
18722 spin_unlock_irq(&phba->hbalock);
18723 spin_lock(shost->host_lock);
18724 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18725 spin_unlock(shost->host_lock);
18726 spin_lock_irq(&phba->hbalock);
18727 break;
18728 }
18729 }
18730 } while (restart_loop);
18731
James Smartd439d282010-09-29 11:18:45 -040018732 spin_unlock_irq(&phba->hbalock);
18733
18734 /* Release the cleaned-up mailbox commands */
18735 while (!list_empty(&mbox_cmd_list)) {
18736 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050018737 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
18738 mp = (struct lpfc_dmabuf *) (mb->context1);
18739 if (mp) {
18740 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
18741 kfree(mp);
18742 }
James Smart78730cf2010-04-06 15:06:30 -040018743 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040018744 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040018745 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018746 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040018747 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020018748 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040018749 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040018750 }
James Smart695a8142010-01-26 23:08:03 -050018751 }
James Smart695a8142010-01-26 23:08:03 -050018752 mempool_free(mb, phba->mbox_mem_pool);
18753 }
James Smartd439d282010-09-29 11:18:45 -040018754
18755 /* Release the ndlp with the cleaned-up active mailbox command */
18756 if (act_mbx_ndlp) {
18757 spin_lock(shost->host_lock);
18758 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
18759 spin_unlock(shost->host_lock);
18760 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050018761 }
James Smart695a8142010-01-26 23:08:03 -050018762}
18763
James Smart2a9bf3d2010-06-07 15:24:45 -040018764/**
18765 * lpfc_drain_txq - Drain the txq
18766 * @phba: Pointer to HBA context object.
18767 *
18768 * This function attempt to submit IOCBs on the txq
18769 * to the adapter. For SLI4 adapters, the txq contains
18770 * ELS IOCBs that have been deferred because the there
18771 * are no SGLs. This congestion can occur with large
18772 * vport counts during node discovery.
18773 **/
18774
18775uint32_t
18776lpfc_drain_txq(struct lpfc_hba *phba)
18777{
18778 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080018779 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090018780 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040018781 unsigned long iflags = 0;
18782 char *fail_msg = NULL;
18783 struct lpfc_sglq *sglq;
James Smart2f077842016-12-19 15:07:29 -080018784 union lpfc_wqe128 wqe128;
18785 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
James Smarta2fc4aef2014-09-03 12:57:55 -040018786 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040018787
James Smart895427b2017-02-12 13:52:30 -080018788 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -070018789 if (unlikely(!pring))
18790 return 0;
James Smart895427b2017-02-12 13:52:30 -080018791
James Smart398d81c2013-05-31 17:04:19 -040018792 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050018793 list_for_each_entry(piocbq, &pring->txq, list) {
18794 txq_cnt++;
18795 }
18796
18797 if (txq_cnt > pring->txq_max)
18798 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018799
James Smart398d81c2013-05-31 17:04:19 -040018800 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018801
James Smart0e9bb8d2013-03-01 16:35:12 -050018802 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040018803 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018804
James Smart19ca7602010-11-20 23:11:55 -050018805 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040018806 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040018807 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040018808 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18809 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050018810 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040018811 break;
18812 }
James Smart895427b2017-02-12 13:52:30 -080018813 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040018814 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050018815 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040018816 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018817 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040018818 }
James Smart0e9bb8d2013-03-01 16:35:12 -050018819 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040018820
18821 /* The xri and iocb resources secured,
18822 * attempt to issue request
18823 */
James Smart6d368e52011-05-24 11:44:12 -040018824 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040018825 piocbq->sli4_xritag = sglq->sli4_xritag;
18826 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
18827 fail_msg = "to convert bpl to sgl";
James Smart2f077842016-12-19 15:07:29 -080018828 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018829 fail_msg = "to convert iocb to wqe";
James Smart2f077842016-12-19 15:07:29 -080018830 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040018831 fail_msg = " - Wq is full";
18832 else
18833 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
18834
18835 if (fail_msg) {
18836 /* Failed means we can't issue and need to cancel */
18837 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
18838 "2822 IOCB failed %s iotag 0x%x "
18839 "xri 0x%x\n",
18840 fail_msg,
18841 piocbq->iotag, piocbq->sli4_xritag);
18842 list_add_tail(&piocbq->list, &completions);
18843 }
James Smart398d81c2013-05-31 17:04:19 -040018844 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040018845 }
18846
James Smart2a9bf3d2010-06-07 15:24:45 -040018847 /* Cancel all the IOCBs that cannot be issued */
18848 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
18849 IOERR_SLI_ABORTED);
18850
James Smart0e9bb8d2013-03-01 16:35:12 -050018851 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040018852}
James Smart895427b2017-02-12 13:52:30 -080018853
18854/**
18855 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
18856 * @phba: Pointer to HBA context object.
18857 * @pwqe: Pointer to command WQE.
18858 * @sglq: Pointer to the scatter gather queue object.
18859 *
18860 * This routine converts the bpl or bde that is in the WQE
18861 * to a sgl list for the sli4 hardware. The physical address
18862 * of the bpl/bde is converted back to a virtual address.
18863 * If the WQE contains a BPL then the list of BDE's is
18864 * converted to sli4_sge's. If the WQE contains a single
18865 * BDE then it is converted to a single sli_sge.
18866 * The WQE is still in cpu endianness so the contents of
18867 * the bpl can be used without byte swapping.
18868 *
18869 * Returns valid XRI = Success, NO_XRI = Failure.
18870 */
18871static uint16_t
18872lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
18873 struct lpfc_sglq *sglq)
18874{
18875 uint16_t xritag = NO_XRI;
18876 struct ulp_bde64 *bpl = NULL;
18877 struct ulp_bde64 bde;
18878 struct sli4_sge *sgl = NULL;
18879 struct lpfc_dmabuf *dmabuf;
18880 union lpfc_wqe *wqe;
18881 int numBdes = 0;
18882 int i = 0;
18883 uint32_t offset = 0; /* accumulated offset in the sg request list */
18884 int inbound = 0; /* number of sg reply entries inbound from firmware */
18885 uint32_t cmd;
18886
18887 if (!pwqeq || !sglq)
18888 return xritag;
18889
18890 sgl = (struct sli4_sge *)sglq->sgl;
18891 wqe = &pwqeq->wqe;
18892 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
18893
18894 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
18895 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
18896 return sglq->sli4_xritag;
18897 numBdes = pwqeq->rsvd2;
18898 if (numBdes) {
18899 /* The addrHigh and addrLow fields within the WQE
18900 * have not been byteswapped yet so there is no
18901 * need to swap them back.
18902 */
18903 if (pwqeq->context3)
18904 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
18905 else
18906 return xritag;
18907
18908 bpl = (struct ulp_bde64 *)dmabuf->virt;
18909 if (!bpl)
18910 return xritag;
18911
18912 for (i = 0; i < numBdes; i++) {
18913 /* Should already be byte swapped. */
18914 sgl->addr_hi = bpl->addrHigh;
18915 sgl->addr_lo = bpl->addrLow;
18916
18917 sgl->word2 = le32_to_cpu(sgl->word2);
18918 if ((i+1) == numBdes)
18919 bf_set(lpfc_sli4_sge_last, sgl, 1);
18920 else
18921 bf_set(lpfc_sli4_sge_last, sgl, 0);
18922 /* swap the size field back to the cpu so we
18923 * can assign it to the sgl.
18924 */
18925 bde.tus.w = le32_to_cpu(bpl->tus.w);
18926 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
18927 /* The offsets in the sgl need to be accumulated
18928 * separately for the request and reply lists.
18929 * The request is always first, the reply follows.
18930 */
18931 switch (cmd) {
18932 case CMD_GEN_REQUEST64_WQE:
18933 /* add up the reply sg entries */
18934 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
18935 inbound++;
18936 /* first inbound? reset the offset */
18937 if (inbound == 1)
18938 offset = 0;
18939 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18940 bf_set(lpfc_sli4_sge_type, sgl,
18941 LPFC_SGE_TYPE_DATA);
18942 offset += bde.tus.f.bdeSize;
18943 break;
18944 case CMD_FCP_TRSP64_WQE:
18945 bf_set(lpfc_sli4_sge_offset, sgl, 0);
18946 bf_set(lpfc_sli4_sge_type, sgl,
18947 LPFC_SGE_TYPE_DATA);
18948 break;
18949 case CMD_FCP_TSEND64_WQE:
18950 case CMD_FCP_TRECEIVE64_WQE:
18951 bf_set(lpfc_sli4_sge_type, sgl,
18952 bpl->tus.f.bdeFlags);
18953 if (i < 3)
18954 offset = 0;
18955 else
18956 offset += bde.tus.f.bdeSize;
18957 bf_set(lpfc_sli4_sge_offset, sgl, offset);
18958 break;
18959 }
18960 sgl->word2 = cpu_to_le32(sgl->word2);
18961 bpl++;
18962 sgl++;
18963 }
18964 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
18965 /* The addrHigh and addrLow fields of the BDE have not
18966 * been byteswapped yet so they need to be swapped
18967 * before putting them in the sgl.
18968 */
18969 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
18970 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
18971 sgl->word2 = le32_to_cpu(sgl->word2);
18972 bf_set(lpfc_sli4_sge_last, sgl, 1);
18973 sgl->word2 = cpu_to_le32(sgl->word2);
18974 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
18975 }
18976 return sglq->sli4_xritag;
18977}
18978
18979/**
18980 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
18981 * @phba: Pointer to HBA context object.
18982 * @ring_number: Base sli ring number
18983 * @pwqe: Pointer to command WQE.
18984 **/
18985int
18986lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
18987 struct lpfc_iocbq *pwqe)
18988{
18989 union lpfc_wqe *wqe = &pwqe->wqe;
James Smartf358dd02017-02-12 13:52:34 -080018990 struct lpfc_nvmet_rcv_ctx *ctxp;
James Smart895427b2017-02-12 13:52:30 -080018991 struct lpfc_queue *wq;
18992 struct lpfc_sglq *sglq;
18993 struct lpfc_sli_ring *pring;
18994 unsigned long iflags;
Dick Kennedycd22d602017-08-23 16:55:35 -070018995 uint32_t ret = 0;
James Smart895427b2017-02-12 13:52:30 -080018996
18997 /* NVME_LS and NVME_LS ABTS requests. */
18998 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
18999 pring = phba->sli4_hba.nvmels_wq->pring;
19000 spin_lock_irqsave(&pring->ring_lock, iflags);
19001 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
19002 if (!sglq) {
19003 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19004 return WQE_BUSY;
19005 }
19006 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19007 pwqe->sli4_xritag = sglq->sli4_xritag;
19008 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
19009 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19010 return WQE_ERROR;
19011 }
19012 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19013 pwqe->sli4_xritag);
Dick Kennedycd22d602017-08-23 16:55:35 -070019014 ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
19015 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019016 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019017 return ret;
James Smart895427b2017-02-12 13:52:30 -080019018 }
Dick Kennedycd22d602017-08-23 16:55:35 -070019019
James Smart895427b2017-02-12 13:52:30 -080019020 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19021 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19022 return 0;
19023 }
19024
19025 /* NVME_FCREQ and NVME_ABTS requests */
19026 if (pwqe->iocb_flag & LPFC_IO_NVME) {
19027 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19028 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19029
19030 spin_lock_irqsave(&pring->ring_lock, iflags);
19031 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19032 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19033 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019034 ret = lpfc_sli4_wq_put(wq, wqe);
19035 if (ret) {
James Smart895427b2017-02-12 13:52:30 -080019036 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019037 return ret;
James Smart895427b2017-02-12 13:52:30 -080019038 }
19039 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19040 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19041 return 0;
19042 }
19043
James Smartf358dd02017-02-12 13:52:34 -080019044 /* NVMET requests */
19045 if (pwqe->iocb_flag & LPFC_IO_NVMET) {
19046 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
19047 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
19048
19049 spin_lock_irqsave(&pring->ring_lock, iflags);
19050 ctxp = pwqe->context2;
James Smart6c621a22017-05-15 15:20:45 -070019051 sglq = ctxp->ctxbuf->sglq;
James Smartf358dd02017-02-12 13:52:34 -080019052 if (pwqe->sli4_xritag == NO_XRI) {
19053 pwqe->sli4_lxritag = sglq->sli4_lxritag;
19054 pwqe->sli4_xritag = sglq->sli4_xritag;
19055 }
19056 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
19057 pwqe->sli4_xritag);
19058 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
19059 bf_set(wqe_cqid, &wqe->generic.wqe_com,
19060 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
Dick Kennedycd22d602017-08-23 16:55:35 -070019061 ret = lpfc_sli4_wq_put(wq, wqe);
19062 if (ret) {
James Smartf358dd02017-02-12 13:52:34 -080019063 spin_unlock_irqrestore(&pring->ring_lock, iflags);
Dick Kennedycd22d602017-08-23 16:55:35 -070019064 return ret;
James Smartf358dd02017-02-12 13:52:34 -080019065 }
19066 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
19067 spin_unlock_irqrestore(&pring->ring_lock, iflags);
19068 return 0;
19069 }
James Smart895427b2017-02-12 13:52:30 -080019070 return WQE_ERROR;
19071}