blob: 52fa5c77f3bcfc64e6238b4db88a849b417d1cfc [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 Smart50611572016-03-31 14:12:34 -07004 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010027#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050028
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040029#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050030#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040033#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040034#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040035#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050036
James Smart895427b2017-02-12 13:52:30 -080037#include <linux/nvme-fc-driver.h>
38
James Smartda0436e2009-05-22 14:51:39 -040039#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050040#include "lpfc_hw.h"
41#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040042#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040043#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc_disc.h"
dea31012005-04-17 16:05:31 -050045#include "lpfc.h"
James Smart895427b2017-02-12 13:52:30 -080046#include "lpfc_scsi.h"
47#include "lpfc_nvme.h"
dea31012005-04-17 16:05:31 -050048#include "lpfc_crtn.h"
49#include "lpfc_logmsg.h"
50#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050051#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040052#include "lpfc_vport.h"
James Smart61bda8f2016-10-13 15:06:05 -070053#include "lpfc_version.h"
dea31012005-04-17 16:05:31 -050054
55/* There are only four IOCB completion types. */
56typedef enum _lpfc_iocb_type {
57 LPFC_UNKNOWN_IOCB,
58 LPFC_UNSOL_IOCB,
59 LPFC_SOL_IOCB,
60 LPFC_ABORT_IOCB
61} lpfc_iocb_type;
62
James Smart4f774512009-05-22 14:52:35 -040063
64/* Provide function prototypes local to this module. */
65static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
66 uint32_t);
67static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040068 uint8_t *, uint32_t *);
69static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
70 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040071static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
72 struct hbq_dmabuf *);
James Smart895427b2017-02-12 13:52:30 -080073static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
James Smart05580562011-05-24 11:40:48 -040074 struct lpfc_cqe *);
James Smart895427b2017-02-12 13:52:30 -080075static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
James Smart8a9d2e82012-05-09 21:16:12 -040076 int);
James Smartba20c852012-08-03 12:36:52 -040077static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
78 uint32_t);
James Smarte8d3c3b2013-10-10 12:21:30 -040079static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
80static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart895427b2017-02-12 13:52:30 -080081static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
82 struct lpfc_sli_ring *pring,
83 struct lpfc_iocbq *cmdiocb);
James Smart05580562011-05-24 11:40:48 -040084
James Smart4f774512009-05-22 14:52:35 -040085static IOCB_t *
86lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
87{
88 return &iocbq->iocb;
89}
90
91/**
92 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
93 * @q: The Work Queue to operate on.
94 * @wqe: The work Queue Entry to put on the Work queue.
95 *
96 * This routine will copy the contents of @wqe to the next available entry on
97 * the @q. This function will then ring the Work Queue Doorbell to signal the
98 * HBA to start processing the Work Queue Entry. This function returns 0 if
99 * successful. If no entries are available on @q then this function will return
100 * -ENOMEM.
101 * The caller is expected to hold the hbalock when calling this routine.
102 **/
103static uint32_t
104lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
105{
James Smart2e90f4b2011-12-13 13:22:37 -0500106 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400107 struct lpfc_register doorbell;
108 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400109 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400110
James Smart2e90f4b2011-12-13 13:22:37 -0500111 /* sanity check on queue memory */
112 if (unlikely(!q))
113 return -ENOMEM;
114 temp_wqe = q->qe[q->host_index].wqe;
115
James Smart4f774512009-05-22 14:52:35 -0400116 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400117 idx = ((q->host_index + 1) % q->entry_count);
118 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400119 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400120 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400121 }
122 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400123 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500124 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400125 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500126 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
127 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400128 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart6b3b3bd2016-12-19 15:07:30 -0800129 /* ensure WQE bcopy flushed before doorbell write */
130 wmb();
James Smart4f774512009-05-22 14:52:35 -0400131
132 /* Update the host index before invoking device */
133 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400134
135 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400136
137 /* Ring Doorbell */
138 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500139 if (q->db_format == LPFC_DB_LIST_FORMAT) {
140 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
141 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
142 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
143 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
144 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
145 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
146 } else {
147 return -EINVAL;
148 }
149 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400150
151 return 0;
152}
153
154/**
155 * lpfc_sli4_wq_release - Updates internal hba index for WQ
156 * @q: The Work Queue to operate on.
157 * @index: The index to advance the hba index to.
158 *
159 * This routine will update the HBA index of a queue to reflect consumption of
160 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
161 * an entry the host calls this function to update the queue's internal
162 * pointers. This routine returns the number of entries that were consumed by
163 * the HBA.
164 **/
165static uint32_t
166lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
167{
168 uint32_t released = 0;
169
James Smart2e90f4b2011-12-13 13:22:37 -0500170 /* sanity check on queue memory */
171 if (unlikely(!q))
172 return 0;
173
James Smart4f774512009-05-22 14:52:35 -0400174 if (q->hba_index == index)
175 return 0;
176 do {
177 q->hba_index = ((q->hba_index + 1) % q->entry_count);
178 released++;
179 } while (q->hba_index != index);
180 return released;
181}
182
183/**
184 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
185 * @q: The Mailbox Queue to operate on.
186 * @wqe: The Mailbox Queue Entry to put on the Work queue.
187 *
188 * This routine will copy the contents of @mqe to the next available entry on
189 * the @q. This function will then ring the Work Queue Doorbell to signal the
190 * HBA to start processing the Work Queue Entry. This function returns 0 if
191 * successful. If no entries are available on @q then this function will return
192 * -ENOMEM.
193 * The caller is expected to hold the hbalock when calling this routine.
194 **/
195static uint32_t
196lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
197{
James Smart2e90f4b2011-12-13 13:22:37 -0500198 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400199 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400200
James Smart2e90f4b2011-12-13 13:22:37 -0500201 /* sanity check on queue memory */
202 if (unlikely(!q))
203 return -ENOMEM;
204 temp_mqe = q->qe[q->host_index].mqe;
205
James Smart4f774512009-05-22 14:52:35 -0400206 /* If the host has not yet processed the next entry then we are done */
207 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
208 return -ENOMEM;
209 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
210 /* Save off the mailbox pointer for completion */
211 q->phba->mbox = (MAILBOX_t *)temp_mqe;
212
213 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400214 q->host_index = ((q->host_index + 1) % q->entry_count);
215
216 /* Ring Doorbell */
217 doorbell.word0 = 0;
218 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
219 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
220 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400221 return 0;
222}
223
224/**
225 * lpfc_sli4_mq_release - Updates internal hba index for MQ
226 * @q: The Mailbox Queue to operate on.
227 *
228 * This routine will update the HBA index of a queue to reflect consumption of
229 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
230 * an entry the host calls this function to update the queue's internal
231 * pointers. This routine returns the number of entries that were consumed by
232 * the HBA.
233 **/
234static uint32_t
235lpfc_sli4_mq_release(struct lpfc_queue *q)
236{
James Smart2e90f4b2011-12-13 13:22:37 -0500237 /* sanity check on queue memory */
238 if (unlikely(!q))
239 return 0;
240
James Smart4f774512009-05-22 14:52:35 -0400241 /* Clear the mailbox pointer for completion */
242 q->phba->mbox = NULL;
243 q->hba_index = ((q->hba_index + 1) % q->entry_count);
244 return 1;
245}
246
247/**
248 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
249 * @q: The Event Queue to get the first valid EQE from
250 *
251 * This routine will get the first valid Event Queue Entry from @q, update
252 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
253 * the Queue (no more work to do), or the Queue is full of EQEs that have been
254 * processed, but not popped back to the HBA then this routine will return NULL.
255 **/
256static struct lpfc_eqe *
257lpfc_sli4_eq_get(struct lpfc_queue *q)
258{
James Smart2e90f4b2011-12-13 13:22:37 -0500259 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400260 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500261
262 /* sanity check on queue memory */
263 if (unlikely(!q))
264 return NULL;
265 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400266
267 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400268 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400269 return NULL;
270 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400271 idx = ((q->hba_index + 1) % q->entry_count);
272 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400273 return NULL;
274
James Smart027140e2012-08-03 12:35:44 -0400275 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400276
277 /*
278 * insert barrier for instruction interlock : data from the hardware
279 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800280 * upon. Speculative instructions were allowing a bcopy at the start
281 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
282 * after our return, to copy data before the valid bit check above
283 * was done. As such, some of the copied data was stale. The barrier
284 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400285 */
286 mb();
James Smart4f774512009-05-22 14:52:35 -0400287 return eqe;
288}
289
290/**
James Smartba20c852012-08-03 12:36:52 -0400291 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
292 * @q: The Event Queue to disable interrupts
293 *
294 **/
295static inline void
296lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
297{
298 struct lpfc_register doorbell;
299
300 doorbell.word0 = 0;
301 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
302 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
303 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
304 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
305 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
306 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
307}
308
309/**
James Smart4f774512009-05-22 14:52:35 -0400310 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
311 * @q: The Event Queue that the host has completed processing for.
312 * @arm: Indicates whether the host wants to arms this CQ.
313 *
314 * This routine will mark all Event Queue Entries on @q, from the last
315 * known completed entry to the last entry that was processed, as completed
316 * by clearing the valid bit for each completion queue entry. Then it will
317 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
318 * The internal host index in the @q will be updated by this routine to indicate
319 * that the host has finished processing the entries. The @arm parameter
320 * indicates that the queue should be rearmed when ringing the doorbell.
321 *
322 * This function will return the number of EQEs that were popped.
323 **/
324uint32_t
325lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
326{
327 uint32_t released = 0;
328 struct lpfc_eqe *temp_eqe;
329 struct lpfc_register doorbell;
330
James Smart2e90f4b2011-12-13 13:22:37 -0500331 /* sanity check on queue memory */
332 if (unlikely(!q))
333 return 0;
334
James Smart4f774512009-05-22 14:52:35 -0400335 /* while there are valid entries */
336 while (q->hba_index != q->host_index) {
337 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400338 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400339 released++;
340 q->host_index = ((q->host_index + 1) % q->entry_count);
341 }
342 if (unlikely(released == 0 && !arm))
343 return 0;
344
345 /* ring doorbell for number popped */
346 doorbell.word0 = 0;
347 if (arm) {
348 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
349 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
350 }
351 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
352 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500353 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
354 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
355 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400356 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500357 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
358 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
359 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400360 return released;
361}
362
363/**
364 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
365 * @q: The Completion Queue to get the first valid CQE from
366 *
367 * This routine will get the first valid Completion Queue Entry from @q, update
368 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
369 * the Queue (no more work to do), or the Queue is full of CQEs that have been
370 * processed, but not popped back to the HBA then this routine will return NULL.
371 **/
372static struct lpfc_cqe *
373lpfc_sli4_cq_get(struct lpfc_queue *q)
374{
375 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400376 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400377
James Smart2e90f4b2011-12-13 13:22:37 -0500378 /* sanity check on queue memory */
379 if (unlikely(!q))
380 return NULL;
381
James Smart4f774512009-05-22 14:52:35 -0400382 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400383 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400384 return NULL;
385 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400386 idx = ((q->hba_index + 1) % q->entry_count);
387 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400388 return NULL;
389
390 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400391 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400392
393 /*
394 * insert barrier for instruction interlock : data from the hardware
395 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800396 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
397 * instructions allowing action on content before valid bit checked,
398 * add barrier here as well. May not be needed as "content" is a
399 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400400 */
401 mb();
James Smart4f774512009-05-22 14:52:35 -0400402 return cqe;
403}
404
405/**
406 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
407 * @q: The Completion Queue that the host has completed processing for.
408 * @arm: Indicates whether the host wants to arms this CQ.
409 *
410 * This routine will mark all Completion queue entries on @q, from the last
411 * known completed entry to the last entry that was processed, as completed
412 * by clearing the valid bit for each completion queue entry. Then it will
413 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
414 * The internal host index in the @q will be updated by this routine to indicate
415 * that the host has finished processing the entries. The @arm parameter
416 * indicates that the queue should be rearmed when ringing the doorbell.
417 *
418 * This function will return the number of CQEs that were released.
419 **/
420uint32_t
421lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
422{
423 uint32_t released = 0;
424 struct lpfc_cqe *temp_qe;
425 struct lpfc_register doorbell;
426
James Smart2e90f4b2011-12-13 13:22:37 -0500427 /* sanity check on queue memory */
428 if (unlikely(!q))
429 return 0;
James Smart4f774512009-05-22 14:52:35 -0400430 /* while there are valid entries */
431 while (q->hba_index != q->host_index) {
432 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400433 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400434 released++;
435 q->host_index = ((q->host_index + 1) % q->entry_count);
436 }
437 if (unlikely(released == 0 && !arm))
438 return 0;
439
440 /* ring doorbell for number popped */
441 doorbell.word0 = 0;
442 if (arm)
443 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
444 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
445 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500446 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
447 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
448 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400449 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
450 return released;
451}
452
453/**
454 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
455 * @q: The Header Receive Queue to operate on.
456 * @wqe: The Receive Queue Entry to put on the Receive queue.
457 *
458 * This routine will copy the contents of @wqe to the next available entry on
459 * the @q. This function will then ring the Receive Queue Doorbell to signal the
460 * HBA to start processing the Receive Queue Entry. This function returns the
461 * index that the rqe was copied to if successful. If no entries are available
462 * on @q then this function will return -ENOMEM.
463 * The caller is expected to hold the hbalock when calling this routine.
464 **/
James Smart895427b2017-02-12 13:52:30 -0800465int
James Smart4f774512009-05-22 14:52:35 -0400466lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
467 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
468{
James Smart2e90f4b2011-12-13 13:22:37 -0500469 struct lpfc_rqe *temp_hrqe;
470 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400471 struct lpfc_register doorbell;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500472 int put_index;
James Smart4f774512009-05-22 14:52:35 -0400473
James Smart2e90f4b2011-12-13 13:22:37 -0500474 /* sanity check on queue memory */
475 if (unlikely(!hq) || unlikely(!dq))
476 return -ENOMEM;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500477 put_index = hq->host_index;
James Smart2e90f4b2011-12-13 13:22:37 -0500478 temp_hrqe = hq->qe[hq->host_index].rqe;
479 temp_drqe = dq->qe[dq->host_index].rqe;
480
James Smart4f774512009-05-22 14:52:35 -0400481 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
482 return -EINVAL;
483 if (hq->host_index != dq->host_index)
484 return -EINVAL;
485 /* If the host has not yet processed the next entry then we are done */
486 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
487 return -EBUSY;
488 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
489 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
490
491 /* Update the host index to point to the next slot */
492 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
493 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
494
495 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400496 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400497 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500498 if (hq->db_format == LPFC_DB_RING_FORMAT) {
499 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
500 hq->entry_repost);
501 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
502 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
503 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
504 hq->entry_repost);
505 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
506 hq->host_index);
507 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
508 } else {
509 return -EINVAL;
510 }
511 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400512 }
513 return put_index;
514}
515
516/**
517 * lpfc_sli4_rq_release - Updates internal hba index for RQ
518 * @q: The Header Receive Queue to operate on.
519 *
520 * This routine will update the HBA index of a queue to reflect consumption of
521 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
522 * consumed an entry the host calls this function to update the queue's
523 * internal pointers. This routine returns the number of entries that were
524 * consumed by the HBA.
525 **/
526static uint32_t
527lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
528{
James Smart2e90f4b2011-12-13 13:22:37 -0500529 /* sanity check on queue memory */
530 if (unlikely(!hq) || unlikely(!dq))
531 return 0;
532
James Smart4f774512009-05-22 14:52:35 -0400533 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
534 return 0;
535 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
536 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
537 return 1;
538}
539
James Smarte59058c2008-08-24 21:49:00 -0400540/**
James Smart3621a712009-04-06 18:47:14 -0400541 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400542 * @phba: Pointer to HBA context object.
543 * @pring: Pointer to driver SLI ring object.
544 *
545 * This function returns pointer to next command iocb entry
546 * in the command ring. The caller must hold hbalock to prevent
547 * other threads consume the next command iocb.
548 * SLI-2/SLI-3 provide different sized iocbs.
549 **/
James Smarted957682007-06-17 19:56:37 -0500550static inline IOCB_t *
551lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
552{
James Smart7e56aa22012-08-03 12:35:34 -0400553 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
554 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500555}
556
James Smarte59058c2008-08-24 21:49:00 -0400557/**
James Smart3621a712009-04-06 18:47:14 -0400558 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400559 * @phba: Pointer to HBA context object.
560 * @pring: Pointer to driver SLI ring object.
561 *
562 * This function returns pointer to next response iocb entry
563 * in the response ring. The caller must hold hbalock to make sure
564 * that no other thread consume the next response iocb.
565 * SLI-2/SLI-3 provide different sized iocbs.
566 **/
James Smarted957682007-06-17 19:56:37 -0500567static inline IOCB_t *
568lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
569{
James Smart7e56aa22012-08-03 12:35:34 -0400570 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
571 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500572}
573
James Smarte59058c2008-08-24 21:49:00 -0400574/**
James Smart3621a712009-04-06 18:47:14 -0400575 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400576 * @phba: Pointer to HBA context object.
577 *
578 * This function is called with hbalock held. This function
579 * allocates a new driver iocb object from the iocb pool. If the
580 * allocation is successful, it returns pointer to the newly
581 * allocated iocb object else it returns NULL.
582 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400583struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500584__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400585{
586 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
587 struct lpfc_iocbq * iocbq = NULL;
588
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100589 lockdep_assert_held(&phba->hbalock);
590
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400591 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400592 if (iocbq)
593 phba->iocb_cnt++;
594 if (phba->iocb_cnt > phba->iocb_max)
595 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400596 return iocbq;
597}
598
James Smarte59058c2008-08-24 21:49:00 -0400599/**
James Smartda0436e2009-05-22 14:51:39 -0400600 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
601 * @phba: Pointer to HBA context object.
602 * @xritag: XRI value.
603 *
604 * This function clears the sglq pointer from the array of acive
605 * sglq's. The xritag that is passed in is used to index into the
606 * array. Before the xritag can be used it needs to be adjusted
607 * by subtracting the xribase.
608 *
609 * Returns sglq ponter = success, NULL = Failure.
610 **/
James Smart895427b2017-02-12 13:52:30 -0800611struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400612__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
613{
James Smartda0436e2009-05-22 14:51:39 -0400614 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400615
616 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
617 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400618 return sglq;
619}
620
621/**
622 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
623 * @phba: Pointer to HBA context object.
624 * @xritag: XRI value.
625 *
626 * This function returns the sglq pointer from the array of acive
627 * sglq's. The xritag that is passed in is used to index into the
628 * array. Before the xritag can be used it needs to be adjusted
629 * by subtracting the xribase.
630 *
631 * Returns sglq ponter = success, NULL = Failure.
632 **/
James Smart0f65ff62010-02-26 14:14:23 -0500633struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400634__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
635{
James Smartda0436e2009-05-22 14:51:39 -0400636 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400637
638 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400639 return sglq;
640}
641
642/**
James Smart1151e3e2011-02-16 12:39:35 -0500643 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500644 * @phba: Pointer to HBA context object.
645 * @xritag: xri used in this exchange.
646 * @rrq: The RRQ to be cleared.
647 *
James Smart19ca7602010-11-20 23:11:55 -0500648 **/
James Smart1151e3e2011-02-16 12:39:35 -0500649void
650lpfc_clr_rrq_active(struct lpfc_hba *phba,
651 uint16_t xritag,
652 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500653{
James Smart1151e3e2011-02-16 12:39:35 -0500654 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500655
James Smart1151e3e2011-02-16 12:39:35 -0500656 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
657 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500658
659 /* The target DID could have been swapped (cable swap)
660 * we should use the ndlp from the findnode if it is
661 * available.
662 */
James Smart1151e3e2011-02-16 12:39:35 -0500663 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500664 ndlp = rrq->ndlp;
665
James Smart1151e3e2011-02-16 12:39:35 -0500666 if (!ndlp)
667 goto out;
668
James Smartcff261f2013-12-17 20:29:47 -0500669 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500670 rrq->send_rrq = 0;
671 rrq->xritag = 0;
672 rrq->rrq_stop_time = 0;
673 }
James Smart1151e3e2011-02-16 12:39:35 -0500674out:
James Smart19ca7602010-11-20 23:11:55 -0500675 mempool_free(rrq, phba->rrq_pool);
676}
677
678/**
679 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
680 * @phba: Pointer to HBA context object.
681 *
682 * This function is called with hbalock held. This function
683 * Checks if stop_time (ratov from setting rrq active) has
684 * been reached, if it has and the send_rrq flag is set then
685 * it will call lpfc_send_rrq. If the send_rrq flag is not set
686 * then it will just call the routine to clear the rrq and
687 * free the rrq resource.
688 * The timer is set to the next rrq that is going to expire before
689 * leaving the routine.
690 *
691 **/
692void
693lpfc_handle_rrq_active(struct lpfc_hba *phba)
694{
695 struct lpfc_node_rrq *rrq;
696 struct lpfc_node_rrq *nextrrq;
697 unsigned long next_time;
698 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500699 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500700
701 spin_lock_irqsave(&phba->hbalock, iflags);
702 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400703 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500704 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500705 &phba->active_rrq_list, list) {
706 if (time_after(jiffies, rrq->rrq_stop_time))
707 list_move(&rrq->list, &send_rrq);
708 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500709 next_time = rrq->rrq_stop_time;
710 }
711 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500712 if ((!list_empty(&phba->active_rrq_list)) &&
713 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500714 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500715 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
716 list_del(&rrq->list);
717 if (!rrq->send_rrq)
718 /* this call will free the rrq */
719 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
720 else if (lpfc_send_rrq(phba, rrq)) {
721 /* if we send the rrq then the completion handler
722 * will clear the bit in the xribitmap.
723 */
724 lpfc_clr_rrq_active(phba, rrq->xritag,
725 rrq);
726 }
727 }
James Smart19ca7602010-11-20 23:11:55 -0500728}
729
730/**
731 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
732 * @vport: Pointer to vport context object.
733 * @xri: The xri used in the exchange.
734 * @did: The targets DID for this exchange.
735 *
736 * returns NULL = rrq not found in the phba->active_rrq_list.
737 * rrq = rrq for this xri and target.
738 **/
739struct lpfc_node_rrq *
740lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
741{
742 struct lpfc_hba *phba = vport->phba;
743 struct lpfc_node_rrq *rrq;
744 struct lpfc_node_rrq *nextrrq;
745 unsigned long iflags;
746
747 if (phba->sli_rev != LPFC_SLI_REV4)
748 return NULL;
749 spin_lock_irqsave(&phba->hbalock, iflags);
750 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
751 if (rrq->vport == vport && rrq->xritag == xri &&
752 rrq->nlp_DID == did){
753 list_del(&rrq->list);
754 spin_unlock_irqrestore(&phba->hbalock, iflags);
755 return rrq;
756 }
757 }
758 spin_unlock_irqrestore(&phba->hbalock, iflags);
759 return NULL;
760}
761
762/**
763 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
764 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500765 * @ndlp: Pointer to the lpfc_node_list structure.
766 * If ndlp is NULL Remove all active RRQs for this vport from the
767 * phba->active_rrq_list and clear the rrq.
768 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500769 **/
770void
James Smart1151e3e2011-02-16 12:39:35 -0500771lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500772
773{
774 struct lpfc_hba *phba = vport->phba;
775 struct lpfc_node_rrq *rrq;
776 struct lpfc_node_rrq *nextrrq;
777 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500778 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500779
780 if (phba->sli_rev != LPFC_SLI_REV4)
781 return;
James Smart1151e3e2011-02-16 12:39:35 -0500782 if (!ndlp) {
783 lpfc_sli4_vport_delete_els_xri_aborted(vport);
784 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500785 }
James Smart1151e3e2011-02-16 12:39:35 -0500786 spin_lock_irqsave(&phba->hbalock, iflags);
787 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
788 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
789 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500790 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500791
792 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
793 list_del(&rrq->list);
794 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
795 }
James Smart19ca7602010-11-20 23:11:55 -0500796}
797
798/**
James Smart1151e3e2011-02-16 12:39:35 -0500799 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500800 * @phba: Pointer to HBA context object.
801 * @ndlp: Targets nodelist pointer for this exchange.
802 * @xritag the xri in the bitmap to test.
803 *
804 * This function is called with hbalock held. This function
805 * returns 0 = rrq not active for this xri
806 * 1 = rrq is valid for this xri.
807 **/
James Smart1151e3e2011-02-16 12:39:35 -0500808int
809lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500810 uint16_t xritag)
811{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100812 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -0500813 if (!ndlp)
814 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500815 if (!ndlp->active_rrqs_xri_bitmap)
816 return 0;
817 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500818 return 1;
819 else
820 return 0;
821}
822
823/**
824 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
825 * @phba: Pointer to HBA context object.
826 * @ndlp: nodelist pointer for this target.
827 * @xritag: xri used in this exchange.
828 * @rxid: Remote Exchange ID.
829 * @send_rrq: Flag used to determine if we should send rrq els cmd.
830 *
831 * This function takes the hbalock.
832 * The active bit is always set in the active rrq xri_bitmap even
833 * if there is no slot avaiable for the other rrq information.
834 *
835 * returns 0 rrq actived for this xri
836 * < 0 No memory or invalid ndlp.
837 **/
838int
839lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500840 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500841{
James Smart19ca7602010-11-20 23:11:55 -0500842 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500843 struct lpfc_node_rrq *rrq;
844 int empty;
845
846 if (!ndlp)
847 return -EINVAL;
848
849 if (!phba->cfg_enable_rrq)
850 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500851
852 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500853 if (phba->pport->load_flag & FC_UNLOADING) {
854 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
855 goto out;
856 }
857
858 /*
859 * set the active bit even if there is no mem available.
860 */
861 if (NLP_CHK_FREE_REQ(ndlp))
862 goto out;
863
864 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
865 goto out;
866
James Smartcff261f2013-12-17 20:29:47 -0500867 if (!ndlp->active_rrqs_xri_bitmap)
868 goto out;
869
870 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -0500871 goto out;
872
James Smart19ca7602010-11-20 23:11:55 -0500873 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500874 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
875 if (!rrq) {
876 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
877 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
878 " DID:0x%x Send:%d\n",
879 xritag, rxid, ndlp->nlp_DID, send_rrq);
880 return -EINVAL;
881 }
James Smarte5771b42013-03-01 16:37:14 -0500882 if (phba->cfg_enable_rrq == 1)
883 rrq->send_rrq = send_rrq;
884 else
885 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500886 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -0400887 rrq->rrq_stop_time = jiffies +
888 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -0500889 rrq->ndlp = ndlp;
890 rrq->nlp_DID = ndlp->nlp_DID;
891 rrq->vport = ndlp->vport;
892 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500893 spin_lock_irqsave(&phba->hbalock, iflags);
894 empty = list_empty(&phba->active_rrq_list);
895 list_add_tail(&rrq->list, &phba->active_rrq_list);
896 phba->hba_flag |= HBA_RRQ_ACTIVE;
897 if (empty)
898 lpfc_worker_wake_up(phba);
899 spin_unlock_irqrestore(&phba->hbalock, iflags);
900 return 0;
901out:
902 spin_unlock_irqrestore(&phba->hbalock, iflags);
903 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
904 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
905 " DID:0x%x Send:%d\n",
906 xritag, rxid, ndlp->nlp_DID, send_rrq);
907 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500908}
909
910/**
James Smart895427b2017-02-12 13:52:30 -0800911 * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
James Smartda0436e2009-05-22 14:51:39 -0400912 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500913 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400914 *
James Smartdafe8ce2014-09-03 12:56:40 -0400915 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400916 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400917 * list is not empty then it is successful, it returns pointer to the newly
918 * allocated sglq object else it returns NULL.
919 **/
920static struct lpfc_sglq *
James Smart895427b2017-02-12 13:52:30 -0800921__lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400922{
James Smart895427b2017-02-12 13:52:30 -0800923 struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
James Smartda0436e2009-05-22 14:51:39 -0400924 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500925 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500926 struct lpfc_scsi_buf *lpfc_cmd;
927 struct lpfc_nodelist *ndlp;
928 int found = 0;
929
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100930 lockdep_assert_held(&phba->hbalock);
931
James Smart19ca7602010-11-20 23:11:55 -0500932 if (piocbq->iocb_flag & LPFC_IO_FCP) {
933 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
934 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500935 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -0400936 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -0500937 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -0400938 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
939 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
940 ndlp = NULL;
941 else
942 ndlp = piocbq->context_un.ndlp;
943 } else {
James Smart19ca7602010-11-20 23:11:55 -0500944 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -0400945 }
James Smart19ca7602010-11-20 23:11:55 -0500946
James Smart895427b2017-02-12 13:52:30 -0800947 spin_lock(&phba->sli4_hba.sgl_list_lock);
948 list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500949 start_sglq = sglq;
950 while (!found) {
951 if (!sglq)
952 return NULL;
James Smart895427b2017-02-12 13:52:30 -0800953 if (ndlp && ndlp->active_rrqs_xri_bitmap &&
954 test_bit(sglq->sli4_lxritag,
955 ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500956 /* This xri has an rrq outstanding for this DID.
957 * put it back in the list and get another xri.
958 */
James Smart895427b2017-02-12 13:52:30 -0800959 list_add_tail(&sglq->list, lpfc_els_sgl_list);
James Smart19ca7602010-11-20 23:11:55 -0500960 sglq = NULL;
James Smart895427b2017-02-12 13:52:30 -0800961 list_remove_head(lpfc_els_sgl_list, sglq,
James Smart19ca7602010-11-20 23:11:55 -0500962 struct lpfc_sglq, list);
963 if (sglq == start_sglq) {
964 sglq = NULL;
965 break;
966 } else
967 continue;
968 }
969 sglq->ndlp = ndlp;
970 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400971 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500972 sglq->state = SGL_ALLOCATED;
973 }
James Smart895427b2017-02-12 13:52:30 -0800974 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smartda0436e2009-05-22 14:51:39 -0400975 return sglq;
976}
977
978/**
James Smart3621a712009-04-06 18:47:14 -0400979 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400980 * @phba: Pointer to HBA context object.
981 *
982 * This function is called with no lock held. This function
983 * allocates a new driver iocb object from the iocb pool. If the
984 * allocation is successful, it returns pointer to the newly
985 * allocated iocb object else it returns NULL.
986 **/
James Smart2e0fef82007-06-17 19:56:36 -0500987struct lpfc_iocbq *
988lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500989{
James Smart2e0fef82007-06-17 19:56:36 -0500990 struct lpfc_iocbq * iocbq = NULL;
991 unsigned long iflags;
992
993 spin_lock_irqsave(&phba->hbalock, iflags);
994 iocbq = __lpfc_sli_get_iocbq(phba);
995 spin_unlock_irqrestore(&phba->hbalock, iflags);
996 return iocbq;
997}
998
James Smarte59058c2008-08-24 21:49:00 -0400999/**
James Smart4f774512009-05-22 14:52:35 -04001000 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
1001 * @phba: Pointer to HBA context object.
1002 * @iocbq: Pointer to driver iocb object.
1003 *
1004 * This function is called with hbalock held to release driver
1005 * iocb object to the iocb pool. The iotag in the iocb object
1006 * does not change for each use of the iocb object. This function
1007 * clears all other fields of the iocb object when it is freed.
1008 * The sqlq structure that holds the xritag and phys and virtual
1009 * mappings for the scatter gather list is retrieved from the
1010 * active array of sglq. The get of the sglq pointer also clears
1011 * the entry in the array. If the status of the IO indiactes that
1012 * this IO was aborted then the sglq entry it put on the
1013 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1014 * IO has good status or fails for any other reason then the sglq
James Smart895427b2017-02-12 13:52:30 -08001015 * entry is added to the free list (lpfc_els_sgl_list).
James Smart4f774512009-05-22 14:52:35 -04001016 **/
1017static void
1018__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1019{
1020 struct lpfc_sglq *sglq;
1021 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001022 unsigned long iflag = 0;
James Smart895427b2017-02-12 13:52:30 -08001023 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04001024
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001025 lockdep_assert_held(&phba->hbalock);
1026
James Smart4f774512009-05-22 14:52:35 -04001027 if (iocbq->sli4_xritag == NO_XRI)
1028 sglq = NULL;
1029 else
James Smart6d368e52011-05-24 11:44:12 -04001030 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1031
James Smart0e9bb8d2013-03-01 16:35:12 -05001032
James Smart4f774512009-05-22 14:52:35 -04001033 if (sglq) {
James Smart895427b2017-02-12 13:52:30 -08001034 pring = phba->sli4_hba.els_wq->pring;
James Smart0f65ff62010-02-26 14:14:23 -05001035 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1036 (sglq->state != SGL_XRI_ABORTED)) {
James Smart895427b2017-02-12 13:52:30 -08001037 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1038 iflag);
James Smart4f774512009-05-22 14:52:35 -04001039 list_add(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001040 &phba->sli4_hba.lpfc_abts_els_sgl_list);
James Smart4f774512009-05-22 14:52:35 -04001041 spin_unlock_irqrestore(
James Smart895427b2017-02-12 13:52:30 -08001042 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001043 } else {
James Smart895427b2017-02-12 13:52:30 -08001044 spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
1045 iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001046 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001047 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001048 list_add_tail(&sglq->list,
James Smart895427b2017-02-12 13:52:30 -08001049 &phba->sli4_hba.lpfc_els_sgl_list);
1050 spin_unlock_irqrestore(
1051 &phba->sli4_hba.sgl_list_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001052
1053 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001054 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001055 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001056 }
James Smart4f774512009-05-22 14:52:35 -04001057 }
1058
James Smart4f774512009-05-22 14:52:35 -04001059 /*
1060 * Clean all volatile data fields, preserve iotag and node struct.
1061 */
1062 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001063 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001064 iocbq->sli4_xritag = NO_XRI;
James Smart895427b2017-02-12 13:52:30 -08001065 iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVME_LS);
James Smart4f774512009-05-22 14:52:35 -04001066 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1067}
1068
James Smart2a9bf3d2010-06-07 15:24:45 -04001069
James Smart4f774512009-05-22 14:52:35 -04001070/**
James Smart3772a992009-05-22 14:50:54 -04001071 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1072 * @phba: Pointer to HBA context object.
1073 * @iocbq: Pointer to driver iocb object.
1074 *
1075 * This function is called with hbalock held to release driver
1076 * iocb object to the iocb pool. The iotag in the iocb object
1077 * does not change for each use of the iocb object. This function
1078 * clears all other fields of the iocb object when it is freed.
1079 **/
1080static void
1081__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1082{
1083 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1084
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001085 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001086
1087 /*
James Smart3772a992009-05-22 14:50:54 -04001088 * Clean all volatile data fields, preserve iotag and node struct.
1089 */
1090 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1091 iocbq->sli4_xritag = NO_XRI;
1092 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1093}
1094
1095/**
James Smart3621a712009-04-06 18:47:14 -04001096 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001097 * @phba: Pointer to HBA context object.
1098 * @iocbq: Pointer to driver iocb object.
1099 *
1100 * This function is called with hbalock held to release driver
1101 * iocb object to the iocb pool. The iotag in the iocb object
1102 * does not change for each use of the iocb object. This function
1103 * clears all other fields of the iocb object when it is freed.
1104 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001105static void
James Smart2e0fef82007-06-17 19:56:36 -05001106__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1107{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001108 lockdep_assert_held(&phba->hbalock);
1109
James Smart3772a992009-05-22 14:50:54 -04001110 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001111 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001112}
1113
James Smarte59058c2008-08-24 21:49:00 -04001114/**
James Smart3621a712009-04-06 18:47:14 -04001115 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001116 * @phba: Pointer to HBA context object.
1117 * @iocbq: Pointer to driver iocb object.
1118 *
1119 * This function is called with no lock held to release the iocb to
1120 * iocb pool.
1121 **/
James Smart2e0fef82007-06-17 19:56:36 -05001122void
1123lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1124{
1125 unsigned long iflags;
1126
1127 /*
1128 * Clean all volatile data fields, preserve iotag and node struct.
1129 */
1130 spin_lock_irqsave(&phba->hbalock, iflags);
1131 __lpfc_sli_release_iocbq(phba, iocbq);
1132 spin_unlock_irqrestore(&phba->hbalock, iflags);
1133}
1134
James Smarte59058c2008-08-24 21:49:00 -04001135/**
James Smarta257bf92009-04-06 18:48:10 -04001136 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1137 * @phba: Pointer to HBA context object.
1138 * @iocblist: List of IOCBs.
1139 * @ulpstatus: ULP status in IOCB command field.
1140 * @ulpWord4: ULP word-4 in IOCB command field.
1141 *
1142 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1143 * on the list by invoking the complete callback function associated with the
1144 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1145 * fields.
1146 **/
1147void
1148lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1149 uint32_t ulpstatus, uint32_t ulpWord4)
1150{
1151 struct lpfc_iocbq *piocb;
1152
1153 while (!list_empty(iocblist)) {
1154 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001155 if (!piocb->iocb_cmpl)
1156 lpfc_sli_release_iocbq(phba, piocb);
1157 else {
1158 piocb->iocb.ulpStatus = ulpstatus;
1159 piocb->iocb.un.ulpWord[4] = ulpWord4;
1160 (piocb->iocb_cmpl) (phba, piocb, piocb);
1161 }
1162 }
1163 return;
1164}
1165
1166/**
James Smart3621a712009-04-06 18:47:14 -04001167 * lpfc_sli_iocb_cmd_type - Get the iocb type
1168 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001169 *
1170 * This function is called by ring event handler function to get the iocb type.
1171 * This function translates the iocb command to an iocb command type used to
1172 * decide the final disposition of each completed IOCB.
1173 * The function returns
1174 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1175 * LPFC_SOL_IOCB if it is a solicited iocb completion
1176 * LPFC_ABORT_IOCB if it is an abort iocb
1177 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1178 *
1179 * The caller is not required to hold any lock.
1180 **/
dea31012005-04-17 16:05:31 -05001181static lpfc_iocb_type
1182lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1183{
1184 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1185
1186 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1187 return 0;
1188
1189 switch (iocb_cmnd) {
1190 case CMD_XMIT_SEQUENCE_CR:
1191 case CMD_XMIT_SEQUENCE_CX:
1192 case CMD_XMIT_BCAST_CN:
1193 case CMD_XMIT_BCAST_CX:
1194 case CMD_ELS_REQUEST_CR:
1195 case CMD_ELS_REQUEST_CX:
1196 case CMD_CREATE_XRI_CR:
1197 case CMD_CREATE_XRI_CX:
1198 case CMD_GET_RPI_CN:
1199 case CMD_XMIT_ELS_RSP_CX:
1200 case CMD_GET_RPI_CR:
1201 case CMD_FCP_IWRITE_CR:
1202 case CMD_FCP_IWRITE_CX:
1203 case CMD_FCP_IREAD_CR:
1204 case CMD_FCP_IREAD_CX:
1205 case CMD_FCP_ICMND_CR:
1206 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001207 case CMD_FCP_TSEND_CX:
1208 case CMD_FCP_TRSP_CX:
1209 case CMD_FCP_TRECEIVE_CX:
1210 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001211 case CMD_ADAPTER_MSG:
1212 case CMD_ADAPTER_DUMP:
1213 case CMD_XMIT_SEQUENCE64_CR:
1214 case CMD_XMIT_SEQUENCE64_CX:
1215 case CMD_XMIT_BCAST64_CN:
1216 case CMD_XMIT_BCAST64_CX:
1217 case CMD_ELS_REQUEST64_CR:
1218 case CMD_ELS_REQUEST64_CX:
1219 case CMD_FCP_IWRITE64_CR:
1220 case CMD_FCP_IWRITE64_CX:
1221 case CMD_FCP_IREAD64_CR:
1222 case CMD_FCP_IREAD64_CX:
1223 case CMD_FCP_ICMND64_CR:
1224 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001225 case CMD_FCP_TSEND64_CX:
1226 case CMD_FCP_TRSP64_CX:
1227 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001228 case CMD_GEN_REQUEST64_CR:
1229 case CMD_GEN_REQUEST64_CX:
1230 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001231 case DSSCMD_IWRITE64_CR:
1232 case DSSCMD_IWRITE64_CX:
1233 case DSSCMD_IREAD64_CR:
1234 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001235 type = LPFC_SOL_IOCB;
1236 break;
1237 case CMD_ABORT_XRI_CN:
1238 case CMD_ABORT_XRI_CX:
1239 case CMD_CLOSE_XRI_CN:
1240 case CMD_CLOSE_XRI_CX:
1241 case CMD_XRI_ABORTED_CX:
1242 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001243 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001244 type = LPFC_ABORT_IOCB;
1245 break;
1246 case CMD_RCV_SEQUENCE_CX:
1247 case CMD_RCV_ELS_REQ_CX:
1248 case CMD_RCV_SEQUENCE64_CX:
1249 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001250 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001251 case CMD_IOCB_RCV_SEQ64_CX:
1252 case CMD_IOCB_RCV_ELS64_CX:
1253 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001254 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001255 type = LPFC_UNSOL_IOCB;
1256 break;
James Smart3163f722008-02-08 18:50:25 -05001257 case CMD_IOCB_XMIT_MSEQ64_CR:
1258 case CMD_IOCB_XMIT_MSEQ64_CX:
1259 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1260 case CMD_IOCB_RCV_ELS_LIST64_CX:
1261 case CMD_IOCB_CLOSE_EXTENDED_CN:
1262 case CMD_IOCB_ABORT_EXTENDED_CN:
1263 case CMD_IOCB_RET_HBQE64_CN:
1264 case CMD_IOCB_FCP_IBIDIR64_CR:
1265 case CMD_IOCB_FCP_IBIDIR64_CX:
1266 case CMD_IOCB_FCP_ITASKMGT64_CX:
1267 case CMD_IOCB_LOGENTRY_CN:
1268 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1269 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001270 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001271 type = LPFC_UNKNOWN_IOCB;
1272 break;
dea31012005-04-17 16:05:31 -05001273 default:
1274 type = LPFC_UNKNOWN_IOCB;
1275 break;
1276 }
1277
1278 return type;
1279}
1280
James Smarte59058c2008-08-24 21:49:00 -04001281/**
James Smart3621a712009-04-06 18:47:14 -04001282 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001283 * @phba: Pointer to HBA context object.
1284 *
1285 * This function is called from SLI initialization code
1286 * to configure every ring of the HBA's SLI interface. The
1287 * caller is not required to hold any lock. This function issues
1288 * a config_ring mailbox command for each ring.
1289 * This function returns zero if successful else returns a negative
1290 * error code.
1291 **/
dea31012005-04-17 16:05:31 -05001292static int
James Smarted957682007-06-17 19:56:37 -05001293lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001294{
1295 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001296 LPFC_MBOXQ_t *pmb;
1297 MAILBOX_t *pmbox;
1298 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001299
James Smarted957682007-06-17 19:56:37 -05001300 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1301 if (!pmb)
1302 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001303 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001304 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001305 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001306 lpfc_config_ring(phba, i, pmb);
1307 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1308 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001310 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001311 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1312 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001313 rc, pmbox->mbxCommand,
1314 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001315 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001316 ret = -ENXIO;
1317 break;
dea31012005-04-17 16:05:31 -05001318 }
1319 }
James Smarted957682007-06-17 19:56:37 -05001320 mempool_free(pmb, phba->mbox_mem_pool);
1321 return ret;
dea31012005-04-17 16:05:31 -05001322}
1323
James Smarte59058c2008-08-24 21:49:00 -04001324/**
James Smart3621a712009-04-06 18:47:14 -04001325 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001326 * @phba: Pointer to HBA context object.
1327 * @pring: Pointer to driver SLI ring object.
1328 * @piocb: Pointer to the driver iocb object.
1329 *
1330 * This function is called with hbalock held. The function adds the
1331 * new iocb to txcmplq of the given ring. This function always returns
1332 * 0. If this function is called for ELS ring, this function checks if
1333 * there is a vport associated with the ELS command. This function also
1334 * starts els_tmofunc timer if this is an ELS command.
1335 **/
dea31012005-04-17 16:05:31 -05001336static int
James Smart2e0fef82007-06-17 19:56:36 -05001337lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1338 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001339{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001340 lockdep_assert_held(&phba->hbalock);
1341
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001342 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001343
dea31012005-04-17 16:05:31 -05001344 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001345 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001346
James Smart92d7f7b2007-06-17 19:56:38 -05001347 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1348 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001349 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1350 BUG_ON(!piocb->vport);
1351 if (!(piocb->vport->load_flag & FC_UNLOADING))
1352 mod_timer(&piocb->vport->els_tmofunc,
1353 jiffies +
1354 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1355 }
dea31012005-04-17 16:05:31 -05001356
James Smart2e0fef82007-06-17 19:56:36 -05001357 return 0;
dea31012005-04-17 16:05:31 -05001358}
1359
James Smarte59058c2008-08-24 21:49:00 -04001360/**
James Smart3621a712009-04-06 18:47:14 -04001361 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001362 * @phba: Pointer to HBA context object.
1363 * @pring: Pointer to driver SLI ring object.
1364 *
1365 * This function is called with hbalock held to get next
1366 * iocb in txq of the given ring. If there is any iocb in
1367 * the txq, the function returns first iocb in the list after
1368 * removing the iocb from the list, else it returns NULL.
1369 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001370struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001371lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001372{
dea31012005-04-17 16:05:31 -05001373 struct lpfc_iocbq *cmd_iocb;
1374
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001375 lockdep_assert_held(&phba->hbalock);
1376
James Smart858c9f62007-06-17 19:56:39 -05001377 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001378 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001379}
1380
James Smarte59058c2008-08-24 21:49:00 -04001381/**
James Smart3621a712009-04-06 18:47:14 -04001382 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001383 * @phba: Pointer to HBA context object.
1384 * @pring: Pointer to driver SLI ring object.
1385 *
1386 * This function is called with hbalock held and the caller must post the
1387 * iocb without releasing the lock. If the caller releases the lock,
1388 * iocb slot returned by the function is not guaranteed to be available.
1389 * The function returns pointer to the next available iocb slot if there
1390 * is available slot in the ring, else it returns NULL.
1391 * If the get index of the ring is ahead of the put index, the function
1392 * will post an error attention event to the worker thread to take the
1393 * HBA to offline state.
1394 **/
dea31012005-04-17 16:05:31 -05001395static IOCB_t *
1396lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1397{
James Smart34b02dc2008-08-24 21:49:55 -04001398 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001399 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001400
1401 lockdep_assert_held(&phba->hbalock);
1402
James Smart7e56aa22012-08-03 12:35:34 -04001403 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1404 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1405 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001406
James Smart7e56aa22012-08-03 12:35:34 -04001407 if (unlikely(pring->sli.sli3.local_getidx ==
1408 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001409
James Smart7e56aa22012-08-03 12:35:34 -04001410 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001411
James Smart7e56aa22012-08-03 12:35:34 -04001412 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001413 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001414 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001415 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001416 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001417 pring->sli.sli3.local_getidx,
1418 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001419
James Smart2e0fef82007-06-17 19:56:36 -05001420 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001421 /*
1422 * All error attention handlers are posted to
1423 * worker thread
1424 */
1425 phba->work_ha |= HA_ERATT;
1426 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001427
James Smart5e9d9b82008-06-14 22:52:53 -04001428 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001429
1430 return NULL;
1431 }
1432
James Smart7e56aa22012-08-03 12:35:34 -04001433 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001434 return NULL;
1435 }
1436
James Smarted957682007-06-17 19:56:37 -05001437 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001438}
1439
James Smarte59058c2008-08-24 21:49:00 -04001440/**
James Smart3621a712009-04-06 18:47:14 -04001441 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001442 * @phba: Pointer to HBA context object.
1443 * @iocbq: Pointer to driver iocb object.
1444 *
1445 * This function gets an iotag for the iocb. If there is no unused iotag and
1446 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1447 * array and assigns a new iotag.
1448 * The function returns the allocated iotag if successful, else returns zero.
1449 * Zero is not a valid iotag.
1450 * The caller is not required to hold any lock.
1451 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001452uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001453lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001454{
James Smart2e0fef82007-06-17 19:56:36 -05001455 struct lpfc_iocbq **new_arr;
1456 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001457 size_t new_len;
1458 struct lpfc_sli *psli = &phba->sli;
1459 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001460
James Smart2e0fef82007-06-17 19:56:36 -05001461 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001462 iotag = psli->last_iotag;
1463 if(++iotag < psli->iocbq_lookup_len) {
1464 psli->last_iotag = iotag;
1465 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001466 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001467 iocbq->iotag = iotag;
1468 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001469 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001470 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1471 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001472 spin_unlock_irq(&phba->hbalock);
1473 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001474 GFP_KERNEL);
1475 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001476 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001477 old_arr = psli->iocbq_lookup;
1478 if (new_len <= psli->iocbq_lookup_len) {
1479 /* highly unprobable case */
1480 kfree(new_arr);
1481 iotag = psli->last_iotag;
1482 if(++iotag < psli->iocbq_lookup_len) {
1483 psli->last_iotag = iotag;
1484 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001485 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001486 iocbq->iotag = iotag;
1487 return iotag;
1488 }
James Smart2e0fef82007-06-17 19:56:36 -05001489 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001490 return 0;
1491 }
1492 if (psli->iocbq_lookup)
1493 memcpy(new_arr, old_arr,
1494 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001495 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001496 psli->iocbq_lookup = new_arr;
1497 psli->iocbq_lookup_len = new_len;
1498 psli->last_iotag = iotag;
1499 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001500 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001501 iocbq->iotag = iotag;
1502 kfree(old_arr);
1503 return iotag;
1504 }
James Smart8f6d98d2006-08-01 07:34:00 -04001505 } else
James Smart2e0fef82007-06-17 19:56:36 -05001506 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001507
James Smartbc739052010-08-04 16:11:18 -04001508 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001509 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1510 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001511
James Bottomley604a3e32005-10-29 10:28:33 -05001512 return 0;
dea31012005-04-17 16:05:31 -05001513}
1514
James Smarte59058c2008-08-24 21:49:00 -04001515/**
James Smart3621a712009-04-06 18:47:14 -04001516 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001517 * @phba: Pointer to HBA context object.
1518 * @pring: Pointer to driver SLI ring object.
1519 * @iocb: Pointer to iocb slot in the ring.
1520 * @nextiocb: Pointer to driver iocb object which need to be
1521 * posted to firmware.
1522 *
1523 * This function is called with hbalock held to post a new iocb to
1524 * the firmware. This function copies the new iocb to ring iocb slot and
1525 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1526 * a completion call back for this iocb else the function will free the
1527 * iocb object.
1528 **/
dea31012005-04-17 16:05:31 -05001529static void
1530lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1531 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1532{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001533 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001534 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001535 * Set up an iotag
dea31012005-04-17 16:05:31 -05001536 */
James Bottomley604a3e32005-10-29 10:28:33 -05001537 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001538
James Smarte2a0a9d2008-12-04 22:40:02 -05001539
James Smarta58cbd52007-08-02 11:09:43 -04001540 if (pring->ringno == LPFC_ELS_RING) {
1541 lpfc_debugfs_slow_ring_trc(phba,
1542 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1543 *(((uint32_t *) &nextiocb->iocb) + 4),
1544 *(((uint32_t *) &nextiocb->iocb) + 6),
1545 *(((uint32_t *) &nextiocb->iocb) + 7));
1546 }
1547
dea31012005-04-17 16:05:31 -05001548 /*
1549 * Issue iocb command to adapter
1550 */
James Smart92d7f7b2007-06-17 19:56:38 -05001551 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001552 wmb();
1553 pring->stats.iocb_cmd++;
1554
1555 /*
1556 * If there is no completion routine to call, we can release the
1557 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1558 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1559 */
1560 if (nextiocb->iocb_cmpl)
1561 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001562 else
James Smart2e0fef82007-06-17 19:56:36 -05001563 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001564
1565 /*
1566 * Let the HBA know what IOCB slot will be the next one the
1567 * driver will put a command into.
1568 */
James Smart7e56aa22012-08-03 12:35:34 -04001569 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1570 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001571}
1572
James Smarte59058c2008-08-24 21:49:00 -04001573/**
James Smart3621a712009-04-06 18:47:14 -04001574 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001575 * @phba: Pointer to HBA context object.
1576 * @pring: Pointer to driver SLI ring object.
1577 *
1578 * The caller is not required to hold any lock for calling this function.
1579 * This function updates the chip attention bits for the ring to inform firmware
1580 * that there are pending work to be done for this ring and requests an
1581 * interrupt when there is space available in the ring. This function is
1582 * called when the driver is unable to post more iocbs to the ring due
1583 * to unavailability of space in the ring.
1584 **/
dea31012005-04-17 16:05:31 -05001585static void
James Smart2e0fef82007-06-17 19:56:36 -05001586lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001587{
1588 int ringno = pring->ringno;
1589
1590 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1591
1592 wmb();
1593
1594 /*
1595 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1596 * The HBA will tell us when an IOCB entry is available.
1597 */
1598 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1599 readl(phba->CAregaddr); /* flush */
1600
1601 pring->stats.iocb_cmd_full++;
1602}
1603
James Smarte59058c2008-08-24 21:49:00 -04001604/**
James Smart3621a712009-04-06 18:47:14 -04001605 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001606 * @phba: Pointer to HBA context object.
1607 * @pring: Pointer to driver SLI ring object.
1608 *
1609 * This function updates the chip attention register bit for the
1610 * given ring to inform HBA that there is more work to be done
1611 * in this ring. The caller is not required to hold any lock.
1612 **/
dea31012005-04-17 16:05:31 -05001613static void
James Smart2e0fef82007-06-17 19:56:36 -05001614lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001615{
1616 int ringno = pring->ringno;
1617
1618 /*
1619 * Tell the HBA that there is work to do in this ring.
1620 */
James Smart34b02dc2008-08-24 21:49:55 -04001621 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1622 wmb();
1623 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1624 readl(phba->CAregaddr); /* flush */
1625 }
dea31012005-04-17 16:05:31 -05001626}
1627
James Smarte59058c2008-08-24 21:49:00 -04001628/**
James Smart3621a712009-04-06 18:47:14 -04001629 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001630 * @phba: Pointer to HBA context object.
1631 * @pring: Pointer to driver SLI ring object.
1632 *
1633 * This function is called with hbalock held to post pending iocbs
1634 * in the txq to the firmware. This function is called when driver
1635 * detects space available in the ring.
1636 **/
dea31012005-04-17 16:05:31 -05001637static void
James Smart2e0fef82007-06-17 19:56:36 -05001638lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001639{
1640 IOCB_t *iocb;
1641 struct lpfc_iocbq *nextiocb;
1642
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001643 lockdep_assert_held(&phba->hbalock);
1644
dea31012005-04-17 16:05:31 -05001645 /*
1646 * Check to see if:
1647 * (a) there is anything on the txq to send
1648 * (b) link is up
1649 * (c) link attention events can be processed (fcp ring only)
1650 * (d) IOCB processing is not blocked by the outstanding mbox command.
1651 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001652
1653 if (lpfc_is_link_up(phba) &&
1654 (!list_empty(&pring->txq)) &&
James Smart895427b2017-02-12 13:52:30 -08001655 (pring->ringno != LPFC_FCP_RING ||
James Smart0b727fe2007-10-27 13:37:25 -04001656 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001657
1658 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1659 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1660 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1661
1662 if (iocb)
1663 lpfc_sli_update_ring(phba, pring);
1664 else
1665 lpfc_sli_update_full_ring(phba, pring);
1666 }
1667
1668 return;
1669}
1670
James Smarte59058c2008-08-24 21:49:00 -04001671/**
James Smart3621a712009-04-06 18:47:14 -04001672 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001673 * @phba: Pointer to HBA context object.
1674 * @hbqno: HBQ number.
1675 *
1676 * This function is called with hbalock held to get the next
1677 * available slot for the given HBQ. If there is free slot
1678 * available for the HBQ it will return pointer to the next available
1679 * HBQ entry else it will return NULL.
1680 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001681static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001682lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1683{
1684 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1685
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001686 lockdep_assert_held(&phba->hbalock);
1687
James Smarted957682007-06-17 19:56:37 -05001688 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1689 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1690 hbqp->next_hbqPutIdx = 0;
1691
1692 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001693 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001694 uint32_t getidx = le32_to_cpu(raw_index);
1695
1696 hbqp->local_hbqGetIdx = getidx;
1697
1698 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1699 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001700 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001701 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001702 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001703 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001704 hbqp->entry_count);
1705
1706 phba->link_state = LPFC_HBA_ERROR;
1707 return NULL;
1708 }
1709
1710 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1711 return NULL;
1712 }
1713
James Smart51ef4c22007-08-02 11:10:31 -04001714 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1715 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001716}
1717
James Smarte59058c2008-08-24 21:49:00 -04001718/**
James Smart3621a712009-04-06 18:47:14 -04001719 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001720 * @phba: Pointer to HBA context object.
1721 *
1722 * This function is called with no lock held to free all the
1723 * hbq buffers while uninitializing the SLI interface. It also
1724 * frees the HBQ buffers returned by the firmware but not yet
1725 * processed by the upper layers.
1726 **/
James Smarted957682007-06-17 19:56:37 -05001727void
1728lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1729{
James Smart92d7f7b2007-06-17 19:56:38 -05001730 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1731 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001732 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001733 int i, hbq_count;
James Smarted957682007-06-17 19:56:37 -05001734
James Smart51ef4c22007-08-02 11:10:31 -04001735 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001736 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001737 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001738 for (i = 0; i < hbq_count; ++i) {
1739 list_for_each_entry_safe(dmabuf, next_dmabuf,
1740 &phba->hbqs[i].hbq_buffer_list, list) {
1741 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1742 list_del(&hbq_buf->dbuf.list);
1743 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1744 }
James Smarta8adb832007-10-27 13:37:53 -04001745 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001746 }
James Smart3163f722008-02-08 18:50:25 -05001747
1748 /* Mark the HBQs not in use */
1749 phba->hbq_in_use = 0;
1750 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001751}
1752
James Smarte59058c2008-08-24 21:49:00 -04001753/**
James Smart3621a712009-04-06 18:47:14 -04001754 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001755 * @phba: Pointer to HBA context object.
1756 * @hbqno: HBQ number.
1757 * @hbq_buf: Pointer to HBQ buffer.
1758 *
1759 * This function is called with the hbalock held to post a
1760 * hbq buffer to the firmware. If the function finds an empty
1761 * slot in the HBQ, it will post the buffer. The function will return
1762 * pointer to the hbq entry if it successfully post the buffer
1763 * else it will return NULL.
1764 **/
James Smart3772a992009-05-22 14:50:54 -04001765static int
James Smarted957682007-06-17 19:56:37 -05001766lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001767 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001768{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001769 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001770 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1771}
1772
1773/**
1774 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1775 * @phba: Pointer to HBA context object.
1776 * @hbqno: HBQ number.
1777 * @hbq_buf: Pointer to HBQ buffer.
1778 *
1779 * This function is called with the hbalock held to post a hbq buffer to the
1780 * firmware. If the function finds an empty slot in the HBQ, it will post the
1781 * buffer and place it on the hbq_buffer_list. The function will return zero if
1782 * it successfully post the buffer else it will return an error.
1783 **/
1784static int
1785lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1786 struct hbq_dmabuf *hbq_buf)
1787{
James Smarted957682007-06-17 19:56:37 -05001788 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001789 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001790
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001791 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05001792 /* Get next HBQ entry slot to use */
1793 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1794 if (hbqe) {
1795 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1796
James Smart92d7f7b2007-06-17 19:56:38 -05001797 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1798 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart895427b2017-02-12 13:52:30 -08001799 hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
James Smarted957682007-06-17 19:56:37 -05001800 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001801 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1802 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1803 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001804 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1805 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001806 /* flush */
James Smarted957682007-06-17 19:56:37 -05001807 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001808 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001809 return 0;
1810 } else
1811 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001812}
1813
James Smart4f774512009-05-22 14:52:35 -04001814/**
1815 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1816 * @phba: Pointer to HBA context object.
1817 * @hbqno: HBQ number.
1818 * @hbq_buf: Pointer to HBQ buffer.
1819 *
1820 * This function is called with the hbalock held to post an RQE to the SLI4
1821 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1822 * the hbq_buffer_list and return zero, otherwise it will return an error.
1823 **/
1824static int
1825lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1826 struct hbq_dmabuf *hbq_buf)
1827{
1828 int rc;
1829 struct lpfc_rqe hrqe;
1830 struct lpfc_rqe drqe;
James Smart895427b2017-02-12 13:52:30 -08001831 struct lpfc_queue *hrq;
1832 struct lpfc_queue *drq;
1833
1834 if (hbqno != LPFC_ELS_HBQ)
1835 return 1;
1836 hrq = phba->sli4_hba.hdr_rq;
1837 drq = phba->sli4_hba.dat_rq;
James Smart4f774512009-05-22 14:52:35 -04001838
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001839 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04001840 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1841 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1842 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1843 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
James Smart895427b2017-02-12 13:52:30 -08001844 rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
James Smart4f774512009-05-22 14:52:35 -04001845 if (rc < 0)
1846 return rc;
James Smart895427b2017-02-12 13:52:30 -08001847 hbq_buf->tag = (rc | (hbqno << 16));
James Smart4f774512009-05-22 14:52:35 -04001848 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1849 return 0;
1850}
1851
James Smarte59058c2008-08-24 21:49:00 -04001852/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001853static struct lpfc_hbq_init lpfc_els_hbq = {
1854 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001855 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001856 .mask_count = 0,
1857 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001858 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001859 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001860 .init_count = 40,
1861 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001862};
James Smarted957682007-06-17 19:56:37 -05001863
James Smarte59058c2008-08-24 21:49:00 -04001864/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001865struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001866 &lpfc_els_hbq,
1867};
1868
James Smarte59058c2008-08-24 21:49:00 -04001869/**
James Smart3621a712009-04-06 18:47:14 -04001870 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001871 * @phba: Pointer to HBA context object.
1872 * @hbqno: HBQ number.
1873 * @count: Number of HBQ buffers to be posted.
1874 *
James Smartd7c255b2008-08-24 21:50:00 -04001875 * This function is called with no lock held to post more hbq buffers to the
1876 * given HBQ. The function returns the number of HBQ buffers successfully
1877 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001878 **/
James Smart311464e2007-08-02 11:10:37 -04001879static int
James Smart92d7f7b2007-06-17 19:56:38 -05001880lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1881{
James Smartd7c255b2008-08-24 21:50:00 -04001882 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001883 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001884 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001885 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001886 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001887 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001888
James Smartd7c255b2008-08-24 21:50:00 -04001889 if ((phba->hbqs[hbqno].buffer_count + count) >
1890 lpfc_hbq_defs[hbqno]->entry_count)
1891 count = lpfc_hbq_defs[hbqno]->entry_count -
1892 phba->hbqs[hbqno].buffer_count;
1893 if (!count)
1894 return 0;
1895 /* Allocate HBQ entries */
1896 for (i = 0; i < count; i++) {
1897 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1898 if (!hbq_buffer)
1899 break;
1900 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1901 }
James Smart3163f722008-02-08 18:50:25 -05001902 /* Check whether HBQ is still in use */
1903 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001904 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001905 goto err;
1906 while (!list_empty(&hbq_buf_list)) {
1907 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1908 dbuf.list);
1909 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1910 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001911 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001912 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001913 posted++;
1914 } else
James Smart51ef4c22007-08-02 11:10:31 -04001915 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001916 }
James Smart3163f722008-02-08 18:50:25 -05001917 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001918 return posted;
1919err:
1920 spin_unlock_irqrestore(&phba->hbalock, flags);
1921 while (!list_empty(&hbq_buf_list)) {
1922 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1923 dbuf.list);
1924 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1925 }
James Smart92d7f7b2007-06-17 19:56:38 -05001926 return 0;
James Smarted957682007-06-17 19:56:37 -05001927}
1928
James Smarte59058c2008-08-24 21:49:00 -04001929/**
James Smart3621a712009-04-06 18:47:14 -04001930 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001931 * @phba: Pointer to HBA context object.
1932 * @qno: HBQ number.
1933 *
1934 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001935 * is called with no lock held. The function returns the number of HBQ entries
1936 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001937 **/
James Smarted957682007-06-17 19:56:37 -05001938int
James Smart92d7f7b2007-06-17 19:56:38 -05001939lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001940{
James Smartdef9c7a2009-12-21 17:02:28 -05001941 if (phba->sli_rev == LPFC_SLI_REV4)
1942 return 0;
1943 else
1944 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1945 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001946}
1947
James Smarte59058c2008-08-24 21:49:00 -04001948/**
James Smart3621a712009-04-06 18:47:14 -04001949 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001950 * @phba: Pointer to HBA context object.
1951 * @qno: HBQ queue number.
1952 *
1953 * This function is called from SLI initialization code path with
1954 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001955 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001956 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001957static int
James Smart92d7f7b2007-06-17 19:56:38 -05001958lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001959{
James Smartdef9c7a2009-12-21 17:02:28 -05001960 if (phba->sli_rev == LPFC_SLI_REV4)
1961 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001962 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001963 else
1964 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1965 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001966}
1967
James Smarte59058c2008-08-24 21:49:00 -04001968/**
James Smart3772a992009-05-22 14:50:54 -04001969 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1970 * @phba: Pointer to HBA context object.
1971 * @hbqno: HBQ number.
1972 *
1973 * This function removes the first hbq buffer on an hbq list and returns a
1974 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1975 **/
1976static struct hbq_dmabuf *
1977lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1978{
1979 struct lpfc_dmabuf *d_buf;
1980
1981 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1982 if (!d_buf)
1983 return NULL;
1984 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1985}
1986
1987/**
James Smart3621a712009-04-06 18:47:14 -04001988 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001989 * @phba: Pointer to HBA context object.
1990 * @tag: Tag of the hbq buffer.
1991 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02001992 * This function searches for the hbq buffer associated with the given tag in
1993 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
1994 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04001995 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001996static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001997lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1998{
James Smart92d7f7b2007-06-17 19:56:38 -05001999 struct lpfc_dmabuf *d_buf;
2000 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002001 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002002
James Smart51ef4c22007-08-02 11:10:31 -04002003 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002004 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002005 return NULL;
2006
James Smart3772a992009-05-22 14:50:54 -04002007 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002008 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002009 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002010 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002011 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002012 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002013 }
2014 }
James Smart3772a992009-05-22 14:50:54 -04002015 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002016 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002017 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002018 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002019 return NULL;
James Smarted957682007-06-17 19:56:37 -05002020}
2021
James Smarte59058c2008-08-24 21:49:00 -04002022/**
James Smart3621a712009-04-06 18:47:14 -04002023 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002024 * @phba: Pointer to HBA context object.
2025 * @hbq_buffer: Pointer to HBQ buffer.
2026 *
2027 * This function is called with hbalock. This function gives back
2028 * the hbq buffer to firmware. If the HBQ does not have space to
2029 * post the buffer, it will free the buffer.
2030 **/
James Smarted957682007-06-17 19:56:37 -05002031void
James Smart51ef4c22007-08-02 11:10:31 -04002032lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002033{
2034 uint32_t hbqno;
2035
James Smart51ef4c22007-08-02 11:10:31 -04002036 if (hbq_buffer) {
2037 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002038 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002039 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002040 }
2041}
2042
James Smarte59058c2008-08-24 21:49:00 -04002043/**
James Smart3621a712009-04-06 18:47:14 -04002044 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002045 * @mbxCommand: mailbox command code.
2046 *
2047 * This function is called by the mailbox event handler function to verify
2048 * that the completed mailbox command is a legitimate mailbox command. If the
2049 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2050 * and the mailbox event handler will take the HBA offline.
2051 **/
dea31012005-04-17 16:05:31 -05002052static int
2053lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2054{
2055 uint8_t ret;
2056
2057 switch (mbxCommand) {
2058 case MBX_LOAD_SM:
2059 case MBX_READ_NV:
2060 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002061 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002062 case MBX_RUN_BIU_DIAG:
2063 case MBX_INIT_LINK:
2064 case MBX_DOWN_LINK:
2065 case MBX_CONFIG_LINK:
2066 case MBX_CONFIG_RING:
2067 case MBX_RESET_RING:
2068 case MBX_READ_CONFIG:
2069 case MBX_READ_RCONFIG:
2070 case MBX_READ_SPARM:
2071 case MBX_READ_STATUS:
2072 case MBX_READ_RPI:
2073 case MBX_READ_XRI:
2074 case MBX_READ_REV:
2075 case MBX_READ_LNK_STAT:
2076 case MBX_REG_LOGIN:
2077 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002078 case MBX_CLEAR_LA:
2079 case MBX_DUMP_MEMORY:
2080 case MBX_DUMP_CONTEXT:
2081 case MBX_RUN_DIAGS:
2082 case MBX_RESTART:
2083 case MBX_UPDATE_CFG:
2084 case MBX_DOWN_LOAD:
2085 case MBX_DEL_LD_ENTRY:
2086 case MBX_RUN_PROGRAM:
2087 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002088 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002089 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002090 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002091 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002092 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002093 case MBX_LOAD_AREA:
2094 case MBX_RUN_BIU_DIAG64:
2095 case MBX_CONFIG_PORT:
2096 case MBX_READ_SPARM64:
2097 case MBX_READ_RPI64:
2098 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002099 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002100 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002101 case MBX_SET_DEBUG:
2102 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002103 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002104 case MBX_REG_VPI:
2105 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002106 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002107 case MBX_PORT_CAPABILITIES:
2108 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002109 case MBX_SLI4_CONFIG:
2110 case MBX_SLI4_REQ_FTRS:
2111 case MBX_REG_FCFI:
2112 case MBX_UNREG_FCFI:
2113 case MBX_REG_VFI:
2114 case MBX_UNREG_VFI:
2115 case MBX_INIT_VPI:
2116 case MBX_INIT_VFI:
2117 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002118 case MBX_READ_EVENT_LOG_STATUS:
2119 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002120 case MBX_SECURITY_MGMT:
2121 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002122 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002123 ret = mbxCommand;
2124 break;
2125 default:
2126 ret = MBX_SHUTDOWN;
2127 break;
2128 }
James Smart2e0fef82007-06-17 19:56:36 -05002129 return ret;
dea31012005-04-17 16:05:31 -05002130}
James Smarte59058c2008-08-24 21:49:00 -04002131
2132/**
James Smart3621a712009-04-06 18:47:14 -04002133 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002134 * @phba: Pointer to HBA context object.
2135 * @pmboxq: Pointer to mailbox command.
2136 *
2137 * This is completion handler function for mailbox commands issued from
2138 * lpfc_sli_issue_mbox_wait function. This function is called by the
2139 * mailbox event handler function with no lock held. This function
2140 * will wake up thread waiting on the wait queue pointed by context1
2141 * of the mailbox.
2142 **/
James Smart04c68492009-05-22 14:52:52 -04002143void
James Smart2e0fef82007-06-17 19:56:36 -05002144lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002145{
2146 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002147 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002148
2149 /*
2150 * If pdone_q is empty, the driver thread gave up waiting and
2151 * continued running.
2152 */
James Smart7054a602007-04-25 09:52:34 -04002153 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002154 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002155 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2156 if (pdone_q)
2157 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002158 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002159 return;
2160}
2161
James Smarte59058c2008-08-24 21:49:00 -04002162
2163/**
James Smart3621a712009-04-06 18:47:14 -04002164 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002165 * @phba: Pointer to HBA context object.
2166 * @pmb: Pointer to mailbox object.
2167 *
2168 * This function is the default mailbox completion handler. It
2169 * frees the memory resources associated with the completed mailbox
2170 * command. If the completed command is a REG_LOGIN mailbox command,
2171 * this function will issue a UREG_LOGIN to re-claim the RPI.
2172 **/
dea31012005-04-17 16:05:31 -05002173void
James Smart2e0fef82007-06-17 19:56:36 -05002174lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002175{
James Smartd439d282010-09-29 11:18:45 -04002176 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002177 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002178 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002179 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002180 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002181 int rc;
2182
dea31012005-04-17 16:05:31 -05002183 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002184
dea31012005-04-17 16:05:31 -05002185 if (mp) {
2186 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2187 kfree(mp);
2188 }
James Smart7054a602007-04-25 09:52:34 -04002189
2190 /*
2191 * If a REG_LOGIN succeeded after node is destroyed or node
2192 * is in re-discovery driver need to cleanup the RPI.
2193 */
James Smart2e0fef82007-06-17 19:56:36 -05002194 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002195 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2196 !pmb->u.mb.mbxStatus) {
2197 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002198 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002199 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002200 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002201 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002202 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2203 if (rc != MBX_NOT_FINISHED)
2204 return;
2205 }
2206
James Smart695a8142010-01-26 23:08:03 -05002207 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2208 !(phba->pport->load_flag & FC_UNLOADING) &&
2209 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002210 shost = lpfc_shost_from_vport(vport);
2211 spin_lock_irq(shost->host_lock);
2212 vport->vpi_state |= LPFC_VPI_REGISTERED;
2213 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2214 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002215 }
2216
James Smartd439d282010-09-29 11:18:45 -04002217 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2218 ndlp = (struct lpfc_nodelist *)pmb->context2;
2219 lpfc_nlp_put(ndlp);
2220 pmb->context2 = NULL;
2221 }
2222
James Smartdcf2a4e2010-09-29 11:18:53 -04002223 /* Check security permission status on INIT_LINK mailbox command */
2224 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2225 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2226 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2227 "2860 SLI authentication is required "
2228 "for INIT_LINK but has not done yet\n");
2229
James Smart04c68492009-05-22 14:52:52 -04002230 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2231 lpfc_sli4_mbox_cmd_free(phba, pmb);
2232 else
2233 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002234}
James Smartbe6bb942015-04-07 15:07:22 -04002235 /**
2236 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2237 * @phba: Pointer to HBA context object.
2238 * @pmb: Pointer to mailbox object.
2239 *
2240 * This function is the unreg rpi mailbox completion handler. It
2241 * frees the memory resources associated with the completed mailbox
2242 * command. An additional refrenece is put on the ndlp to prevent
2243 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2244 * the unreg mailbox command completes, this routine puts the
2245 * reference back.
2246 *
2247 **/
2248void
2249lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2250{
2251 struct lpfc_vport *vport = pmb->vport;
2252 struct lpfc_nodelist *ndlp;
2253
2254 ndlp = pmb->context1;
2255 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2256 if (phba->sli_rev == LPFC_SLI_REV4 &&
2257 (bf_get(lpfc_sli_intf_if_type,
2258 &phba->sli4_hba.sli_intf) ==
2259 LPFC_SLI_INTF_IF_TYPE_2)) {
2260 if (ndlp) {
2261 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2262 "0010 UNREG_LOGIN vpi:%x "
2263 "rpi:%x DID:%x map:%x %p\n",
2264 vport->vpi, ndlp->nlp_rpi,
2265 ndlp->nlp_DID,
2266 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002267 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002268 lpfc_nlp_put(ndlp);
2269 }
2270 }
2271 }
2272
2273 mempool_free(pmb, phba->mbox_mem_pool);
2274}
dea31012005-04-17 16:05:31 -05002275
James Smarte59058c2008-08-24 21:49:00 -04002276/**
James Smart3621a712009-04-06 18:47:14 -04002277 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002278 * @phba: Pointer to HBA context object.
2279 *
2280 * This function is called with no lock held. This function processes all
2281 * the completed mailbox commands and gives it to upper layers. The interrupt
2282 * service routine processes mailbox completion interrupt and adds completed
2283 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2284 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2285 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2286 * function returns the mailbox commands to the upper layer by calling the
2287 * completion handler function of each mailbox.
2288 **/
dea31012005-04-17 16:05:31 -05002289int
James Smart2e0fef82007-06-17 19:56:36 -05002290lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002291{
James Smart92d7f7b2007-06-17 19:56:38 -05002292 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002293 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002294 int rc;
2295 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002296
2297 phba->sli.slistat.mbox_event++;
2298
James Smart92d7f7b2007-06-17 19:56:38 -05002299 /* Get all completed mailboxe buffers into the cmplq */
2300 spin_lock_irq(&phba->hbalock);
2301 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2302 spin_unlock_irq(&phba->hbalock);
2303
dea31012005-04-17 16:05:31 -05002304 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002305 do {
2306 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2307 if (pmb == NULL)
2308 break;
2309
James Smart04c68492009-05-22 14:52:52 -04002310 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002311
James Smart858c9f62007-06-17 19:56:39 -05002312 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2313 if (pmb->vport) {
2314 lpfc_debugfs_disc_trc(pmb->vport,
2315 LPFC_DISC_TRC_MBOX_VPORT,
2316 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2317 (uint32_t)pmbox->mbxCommand,
2318 pmbox->un.varWords[0],
2319 pmbox->un.varWords[1]);
2320 }
2321 else {
2322 lpfc_debugfs_disc_trc(phba->pport,
2323 LPFC_DISC_TRC_MBOX,
2324 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2325 (uint32_t)pmbox->mbxCommand,
2326 pmbox->un.varWords[0],
2327 pmbox->un.varWords[1]);
2328 }
2329 }
2330
dea31012005-04-17 16:05:31 -05002331 /*
2332 * It is a fatal error if unknown mbox command completion.
2333 */
2334 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2335 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002336 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002337 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002338 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002339 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002340 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002341 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002342 lpfc_sli_config_mbox_subsys_get(phba,
2343 pmb),
2344 lpfc_sli_config_mbox_opcode_get(phba,
2345 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002346 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002347 phba->work_hs = HS_FFER3;
2348 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002349 continue;
dea31012005-04-17 16:05:31 -05002350 }
2351
dea31012005-04-17 16:05:31 -05002352 if (pmbox->mbxStatus) {
2353 phba->sli.slistat.mbox_stat_err++;
2354 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2355 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002356 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002357 LOG_MBOX | LOG_SLI,
2358 "(%d):0305 Mbox cmd cmpl "
2359 "error - RETRYing Data: x%x "
2360 "(x%x/x%x) x%x x%x x%x\n",
2361 pmb->vport ? pmb->vport->vpi : 0,
2362 pmbox->mbxCommand,
2363 lpfc_sli_config_mbox_subsys_get(phba,
2364 pmb),
2365 lpfc_sli_config_mbox_opcode_get(phba,
2366 pmb),
2367 pmbox->mbxStatus,
2368 pmbox->un.varWords[0],
2369 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002370 pmbox->mbxStatus = 0;
2371 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002372 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002373 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002374 continue;
dea31012005-04-17 16:05:31 -05002375 }
2376 }
2377
2378 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002379 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002380 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002381 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2382 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002383 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002384 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002385 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2386 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002387 pmb->mbox_cmpl,
2388 *((uint32_t *) pmbox),
2389 pmbox->un.varWords[0],
2390 pmbox->un.varWords[1],
2391 pmbox->un.varWords[2],
2392 pmbox->un.varWords[3],
2393 pmbox->un.varWords[4],
2394 pmbox->un.varWords[5],
2395 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002396 pmbox->un.varWords[7],
2397 pmbox->un.varWords[8],
2398 pmbox->un.varWords[9],
2399 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002400
James Smart92d7f7b2007-06-17 19:56:38 -05002401 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002402 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002403 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002404 return 0;
dea31012005-04-17 16:05:31 -05002405}
James Smart92d7f7b2007-06-17 19:56:38 -05002406
James Smarte59058c2008-08-24 21:49:00 -04002407/**
James Smart3621a712009-04-06 18:47:14 -04002408 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002409 * @phba: Pointer to HBA context object.
2410 * @pring: Pointer to driver SLI ring object.
2411 * @tag: buffer tag.
2412 *
2413 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2414 * is set in the tag the buffer is posted for a particular exchange,
2415 * the function will return the buffer without replacing the buffer.
2416 * If the buffer is for unsolicited ELS or CT traffic, this function
2417 * returns the buffer and also posts another buffer to the firmware.
2418 **/
James Smart76bb24e2007-10-27 13:38:00 -04002419static struct lpfc_dmabuf *
2420lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002421 struct lpfc_sli_ring *pring,
2422 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002423{
James Smart9f1e1b52008-12-04 22:39:40 -05002424 struct hbq_dmabuf *hbq_entry;
2425
James Smart76bb24e2007-10-27 13:38:00 -04002426 if (tag & QUE_BUFTAG_BIT)
2427 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002428 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2429 if (!hbq_entry)
2430 return NULL;
2431 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002432}
James Smart57127f12007-10-27 13:37:05 -04002433
James Smart3772a992009-05-22 14:50:54 -04002434/**
2435 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2436 * @phba: Pointer to HBA context object.
2437 * @pring: Pointer to driver SLI ring object.
2438 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2439 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2440 * @fch_type: the type for the first frame of the sequence.
2441 *
2442 * This function is called with no lock held. This function uses the r_ctl and
2443 * type of the received sequence to find the correct callback function to call
2444 * to process the sequence.
2445 **/
2446static int
2447lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2448 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2449 uint32_t fch_type)
2450{
2451 int i;
2452
2453 /* unSolicited Responses */
2454 if (pring->prt[0].profile) {
2455 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2456 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2457 saveq);
2458 return 1;
2459 }
2460 /* We must search, based on rctl / type
2461 for the right routine */
2462 for (i = 0; i < pring->num_mask; i++) {
2463 if ((pring->prt[i].rctl == fch_r_ctl) &&
2464 (pring->prt[i].type == fch_type)) {
2465 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2466 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2467 (phba, pring, saveq);
2468 return 1;
2469 }
2470 }
2471 return 0;
2472}
James Smarte59058c2008-08-24 21:49:00 -04002473
2474/**
James Smart3621a712009-04-06 18:47:14 -04002475 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002476 * @phba: Pointer to HBA context object.
2477 * @pring: Pointer to driver SLI ring object.
2478 * @saveq: Pointer to the unsolicited iocb.
2479 *
2480 * This function is called with no lock held by the ring event handler
2481 * when there is an unsolicited iocb posted to the response ring by the
2482 * firmware. This function gets the buffer associated with the iocbs
2483 * and calls the event handler for the ring. This function handles both
2484 * qring buffers and hbq buffers.
2485 * When the function returns 1 the caller can free the iocb object otherwise
2486 * upper layer functions will free the iocb objects.
2487 **/
dea31012005-04-17 16:05:31 -05002488static int
2489lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2490 struct lpfc_iocbq *saveq)
2491{
2492 IOCB_t * irsp;
2493 WORD5 * w5p;
2494 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002495 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002496 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002497
dea31012005-04-17 16:05:31 -05002498 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002499
2500 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2501 if (pring->lpfc_sli_rcv_async_status)
2502 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2503 else
2504 lpfc_printf_log(phba,
2505 KERN_WARNING,
2506 LOG_SLI,
2507 "0316 Ring %d handler: unexpected "
2508 "ASYNC_STATUS iocb received evt_code "
2509 "0x%x\n",
2510 pring->ringno,
2511 irsp->un.asyncstat.evt_code);
2512 return 1;
2513 }
2514
James Smart3163f722008-02-08 18:50:25 -05002515 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2516 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2517 if (irsp->ulpBdeCount > 0) {
2518 dmzbuf = lpfc_sli_get_buff(phba, pring,
2519 irsp->un.ulpWord[3]);
2520 lpfc_in_buf_free(phba, dmzbuf);
2521 }
2522
2523 if (irsp->ulpBdeCount > 1) {
2524 dmzbuf = lpfc_sli_get_buff(phba, pring,
2525 irsp->unsli3.sli3Words[3]);
2526 lpfc_in_buf_free(phba, dmzbuf);
2527 }
2528
2529 if (irsp->ulpBdeCount > 2) {
2530 dmzbuf = lpfc_sli_get_buff(phba, pring,
2531 irsp->unsli3.sli3Words[7]);
2532 lpfc_in_buf_free(phba, dmzbuf);
2533 }
2534
2535 return 1;
2536 }
2537
James Smart92d7f7b2007-06-17 19:56:38 -05002538 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002539 if (irsp->ulpBdeCount != 0) {
2540 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002541 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002542 if (!saveq->context2)
2543 lpfc_printf_log(phba,
2544 KERN_ERR,
2545 LOG_SLI,
2546 "0341 Ring %d Cannot find buffer for "
2547 "an unsolicited iocb. tag 0x%x\n",
2548 pring->ringno,
2549 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002550 }
2551 if (irsp->ulpBdeCount == 2) {
2552 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002553 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002554 if (!saveq->context3)
2555 lpfc_printf_log(phba,
2556 KERN_ERR,
2557 LOG_SLI,
2558 "0342 Ring %d Cannot find buffer for an"
2559 " unsolicited iocb. tag 0x%x\n",
2560 pring->ringno,
2561 irsp->unsli3.sli3Words[7]);
2562 }
2563 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002564 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002565 if (irsp->ulpBdeCount != 0) {
2566 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2567 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002568 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002569 lpfc_printf_log(phba,
2570 KERN_ERR,
2571 LOG_SLI,
2572 "0343 Ring %d Cannot find "
2573 "buffer for an unsolicited iocb"
2574 ". tag 0x%x\n", pring->ringno,
2575 irsp->un.ulpWord[3]);
2576 }
2577 if (irsp->ulpBdeCount == 2) {
2578 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2579 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002580 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002581 lpfc_printf_log(phba,
2582 KERN_ERR,
2583 LOG_SLI,
2584 "0344 Ring %d Cannot find "
2585 "buffer for an unsolicited "
2586 "iocb. tag 0x%x\n",
2587 pring->ringno,
2588 irsp->unsli3.sli3Words[7]);
2589 }
2590 }
James Smart92d7f7b2007-06-17 19:56:38 -05002591 }
James Smart9c2face2008-01-11 01:53:18 -05002592 if (irsp->ulpBdeCount != 0 &&
2593 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2594 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2595 int found = 0;
2596
2597 /* search continue save q for same XRI */
2598 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002599 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2600 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002601 list_add_tail(&saveq->list, &iocbq->list);
2602 found = 1;
2603 break;
2604 }
2605 }
2606 if (!found)
2607 list_add_tail(&saveq->clist,
2608 &pring->iocb_continue_saveq);
2609 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2610 list_del_init(&iocbq->clist);
2611 saveq = iocbq;
2612 irsp = &(saveq->iocb);
2613 } else
2614 return 0;
2615 }
2616 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2617 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2618 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002619 Rctl = FC_RCTL_ELS_REQ;
2620 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002621 } else {
2622 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2623 Rctl = w5p->hcsw.Rctl;
2624 Type = w5p->hcsw.Type;
2625
2626 /* Firmware Workaround */
2627 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2628 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2629 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002630 Rctl = FC_RCTL_ELS_REQ;
2631 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002632 w5p->hcsw.Rctl = Rctl;
2633 w5p->hcsw.Type = Type;
2634 }
2635 }
James Smart92d7f7b2007-06-17 19:56:38 -05002636
James Smart3772a992009-05-22 14:50:54 -04002637 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002638 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002639 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002640 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002641 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002642
James Smart92d7f7b2007-06-17 19:56:38 -05002643 return 1;
dea31012005-04-17 16:05:31 -05002644}
2645
James Smarte59058c2008-08-24 21:49:00 -04002646/**
James Smart3621a712009-04-06 18:47:14 -04002647 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002648 * @phba: Pointer to HBA context object.
2649 * @pring: Pointer to driver SLI ring object.
2650 * @prspiocb: Pointer to response iocb object.
2651 *
2652 * This function looks up the iocb_lookup table to get the command iocb
2653 * corresponding to the given response iocb using the iotag of the
2654 * response iocb. This function is called with the hbalock held.
2655 * This function returns the command iocb object if it finds the command
2656 * iocb else returns NULL.
2657 **/
dea31012005-04-17 16:05:31 -05002658static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002659lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2660 struct lpfc_sli_ring *pring,
2661 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002662{
dea31012005-04-17 16:05:31 -05002663 struct lpfc_iocbq *cmd_iocb = NULL;
2664 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002665 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002666
James Bottomley604a3e32005-10-29 10:28:33 -05002667 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002668
James Bottomley604a3e32005-10-29 10:28:33 -05002669 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2670 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002671 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002672 /* remove from txcmpl queue list */
2673 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002674 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002675 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002676 }
dea31012005-04-17 16:05:31 -05002677 }
2678
dea31012005-04-17 16:05:31 -05002679 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002680 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002681 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002682 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002683 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002684 return NULL;
2685}
2686
James Smarte59058c2008-08-24 21:49:00 -04002687/**
James Smart3772a992009-05-22 14:50:54 -04002688 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2689 * @phba: Pointer to HBA context object.
2690 * @pring: Pointer to driver SLI ring object.
2691 * @iotag: IOCB tag.
2692 *
2693 * This function looks up the iocb_lookup table to get the command iocb
2694 * corresponding to the given iotag. This function is called with the
2695 * hbalock held.
2696 * This function returns the command iocb object if it finds the command
2697 * iocb else returns NULL.
2698 **/
2699static struct lpfc_iocbq *
2700lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2701 struct lpfc_sli_ring *pring, uint16_t iotag)
2702{
James Smart895427b2017-02-12 13:52:30 -08002703 struct lpfc_iocbq *cmd_iocb = NULL;
James Smart3772a992009-05-22 14:50:54 -04002704
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002705 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002706 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2707 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002708 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2709 /* remove from txcmpl queue list */
2710 list_del_init(&cmd_iocb->list);
2711 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002712 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002713 }
James Smart3772a992009-05-22 14:50:54 -04002714 }
James Smart89533e92016-10-13 15:06:15 -07002715
James Smart3772a992009-05-22 14:50:54 -04002716 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08002717 "0372 iotag x%x lookup error: max iotag (x%x) "
2718 "iocb_flag x%x\n",
2719 iotag, phba->sli.last_iotag,
2720 cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
James Smart3772a992009-05-22 14:50:54 -04002721 return NULL;
2722}
2723
2724/**
James Smart3621a712009-04-06 18:47:14 -04002725 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002726 * @phba: Pointer to HBA context object.
2727 * @pring: Pointer to driver SLI ring object.
2728 * @saveq: Pointer to the response iocb to be processed.
2729 *
2730 * This function is called by the ring event handler for non-fcp
2731 * rings when there is a new response iocb in the response ring.
2732 * The caller is not required to hold any locks. This function
2733 * gets the command iocb associated with the response iocb and
2734 * calls the completion handler for the command iocb. If there
2735 * is no completion handler, the function will free the resources
2736 * associated with command iocb. If the response iocb is for
2737 * an already aborted command iocb, the status of the completion
2738 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2739 * This function always returns 1.
2740 **/
dea31012005-04-17 16:05:31 -05002741static int
James Smart2e0fef82007-06-17 19:56:36 -05002742lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002743 struct lpfc_iocbq *saveq)
2744{
James Smart2e0fef82007-06-17 19:56:36 -05002745 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002746 int rc = 1;
2747 unsigned long iflag;
2748
2749 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002750 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002751 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002752 spin_unlock_irqrestore(&phba->hbalock, iflag);
2753
dea31012005-04-17 16:05:31 -05002754 if (cmdiocbp) {
2755 if (cmdiocbp->iocb_cmpl) {
2756 /*
James Smartea2151b2008-09-07 11:52:10 -04002757 * If an ELS command failed send an event to mgmt
2758 * application.
2759 */
2760 if (saveq->iocb.ulpStatus &&
2761 (pring->ringno == LPFC_ELS_RING) &&
2762 (cmdiocbp->iocb.ulpCommand ==
2763 CMD_ELS_REQUEST64_CR))
2764 lpfc_send_els_failure_event(phba,
2765 cmdiocbp, saveq);
2766
2767 /*
dea31012005-04-17 16:05:31 -05002768 * Post all ELS completions to the worker thread.
2769 * All other are passed to the completion callback.
2770 */
2771 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002772 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2773 (cmdiocbp->iocb_flag &
2774 LPFC_DRIVER_ABORTED)) {
2775 spin_lock_irqsave(&phba->hbalock,
2776 iflag);
James Smart07951072007-04-25 09:51:38 -04002777 cmdiocbp->iocb_flag &=
2778 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002779 spin_unlock_irqrestore(&phba->hbalock,
2780 iflag);
James Smart07951072007-04-25 09:51:38 -04002781 saveq->iocb.ulpStatus =
2782 IOSTAT_LOCAL_REJECT;
2783 saveq->iocb.un.ulpWord[4] =
2784 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002785
2786 /* Firmware could still be in progress
2787 * of DMAing payload, so don't free data
2788 * buffer till after a hbeat.
2789 */
James Smart341af102010-01-26 23:07:37 -05002790 spin_lock_irqsave(&phba->hbalock,
2791 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002792 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002793 spin_unlock_irqrestore(&phba->hbalock,
2794 iflag);
2795 }
James Smart0f65ff62010-02-26 14:14:23 -05002796 if (phba->sli_rev == LPFC_SLI_REV4) {
2797 if (saveq->iocb_flag &
2798 LPFC_EXCHANGE_BUSY) {
2799 /* Set cmdiocb flag for the
2800 * exchange busy so sgl (xri)
2801 * will not be released until
2802 * the abort xri is received
2803 * from hba.
2804 */
2805 spin_lock_irqsave(
2806 &phba->hbalock, iflag);
2807 cmdiocbp->iocb_flag |=
2808 LPFC_EXCHANGE_BUSY;
2809 spin_unlock_irqrestore(
2810 &phba->hbalock, iflag);
2811 }
2812 if (cmdiocbp->iocb_flag &
2813 LPFC_DRIVER_ABORTED) {
2814 /*
2815 * Clear LPFC_DRIVER_ABORTED
2816 * bit in case it was driver
2817 * initiated abort.
2818 */
2819 spin_lock_irqsave(
2820 &phba->hbalock, iflag);
2821 cmdiocbp->iocb_flag &=
2822 ~LPFC_DRIVER_ABORTED;
2823 spin_unlock_irqrestore(
2824 &phba->hbalock, iflag);
2825 cmdiocbp->iocb.ulpStatus =
2826 IOSTAT_LOCAL_REJECT;
2827 cmdiocbp->iocb.un.ulpWord[4] =
2828 IOERR_ABORT_REQUESTED;
2829 /*
2830 * For SLI4, irsiocb contains
2831 * NO_XRI in sli_xritag, it
2832 * shall not affect releasing
2833 * sgl (xri) process.
2834 */
2835 saveq->iocb.ulpStatus =
2836 IOSTAT_LOCAL_REJECT;
2837 saveq->iocb.un.ulpWord[4] =
2838 IOERR_SLI_ABORTED;
2839 spin_lock_irqsave(
2840 &phba->hbalock, iflag);
2841 saveq->iocb_flag |=
2842 LPFC_DELAY_MEM_FREE;
2843 spin_unlock_irqrestore(
2844 &phba->hbalock, iflag);
2845 }
James Smart07951072007-04-25 09:51:38 -04002846 }
dea31012005-04-17 16:05:31 -05002847 }
James Smart2e0fef82007-06-17 19:56:36 -05002848 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002849 } else
2850 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002851 } else {
2852 /*
2853 * Unknown initiating command based on the response iotag.
2854 * This could be the case on the ELS ring because of
2855 * lpfc_els_abort().
2856 */
2857 if (pring->ringno != LPFC_ELS_RING) {
2858 /*
2859 * Ring <ringno> handler: unexpected completion IoTag
2860 * <IoTag>
2861 */
James Smarta257bf92009-04-06 18:48:10 -04002862 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002863 "0322 Ring %d handler: "
2864 "unexpected completion IoTag x%x "
2865 "Data: x%x x%x x%x x%x\n",
2866 pring->ringno,
2867 saveq->iocb.ulpIoTag,
2868 saveq->iocb.ulpStatus,
2869 saveq->iocb.un.ulpWord[4],
2870 saveq->iocb.ulpCommand,
2871 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002872 }
2873 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002874
dea31012005-04-17 16:05:31 -05002875 return rc;
2876}
2877
James Smarte59058c2008-08-24 21:49:00 -04002878/**
James Smart3621a712009-04-06 18:47:14 -04002879 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002880 * @phba: Pointer to HBA context object.
2881 * @pring: Pointer to driver SLI ring object.
2882 *
2883 * This function is called from the iocb ring event handlers when
2884 * put pointer is ahead of the get pointer for a ring. This function signal
2885 * an error attention condition to the worker thread and the worker
2886 * thread will transition the HBA to offline state.
2887 **/
James Smart2e0fef82007-06-17 19:56:36 -05002888static void
2889lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002890{
James Smart34b02dc2008-08-24 21:49:55 -04002891 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002892 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002893 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002894 * rsp ring <portRspMax>
2895 */
2896 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002897 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002898 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002899 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002900 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002901
James Smart2e0fef82007-06-17 19:56:36 -05002902 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002903
2904 /*
2905 * All error attention handlers are posted to
2906 * worker thread
2907 */
2908 phba->work_ha |= HA_ERATT;
2909 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002910
James Smart5e9d9b82008-06-14 22:52:53 -04002911 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002912
2913 return;
2914}
2915
James Smarte59058c2008-08-24 21:49:00 -04002916/**
James Smart3621a712009-04-06 18:47:14 -04002917 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002918 * @ptr: Pointer to address of HBA context object.
2919 *
2920 * This function is invoked by the Error Attention polling timer when the
2921 * timer times out. It will check the SLI Error Attention register for
2922 * possible attention events. If so, it will post an Error Attention event
2923 * and wake up worker thread to process it. Otherwise, it will set up the
2924 * Error Attention polling timer for the next poll.
2925 **/
2926void lpfc_poll_eratt(unsigned long ptr)
2927{
2928 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04002929 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04002930 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002931
2932 phba = (struct lpfc_hba *)ptr;
2933
James Smartaa6fbb72012-08-03 12:36:03 -04002934 /* Here we will also keep track of interrupts per sec of the hba */
2935 sli_intr = phba->sli.slistat.sli_intr;
2936
2937 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2938 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2939 sli_intr);
2940 else
2941 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2942
James Smart65791f12016-07-06 12:35:56 -07002943 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
2944 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04002945 phba->sli.slistat.sli_ips = cnt;
2946
2947 phba->sli.slistat.sli_prev_intr = sli_intr;
2948
James Smart93996272008-08-24 21:50:30 -04002949 /* Check chip HA register for error event */
2950 eratt = lpfc_sli_check_eratt(phba);
2951
2952 if (eratt)
2953 /* Tell the worker thread there is work to do */
2954 lpfc_worker_wake_up(phba);
2955 else
2956 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04002957 mod_timer(&phba->eratt_poll,
2958 jiffies +
James Smart65791f12016-07-06 12:35:56 -07002959 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04002960 return;
2961}
2962
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002963
James Smarte59058c2008-08-24 21:49:00 -04002964/**
James Smart3621a712009-04-06 18:47:14 -04002965 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002966 * @phba: Pointer to HBA context object.
2967 * @pring: Pointer to driver SLI ring object.
2968 * @mask: Host attention register mask for this ring.
2969 *
2970 * This function is called from the interrupt context when there is a ring
2971 * event for the fcp ring. The caller does not hold any lock.
2972 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002973 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002974 * LE bit set. The function will call the completion handler of the command iocb
2975 * if the response iocb indicates a completion for a command iocb or it is
2976 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2977 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002978 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002979 * to check it explicitly.
2980 */
2981int
James Smart2e0fef82007-06-17 19:56:36 -05002982lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2983 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002984{
James Smart34b02dc2008-08-24 21:49:55 -04002985 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002986 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002987 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002988 struct lpfc_iocbq *cmdiocbq = NULL;
2989 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002990 uint32_t status;
2991 uint32_t portRspPut, portRspMax;
2992 int rc = 1;
2993 lpfc_iocb_type type;
2994 unsigned long iflag;
2995 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002996
James Smart2e0fef82007-06-17 19:56:36 -05002997 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002998 pring->stats.iocb_event++;
2999
dea31012005-04-17 16:05:31 -05003000 /*
3001 * The next available response entry should never exceed the maximum
3002 * entries. If it does, treat it as an adapter hardware error.
3003 */
James Smart7e56aa22012-08-03 12:35:34 -04003004 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003005 portRspPut = le32_to_cpu(pgp->rspPutInx);
3006 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003007 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003008 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003009 return 1;
3010 }
James Smart45ed1192009-10-02 15:17:02 -04003011 if (phba->fcp_ring_in_use) {
3012 spin_unlock_irqrestore(&phba->hbalock, iflag);
3013 return 1;
3014 } else
3015 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003016
3017 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003018 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003019 /*
3020 * Fetch an entry off the ring and copy it into a local data
3021 * structure. The copy involves a byte-swap since the
3022 * network byte order and pci byte orders are different.
3023 */
James Smarted957682007-06-17 19:56:37 -05003024 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003025 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003026
James Smart7e56aa22012-08-03 12:35:34 -04003027 if (++pring->sli.sli3.rspidx >= portRspMax)
3028 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003029
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003030 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3031 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003032 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003033 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003034 irsp = &rspiocbq.iocb;
3035
dea31012005-04-17 16:05:31 -05003036 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3037 pring->stats.iocb_rsp++;
3038 rsp_cmpl++;
3039
3040 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003041 /*
3042 * If resource errors reported from HBA, reduce
3043 * queuedepths of the SCSI device.
3044 */
3045 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003046 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3047 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003048 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003049 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003050 spin_lock_irqsave(&phba->hbalock, iflag);
3051 }
3052
dea31012005-04-17 16:05:31 -05003053 /* Rsp ring <ringno> error: IOCB */
3054 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003055 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003056 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003057 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003058 irsp->un.ulpWord[0],
3059 irsp->un.ulpWord[1],
3060 irsp->un.ulpWord[2],
3061 irsp->un.ulpWord[3],
3062 irsp->un.ulpWord[4],
3063 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003064 *(uint32_t *)&irsp->un1,
3065 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003066 }
3067
3068 switch (type) {
3069 case LPFC_ABORT_IOCB:
3070 case LPFC_SOL_IOCB:
3071 /*
3072 * Idle exchange closed via ABTS from port. No iocb
3073 * resources need to be recovered.
3074 */
3075 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003076 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003077 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003078 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003079 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003080 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003081 break;
3082 }
3083
James Bottomley604a3e32005-10-29 10:28:33 -05003084 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3085 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003086 if (unlikely(!cmdiocbq))
3087 break;
3088 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3089 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3090 if (cmdiocbq->iocb_cmpl) {
3091 spin_unlock_irqrestore(&phba->hbalock, iflag);
3092 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3093 &rspiocbq);
3094 spin_lock_irqsave(&phba->hbalock, iflag);
3095 }
dea31012005-04-17 16:05:31 -05003096 break;
James Smarta4bc3372006-12-02 13:34:16 -05003097 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003098 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003099 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003100 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003101 break;
dea31012005-04-17 16:05:31 -05003102 default:
3103 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3104 char adaptermsg[LPFC_MAX_ADPTMSG];
3105 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3106 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3107 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003108 dev_warn(&((phba->pcidev)->dev),
3109 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003110 phba->brd_no, adaptermsg);
3111 } else {
3112 /* Unknown IOCB command */
3113 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003114 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003115 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003116 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003117 irsp->ulpStatus,
3118 irsp->ulpIoTag,
3119 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003120 }
3121 break;
3122 }
3123
3124 /*
3125 * The response IOCB has been processed. Update the ring
3126 * pointer in SLIM. If the port response put pointer has not
3127 * been updated, sync the pgp->rspPutInx and fetch the new port
3128 * response put pointer.
3129 */
James Smart7e56aa22012-08-03 12:35:34 -04003130 writel(pring->sli.sli3.rspidx,
3131 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003132
James Smart7e56aa22012-08-03 12:35:34 -04003133 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003134 portRspPut = le32_to_cpu(pgp->rspPutInx);
3135 }
3136
3137 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3138 pring->stats.iocb_rsp_full++;
3139 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3140 writel(status, phba->CAregaddr);
3141 readl(phba->CAregaddr);
3142 }
3143 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3144 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3145 pring->stats.iocb_cmd_empty++;
3146
3147 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003148 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003149 lpfc_sli_resume_iocb(phba, pring);
3150
3151 if ((pring->lpfc_sli_cmd_available))
3152 (pring->lpfc_sli_cmd_available) (phba, pring);
3153
3154 }
3155
James Smart45ed1192009-10-02 15:17:02 -04003156 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003157 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003158 return rc;
3159}
3160
James Smarte59058c2008-08-24 21:49:00 -04003161/**
James Smart3772a992009-05-22 14:50:54 -04003162 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3163 * @phba: Pointer to HBA context object.
3164 * @pring: Pointer to driver SLI ring object.
3165 * @rspiocbp: Pointer to driver response IOCB object.
3166 *
3167 * This function is called from the worker thread when there is a slow-path
3168 * response IOCB to process. This function chains all the response iocbs until
3169 * seeing the iocb with the LE bit set. The function will call
3170 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3171 * completion of a command iocb. The function will call the
3172 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3173 * The function frees the resources or calls the completion handler if this
3174 * iocb is an abort completion. The function returns NULL when the response
3175 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3176 * this function shall chain the iocb on to the iocb_continueq and return the
3177 * response iocb passed in.
3178 **/
3179static struct lpfc_iocbq *
3180lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3181 struct lpfc_iocbq *rspiocbp)
3182{
3183 struct lpfc_iocbq *saveq;
3184 struct lpfc_iocbq *cmdiocbp;
3185 struct lpfc_iocbq *next_iocb;
3186 IOCB_t *irsp = NULL;
3187 uint32_t free_saveq;
3188 uint8_t iocb_cmd_type;
3189 lpfc_iocb_type type;
3190 unsigned long iflag;
3191 int rc;
3192
3193 spin_lock_irqsave(&phba->hbalock, iflag);
3194 /* First add the response iocb to the countinueq list */
3195 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3196 pring->iocb_continueq_cnt++;
3197
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003198 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003199 irsp = &rspiocbp->iocb;
3200 if (irsp->ulpLe) {
3201 /*
3202 * By default, the driver expects to free all resources
3203 * associated with this iocb completion.
3204 */
3205 free_saveq = 1;
3206 saveq = list_get_first(&pring->iocb_continueq,
3207 struct lpfc_iocbq, list);
3208 irsp = &(saveq->iocb);
3209 list_del_init(&pring->iocb_continueq);
3210 pring->iocb_continueq_cnt = 0;
3211
3212 pring->stats.iocb_rsp++;
3213
3214 /*
3215 * If resource errors reported from HBA, reduce
3216 * queuedepths of the SCSI device.
3217 */
3218 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003219 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3220 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003221 spin_unlock_irqrestore(&phba->hbalock, iflag);
3222 phba->lpfc_rampdown_queue_depth(phba);
3223 spin_lock_irqsave(&phba->hbalock, iflag);
3224 }
3225
3226 if (irsp->ulpStatus) {
3227 /* Rsp ring <ringno> error: IOCB */
3228 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3229 "0328 Rsp Ring %d error: "
3230 "IOCB Data: "
3231 "x%x x%x x%x x%x "
3232 "x%x x%x x%x x%x "
3233 "x%x x%x x%x x%x "
3234 "x%x x%x x%x x%x\n",
3235 pring->ringno,
3236 irsp->un.ulpWord[0],
3237 irsp->un.ulpWord[1],
3238 irsp->un.ulpWord[2],
3239 irsp->un.ulpWord[3],
3240 irsp->un.ulpWord[4],
3241 irsp->un.ulpWord[5],
3242 *(((uint32_t *) irsp) + 6),
3243 *(((uint32_t *) irsp) + 7),
3244 *(((uint32_t *) irsp) + 8),
3245 *(((uint32_t *) irsp) + 9),
3246 *(((uint32_t *) irsp) + 10),
3247 *(((uint32_t *) irsp) + 11),
3248 *(((uint32_t *) irsp) + 12),
3249 *(((uint32_t *) irsp) + 13),
3250 *(((uint32_t *) irsp) + 14),
3251 *(((uint32_t *) irsp) + 15));
3252 }
3253
3254 /*
3255 * Fetch the IOCB command type and call the correct completion
3256 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3257 * get freed back to the lpfc_iocb_list by the discovery
3258 * kernel thread.
3259 */
3260 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3261 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3262 switch (type) {
3263 case LPFC_SOL_IOCB:
3264 spin_unlock_irqrestore(&phba->hbalock, iflag);
3265 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3266 spin_lock_irqsave(&phba->hbalock, iflag);
3267 break;
3268
3269 case LPFC_UNSOL_IOCB:
3270 spin_unlock_irqrestore(&phba->hbalock, iflag);
3271 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3272 spin_lock_irqsave(&phba->hbalock, iflag);
3273 if (!rc)
3274 free_saveq = 0;
3275 break;
3276
3277 case LPFC_ABORT_IOCB:
3278 cmdiocbp = NULL;
3279 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3280 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3281 saveq);
3282 if (cmdiocbp) {
3283 /* Call the specified completion routine */
3284 if (cmdiocbp->iocb_cmpl) {
3285 spin_unlock_irqrestore(&phba->hbalock,
3286 iflag);
3287 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3288 saveq);
3289 spin_lock_irqsave(&phba->hbalock,
3290 iflag);
3291 } else
3292 __lpfc_sli_release_iocbq(phba,
3293 cmdiocbp);
3294 }
3295 break;
3296
3297 case LPFC_UNKNOWN_IOCB:
3298 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3299 char adaptermsg[LPFC_MAX_ADPTMSG];
3300 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3301 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3302 MAX_MSG_DATA);
3303 dev_warn(&((phba->pcidev)->dev),
3304 "lpfc%d: %s\n",
3305 phba->brd_no, adaptermsg);
3306 } else {
3307 /* Unknown IOCB command */
3308 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3309 "0335 Unknown IOCB "
3310 "command Data: x%x "
3311 "x%x x%x x%x\n",
3312 irsp->ulpCommand,
3313 irsp->ulpStatus,
3314 irsp->ulpIoTag,
3315 irsp->ulpContext);
3316 }
3317 break;
3318 }
3319
3320 if (free_saveq) {
3321 list_for_each_entry_safe(rspiocbp, next_iocb,
3322 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003323 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003324 __lpfc_sli_release_iocbq(phba, rspiocbp);
3325 }
3326 __lpfc_sli_release_iocbq(phba, saveq);
3327 }
3328 rspiocbp = NULL;
3329 }
3330 spin_unlock_irqrestore(&phba->hbalock, iflag);
3331 return rspiocbp;
3332}
3333
3334/**
3335 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003336 * @phba: Pointer to HBA context object.
3337 * @pring: Pointer to driver SLI ring object.
3338 * @mask: Host attention register mask for this ring.
3339 *
James Smart3772a992009-05-22 14:50:54 -04003340 * This routine wraps the actual slow_ring event process routine from the
3341 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003342 **/
James Smart3772a992009-05-22 14:50:54 -04003343void
James Smart2e0fef82007-06-17 19:56:36 -05003344lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3345 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003346{
James Smart3772a992009-05-22 14:50:54 -04003347 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3348}
3349
3350/**
3351 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3352 * @phba: Pointer to HBA context object.
3353 * @pring: Pointer to driver SLI ring object.
3354 * @mask: Host attention register mask for this ring.
3355 *
3356 * This function is called from the worker thread when there is a ring event
3357 * for non-fcp rings. The caller does not hold any lock. The function will
3358 * remove each response iocb in the response ring and calls the handle
3359 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3360 **/
3361static void
3362lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3363 struct lpfc_sli_ring *pring, uint32_t mask)
3364{
James Smart34b02dc2008-08-24 21:49:55 -04003365 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003366 IOCB_t *entry;
3367 IOCB_t *irsp = NULL;
3368 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003369 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003370 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003371 uint32_t status;
dea31012005-04-17 16:05:31 -05003372
James Smart34b02dc2008-08-24 21:49:55 -04003373 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003374 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003375 pring->stats.iocb_event++;
3376
dea31012005-04-17 16:05:31 -05003377 /*
3378 * The next available response entry should never exceed the maximum
3379 * entries. If it does, treat it as an adapter hardware error.
3380 */
James Smart7e56aa22012-08-03 12:35:34 -04003381 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003382 portRspPut = le32_to_cpu(pgp->rspPutInx);
3383 if (portRspPut >= portRspMax) {
3384 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003385 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003386 * rsp ring <portRspMax>
3387 */
James Smarted957682007-06-17 19:56:37 -05003388 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003389 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003390 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003391 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003392
James Smart2e0fef82007-06-17 19:56:36 -05003393 phba->link_state = LPFC_HBA_ERROR;
3394 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003395
3396 phba->work_hs = HS_FFER3;
3397 lpfc_handle_eratt(phba);
3398
James Smart3772a992009-05-22 14:50:54 -04003399 return;
dea31012005-04-17 16:05:31 -05003400 }
3401
3402 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003403 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003404 /*
3405 * Build a completion list and call the appropriate handler.
3406 * The process is to get the next available response iocb, get
3407 * a free iocb from the list, copy the response data into the
3408 * free iocb, insert to the continuation list, and update the
3409 * next response index to slim. This process makes response
3410 * iocb's in the ring available to DMA as fast as possible but
3411 * pays a penalty for a copy operation. Since the iocb is
3412 * only 32 bytes, this penalty is considered small relative to
3413 * the PCI reads for register values and a slim write. When
3414 * the ulpLe field is set, the entire Command has been
3415 * received.
3416 */
James Smarted957682007-06-17 19:56:37 -05003417 entry = lpfc_resp_iocb(phba, pring);
3418
James Smart858c9f62007-06-17 19:56:39 -05003419 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003420 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003421 if (rspiocbp == NULL) {
3422 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003423 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003424 break;
3425 }
3426
James Smarted957682007-06-17 19:56:37 -05003427 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3428 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003429 irsp = &rspiocbp->iocb;
3430
James Smart7e56aa22012-08-03 12:35:34 -04003431 if (++pring->sli.sli3.rspidx >= portRspMax)
3432 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003433
James Smarta58cbd52007-08-02 11:09:43 -04003434 if (pring->ringno == LPFC_ELS_RING) {
3435 lpfc_debugfs_slow_ring_trc(phba,
3436 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3437 *(((uint32_t *) irsp) + 4),
3438 *(((uint32_t *) irsp) + 6),
3439 *(((uint32_t *) irsp) + 7));
3440 }
3441
James Smart7e56aa22012-08-03 12:35:34 -04003442 writel(pring->sli.sli3.rspidx,
3443 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003444
James Smart3772a992009-05-22 14:50:54 -04003445 spin_unlock_irqrestore(&phba->hbalock, iflag);
3446 /* Handle the response IOCB */
3447 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3448 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003449
3450 /*
3451 * If the port response put pointer has not been updated, sync
3452 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3453 * response put pointer.
3454 */
James Smart7e56aa22012-08-03 12:35:34 -04003455 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003456 portRspPut = le32_to_cpu(pgp->rspPutInx);
3457 }
James Smart7e56aa22012-08-03 12:35:34 -04003458 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003459
James Smart92d7f7b2007-06-17 19:56:38 -05003460 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003461 /* At least one response entry has been freed */
3462 pring->stats.iocb_rsp_full++;
3463 /* SET RxRE_RSP in Chip Att register */
3464 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3465 writel(status, phba->CAregaddr);
3466 readl(phba->CAregaddr); /* flush */
3467 }
3468 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3469 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3470 pring->stats.iocb_cmd_empty++;
3471
3472 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003473 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003474 lpfc_sli_resume_iocb(phba, pring);
3475
3476 if ((pring->lpfc_sli_cmd_available))
3477 (pring->lpfc_sli_cmd_available) (phba, pring);
3478
3479 }
3480
James Smart2e0fef82007-06-17 19:56:36 -05003481 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003482 return;
dea31012005-04-17 16:05:31 -05003483}
3484
James Smarte59058c2008-08-24 21:49:00 -04003485/**
James Smart4f774512009-05-22 14:52:35 -04003486 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3487 * @phba: Pointer to HBA context object.
3488 * @pring: Pointer to driver SLI ring object.
3489 * @mask: Host attention register mask for this ring.
3490 *
3491 * This function is called from the worker thread when there is a pending
3492 * ELS response iocb on the driver internal slow-path response iocb worker
3493 * queue. The caller does not hold any lock. The function will remove each
3494 * response iocb from the response worker queue and calls the handle
3495 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3496 **/
3497static void
3498lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3499 struct lpfc_sli_ring *pring, uint32_t mask)
3500{
3501 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003502 struct hbq_dmabuf *dmabuf;
3503 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003504 unsigned long iflag;
3505
James Smart45ed1192009-10-02 15:17:02 -04003506 spin_lock_irqsave(&phba->hbalock, iflag);
3507 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3508 spin_unlock_irqrestore(&phba->hbalock, iflag);
3509 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003510 /* Get the response iocb from the head of work queue */
3511 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003512 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003513 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003514 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003515
3516 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3517 case CQE_CODE_COMPL_WQE:
3518 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3519 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003520 /* Translate ELS WCQE to response IOCBQ */
3521 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3522 irspiocbq);
3523 if (irspiocbq)
3524 lpfc_sli_sp_handle_rspiocb(phba, pring,
3525 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003526 break;
3527 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003528 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003529 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3530 cq_event);
3531 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3532 break;
3533 default:
3534 break;
3535 }
James Smart4f774512009-05-22 14:52:35 -04003536 }
3537}
3538
3539/**
James Smart3621a712009-04-06 18:47:14 -04003540 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003541 * @phba: Pointer to HBA context object.
3542 * @pring: Pointer to driver SLI ring object.
3543 *
3544 * This function aborts all iocbs in the given ring and frees all the iocb
3545 * objects in txq. This function issues an abort iocb for all the iocb commands
3546 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3547 * the return of this function. The caller is not required to hold any locks.
3548 **/
James Smart2e0fef82007-06-17 19:56:36 -05003549void
dea31012005-04-17 16:05:31 -05003550lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3551{
James Smart2534ba72007-04-25 09:52:20 -04003552 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003553 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003554
James Smart92d7f7b2007-06-17 19:56:38 -05003555 if (pring->ringno == LPFC_ELS_RING) {
3556 lpfc_fabric_abort_hba(phba);
3557 }
3558
dea31012005-04-17 16:05:31 -05003559 /* Error everything on txq and txcmplq
3560 * First do the txq.
3561 */
James Smartdb55fba2014-04-04 13:52:02 -04003562 if (phba->sli_rev >= LPFC_SLI_REV4) {
3563 spin_lock_irq(&pring->ring_lock);
3564 list_splice_init(&pring->txq, &completions);
3565 pring->txq_cnt = 0;
3566 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003567
James Smartdb55fba2014-04-04 13:52:02 -04003568 spin_lock_irq(&phba->hbalock);
3569 /* Next issue ABTS for everything on the txcmplq */
3570 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3571 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3572 spin_unlock_irq(&phba->hbalock);
3573 } else {
3574 spin_lock_irq(&phba->hbalock);
3575 list_splice_init(&pring->txq, &completions);
3576 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003577
James Smartdb55fba2014-04-04 13:52:02 -04003578 /* Next issue ABTS for everything on the txcmplq */
3579 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3580 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3581 spin_unlock_irq(&phba->hbalock);
3582 }
James Smart2534ba72007-04-25 09:52:20 -04003583
James Smarta257bf92009-04-06 18:48:10 -04003584 /* Cancel all the IOCBs from the completions list */
3585 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3586 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003587}
3588
James Smarte59058c2008-08-24 21:49:00 -04003589/**
James Smart895427b2017-02-12 13:52:30 -08003590 * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
3591 * @phba: Pointer to HBA context object.
3592 * @pring: Pointer to driver SLI ring object.
3593 *
3594 * This function aborts all iocbs in the given ring and frees all the iocb
3595 * objects in txq. This function issues an abort iocb for all the iocb commands
3596 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3597 * the return of this function. The caller is not required to hold any locks.
3598 **/
3599void
3600lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3601{
3602 LIST_HEAD(completions);
3603 struct lpfc_iocbq *iocb, *next_iocb;
3604
3605 if (pring->ringno == LPFC_ELS_RING)
3606 lpfc_fabric_abort_hba(phba);
3607
3608 spin_lock_irq(&phba->hbalock);
3609 /* Next issue ABTS for everything on the txcmplq */
3610 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3611 lpfc_sli4_abort_nvme_io(phba, pring, iocb);
3612 spin_unlock_irq(&phba->hbalock);
3613}
3614
3615
3616/**
James Smartdb55fba2014-04-04 13:52:02 -04003617 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3618 * @phba: Pointer to HBA context object.
3619 * @pring: Pointer to driver SLI ring object.
3620 *
3621 * This function aborts all iocbs in FCP rings and frees all the iocb
3622 * objects in txq. This function issues an abort iocb for all the iocb commands
3623 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3624 * the return of this function. The caller is not required to hold any locks.
3625 **/
3626void
3627lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3628{
3629 struct lpfc_sli *psli = &phba->sli;
3630 struct lpfc_sli_ring *pring;
3631 uint32_t i;
3632
3633 /* Look on all the FCP Rings for the iotag */
3634 if (phba->sli_rev >= LPFC_SLI_REV4) {
3635 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003636 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smartdb55fba2014-04-04 13:52:02 -04003637 lpfc_sli_abort_iocb_ring(phba, pring);
3638 }
3639 } else {
James Smart895427b2017-02-12 13:52:30 -08003640 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003641 lpfc_sli_abort_iocb_ring(phba, pring);
3642 }
3643}
3644
James Smart895427b2017-02-12 13:52:30 -08003645/**
3646 * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
3647 * @phba: Pointer to HBA context object.
3648 *
3649 * This function aborts all wqes in NVME rings. This function issues an
3650 * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
3651 * the txcmplq is not guaranteed to complete before the return of this
3652 * function. The caller is not required to hold any locks.
3653 **/
3654void
3655lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
3656{
3657 struct lpfc_sli_ring *pring;
3658 uint32_t i;
3659
3660 if (phba->sli_rev < LPFC_SLI_REV4)
3661 return;
3662
3663 /* Abort all IO on each NVME ring. */
3664 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3665 pring = phba->sli4_hba.nvme_wq[i]->pring;
3666 lpfc_sli_abort_wqe_ring(phba, pring);
3667 }
3668}
3669
James Smartdb55fba2014-04-04 13:52:02 -04003670
3671/**
James Smart3621a712009-04-06 18:47:14 -04003672 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003673 * @phba: Pointer to HBA context object.
3674 *
3675 * This function flushes all iocbs in the fcp ring and frees all the iocb
3676 * objects in txq and txcmplq. This function will not issue abort iocbs
3677 * for all the iocb commands in txcmplq, they will just be returned with
3678 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3679 * slot has been permanently disabled.
3680 **/
3681void
3682lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3683{
3684 LIST_HEAD(txq);
3685 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003686 struct lpfc_sli *psli = &phba->sli;
3687 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003688 uint32_t i;
James Smarta8e497d2008-08-24 21:50:11 -04003689
3690 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003691 /* Indicate the I/O queues are flushed */
3692 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003693 spin_unlock_irq(&phba->hbalock);
3694
James Smartdb55fba2014-04-04 13:52:02 -04003695 /* Look on all the FCP Rings for the iotag */
3696 if (phba->sli_rev >= LPFC_SLI_REV4) {
3697 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
James Smart895427b2017-02-12 13:52:30 -08003698 pring = phba->sli4_hba.fcp_wq[i]->pring;
James Smarta8e497d2008-08-24 21:50:11 -04003699
James Smartdb55fba2014-04-04 13:52:02 -04003700 spin_lock_irq(&pring->ring_lock);
3701 /* Retrieve everything on txq */
3702 list_splice_init(&pring->txq, &txq);
3703 /* Retrieve everything on the txcmplq */
3704 list_splice_init(&pring->txcmplq, &txcmplq);
3705 pring->txq_cnt = 0;
3706 pring->txcmplq_cnt = 0;
3707 spin_unlock_irq(&pring->ring_lock);
3708
3709 /* Flush the txq */
3710 lpfc_sli_cancel_iocbs(phba, &txq,
3711 IOSTAT_LOCAL_REJECT,
3712 IOERR_SLI_DOWN);
3713 /* Flush the txcmpq */
3714 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3715 IOSTAT_LOCAL_REJECT,
3716 IOERR_SLI_DOWN);
3717 }
3718 } else {
James Smart895427b2017-02-12 13:52:30 -08003719 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smartdb55fba2014-04-04 13:52:02 -04003720
3721 spin_lock_irq(&phba->hbalock);
3722 /* Retrieve everything on txq */
3723 list_splice_init(&pring->txq, &txq);
3724 /* Retrieve everything on the txcmplq */
3725 list_splice_init(&pring->txcmplq, &txcmplq);
3726 pring->txq_cnt = 0;
3727 pring->txcmplq_cnt = 0;
3728 spin_unlock_irq(&phba->hbalock);
3729
3730 /* Flush the txq */
3731 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3732 IOERR_SLI_DOWN);
3733 /* Flush the txcmpq */
3734 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3735 IOERR_SLI_DOWN);
3736 }
James Smarta8e497d2008-08-24 21:50:11 -04003737}
3738
3739/**
James Smart895427b2017-02-12 13:52:30 -08003740 * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
3741 * @phba: Pointer to HBA context object.
3742 *
3743 * This function flushes all wqes in the nvme rings and frees all resources
3744 * in the txcmplq. This function does not issue abort wqes for the IO
3745 * commands in txcmplq, they will just be returned with
3746 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3747 * slot has been permanently disabled.
3748 **/
3749void
3750lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
3751{
3752 LIST_HEAD(txcmplq);
3753 struct lpfc_sli_ring *pring;
3754 uint32_t i;
3755
3756 if (phba->sli_rev < LPFC_SLI_REV4)
3757 return;
3758
3759 /* Hint to other driver operations that a flush is in progress. */
3760 spin_lock_irq(&phba->hbalock);
3761 phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
3762 spin_unlock_irq(&phba->hbalock);
3763
3764 /* Cycle through all NVME rings and complete each IO with
3765 * a local driver reason code. This is a flush so no
3766 * abort exchange to FW.
3767 */
3768 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
3769 pring = phba->sli4_hba.nvme_wq[i]->pring;
3770
3771 /* Retrieve everything on the txcmplq */
3772 spin_lock_irq(&pring->ring_lock);
3773 list_splice_init(&pring->txcmplq, &txcmplq);
3774 pring->txcmplq_cnt = 0;
3775 spin_unlock_irq(&pring->ring_lock);
3776
3777 /* Flush the txcmpq &&&PAE */
3778 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3779 IOSTAT_LOCAL_REJECT,
3780 IOERR_SLI_DOWN);
3781 }
3782}
3783
3784/**
James Smart3772a992009-05-22 14:50:54 -04003785 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003786 * @phba: Pointer to HBA context object.
3787 * @mask: Bit mask to be checked.
3788 *
3789 * This function reads the host status register and compares
3790 * with the provided bit mask to check if HBA completed
3791 * the restart. This function will wait in a loop for the
3792 * HBA to complete restart. If the HBA does not restart within
3793 * 15 iterations, the function will reset the HBA again. The
3794 * function returns 1 when HBA fail to restart otherwise returns
3795 * zero.
3796 **/
James Smart3772a992009-05-22 14:50:54 -04003797static int
3798lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003799{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003800 uint32_t status;
3801 int i = 0;
3802 int retval = 0;
dea31012005-04-17 16:05:31 -05003803
Jamie Wellnitz41415862006-02-28 19:25:27 -05003804 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003805 if (lpfc_readl(phba->HSregaddr, &status))
3806 return 1;
dea31012005-04-17 16:05:31 -05003807
Jamie Wellnitz41415862006-02-28 19:25:27 -05003808 /*
3809 * Check status register every 100ms for 5 retries, then every
3810 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3811 * every 2.5 sec for 4.
3812 * Break our of the loop if errors occurred during init.
3813 */
3814 while (((status & mask) != mask) &&
3815 !(status & HS_FFERM) &&
3816 i++ < 20) {
dea31012005-04-17 16:05:31 -05003817
Jamie Wellnitz41415862006-02-28 19:25:27 -05003818 if (i <= 5)
3819 msleep(10);
3820 else if (i <= 10)
3821 msleep(500);
3822 else
3823 msleep(2500);
dea31012005-04-17 16:05:31 -05003824
Jamie Wellnitz41415862006-02-28 19:25:27 -05003825 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003826 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003827 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003828 lpfc_sli_brdrestart(phba);
3829 }
3830 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003831 if (lpfc_readl(phba->HSregaddr, &status)) {
3832 retval = 1;
3833 break;
3834 }
dea31012005-04-17 16:05:31 -05003835 }
dea31012005-04-17 16:05:31 -05003836
Jamie Wellnitz41415862006-02-28 19:25:27 -05003837 /* Check to see if any errors occurred during init */
3838 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3840 "2751 Adapter failed to restart, "
3841 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3842 status,
3843 readl(phba->MBslimaddr + 0xa8),
3844 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003845 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003846 retval = 1;
3847 }
dea31012005-04-17 16:05:31 -05003848
Jamie Wellnitz41415862006-02-28 19:25:27 -05003849 return retval;
dea31012005-04-17 16:05:31 -05003850}
3851
James Smartda0436e2009-05-22 14:51:39 -04003852/**
3853 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3854 * @phba: Pointer to HBA context object.
3855 * @mask: Bit mask to be checked.
3856 *
3857 * This function checks the host status register to check if HBA is
3858 * ready. This function will wait in a loop for the HBA to be ready
3859 * If the HBA is not ready , the function will will reset the HBA PCI
3860 * function again. The function returns 1 when HBA fail to be ready
3861 * otherwise returns zero.
3862 **/
3863static int
3864lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3865{
3866 uint32_t status;
3867 int retval = 0;
3868
3869 /* Read the HBA Host Status Register */
3870 status = lpfc_sli4_post_status_check(phba);
3871
3872 if (status) {
3873 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3874 lpfc_sli_brdrestart(phba);
3875 status = lpfc_sli4_post_status_check(phba);
3876 }
3877
3878 /* Check to see if any errors occurred during init */
3879 if (status) {
3880 phba->link_state = LPFC_HBA_ERROR;
3881 retval = 1;
3882 } else
3883 phba->sli4_hba.intr_enable = 0;
3884
3885 return retval;
3886}
3887
3888/**
3889 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3890 * @phba: Pointer to HBA context object.
3891 * @mask: Bit mask to be checked.
3892 *
3893 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3894 * from the API jump table function pointer from the lpfc_hba struct.
3895 **/
3896int
3897lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3898{
3899 return phba->lpfc_sli_brdready(phba, mask);
3900}
3901
James Smart92908312006-03-07 15:04:13 -05003902#define BARRIER_TEST_PATTERN (0xdeadbeef)
3903
James Smarte59058c2008-08-24 21:49:00 -04003904/**
James Smart3621a712009-04-06 18:47:14 -04003905 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003906 * @phba: Pointer to HBA context object.
3907 *
James Smart1b511972011-12-13 13:23:09 -05003908 * This function is called before resetting an HBA. This function is called
3909 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003910 **/
James Smart2e0fef82007-06-17 19:56:36 -05003911void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003912{
James Smart65a29c12006-07-06 15:50:50 -04003913 uint32_t __iomem *resp_buf;
3914 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003915 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003916 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003917 int i;
3918 uint8_t hdrtype;
3919
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003920 lockdep_assert_held(&phba->hbalock);
3921
James Smart92908312006-03-07 15:04:13 -05003922 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3923 if (hdrtype != 0x80 ||
3924 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3925 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3926 return;
3927
3928 /*
3929 * Tell the other part of the chip to suspend temporarily all
3930 * its DMA activity.
3931 */
James Smart65a29c12006-07-06 15:50:50 -04003932 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003933
3934 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003935 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3936 return;
James Smart92908312006-03-07 15:04:13 -05003937 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3938 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003939 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003940
James Smart9940b972011-03-11 16:06:12 -05003941 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3942 return;
3943 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003944 /* Clear Chip error bit */
3945 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003946 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003947 }
3948
3949 mbox = 0;
3950 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3951 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3952
3953 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003954 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003955 writel(mbox, mbox_buf);
3956
James Smart9940b972011-03-11 16:06:12 -05003957 for (i = 0; i < 50; i++) {
3958 if (lpfc_readl((resp_buf + 1), &resp_data))
3959 return;
3960 if (resp_data != ~(BARRIER_TEST_PATTERN))
3961 mdelay(1);
3962 else
3963 break;
3964 }
3965 resp_data = 0;
3966 if (lpfc_readl((resp_buf + 1), &resp_data))
3967 return;
3968 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003969 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003970 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003971 goto restore_hc;
3972 else
3973 goto clear_errat;
3974 }
3975
3976 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003977 resp_data = 0;
3978 for (i = 0; i < 500; i++) {
3979 if (lpfc_readl(resp_buf, &resp_data))
3980 return;
3981 if (resp_data != mbox)
3982 mdelay(1);
3983 else
3984 break;
3985 }
James Smart92908312006-03-07 15:04:13 -05003986
3987clear_errat:
3988
James Smart9940b972011-03-11 16:06:12 -05003989 while (++i < 500) {
3990 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3991 return;
3992 if (!(ha_copy & HA_ERATT))
3993 mdelay(1);
3994 else
3995 break;
3996 }
James Smart92908312006-03-07 15:04:13 -05003997
3998 if (readl(phba->HAregaddr) & HA_ERATT) {
3999 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004000 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004001 }
4002
4003restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05004004 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05004005 writel(hc_copy, phba->HCregaddr);
4006 readl(phba->HCregaddr); /* flush */
4007}
4008
James Smarte59058c2008-08-24 21:49:00 -04004009/**
James Smart3621a712009-04-06 18:47:14 -04004010 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004011 * @phba: Pointer to HBA context object.
4012 *
4013 * This function issues a kill_board mailbox command and waits for
4014 * the error attention interrupt. This function is called for stopping
4015 * the firmware processing. The caller is not required to hold any
4016 * locks. This function calls lpfc_hba_down_post function to free
4017 * any pending commands after the kill. The function will return 1 when it
4018 * fails to kill the board else will return 0.
4019 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004020int
James Smart2e0fef82007-06-17 19:56:36 -05004021lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05004022{
Jamie Wellnitz41415862006-02-28 19:25:27 -05004023 struct lpfc_sli *psli;
4024 LPFC_MBOXQ_t *pmb;
4025 uint32_t status;
4026 uint32_t ha_copy;
4027 int retval;
4028 int i = 0;
4029
4030 psli = &phba->sli;
4031
4032 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05004033 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004034 "0329 Kill HBA Data: x%x x%x\n",
4035 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004036
James Smart98c9ea52007-10-27 13:37:33 -04004037 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4038 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004039 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004040
4041 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05004042 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05004043 if (lpfc_readl(phba->HCregaddr, &status)) {
4044 spin_unlock_irq(&phba->hbalock);
4045 mempool_free(pmb, phba->mbox_mem_pool);
4046 return 1;
4047 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05004048 status &= ~HC_ERINT_ENA;
4049 writel(status, phba->HCregaddr);
4050 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05004051 phba->link_flag |= LS_IGNORE_ERATT;
4052 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004053
4054 lpfc_kill_board(phba, pmb);
4055 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4056 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4057
4058 if (retval != MBX_SUCCESS) {
4059 if (retval != MBX_BUSY)
4060 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05004061 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4062 "2752 KILL_BOARD command failed retval %d\n",
4063 retval);
James Smart2e0fef82007-06-17 19:56:36 -05004064 spin_lock_irq(&phba->hbalock);
4065 phba->link_flag &= ~LS_IGNORE_ERATT;
4066 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004067 return 1;
4068 }
4069
James Smartf4b4c682009-05-22 14:53:12 -04004070 spin_lock_irq(&phba->hbalock);
4071 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
4072 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05004073
Jamie Wellnitz41415862006-02-28 19:25:27 -05004074 mempool_free(pmb, phba->mbox_mem_pool);
4075
4076 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
4077 * attention every 100ms for 3 seconds. If we don't get ERATT after
4078 * 3 seconds we still set HBA_ERROR state because the status of the
4079 * board is now undefined.
4080 */
James Smart9940b972011-03-11 16:06:12 -05004081 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4082 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004083 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
4084 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05004085 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4086 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004087 }
4088
4089 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004090 if (ha_copy & HA_ERATT) {
4091 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004092 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004093 }
James Smart2e0fef82007-06-17 19:56:36 -05004094 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004095 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004096 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004097 phba->link_flag &= ~LS_IGNORE_ERATT;
4098 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004099
Jamie Wellnitz41415862006-02-28 19:25:27 -05004100 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004101 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004102
James Smart2e0fef82007-06-17 19:56:36 -05004103 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004104}
4105
James Smarte59058c2008-08-24 21:49:00 -04004106/**
James Smart3772a992009-05-22 14:50:54 -04004107 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004108 * @phba: Pointer to HBA context object.
4109 *
4110 * This function resets the HBA by writing HC_INITFF to the control
4111 * register. After the HBA resets, this function resets all the iocb ring
4112 * indices. This function disables PCI layer parity checking during
4113 * the reset.
4114 * This function returns 0 always.
4115 * The caller is not required to hold any locks.
4116 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004117int
James Smart2e0fef82007-06-17 19:56:36 -05004118lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004119{
4120 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004121 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004122 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004123 int i;
dea31012005-04-17 16:05:31 -05004124
Jamie Wellnitz41415862006-02-28 19:25:27 -05004125 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004126
Jamie Wellnitz41415862006-02-28 19:25:27 -05004127 /* Reset HBA */
4128 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004129 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004130 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05004131
4132 /* perform board reset */
4133 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004134 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004135 phba->pport->fc_myDID = 0;
4136 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05004137
Jamie Wellnitz41415862006-02-28 19:25:27 -05004138 /* Turn off parity checking and serr during the physical reset */
4139 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4140 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4141 (cfg_value &
4142 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4143
James Smart3772a992009-05-22 14:50:54 -04004144 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4145
Jamie Wellnitz41415862006-02-28 19:25:27 -05004146 /* Now toggle INITFF bit in the Host Control Register */
4147 writel(HC_INITFF, phba->HCregaddr);
4148 mdelay(1);
4149 readl(phba->HCregaddr); /* flush */
4150 writel(0, phba->HCregaddr);
4151 readl(phba->HCregaddr); /* flush */
4152
4153 /* Restore PCI cmd register */
4154 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004155
4156 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004157 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08004158 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05004159 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004160 pring->sli.sli3.rspidx = 0;
4161 pring->sli.sli3.next_cmdidx = 0;
4162 pring->sli.sli3.local_getidx = 0;
4163 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004164 pring->missbufcnt = 0;
4165 }
dea31012005-04-17 16:05:31 -05004166
James Smart2e0fef82007-06-17 19:56:36 -05004167 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004168 return 0;
4169}
4170
James Smarte59058c2008-08-24 21:49:00 -04004171/**
James Smartda0436e2009-05-22 14:51:39 -04004172 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4173 * @phba: Pointer to HBA context object.
4174 *
4175 * This function resets a SLI4 HBA. This function disables PCI layer parity
4176 * checking during resets the device. The caller is not required to hold
4177 * any locks.
4178 *
4179 * This function returns 0 always.
4180 **/
4181int
4182lpfc_sli4_brdreset(struct lpfc_hba *phba)
4183{
4184 struct lpfc_sli *psli = &phba->sli;
4185 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004186 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004187
4188 /* Reset HBA */
4189 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004190 "0295 Reset HBA Data: x%x x%x x%x\n",
4191 phba->pport->port_state, psli->sli_flag,
4192 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004193
4194 /* perform board reset */
4195 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004196 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004197 phba->pport->fc_myDID = 0;
4198 phba->pport->fc_prevDID = 0;
4199
James Smartda0436e2009-05-22 14:51:39 -04004200 spin_lock_irq(&phba->hbalock);
4201 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4202 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004203 spin_unlock_irq(&phba->hbalock);
4204
James Smart02936352014-04-04 13:52:12 -04004205 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4206 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4207 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4208 return rc;
4209 }
4210
James Smartda0436e2009-05-22 14:51:39 -04004211 /* Now physically reset the device */
4212 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4213 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004214
4215 /* Turn off parity checking and serr during the physical reset */
4216 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4217 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4218 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4219
James Smart88318812012-09-29 11:29:29 -04004220 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004221 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04004222 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004223
James Smartbe858b62010-12-15 17:57:20 -05004224 /* Restore PCI cmd register */
4225 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4226
James Smart27b01b82012-05-09 21:19:44 -04004227 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004228}
4229
4230/**
4231 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004232 * @phba: Pointer to HBA context object.
4233 *
4234 * This function is called in the SLI initialization code path to
4235 * restart the HBA. The caller is not required to hold any lock.
4236 * This function writes MBX_RESTART mailbox command to the SLIM and
4237 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4238 * function to free any pending commands. The function enables
4239 * POST only during the first initialization. The function returns zero.
4240 * The function does not guarantee completion of MBX_RESTART mailbox
4241 * command before the return of this function.
4242 **/
James Smartda0436e2009-05-22 14:51:39 -04004243static int
4244lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004245{
4246 MAILBOX_t *mb;
4247 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004248 volatile uint32_t word0;
4249 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004250 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004251
James Smart2e0fef82007-06-17 19:56:36 -05004252 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004253
James Smart0d878412009-10-02 15:16:56 -04004254 /* Take PCIe device Advanced Error Reporting (AER) state */
4255 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4256
Jamie Wellnitz41415862006-02-28 19:25:27 -05004257 psli = &phba->sli;
4258
4259 /* Restart HBA */
4260 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004261 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004262 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004263
4264 word0 = 0;
4265 mb = (MAILBOX_t *) &word0;
4266 mb->mbxCommand = MBX_RESTART;
4267 mb->mbxHc = 1;
4268
James Smart92908312006-03-07 15:04:13 -05004269 lpfc_reset_barrier(phba);
4270
Jamie Wellnitz41415862006-02-28 19:25:27 -05004271 to_slim = phba->MBslimaddr;
4272 writel(*(uint32_t *) mb, to_slim);
4273 readl(to_slim); /* flush */
4274
4275 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004276 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004277 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004278 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004279 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004280 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004281 writel(*(uint32_t *) mb, to_slim);
4282 readl(to_slim); /* flush */
4283
4284 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004285 phba->pport->stopped = 0;
4286 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004287 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004288 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004289
James Smart64ba8812006-08-02 15:24:34 -04004290 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4291 psli->stats_start = get_seconds();
4292
James Smarteaf15d52008-12-04 22:39:29 -05004293 /* Give the INITFF and Post time to settle. */
4294 mdelay(100);
dea31012005-04-17 16:05:31 -05004295
James Smart0d878412009-10-02 15:16:56 -04004296 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4297 if (hba_aer_enabled)
4298 pci_disable_pcie_error_reporting(phba->pcidev);
4299
Jamie Wellnitz41415862006-02-28 19:25:27 -05004300 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004301
4302 return 0;
4303}
4304
James Smarte59058c2008-08-24 21:49:00 -04004305/**
James Smartda0436e2009-05-22 14:51:39 -04004306 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4307 * @phba: Pointer to HBA context object.
4308 *
4309 * This function is called in the SLI initialization code path to restart
4310 * a SLI4 HBA. The caller is not required to hold any lock.
4311 * At the end of the function, it calls lpfc_hba_down_post function to
4312 * free any pending commands.
4313 **/
4314static int
4315lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4316{
4317 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004318 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004319 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004320
4321 /* Restart HBA */
4322 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4323 "0296 Restart HBA Data: x%x x%x\n",
4324 phba->pport->port_state, psli->sli_flag);
4325
James Smart75baf692010-06-08 18:31:21 -04004326 /* Take PCIe device Advanced Error Reporting (AER) state */
4327 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4328
James Smart27b01b82012-05-09 21:19:44 -04004329 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004330
4331 spin_lock_irq(&phba->hbalock);
4332 phba->pport->stopped = 0;
4333 phba->link_state = LPFC_INIT_START;
4334 phba->hba_flag = 0;
4335 spin_unlock_irq(&phba->hbalock);
4336
4337 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4338 psli->stats_start = get_seconds();
4339
James Smart75baf692010-06-08 18:31:21 -04004340 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4341 if (hba_aer_enabled)
4342 pci_disable_pcie_error_reporting(phba->pcidev);
4343
James Smartda0436e2009-05-22 14:51:39 -04004344 lpfc_hba_down_post(phba);
4345
James Smart27b01b82012-05-09 21:19:44 -04004346 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004347}
4348
4349/**
4350 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4351 * @phba: Pointer to HBA context object.
4352 *
4353 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4354 * API jump table function pointer from the lpfc_hba struct.
4355**/
4356int
4357lpfc_sli_brdrestart(struct lpfc_hba *phba)
4358{
4359 return phba->lpfc_sli_brdrestart(phba);
4360}
4361
4362/**
James Smart3621a712009-04-06 18:47:14 -04004363 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004364 * @phba: Pointer to HBA context object.
4365 *
4366 * This function is called after a HBA restart to wait for successful
4367 * restart of the HBA. Successful restart of the HBA is indicated by
4368 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4369 * iteration, the function will restart the HBA again. The function returns
4370 * zero if HBA successfully restarted else returns negative error code.
4371 **/
dea31012005-04-17 16:05:31 -05004372static int
4373lpfc_sli_chipset_init(struct lpfc_hba *phba)
4374{
4375 uint32_t status, i = 0;
4376
4377 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004378 if (lpfc_readl(phba->HSregaddr, &status))
4379 return -EIO;
dea31012005-04-17 16:05:31 -05004380
4381 /* Check status register to see what current state is */
4382 i = 0;
4383 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4384
James Smartdcf2a4e2010-09-29 11:18:53 -04004385 /* Check every 10ms for 10 retries, then every 100ms for 90
4386 * retries, then every 1 sec for 50 retires for a total of
4387 * ~60 seconds before reset the board again and check every
4388 * 1 sec for 50 retries. The up to 60 seconds before the
4389 * board ready is required by the Falcon FIPS zeroization
4390 * complete, and any reset the board in between shall cause
4391 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004392 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004393 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004394 /* Adapter failed to init, timeout, status reg
4395 <status> */
James Smarted957682007-06-17 19:56:37 -05004396 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004397 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004398 "timeout, status reg x%x, "
4399 "FW Data: A8 x%x AC x%x\n", status,
4400 readl(phba->MBslimaddr + 0xa8),
4401 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004402 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004403 return -ETIMEDOUT;
4404 }
4405
4406 /* Check to see if any errors occurred during init */
4407 if (status & HS_FFERM) {
4408 /* ERROR: During chipset initialization */
4409 /* Adapter failed to init, chipset, status reg
4410 <status> */
James Smarted957682007-06-17 19:56:37 -05004411 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004412 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004413 "chipset, status reg x%x, "
4414 "FW Data: A8 x%x AC x%x\n", status,
4415 readl(phba->MBslimaddr + 0xa8),
4416 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004417 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004418 return -EIO;
4419 }
4420
James Smartdcf2a4e2010-09-29 11:18:53 -04004421 if (i <= 10)
dea31012005-04-17 16:05:31 -05004422 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004423 else if (i <= 100)
4424 msleep(100);
4425 else
4426 msleep(1000);
dea31012005-04-17 16:05:31 -05004427
James Smartdcf2a4e2010-09-29 11:18:53 -04004428 if (i == 150) {
4429 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004430 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004431 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004432 }
4433 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004434 if (lpfc_readl(phba->HSregaddr, &status))
4435 return -EIO;
dea31012005-04-17 16:05:31 -05004436 }
4437
4438 /* Check to see if any errors occurred during init */
4439 if (status & HS_FFERM) {
4440 /* ERROR: During chipset initialization */
4441 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004442 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004443 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004444 "status reg x%x, "
4445 "FW Data: A8 x%x AC x%x\n", status,
4446 readl(phba->MBslimaddr + 0xa8),
4447 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004448 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004449 return -EIO;
4450 }
4451
4452 /* Clear all interrupt enable conditions */
4453 writel(0, phba->HCregaddr);
4454 readl(phba->HCregaddr); /* flush */
4455
4456 /* setup host attn register */
4457 writel(0xffffffff, phba->HAregaddr);
4458 readl(phba->HAregaddr); /* flush */
4459 return 0;
4460}
4461
James Smarte59058c2008-08-24 21:49:00 -04004462/**
James Smart3621a712009-04-06 18:47:14 -04004463 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004464 *
4465 * This function calculates and returns the number of HBQs required to be
4466 * configured.
4467 **/
James Smart78b2d852007-08-02 11:10:21 -04004468int
James Smarted957682007-06-17 19:56:37 -05004469lpfc_sli_hbq_count(void)
4470{
James Smart92d7f7b2007-06-17 19:56:38 -05004471 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004472}
4473
James Smarte59058c2008-08-24 21:49:00 -04004474/**
James Smart3621a712009-04-06 18:47:14 -04004475 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004476 *
4477 * This function adds the number of hbq entries in every HBQ to get
4478 * the total number of hbq entries required for the HBA and returns
4479 * the total count.
4480 **/
James Smarted957682007-06-17 19:56:37 -05004481static int
4482lpfc_sli_hbq_entry_count(void)
4483{
4484 int hbq_count = lpfc_sli_hbq_count();
4485 int count = 0;
4486 int i;
4487
4488 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004489 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004490 return count;
4491}
4492
James Smarte59058c2008-08-24 21:49:00 -04004493/**
James Smart3621a712009-04-06 18:47:14 -04004494 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004495 *
4496 * This function calculates amount of memory required for all hbq entries
4497 * to be configured and returns the total memory required.
4498 **/
dea31012005-04-17 16:05:31 -05004499int
James Smarted957682007-06-17 19:56:37 -05004500lpfc_sli_hbq_size(void)
4501{
4502 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4503}
4504
James Smarte59058c2008-08-24 21:49:00 -04004505/**
James Smart3621a712009-04-06 18:47:14 -04004506 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004507 * @phba: Pointer to HBA context object.
4508 *
4509 * This function is called during the SLI initialization to configure
4510 * all the HBQs and post buffers to the HBQ. The caller is not
4511 * required to hold any locks. This function will return zero if successful
4512 * else it will return negative error code.
4513 **/
James Smarted957682007-06-17 19:56:37 -05004514static int
4515lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4516{
4517 int hbq_count = lpfc_sli_hbq_count();
4518 LPFC_MBOXQ_t *pmb;
4519 MAILBOX_t *pmbox;
4520 uint32_t hbqno;
4521 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004522
James Smart92d7f7b2007-06-17 19:56:38 -05004523 /* Get a Mailbox buffer to setup mailbox
4524 * commands for HBA initialization
4525 */
James Smarted957682007-06-17 19:56:37 -05004526 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4527
4528 if (!pmb)
4529 return -ENOMEM;
4530
James Smart04c68492009-05-22 14:52:52 -04004531 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004532
4533 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4534 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004535 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004536
4537 hbq_entry_index = 0;
4538 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4539 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4540 phba->hbqs[hbqno].hbqPutIdx = 0;
4541 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4542 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004543 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004544 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4545 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004546 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4547
4548 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4549 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4550 mbxStatus <status>, ring <num> */
4551
4552 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004553 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004554 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004555 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004556 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004557 pmbox->mbxStatus, hbqno);
4558
4559 phba->link_state = LPFC_HBA_ERROR;
4560 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004561 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004562 }
4563 }
4564 phba->hbq_count = hbq_count;
4565
James Smarted957682007-06-17 19:56:37 -05004566 mempool_free(pmb, phba->mbox_mem_pool);
4567
James Smart92d7f7b2007-06-17 19:56:38 -05004568 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004569 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4570 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004571 return 0;
4572}
4573
James Smarte59058c2008-08-24 21:49:00 -04004574/**
James Smart4f774512009-05-22 14:52:35 -04004575 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4576 * @phba: Pointer to HBA context object.
4577 *
4578 * This function is called during the SLI initialization to configure
4579 * all the HBQs and post buffers to the HBQ. The caller is not
4580 * required to hold any locks. This function will return zero if successful
4581 * else it will return negative error code.
4582 **/
4583static int
4584lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4585{
4586 phba->hbq_in_use = 1;
James Smart895427b2017-02-12 13:52:30 -08004587 phba->hbqs[LPFC_ELS_HBQ].entry_count =
4588 lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
James Smart4f774512009-05-22 14:52:35 -04004589 phba->hbq_count = 1;
James Smart895427b2017-02-12 13:52:30 -08004590 lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
James Smart4f774512009-05-22 14:52:35 -04004591 /* Initially populate or replenish the HBQs */
James Smart4f774512009-05-22 14:52:35 -04004592 return 0;
4593}
4594
4595/**
James Smart3621a712009-04-06 18:47:14 -04004596 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004597 * @phba: Pointer to HBA context object.
4598 * @sli_mode: sli mode - 2/3
4599 *
4600 * This function is called by the sli intialization code path
4601 * to issue config_port mailbox command. This function restarts the
4602 * HBA firmware and issues a config_port mailbox command to configure
4603 * the SLI interface in the sli mode specified by sli_mode
4604 * variable. The caller is not required to hold any locks.
4605 * The function returns 0 if successful, else returns negative error
4606 * code.
4607 **/
James Smart93996272008-08-24 21:50:30 -04004608int
4609lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004610{
4611 LPFC_MBOXQ_t *pmb;
4612 uint32_t resetcount = 0, rc = 0, done = 0;
4613
4614 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4615 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004616 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004617 return -ENOMEM;
4618 }
4619
James Smarted957682007-06-17 19:56:37 -05004620 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004621 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004622 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004623 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004624 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004625 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004626 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004627 rc = lpfc_sli_chipset_init(phba);
4628 if (rc)
4629 break;
4630
James Smart2e0fef82007-06-17 19:56:36 -05004631 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004632 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004633 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004634 resetcount++;
4635
James Smarted957682007-06-17 19:56:37 -05004636 /* Call pre CONFIG_PORT mailbox command initialization. A
4637 * value of 0 means the call was successful. Any other
4638 * nonzero value is a failure, but if ERESTART is returned,
4639 * the driver may reset the HBA and try again.
4640 */
dea31012005-04-17 16:05:31 -05004641 rc = lpfc_config_port_prep(phba);
4642 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004643 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004644 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004645 } else if (rc)
dea31012005-04-17 16:05:31 -05004646 break;
James Smart6d368e52011-05-24 11:44:12 -04004647
James Smart2e0fef82007-06-17 19:56:36 -05004648 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004649 lpfc_config_port(phba, pmb);
4650 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004651 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4652 LPFC_SLI3_HBQ_ENABLED |
4653 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004654 LPFC_SLI3_BG_ENABLED |
4655 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004656 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004657 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004658 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004659 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004660 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004661 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004662 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004663 spin_unlock_irq(&phba->hbalock);
4664 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004665 } else {
4666 /* Allow asynchronous mailbox command to go through */
4667 spin_lock_irq(&phba->hbalock);
4668 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4669 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004670 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004671
4672 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4673 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4674 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4675 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004676 }
dea31012005-04-17 16:05:31 -05004677 }
James Smarted957682007-06-17 19:56:37 -05004678 if (!done) {
4679 rc = -EINVAL;
4680 goto do_prep_failed;
4681 }
James Smart04c68492009-05-22 14:52:52 -04004682 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4683 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004684 rc = -ENXIO;
4685 goto do_prep_failed;
4686 }
James Smart04c68492009-05-22 14:52:52 -04004687 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004688 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004689 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4690 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4691 phba->max_vpi : phba->max_vports;
4692
James Smart34b02dc2008-08-24 21:49:55 -04004693 } else
4694 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004695 phba->fips_level = 0;
4696 phba->fips_spec_rev = 0;
4697 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004698 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004699 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4700 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4701 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4702 "2850 Security Crypto Active. FIPS x%d "
4703 "(Spec Rev: x%d)",
4704 phba->fips_level, phba->fips_spec_rev);
4705 }
4706 if (pmb->u.mb.un.varCfgPort.sec_err) {
4707 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4708 "2856 Config Port Security Crypto "
4709 "Error: x%x ",
4710 pmb->u.mb.un.varCfgPort.sec_err);
4711 }
James Smart04c68492009-05-22 14:52:52 -04004712 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004713 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004714 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004715 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004716
4717 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4718 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004719
4720 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004721 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004722 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4723 else
4724 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4725 "0443 Adapter did not grant "
4726 "BlockGuard\n");
4727 }
James Smart34b02dc2008-08-24 21:49:55 -04004728 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004729 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004730 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004731 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004732 }
James Smart92d7f7b2007-06-17 19:56:38 -05004733do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004734 mempool_free(pmb, phba->mbox_mem_pool);
4735 return rc;
4736}
4737
James Smarte59058c2008-08-24 21:49:00 -04004738
4739/**
James Smart3621a712009-04-06 18:47:14 -04004740 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004741 * @phba: Pointer to HBA context object.
4742 *
4743 * This function is the main SLI intialization function. This function
4744 * is called by the HBA intialization code, HBA reset code and HBA
4745 * error attention handler code. Caller is not required to hold any
4746 * locks. This function issues config_port mailbox command to configure
4747 * the SLI, setup iocb rings and HBQ rings. In the end the function
4748 * calls the config_port_post function to issue init_link mailbox
4749 * command and to start the discovery. The function will return zero
4750 * if successful, else it will return negative error code.
4751 **/
James Smarted957682007-06-17 19:56:37 -05004752int
4753lpfc_sli_hba_setup(struct lpfc_hba *phba)
4754{
4755 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004756 int mode = 3, i;
4757 int longs;
James Smarted957682007-06-17 19:56:37 -05004758
James Smart12247e82016-07-06 12:36:09 -07004759 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05004760 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004761 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004763 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004764 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07004765 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004766 break;
4767 }
James Smarted957682007-06-17 19:56:37 -05004768 mode = 2;
4769 break;
4770 case 0:
4771 case 3:
4772 break;
4773 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004774 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004775 "1819 Unrecognized sli_mode parameter: %d.\n",
4776 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004777
4778 break;
4779 }
James Smartb5c53952016-03-31 14:12:30 -07004780 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004781
James Smart93996272008-08-24 21:50:30 -04004782 rc = lpfc_sli_config_port(phba, mode);
4783
James Smart12247e82016-07-06 12:36:09 -07004784 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004785 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004786 "1820 Unable to select SLI-3. "
4787 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004788 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004789 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07004790 else if (rc && mode == 2)
4791 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05004792 if (rc)
dea31012005-04-17 16:05:31 -05004793 goto lpfc_sli_hba_setup_error;
4794
James Smart0d878412009-10-02 15:16:56 -04004795 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4796 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4797 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4798 if (!rc) {
4799 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4800 "2709 This device supports "
4801 "Advanced Error Reporting (AER)\n");
4802 spin_lock_irq(&phba->hbalock);
4803 phba->hba_flag |= HBA_AER_ENABLED;
4804 spin_unlock_irq(&phba->hbalock);
4805 } else {
4806 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4807 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004808 "Advanced Error Reporting (AER): %d\n",
4809 rc);
James Smart0d878412009-10-02 15:16:56 -04004810 phba->cfg_aer_support = 0;
4811 }
4812 }
4813
James Smarted957682007-06-17 19:56:37 -05004814 if (phba->sli_rev == 3) {
4815 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4816 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004817 } else {
4818 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4819 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004820 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004821 }
4822
4823 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004824 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4825 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004826 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004827
4828 if (rc)
4829 goto lpfc_sli_hba_setup_error;
4830
James Smart6d368e52011-05-24 11:44:12 -04004831 /* Initialize VPIs. */
4832 if (phba->sli_rev == LPFC_SLI_REV3) {
4833 /*
4834 * The VPI bitmask and physical ID array are allocated
4835 * and initialized once only - at driver load. A port
4836 * reset doesn't need to reinitialize this memory.
4837 */
4838 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4839 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4840 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4841 GFP_KERNEL);
4842 if (!phba->vpi_bmask) {
4843 rc = -ENOMEM;
4844 goto lpfc_sli_hba_setup_error;
4845 }
4846
4847 phba->vpi_ids = kzalloc(
4848 (phba->max_vpi+1) * sizeof(uint16_t),
4849 GFP_KERNEL);
4850 if (!phba->vpi_ids) {
4851 kfree(phba->vpi_bmask);
4852 rc = -ENOMEM;
4853 goto lpfc_sli_hba_setup_error;
4854 }
4855 for (i = 0; i < phba->max_vpi; i++)
4856 phba->vpi_ids[i] = i;
4857 }
4858 }
4859
James Smart93996272008-08-24 21:50:30 -04004860 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004861 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4862 rc = lpfc_sli_hbq_setup(phba);
4863 if (rc)
4864 goto lpfc_sli_hba_setup_error;
4865 }
James Smart04c68492009-05-22 14:52:52 -04004866 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004867 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004868 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004869
4870 rc = lpfc_config_port_post(phba);
4871 if (rc)
4872 goto lpfc_sli_hba_setup_error;
4873
James Smarted957682007-06-17 19:56:37 -05004874 return rc;
4875
James Smart92d7f7b2007-06-17 19:56:38 -05004876lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004877 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004878 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004879 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004880 return rc;
4881}
4882
James Smartda0436e2009-05-22 14:51:39 -04004883/**
4884 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4885 * @phba: Pointer to HBA context object.
4886 * @mboxq: mailbox pointer.
4887 * This function issue a dump mailbox command to read config region
4888 * 23 and parse the records in the region and populate driver
4889 * data structure.
4890 **/
4891static int
James Smartff78d8f2011-12-13 13:21:35 -05004892lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004893{
James Smartff78d8f2011-12-13 13:21:35 -05004894 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004895 struct lpfc_dmabuf *mp;
4896 struct lpfc_mqe *mqe;
4897 uint32_t data_length;
4898 int rc;
4899
4900 /* Program the default value of vlan_id and fc_map */
4901 phba->valid_vlan = 0;
4902 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4903 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4904 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4905
James Smartff78d8f2011-12-13 13:21:35 -05004906 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4907 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004908 return -ENOMEM;
4909
James Smartff78d8f2011-12-13 13:21:35 -05004910 mqe = &mboxq->u.mqe;
4911 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4912 rc = -ENOMEM;
4913 goto out_free_mboxq;
4914 }
4915
James Smartda0436e2009-05-22 14:51:39 -04004916 mp = (struct lpfc_dmabuf *) mboxq->context1;
4917 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4918
4919 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4920 "(%d):2571 Mailbox cmd x%x Status x%x "
4921 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4922 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4923 "CQ: x%x x%x x%x x%x\n",
4924 mboxq->vport ? mboxq->vport->vpi : 0,
4925 bf_get(lpfc_mqe_command, mqe),
4926 bf_get(lpfc_mqe_status, mqe),
4927 mqe->un.mb_words[0], mqe->un.mb_words[1],
4928 mqe->un.mb_words[2], mqe->un.mb_words[3],
4929 mqe->un.mb_words[4], mqe->un.mb_words[5],
4930 mqe->un.mb_words[6], mqe->un.mb_words[7],
4931 mqe->un.mb_words[8], mqe->un.mb_words[9],
4932 mqe->un.mb_words[10], mqe->un.mb_words[11],
4933 mqe->un.mb_words[12], mqe->un.mb_words[13],
4934 mqe->un.mb_words[14], mqe->un.mb_words[15],
4935 mqe->un.mb_words[16], mqe->un.mb_words[50],
4936 mboxq->mcqe.word0,
4937 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4938 mboxq->mcqe.trailer);
4939
4940 if (rc) {
4941 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4942 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004943 rc = -EIO;
4944 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004945 }
4946 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004947 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004948 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4949 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004950 rc = -EIO;
4951 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004952 }
James Smartda0436e2009-05-22 14:51:39 -04004953
4954 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4955 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4956 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004957 rc = 0;
4958
4959out_free_mboxq:
4960 mempool_free(mboxq, phba->mbox_mem_pool);
4961 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004962}
4963
4964/**
4965 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4966 * @phba: pointer to lpfc hba data structure.
4967 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4968 * @vpd: pointer to the memory to hold resulting port vpd data.
4969 * @vpd_size: On input, the number of bytes allocated to @vpd.
4970 * On output, the number of data bytes in @vpd.
4971 *
4972 * This routine executes a READ_REV SLI4 mailbox command. In
4973 * addition, this routine gets the port vpd data.
4974 *
4975 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004976 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004977 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004978 **/
4979static int
4980lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4981 uint8_t *vpd, uint32_t *vpd_size)
4982{
4983 int rc = 0;
4984 uint32_t dma_size;
4985 struct lpfc_dmabuf *dmabuf;
4986 struct lpfc_mqe *mqe;
4987
4988 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4989 if (!dmabuf)
4990 return -ENOMEM;
4991
4992 /*
4993 * Get a DMA buffer for the vpd data resulting from the READ_REV
4994 * mailbox command.
4995 */
4996 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04004997 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
4998 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04004999 if (!dmabuf->virt) {
5000 kfree(dmabuf);
5001 return -ENOMEM;
5002 }
James Smartda0436e2009-05-22 14:51:39 -04005003
5004 /*
5005 * The SLI4 implementation of READ_REV conflicts at word1,
5006 * bits 31:16 and SLI4 adds vpd functionality not present
5007 * in SLI3. This code corrects the conflicts.
5008 */
5009 lpfc_read_rev(phba, mboxq);
5010 mqe = &mboxq->u.mqe;
5011 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
5012 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
5013 mqe->un.read_rev.word1 &= 0x0000FFFF;
5014 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
5015 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
5016
5017 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5018 if (rc) {
5019 dma_free_coherent(&phba->pcidev->dev, dma_size,
5020 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05005021 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04005022 return -EIO;
5023 }
5024
James Smartda0436e2009-05-22 14:51:39 -04005025 /*
5026 * The available vpd length cannot be bigger than the
5027 * DMA buffer passed to the port. Catch the less than
5028 * case and update the caller's size.
5029 */
5030 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
5031 *vpd_size = mqe->un.read_rev.avail_vpd_len;
5032
James Smartd7c47992010-06-08 18:31:54 -04005033 memcpy(vpd, dmabuf->virt, *vpd_size);
5034
James Smartda0436e2009-05-22 14:51:39 -04005035 dma_free_coherent(&phba->pcidev->dev, dma_size,
5036 dmabuf->virt, dmabuf->phys);
5037 kfree(dmabuf);
5038 return 0;
5039}
5040
5041/**
James Smartcd1c8302011-10-10 21:33:25 -04005042 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
5043 * @phba: pointer to lpfc hba data structure.
5044 *
5045 * This routine retrieves SLI4 device physical port name this PCI function
5046 * is attached to.
5047 *
5048 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005049 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04005050 * otherwise - failed to retrieve physical port name
5051 **/
5052static int
5053lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
5054{
5055 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04005056 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
5057 struct lpfc_controller_attribute *cntl_attr;
5058 struct lpfc_mbx_get_port_name *get_port_name;
5059 void *virtaddr = NULL;
5060 uint32_t alloclen, reqlen;
5061 uint32_t shdr_status, shdr_add_status;
5062 union lpfc_sli4_cfg_shdr *shdr;
5063 char cport_name = 0;
5064 int rc;
5065
5066 /* We assume nothing at this point */
5067 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5068 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
5069
5070 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5071 if (!mboxq)
5072 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04005073 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05005074 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
5075 lpfc_sli4_read_config(phba);
5076 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
5077 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04005078
5079 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
5080 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
5081 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5082 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
5083 LPFC_SLI4_MBX_NEMBED);
5084 if (alloclen < reqlen) {
5085 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5086 "3084 Allocated DMA memory size (%d) is "
5087 "less than the requested DMA memory size "
5088 "(%d)\n", alloclen, reqlen);
5089 rc = -ENOMEM;
5090 goto out_free_mboxq;
5091 }
5092 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5093 virtaddr = mboxq->sge_array->addr[0];
5094 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5095 shdr = &mbx_cntl_attr->cfg_shdr;
5096 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5097 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5098 if (shdr_status || shdr_add_status || rc) {
5099 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5100 "3085 Mailbox x%x (x%x/x%x) failed, "
5101 "rc:x%x, status:x%x, add_status:x%x\n",
5102 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5103 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5104 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5105 rc, shdr_status, shdr_add_status);
5106 rc = -ENXIO;
5107 goto out_free_mboxq;
5108 }
5109 cntl_attr = &mbx_cntl_attr->cntl_attr;
5110 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5111 phba->sli4_hba.lnk_info.lnk_tp =
5112 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5113 phba->sli4_hba.lnk_info.lnk_no =
5114 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5115 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5116 "3086 lnk_type:%d, lnk_numb:%d\n",
5117 phba->sli4_hba.lnk_info.lnk_tp,
5118 phba->sli4_hba.lnk_info.lnk_no);
5119
5120retrieve_ppname:
5121 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5122 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5123 sizeof(struct lpfc_mbx_get_port_name) -
5124 sizeof(struct lpfc_sli4_cfg_mhdr),
5125 LPFC_SLI4_MBX_EMBED);
5126 get_port_name = &mboxq->u.mqe.un.get_port_name;
5127 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5128 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5129 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5130 phba->sli4_hba.lnk_info.lnk_tp);
5131 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5132 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5133 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5134 if (shdr_status || shdr_add_status || rc) {
5135 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5136 "3087 Mailbox x%x (x%x/x%x) failed: "
5137 "rc:x%x, status:x%x, add_status:x%x\n",
5138 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5139 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5140 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5141 rc, shdr_status, shdr_add_status);
5142 rc = -ENXIO;
5143 goto out_free_mboxq;
5144 }
5145 switch (phba->sli4_hba.lnk_info.lnk_no) {
5146 case LPFC_LINK_NUMBER_0:
5147 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5148 &get_port_name->u.response);
5149 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5150 break;
5151 case LPFC_LINK_NUMBER_1:
5152 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5153 &get_port_name->u.response);
5154 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5155 break;
5156 case LPFC_LINK_NUMBER_2:
5157 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5158 &get_port_name->u.response);
5159 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5160 break;
5161 case LPFC_LINK_NUMBER_3:
5162 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5163 &get_port_name->u.response);
5164 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5165 break;
5166 default:
5167 break;
5168 }
5169
5170 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5171 phba->Port[0] = cport_name;
5172 phba->Port[1] = '\0';
5173 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5174 "3091 SLI get port name: %s\n", phba->Port);
5175 }
5176
5177out_free_mboxq:
5178 if (rc != MBX_TIMEOUT) {
5179 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5180 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5181 else
5182 mempool_free(mboxq, phba->mbox_mem_pool);
5183 }
5184 return rc;
5185}
5186
5187/**
James Smartda0436e2009-05-22 14:51:39 -04005188 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5189 * @phba: pointer to lpfc hba data structure.
5190 *
5191 * This routine is called to explicitly arm the SLI4 device's completion and
5192 * event queues
5193 **/
5194static void
5195lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5196{
James Smart895427b2017-02-12 13:52:30 -08005197 int qidx;
James Smartda0436e2009-05-22 14:51:39 -04005198
5199 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5200 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart895427b2017-02-12 13:52:30 -08005201 if (phba->sli4_hba.nvmels_cq)
5202 lpfc_sli4_cq_release(phba->sli4_hba.nvmels_cq,
5203 LPFC_QUEUE_REARM);
5204
5205 if (phba->sli4_hba.fcp_cq)
5206 for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
5207 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[qidx],
5208 LPFC_QUEUE_REARM);
5209
5210 if (phba->sli4_hba.nvme_cq)
5211 for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
5212 lpfc_sli4_cq_release(phba->sli4_hba.nvme_cq[qidx],
5213 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005214
James Smartf38fa0b2014-04-04 13:52:21 -04005215 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005216 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5217
James Smart895427b2017-02-12 13:52:30 -08005218 if (phba->sli4_hba.hba_eq)
5219 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
5220 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[qidx],
5221 LPFC_QUEUE_REARM);
James Smart1ba981f2014-02-20 09:56:45 -05005222
5223 if (phba->cfg_fof)
5224 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005225}
5226
5227/**
James Smart6d368e52011-05-24 11:44:12 -04005228 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5229 * @phba: Pointer to HBA context object.
5230 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005231 * @extnt_count: buffer to hold port available extent count.
5232 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005233 *
James Smartb76f2dc2011-07-22 18:37:42 -04005234 * This function calls the port and retrievs the number of available
5235 * extents and their size for a particular extent type.
5236 *
5237 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005238 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005239int
James Smart6d368e52011-05-24 11:44:12 -04005240lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5241 uint16_t *extnt_count, uint16_t *extnt_size)
5242{
5243 int rc = 0;
5244 uint32_t length;
5245 uint32_t mbox_tmo;
5246 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5247 LPFC_MBOXQ_t *mbox;
5248
5249 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5250 if (!mbox)
5251 return -ENOMEM;
5252
5253 /* Find out how many extents are available for this resource type */
5254 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5255 sizeof(struct lpfc_sli4_cfg_mhdr));
5256 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5257 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5258 length, LPFC_SLI4_MBX_EMBED);
5259
5260 /* Send an extents count of 0 - the GET doesn't use it. */
5261 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5262 LPFC_SLI4_MBX_EMBED);
5263 if (unlikely(rc)) {
5264 rc = -EIO;
5265 goto err_exit;
5266 }
5267
5268 if (!phba->sli4_hba.intr_enable)
5269 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5270 else {
James Smarta183a152011-10-10 21:32:43 -04005271 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005272 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5273 }
5274 if (unlikely(rc)) {
5275 rc = -EIO;
5276 goto err_exit;
5277 }
5278
5279 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5280 if (bf_get(lpfc_mbox_hdr_status,
5281 &rsrc_info->header.cfg_shdr.response)) {
5282 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5283 "2930 Failed to get resource extents "
5284 "Status 0x%x Add'l Status 0x%x\n",
5285 bf_get(lpfc_mbox_hdr_status,
5286 &rsrc_info->header.cfg_shdr.response),
5287 bf_get(lpfc_mbox_hdr_add_status,
5288 &rsrc_info->header.cfg_shdr.response));
5289 rc = -EIO;
5290 goto err_exit;
5291 }
5292
5293 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5294 &rsrc_info->u.rsp);
5295 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5296 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005297
5298 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5299 "3162 Retrieved extents type-%d from port: count:%d, "
5300 "size:%d\n", type, *extnt_count, *extnt_size);
5301
5302err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005303 mempool_free(mbox, phba->mbox_mem_pool);
5304 return rc;
5305}
5306
5307/**
5308 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5309 * @phba: Pointer to HBA context object.
5310 * @type: The extent type to check.
5311 *
5312 * This function reads the current available extents from the port and checks
5313 * if the extent count or extent size has changed since the last access.
5314 * Callers use this routine post port reset to understand if there is a
5315 * extent reprovisioning requirement.
5316 *
5317 * Returns:
5318 * -Error: error indicates problem.
5319 * 1: Extent count or size has changed.
5320 * 0: No changes.
5321 **/
5322static int
5323lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5324{
5325 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5326 uint16_t size_diff, rsrc_ext_size;
5327 int rc = 0;
5328 struct lpfc_rsrc_blks *rsrc_entry;
5329 struct list_head *rsrc_blk_list = NULL;
5330
5331 size_diff = 0;
5332 curr_ext_cnt = 0;
5333 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5334 &rsrc_ext_cnt,
5335 &rsrc_ext_size);
5336 if (unlikely(rc))
5337 return -EIO;
5338
5339 switch (type) {
5340 case LPFC_RSC_TYPE_FCOE_RPI:
5341 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5342 break;
5343 case LPFC_RSC_TYPE_FCOE_VPI:
5344 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5345 break;
5346 case LPFC_RSC_TYPE_FCOE_XRI:
5347 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5348 break;
5349 case LPFC_RSC_TYPE_FCOE_VFI:
5350 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5351 break;
5352 default:
5353 break;
5354 }
5355
5356 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5357 curr_ext_cnt++;
5358 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5359 size_diff++;
5360 }
5361
5362 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5363 rc = 1;
5364
5365 return rc;
5366}
5367
5368/**
5369 * lpfc_sli4_cfg_post_extnts -
5370 * @phba: Pointer to HBA context object.
5371 * @extnt_cnt - number of available extents.
5372 * @type - the extent type (rpi, xri, vfi, vpi).
5373 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5374 * @mbox - pointer to the caller's allocated mailbox structure.
5375 *
5376 * This function executes the extents allocation request. It also
5377 * takes care of the amount of memory needed to allocate or get the
5378 * allocated extents. It is the caller's responsibility to evaluate
5379 * the response.
5380 *
5381 * Returns:
5382 * -Error: Error value describes the condition found.
5383 * 0: if successful
5384 **/
5385static int
James Smart8a9d2e82012-05-09 21:16:12 -04005386lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005387 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5388{
5389 int rc = 0;
5390 uint32_t req_len;
5391 uint32_t emb_len;
5392 uint32_t alloc_len, mbox_tmo;
5393
5394 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005395 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005396
5397 /*
5398 * Calculate the size of an embedded mailbox. The uint32_t
5399 * accounts for extents-specific word.
5400 */
5401 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5402 sizeof(uint32_t);
5403
5404 /*
5405 * Presume the allocation and response will fit into an embedded
5406 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5407 */
5408 *emb = LPFC_SLI4_MBX_EMBED;
5409 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005410 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005411 sizeof(union lpfc_sli4_cfg_shdr) +
5412 sizeof(uint32_t);
5413 *emb = LPFC_SLI4_MBX_NEMBED;
5414 }
5415
5416 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5417 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5418 req_len, *emb);
5419 if (alloc_len < req_len) {
5420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005421 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005422 "less than the requested DMA memory "
5423 "size (x%x)\n", alloc_len, req_len);
5424 return -ENOMEM;
5425 }
James Smart8a9d2e82012-05-09 21:16:12 -04005426 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005427 if (unlikely(rc))
5428 return -EIO;
5429
5430 if (!phba->sli4_hba.intr_enable)
5431 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5432 else {
James Smarta183a152011-10-10 21:32:43 -04005433 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005434 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5435 }
5436
5437 if (unlikely(rc))
5438 rc = -EIO;
5439 return rc;
5440}
5441
5442/**
5443 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5444 * @phba: Pointer to HBA context object.
5445 * @type: The resource extent type to allocate.
5446 *
5447 * This function allocates the number of elements for the specified
5448 * resource type.
5449 **/
5450static int
5451lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5452{
5453 bool emb = false;
5454 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5455 uint16_t rsrc_id, rsrc_start, j, k;
5456 uint16_t *ids;
5457 int i, rc;
5458 unsigned long longs;
5459 unsigned long *bmask;
5460 struct lpfc_rsrc_blks *rsrc_blks;
5461 LPFC_MBOXQ_t *mbox;
5462 uint32_t length;
5463 struct lpfc_id_range *id_array = NULL;
5464 void *virtaddr = NULL;
5465 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5466 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5467 struct list_head *ext_blk_list;
5468
5469 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5470 &rsrc_cnt,
5471 &rsrc_size);
5472 if (unlikely(rc))
5473 return -EIO;
5474
5475 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5476 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5477 "3009 No available Resource Extents "
5478 "for resource type 0x%x: Count: 0x%x, "
5479 "Size 0x%x\n", type, rsrc_cnt,
5480 rsrc_size);
5481 return -ENOMEM;
5482 }
5483
James Smart8a9d2e82012-05-09 21:16:12 -04005484 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5485 "2903 Post resource extents type-0x%x: "
5486 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005487
5488 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5489 if (!mbox)
5490 return -ENOMEM;
5491
James Smart8a9d2e82012-05-09 21:16:12 -04005492 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005493 if (unlikely(rc)) {
5494 rc = -EIO;
5495 goto err_exit;
5496 }
5497
5498 /*
5499 * Figure out where the response is located. Then get local pointers
5500 * to the response data. The port does not guarantee to respond to
5501 * all extents counts request so update the local variable with the
5502 * allocated count from the port.
5503 */
5504 if (emb == LPFC_SLI4_MBX_EMBED) {
5505 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5506 id_array = &rsrc_ext->u.rsp.id[0];
5507 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5508 } else {
5509 virtaddr = mbox->sge_array->addr[0];
5510 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5511 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5512 id_array = &n_rsrc->id;
5513 }
5514
5515 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5516 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5517
5518 /*
5519 * Based on the resource size and count, correct the base and max
5520 * resource values.
5521 */
5522 length = sizeof(struct lpfc_rsrc_blks);
5523 switch (type) {
5524 case LPFC_RSC_TYPE_FCOE_RPI:
5525 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5526 sizeof(unsigned long),
5527 GFP_KERNEL);
5528 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5529 rc = -ENOMEM;
5530 goto err_exit;
5531 }
5532 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5533 sizeof(uint16_t),
5534 GFP_KERNEL);
5535 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5536 kfree(phba->sli4_hba.rpi_bmask);
5537 rc = -ENOMEM;
5538 goto err_exit;
5539 }
5540
5541 /*
5542 * The next_rpi was initialized with the maximum available
5543 * count but the port may allocate a smaller number. Catch
5544 * that case and update the next_rpi.
5545 */
5546 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5547
5548 /* Initialize local ptrs for common extent processing later. */
5549 bmask = phba->sli4_hba.rpi_bmask;
5550 ids = phba->sli4_hba.rpi_ids;
5551 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5552 break;
5553 case LPFC_RSC_TYPE_FCOE_VPI:
5554 phba->vpi_bmask = kzalloc(longs *
5555 sizeof(unsigned long),
5556 GFP_KERNEL);
5557 if (unlikely(!phba->vpi_bmask)) {
5558 rc = -ENOMEM;
5559 goto err_exit;
5560 }
5561 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5562 sizeof(uint16_t),
5563 GFP_KERNEL);
5564 if (unlikely(!phba->vpi_ids)) {
5565 kfree(phba->vpi_bmask);
5566 rc = -ENOMEM;
5567 goto err_exit;
5568 }
5569
5570 /* Initialize local ptrs for common extent processing later. */
5571 bmask = phba->vpi_bmask;
5572 ids = phba->vpi_ids;
5573 ext_blk_list = &phba->lpfc_vpi_blk_list;
5574 break;
5575 case LPFC_RSC_TYPE_FCOE_XRI:
5576 phba->sli4_hba.xri_bmask = kzalloc(longs *
5577 sizeof(unsigned long),
5578 GFP_KERNEL);
5579 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5580 rc = -ENOMEM;
5581 goto err_exit;
5582 }
James Smart8a9d2e82012-05-09 21:16:12 -04005583 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005584 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5585 sizeof(uint16_t),
5586 GFP_KERNEL);
5587 if (unlikely(!phba->sli4_hba.xri_ids)) {
5588 kfree(phba->sli4_hba.xri_bmask);
5589 rc = -ENOMEM;
5590 goto err_exit;
5591 }
5592
5593 /* Initialize local ptrs for common extent processing later. */
5594 bmask = phba->sli4_hba.xri_bmask;
5595 ids = phba->sli4_hba.xri_ids;
5596 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5597 break;
5598 case LPFC_RSC_TYPE_FCOE_VFI:
5599 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5600 sizeof(unsigned long),
5601 GFP_KERNEL);
5602 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5603 rc = -ENOMEM;
5604 goto err_exit;
5605 }
5606 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5607 sizeof(uint16_t),
5608 GFP_KERNEL);
5609 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5610 kfree(phba->sli4_hba.vfi_bmask);
5611 rc = -ENOMEM;
5612 goto err_exit;
5613 }
5614
5615 /* Initialize local ptrs for common extent processing later. */
5616 bmask = phba->sli4_hba.vfi_bmask;
5617 ids = phba->sli4_hba.vfi_ids;
5618 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5619 break;
5620 default:
5621 /* Unsupported Opcode. Fail call. */
5622 id_array = NULL;
5623 bmask = NULL;
5624 ids = NULL;
5625 ext_blk_list = NULL;
5626 goto err_exit;
5627 }
5628
5629 /*
5630 * Complete initializing the extent configuration with the
5631 * allocated ids assigned to this function. The bitmask serves
5632 * as an index into the array and manages the available ids. The
5633 * array just stores the ids communicated to the port via the wqes.
5634 */
5635 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5636 if ((i % 2) == 0)
5637 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5638 &id_array[k]);
5639 else
5640 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5641 &id_array[k]);
5642
5643 rsrc_blks = kzalloc(length, GFP_KERNEL);
5644 if (unlikely(!rsrc_blks)) {
5645 rc = -ENOMEM;
5646 kfree(bmask);
5647 kfree(ids);
5648 goto err_exit;
5649 }
5650 rsrc_blks->rsrc_start = rsrc_id;
5651 rsrc_blks->rsrc_size = rsrc_size;
5652 list_add_tail(&rsrc_blks->list, ext_blk_list);
5653 rsrc_start = rsrc_id;
James Smart895427b2017-02-12 13:52:30 -08005654 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
James Smart6d368e52011-05-24 11:44:12 -04005655 phba->sli4_hba.scsi_xri_start = rsrc_start +
James Smart895427b2017-02-12 13:52:30 -08005656 lpfc_sli4_get_iocb_cnt(phba);
5657 phba->sli4_hba.nvme_xri_start =
5658 phba->sli4_hba.scsi_xri_start +
5659 phba->sli4_hba.scsi_xri_max;
5660 }
James Smart6d368e52011-05-24 11:44:12 -04005661
5662 while (rsrc_id < (rsrc_start + rsrc_size)) {
5663 ids[j] = rsrc_id;
5664 rsrc_id++;
5665 j++;
5666 }
5667 /* Entire word processed. Get next word.*/
5668 if ((i % 2) == 1)
5669 k++;
5670 }
5671 err_exit:
5672 lpfc_sli4_mbox_cmd_free(phba, mbox);
5673 return rc;
5674}
5675
James Smart895427b2017-02-12 13:52:30 -08005676
5677
James Smart6d368e52011-05-24 11:44:12 -04005678/**
5679 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5680 * @phba: Pointer to HBA context object.
5681 * @type: the extent's type.
5682 *
5683 * This function deallocates all extents of a particular resource type.
5684 * SLI4 does not allow for deallocating a particular extent range. It
5685 * is the caller's responsibility to release all kernel memory resources.
5686 **/
5687static int
5688lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5689{
5690 int rc;
5691 uint32_t length, mbox_tmo = 0;
5692 LPFC_MBOXQ_t *mbox;
5693 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5694 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5695
5696 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5697 if (!mbox)
5698 return -ENOMEM;
5699
5700 /*
5701 * This function sends an embedded mailbox because it only sends the
5702 * the resource type. All extents of this type are released by the
5703 * port.
5704 */
5705 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5706 sizeof(struct lpfc_sli4_cfg_mhdr));
5707 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5708 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5709 length, LPFC_SLI4_MBX_EMBED);
5710
5711 /* Send an extents count of 0 - the dealloc doesn't use it. */
5712 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5713 LPFC_SLI4_MBX_EMBED);
5714 if (unlikely(rc)) {
5715 rc = -EIO;
5716 goto out_free_mbox;
5717 }
5718 if (!phba->sli4_hba.intr_enable)
5719 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5720 else {
James Smarta183a152011-10-10 21:32:43 -04005721 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005722 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5723 }
5724 if (unlikely(rc)) {
5725 rc = -EIO;
5726 goto out_free_mbox;
5727 }
5728
5729 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5730 if (bf_get(lpfc_mbox_hdr_status,
5731 &dealloc_rsrc->header.cfg_shdr.response)) {
5732 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5733 "2919 Failed to release resource extents "
5734 "for type %d - Status 0x%x Add'l Status 0x%x. "
5735 "Resource memory not released.\n",
5736 type,
5737 bf_get(lpfc_mbox_hdr_status,
5738 &dealloc_rsrc->header.cfg_shdr.response),
5739 bf_get(lpfc_mbox_hdr_add_status,
5740 &dealloc_rsrc->header.cfg_shdr.response));
5741 rc = -EIO;
5742 goto out_free_mbox;
5743 }
5744
5745 /* Release kernel memory resources for the specific type. */
5746 switch (type) {
5747 case LPFC_RSC_TYPE_FCOE_VPI:
5748 kfree(phba->vpi_bmask);
5749 kfree(phba->vpi_ids);
5750 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5751 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5752 &phba->lpfc_vpi_blk_list, list) {
5753 list_del_init(&rsrc_blk->list);
5754 kfree(rsrc_blk);
5755 }
James Smart16a3a202013-04-17 20:14:38 -04005756 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005757 break;
5758 case LPFC_RSC_TYPE_FCOE_XRI:
5759 kfree(phba->sli4_hba.xri_bmask);
5760 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005761 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5762 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5763 list_del_init(&rsrc_blk->list);
5764 kfree(rsrc_blk);
5765 }
5766 break;
5767 case LPFC_RSC_TYPE_FCOE_VFI:
5768 kfree(phba->sli4_hba.vfi_bmask);
5769 kfree(phba->sli4_hba.vfi_ids);
5770 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5771 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5772 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5773 list_del_init(&rsrc_blk->list);
5774 kfree(rsrc_blk);
5775 }
5776 break;
5777 case LPFC_RSC_TYPE_FCOE_RPI:
5778 /* RPI bitmask and physical id array are cleaned up earlier. */
5779 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5780 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5781 list_del_init(&rsrc_blk->list);
5782 kfree(rsrc_blk);
5783 }
5784 break;
5785 default:
5786 break;
5787 }
5788
5789 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5790
5791 out_free_mbox:
5792 mempool_free(mbox, phba->mbox_mem_pool);
5793 return rc;
5794}
5795
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08005796static void
James Smart7bdedb32016-07-06 12:36:00 -07005797lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5798 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07005799{
James Smart65791f12016-07-06 12:35:56 -07005800 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07005801
James Smart65791f12016-07-06 12:35:56 -07005802 len = sizeof(struct lpfc_mbx_set_feature) -
5803 sizeof(struct lpfc_sli4_cfg_mhdr);
5804 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5805 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5806 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07005807
James Smart7bdedb32016-07-06 12:36:00 -07005808 switch (feature) {
5809 case LPFC_SET_UE_RECOVERY:
5810 bf_set(lpfc_mbx_set_feature_UER,
5811 &mbox->u.mqe.un.set_feature, 1);
5812 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5813 mbox->u.mqe.un.set_feature.param_len = 8;
5814 break;
5815 case LPFC_SET_MDS_DIAGS:
5816 bf_set(lpfc_mbx_set_feature_mds,
5817 &mbox->u.mqe.un.set_feature, 1);
5818 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5819 &mbox->u.mqe.un.set_feature, 0);
5820 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5821 mbox->u.mqe.un.set_feature.param_len = 8;
5822 break;
James Smart65791f12016-07-06 12:35:56 -07005823 }
James Smart7bdedb32016-07-06 12:36:00 -07005824
5825 return;
James Smart65791f12016-07-06 12:35:56 -07005826}
5827
James Smart6d368e52011-05-24 11:44:12 -04005828/**
5829 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5830 * @phba: Pointer to HBA context object.
5831 *
5832 * This function allocates all SLI4 resource identifiers.
5833 **/
5834int
5835lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5836{
5837 int i, rc, error = 0;
5838 uint16_t count, base;
5839 unsigned long longs;
5840
James Smartff78d8f2011-12-13 13:21:35 -05005841 if (!phba->sli4_hba.rpi_hdrs_in_use)
5842 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005843 if (phba->sli4_hba.extents_in_use) {
5844 /*
5845 * The port supports resource extents. The XRI, VPI, VFI, RPI
5846 * resource extent count must be read and allocated before
5847 * provisioning the resource id arrays.
5848 */
5849 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5850 LPFC_IDX_RSRC_RDY) {
5851 /*
5852 * Extent-based resources are set - the driver could
5853 * be in a port reset. Figure out if any corrective
5854 * actions need to be taken.
5855 */
5856 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5857 LPFC_RSC_TYPE_FCOE_VFI);
5858 if (rc != 0)
5859 error++;
5860 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5861 LPFC_RSC_TYPE_FCOE_VPI);
5862 if (rc != 0)
5863 error++;
5864 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5865 LPFC_RSC_TYPE_FCOE_XRI);
5866 if (rc != 0)
5867 error++;
5868 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5869 LPFC_RSC_TYPE_FCOE_RPI);
5870 if (rc != 0)
5871 error++;
5872
5873 /*
5874 * It's possible that the number of resources
5875 * provided to this port instance changed between
5876 * resets. Detect this condition and reallocate
5877 * resources. Otherwise, there is no action.
5878 */
5879 if (error) {
5880 lpfc_printf_log(phba, KERN_INFO,
5881 LOG_MBOX | LOG_INIT,
5882 "2931 Detected extent resource "
5883 "change. Reallocating all "
5884 "extents.\n");
5885 rc = lpfc_sli4_dealloc_extent(phba,
5886 LPFC_RSC_TYPE_FCOE_VFI);
5887 rc = lpfc_sli4_dealloc_extent(phba,
5888 LPFC_RSC_TYPE_FCOE_VPI);
5889 rc = lpfc_sli4_dealloc_extent(phba,
5890 LPFC_RSC_TYPE_FCOE_XRI);
5891 rc = lpfc_sli4_dealloc_extent(phba,
5892 LPFC_RSC_TYPE_FCOE_RPI);
5893 } else
5894 return 0;
5895 }
5896
5897 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5898 if (unlikely(rc))
5899 goto err_exit;
5900
5901 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5902 if (unlikely(rc))
5903 goto err_exit;
5904
5905 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5906 if (unlikely(rc))
5907 goto err_exit;
5908
5909 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5910 if (unlikely(rc))
5911 goto err_exit;
5912 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5913 LPFC_IDX_RSRC_RDY);
5914 return rc;
5915 } else {
5916 /*
5917 * The port does not support resource extents. The XRI, VPI,
5918 * VFI, RPI resource ids were determined from READ_CONFIG.
5919 * Just allocate the bitmasks and provision the resource id
5920 * arrays. If a port reset is active, the resources don't
5921 * need any action - just exit.
5922 */
5923 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005924 LPFC_IDX_RSRC_RDY) {
5925 lpfc_sli4_dealloc_resource_identifiers(phba);
5926 lpfc_sli4_remove_rpis(phba);
5927 }
James Smart6d368e52011-05-24 11:44:12 -04005928 /* RPIs. */
5929 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05005930 if (count <= 0) {
5931 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5932 "3279 Invalid provisioning of "
5933 "rpi:%d\n", count);
5934 rc = -EINVAL;
5935 goto err_exit;
5936 }
James Smart6d368e52011-05-24 11:44:12 -04005937 base = phba->sli4_hba.max_cfg_param.rpi_base;
5938 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5939 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5940 sizeof(unsigned long),
5941 GFP_KERNEL);
5942 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5943 rc = -ENOMEM;
5944 goto err_exit;
5945 }
5946 phba->sli4_hba.rpi_ids = kzalloc(count *
5947 sizeof(uint16_t),
5948 GFP_KERNEL);
5949 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5950 rc = -ENOMEM;
5951 goto free_rpi_bmask;
5952 }
5953
5954 for (i = 0; i < count; i++)
5955 phba->sli4_hba.rpi_ids[i] = base + i;
5956
5957 /* VPIs. */
5958 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05005959 if (count <= 0) {
5960 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5961 "3280 Invalid provisioning of "
5962 "vpi:%d\n", count);
5963 rc = -EINVAL;
5964 goto free_rpi_ids;
5965 }
James Smart6d368e52011-05-24 11:44:12 -04005966 base = phba->sli4_hba.max_cfg_param.vpi_base;
5967 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5968 phba->vpi_bmask = kzalloc(longs *
5969 sizeof(unsigned long),
5970 GFP_KERNEL);
5971 if (unlikely(!phba->vpi_bmask)) {
5972 rc = -ENOMEM;
5973 goto free_rpi_ids;
5974 }
5975 phba->vpi_ids = kzalloc(count *
5976 sizeof(uint16_t),
5977 GFP_KERNEL);
5978 if (unlikely(!phba->vpi_ids)) {
5979 rc = -ENOMEM;
5980 goto free_vpi_bmask;
5981 }
5982
5983 for (i = 0; i < count; i++)
5984 phba->vpi_ids[i] = base + i;
5985
5986 /* XRIs. */
5987 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05005988 if (count <= 0) {
5989 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5990 "3281 Invalid provisioning of "
5991 "xri:%d\n", count);
5992 rc = -EINVAL;
5993 goto free_vpi_ids;
5994 }
James Smart6d368e52011-05-24 11:44:12 -04005995 base = phba->sli4_hba.max_cfg_param.xri_base;
5996 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5997 phba->sli4_hba.xri_bmask = kzalloc(longs *
5998 sizeof(unsigned long),
5999 GFP_KERNEL);
6000 if (unlikely(!phba->sli4_hba.xri_bmask)) {
6001 rc = -ENOMEM;
6002 goto free_vpi_ids;
6003 }
James Smart41899be2012-03-01 22:34:19 -05006004 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006005 phba->sli4_hba.xri_ids = kzalloc(count *
6006 sizeof(uint16_t),
6007 GFP_KERNEL);
6008 if (unlikely(!phba->sli4_hba.xri_ids)) {
6009 rc = -ENOMEM;
6010 goto free_xri_bmask;
6011 }
6012
6013 for (i = 0; i < count; i++)
6014 phba->sli4_hba.xri_ids[i] = base + i;
6015
6016 /* VFIs. */
6017 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05006018 if (count <= 0) {
6019 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6020 "3282 Invalid provisioning of "
6021 "vfi:%d\n", count);
6022 rc = -EINVAL;
6023 goto free_xri_ids;
6024 }
James Smart6d368e52011-05-24 11:44:12 -04006025 base = phba->sli4_hba.max_cfg_param.vfi_base;
6026 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
6027 phba->sli4_hba.vfi_bmask = kzalloc(longs *
6028 sizeof(unsigned long),
6029 GFP_KERNEL);
6030 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
6031 rc = -ENOMEM;
6032 goto free_xri_ids;
6033 }
6034 phba->sli4_hba.vfi_ids = kzalloc(count *
6035 sizeof(uint16_t),
6036 GFP_KERNEL);
6037 if (unlikely(!phba->sli4_hba.vfi_ids)) {
6038 rc = -ENOMEM;
6039 goto free_vfi_bmask;
6040 }
6041
6042 for (i = 0; i < count; i++)
6043 phba->sli4_hba.vfi_ids[i] = base + i;
6044
6045 /*
6046 * Mark all resources ready. An HBA reset doesn't need
6047 * to reset the initialization.
6048 */
6049 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
6050 LPFC_IDX_RSRC_RDY);
6051 return 0;
6052 }
6053
6054 free_vfi_bmask:
6055 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006056 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006057 free_xri_ids:
6058 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006059 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006060 free_xri_bmask:
6061 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006062 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006063 free_vpi_ids:
6064 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006065 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006066 free_vpi_bmask:
6067 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006068 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006069 free_rpi_ids:
6070 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01006071 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006072 free_rpi_bmask:
6073 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01006074 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04006075 err_exit:
6076 return rc;
6077}
6078
6079/**
6080 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
6081 * @phba: Pointer to HBA context object.
6082 *
6083 * This function allocates the number of elements for the specified
6084 * resource type.
6085 **/
6086int
6087lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
6088{
6089 if (phba->sli4_hba.extents_in_use) {
6090 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
6091 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
6092 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
6093 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
6094 } else {
6095 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04006096 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006097 kfree(phba->vpi_ids);
6098 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6099 kfree(phba->sli4_hba.xri_bmask);
6100 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006101 kfree(phba->sli4_hba.vfi_bmask);
6102 kfree(phba->sli4_hba.vfi_ids);
6103 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6104 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6105 }
6106
6107 return 0;
6108}
6109
6110/**
James Smartb76f2dc2011-07-22 18:37:42 -04006111 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6112 * @phba: Pointer to HBA context object.
6113 * @type: The resource extent type.
6114 * @extnt_count: buffer to hold port extent count response
6115 * @extnt_size: buffer to hold port extent size response.
6116 *
6117 * This function calls the port to read the host allocated extents
6118 * for a particular type.
6119 **/
6120int
6121lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6122 uint16_t *extnt_cnt, uint16_t *extnt_size)
6123{
6124 bool emb;
6125 int rc = 0;
6126 uint16_t curr_blks = 0;
6127 uint32_t req_len, emb_len;
6128 uint32_t alloc_len, mbox_tmo;
6129 struct list_head *blk_list_head;
6130 struct lpfc_rsrc_blks *rsrc_blk;
6131 LPFC_MBOXQ_t *mbox;
6132 void *virtaddr = NULL;
6133 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6134 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6135 union lpfc_sli4_cfg_shdr *shdr;
6136
6137 switch (type) {
6138 case LPFC_RSC_TYPE_FCOE_VPI:
6139 blk_list_head = &phba->lpfc_vpi_blk_list;
6140 break;
6141 case LPFC_RSC_TYPE_FCOE_XRI:
6142 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6143 break;
6144 case LPFC_RSC_TYPE_FCOE_VFI:
6145 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6146 break;
6147 case LPFC_RSC_TYPE_FCOE_RPI:
6148 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6149 break;
6150 default:
6151 return -EIO;
6152 }
6153
6154 /* Count the number of extents currently allocatd for this type. */
6155 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6156 if (curr_blks == 0) {
6157 /*
6158 * The GET_ALLOCATED mailbox does not return the size,
6159 * just the count. The size should be just the size
6160 * stored in the current allocated block and all sizes
6161 * for an extent type are the same so set the return
6162 * value now.
6163 */
6164 *extnt_size = rsrc_blk->rsrc_size;
6165 }
6166 curr_blks++;
6167 }
6168
James Smartb76f2dc2011-07-22 18:37:42 -04006169 /*
6170 * Calculate the size of an embedded mailbox. The uint32_t
6171 * accounts for extents-specific word.
6172 */
6173 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6174 sizeof(uint32_t);
6175
6176 /*
6177 * Presume the allocation and response will fit into an embedded
6178 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6179 */
6180 emb = LPFC_SLI4_MBX_EMBED;
6181 req_len = emb_len;
6182 if (req_len > emb_len) {
6183 req_len = curr_blks * sizeof(uint16_t) +
6184 sizeof(union lpfc_sli4_cfg_shdr) +
6185 sizeof(uint32_t);
6186 emb = LPFC_SLI4_MBX_NEMBED;
6187 }
6188
6189 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6190 if (!mbox)
6191 return -ENOMEM;
6192 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6193
6194 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6195 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6196 req_len, emb);
6197 if (alloc_len < req_len) {
6198 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6199 "2983 Allocated DMA memory size (x%x) is "
6200 "less than the requested DMA memory "
6201 "size (x%x)\n", alloc_len, req_len);
6202 rc = -ENOMEM;
6203 goto err_exit;
6204 }
6205 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6206 if (unlikely(rc)) {
6207 rc = -EIO;
6208 goto err_exit;
6209 }
6210
6211 if (!phba->sli4_hba.intr_enable)
6212 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6213 else {
James Smarta183a152011-10-10 21:32:43 -04006214 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006215 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6216 }
6217
6218 if (unlikely(rc)) {
6219 rc = -EIO;
6220 goto err_exit;
6221 }
6222
6223 /*
6224 * Figure out where the response is located. Then get local pointers
6225 * to the response data. The port does not guarantee to respond to
6226 * all extents counts request so update the local variable with the
6227 * allocated count from the port.
6228 */
6229 if (emb == LPFC_SLI4_MBX_EMBED) {
6230 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6231 shdr = &rsrc_ext->header.cfg_shdr;
6232 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6233 } else {
6234 virtaddr = mbox->sge_array->addr[0];
6235 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6236 shdr = &n_rsrc->cfg_shdr;
6237 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6238 }
6239
6240 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6241 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6242 "2984 Failed to read allocated resources "
6243 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6244 type,
6245 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6246 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6247 rc = -EIO;
6248 goto err_exit;
6249 }
6250 err_exit:
6251 lpfc_sli4_mbox_cmd_free(phba, mbox);
6252 return rc;
6253}
6254
6255/**
James Smart895427b2017-02-12 13:52:30 -08006256 * lpfc_sli4_repost_sgl_list - Repsot the buffers sgl pages as block
James Smart8a9d2e82012-05-09 21:16:12 -04006257 * @phba: pointer to lpfc hba data structure.
James Smart895427b2017-02-12 13:52:30 -08006258 * @pring: Pointer to driver SLI ring object.
6259 * @sgl_list: linked link of sgl buffers to post
6260 * @cnt: number of linked list buffers
James Smart8a9d2e82012-05-09 21:16:12 -04006261 *
James Smart895427b2017-02-12 13:52:30 -08006262 * This routine walks the list of buffers that have been allocated and
James Smart8a9d2e82012-05-09 21:16:12 -04006263 * repost them to the port by using SGL block post. This is needed after a
6264 * pci_function_reset/warm_start or start. It attempts to construct blocks
James Smart895427b2017-02-12 13:52:30 -08006265 * of buffer sgls which contains contiguous xris and uses the non-embedded
6266 * SGL block post mailbox commands to post them to the port. For single
James Smart8a9d2e82012-05-09 21:16:12 -04006267 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6268 * mailbox command for posting.
6269 *
6270 * Returns: 0 = success, non-zero failure.
6271 **/
6272static int
James Smart895427b2017-02-12 13:52:30 -08006273lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
6274 struct list_head *sgl_list, int cnt)
James Smart8a9d2e82012-05-09 21:16:12 -04006275{
6276 struct lpfc_sglq *sglq_entry = NULL;
6277 struct lpfc_sglq *sglq_entry_next = NULL;
6278 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart895427b2017-02-12 13:52:30 -08006279 int status, total_cnt;
6280 int post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006281 int last_xritag = NO_XRI;
6282 LIST_HEAD(prep_sgl_list);
6283 LIST_HEAD(blck_sgl_list);
6284 LIST_HEAD(allc_sgl_list);
6285 LIST_HEAD(post_sgl_list);
6286 LIST_HEAD(free_sgl_list);
6287
James Smart38c20672013-03-01 16:37:44 -05006288 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006289 spin_lock(&phba->sli4_hba.sgl_list_lock);
6290 list_splice_init(sgl_list, &allc_sgl_list);
6291 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006292 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006293
James Smart895427b2017-02-12 13:52:30 -08006294 total_cnt = cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006295 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6296 &allc_sgl_list, list) {
6297 list_del_init(&sglq_entry->list);
6298 block_cnt++;
6299 if ((last_xritag != NO_XRI) &&
6300 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6301 /* a hole in xri block, form a sgl posting block */
6302 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6303 post_cnt = block_cnt - 1;
6304 /* prepare list for next posting block */
6305 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6306 block_cnt = 1;
6307 } else {
6308 /* prepare list for next posting block */
6309 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6310 /* enough sgls for non-embed sgl mbox command */
6311 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6312 list_splice_init(&prep_sgl_list,
6313 &blck_sgl_list);
6314 post_cnt = block_cnt;
6315 block_cnt = 0;
6316 }
6317 }
6318 num_posted++;
6319
6320 /* keep track of last sgl's xritag */
6321 last_xritag = sglq_entry->sli4_xritag;
6322
James Smart895427b2017-02-12 13:52:30 -08006323 /* end of repost sgl list condition for buffers */
6324 if (num_posted == total_cnt) {
James Smart8a9d2e82012-05-09 21:16:12 -04006325 if (post_cnt == 0) {
6326 list_splice_init(&prep_sgl_list,
6327 &blck_sgl_list);
6328 post_cnt = block_cnt;
6329 } else if (block_cnt == 1) {
6330 status = lpfc_sli4_post_sgl(phba,
6331 sglq_entry->phys, 0,
6332 sglq_entry->sli4_xritag);
6333 if (!status) {
6334 /* successful, put sgl to posted list */
6335 list_add_tail(&sglq_entry->list,
6336 &post_sgl_list);
6337 } else {
6338 /* Failure, put sgl to free list */
6339 lpfc_printf_log(phba, KERN_WARNING,
6340 LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006341 "3159 Failed to post "
James Smart8a9d2e82012-05-09 21:16:12 -04006342 "sgl, xritag:x%x\n",
6343 sglq_entry->sli4_xritag);
6344 list_add_tail(&sglq_entry->list,
6345 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006346 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006347 }
6348 }
6349 }
6350
6351 /* continue until a nembed page worth of sgls */
6352 if (post_cnt == 0)
6353 continue;
6354
James Smart895427b2017-02-12 13:52:30 -08006355 /* post the buffer list sgls as a block */
6356 status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
6357 post_cnt);
James Smart8a9d2e82012-05-09 21:16:12 -04006358
6359 if (!status) {
6360 /* success, put sgl list to posted sgl list */
6361 list_splice_init(&blck_sgl_list, &post_sgl_list);
6362 } else {
6363 /* Failure, put sgl list to free sgl list */
6364 sglq_entry_first = list_first_entry(&blck_sgl_list,
6365 struct lpfc_sglq,
6366 list);
6367 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006368 "3160 Failed to post sgl-list, "
James Smart8a9d2e82012-05-09 21:16:12 -04006369 "xritag:x%x-x%x\n",
6370 sglq_entry_first->sli4_xritag,
6371 (sglq_entry_first->sli4_xritag +
6372 post_cnt - 1));
6373 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006374 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006375 }
6376
6377 /* don't reset xirtag due to hole in xri block */
6378 if (block_cnt == 0)
6379 last_xritag = NO_XRI;
6380
James Smart895427b2017-02-12 13:52:30 -08006381 /* reset sgl post count for next round of posting */
James Smart8a9d2e82012-05-09 21:16:12 -04006382 post_cnt = 0;
6383 }
6384
James Smart895427b2017-02-12 13:52:30 -08006385 /* free the sgls failed to post */
James Smart8a9d2e82012-05-09 21:16:12 -04006386 lpfc_free_sgl_list(phba, &free_sgl_list);
6387
James Smart895427b2017-02-12 13:52:30 -08006388 /* push sgls posted to the available list */
James Smart8a9d2e82012-05-09 21:16:12 -04006389 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006390 spin_lock_irq(&phba->hbalock);
James Smart895427b2017-02-12 13:52:30 -08006391 spin_lock(&phba->sli4_hba.sgl_list_lock);
6392 list_splice_init(&post_sgl_list, sgl_list);
6393 spin_unlock(&phba->sli4_hba.sgl_list_lock);
James Smart38c20672013-03-01 16:37:44 -05006394 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006395 } else {
6396 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -08006397 "3161 Failure to post sgl to port.\n");
James Smart8a9d2e82012-05-09 21:16:12 -04006398 return -EIO;
6399 }
James Smart895427b2017-02-12 13:52:30 -08006400
6401 /* return the number of XRIs actually posted */
6402 return total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006403}
6404
James Smart61bda8f2016-10-13 15:06:05 -07006405void
6406lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6407{
6408 uint32_t len;
6409
6410 len = sizeof(struct lpfc_mbx_set_host_data) -
6411 sizeof(struct lpfc_sli4_cfg_mhdr);
6412 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6413 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6414 LPFC_SLI4_MBX_EMBED);
6415
6416 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006417 mbox->u.mqe.un.set_host_data.param_len =
6418 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006419 snprintf(mbox->u.mqe.un.set_host_data.data,
6420 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6421 "Linux %s v"LPFC_DRIVER_VERSION,
6422 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6423}
6424
James Smart8a9d2e82012-05-09 21:16:12 -04006425/**
James Smartda0436e2009-05-22 14:51:39 -04006426 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6427 * @phba: Pointer to HBA context object.
6428 *
6429 * This function is the main SLI4 device intialization PCI function. This
6430 * function is called by the HBA intialization code, HBA reset code and
6431 * HBA error attention handler code. Caller is not required to hold any
6432 * locks.
6433 **/
6434int
6435lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6436{
6437 int rc;
6438 LPFC_MBOXQ_t *mboxq;
6439 struct lpfc_mqe *mqe;
6440 uint8_t *vpd;
6441 uint32_t vpd_size;
6442 uint32_t ftr_rsp = 0;
6443 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6444 struct lpfc_vport *vport = phba->pport;
6445 struct lpfc_dmabuf *mp;
6446
6447 /* Perform a PCI function reset to start from clean */
6448 rc = lpfc_pci_function_reset(phba);
6449 if (unlikely(rc))
6450 return -ENODEV;
6451
6452 /* Check the HBA Host Status Register for readyness */
6453 rc = lpfc_sli4_post_status_check(phba);
6454 if (unlikely(rc))
6455 return -ENODEV;
6456 else {
6457 spin_lock_irq(&phba->hbalock);
6458 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6459 spin_unlock_irq(&phba->hbalock);
6460 }
6461
6462 /*
6463 * Allocate a single mailbox container for initializing the
6464 * port.
6465 */
6466 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6467 if (!mboxq)
6468 return -ENOMEM;
6469
James Smartda0436e2009-05-22 14:51:39 -04006470 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006471 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006472 vpd = kzalloc(vpd_size, GFP_KERNEL);
6473 if (!vpd) {
6474 rc = -ENOMEM;
6475 goto out_free_mbox;
6476 }
6477
6478 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006479 if (unlikely(rc)) {
6480 kfree(vpd);
6481 goto out_free_mbox;
6482 }
James Smart572709e2013-07-15 18:32:43 -04006483
James Smartda0436e2009-05-22 14:51:39 -04006484 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006485 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006486 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006487 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006488 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6489 } else {
James Smart76a95d72010-11-20 23:11:48 -05006490 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006491 }
James Smart45ed1192009-10-02 15:17:02 -04006492
6493 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6494 LPFC_DCBX_CEE_MODE)
6495 phba->hba_flag |= HBA_FIP_SUPPORT;
6496 else
6497 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6498
James Smart4f2e66c2012-05-09 21:17:07 -04006499 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6500
James Smartc31098c2011-04-16 11:03:33 -04006501 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006502 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6503 "0376 READ_REV Error. SLI Level %d "
6504 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006505 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006506 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006507 kfree(vpd);
6508 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006509 }
James Smartcd1c8302011-10-10 21:33:25 -04006510
6511 /*
James Smartff78d8f2011-12-13 13:21:35 -05006512 * Continue initialization with default values even if driver failed
6513 * to read FCoE param config regions, only read parameters if the
6514 * board is FCoE
6515 */
6516 if (phba->hba_flag & HBA_FCOE_MODE &&
6517 lpfc_sli4_read_fcoe_params(phba))
6518 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6519 "2570 Failed to read FCoE parameters\n");
6520
6521 /*
James Smartcd1c8302011-10-10 21:33:25 -04006522 * Retrieve sli4 device physical port name, failure of doing it
6523 * is considered as non-fatal.
6524 */
6525 rc = lpfc_sli4_retrieve_pport_name(phba);
6526 if (!rc)
6527 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6528 "3080 Successful retrieving SLI4 device "
6529 "physical port name: %s.\n", phba->Port);
6530
James Smartda0436e2009-05-22 14:51:39 -04006531 /*
6532 * Evaluate the read rev and vpd data. Populate the driver
6533 * state with the results. If this routine fails, the failure
6534 * is not fatal as the driver will use generic values.
6535 */
6536 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6537 if (unlikely(!rc)) {
6538 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6539 "0377 Error %d parsing vpd. "
6540 "Using defaults.\n", rc);
6541 rc = 0;
6542 }
James Smart76a95d72010-11-20 23:11:48 -05006543 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006544
James Smartf1126682009-06-10 17:22:44 -04006545 /* Save information as VPD data */
6546 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6547 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6548 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6549 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6550 &mqe->un.read_rev);
6551 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6552 &mqe->un.read_rev);
6553 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6554 &mqe->un.read_rev);
6555 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6556 &mqe->un.read_rev);
6557 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6558 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6559 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6560 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6561 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6562 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6563 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6564 "(%d):0380 READ_REV Status x%x "
6565 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6566 mboxq->vport ? mboxq->vport->vpi : 0,
6567 bf_get(lpfc_mqe_status, mqe),
6568 phba->vpd.rev.opFwName,
6569 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6570 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006571
James Smart572709e2013-07-15 18:32:43 -04006572 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6573 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6574 if (phba->pport->cfg_lun_queue_depth > rc) {
6575 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6576 "3362 LUN queue depth changed from %d to %d\n",
6577 phba->pport->cfg_lun_queue_depth, rc);
6578 phba->pport->cfg_lun_queue_depth = rc;
6579 }
6580
James Smart65791f12016-07-06 12:35:56 -07006581 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006582 LPFC_SLI_INTF_IF_TYPE_0) {
6583 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6584 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6585 if (rc == MBX_SUCCESS) {
6586 phba->hba_flag |= HBA_RECOVERABLE_UE;
6587 /* Set 1Sec interval to detect UE */
6588 phba->eratt_poll_interval = 1;
6589 phba->sli4_hba.ue_to_sr = bf_get(
6590 lpfc_mbx_set_feature_UESR,
6591 &mboxq->u.mqe.un.set_feature);
6592 phba->sli4_hba.ue_to_rp = bf_get(
6593 lpfc_mbx_set_feature_UERP,
6594 &mboxq->u.mqe.un.set_feature);
6595 }
6596 }
6597
6598 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6599 /* Enable MDS Diagnostics only if the SLI Port supports it */
6600 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6601 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6602 if (rc != MBX_SUCCESS)
6603 phba->mds_diags_support = 0;
6604 }
James Smart572709e2013-07-15 18:32:43 -04006605
James Smartda0436e2009-05-22 14:51:39 -04006606 /*
6607 * Discover the port's supported feature set and match it against the
6608 * hosts requests.
6609 */
6610 lpfc_request_features(phba, mboxq);
6611 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6612 if (unlikely(rc)) {
6613 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006614 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006615 }
6616
6617 /*
6618 * The port must support FCP initiator mode as this is the
6619 * only mode running in the host.
6620 */
6621 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6622 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6623 "0378 No support for fcpi mode.\n");
6624 ftr_rsp++;
6625 }
James Smartfedd3b72011-02-16 12:39:24 -05006626 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6627 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6628 else
6629 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006630 /*
6631 * If the port cannot support the host's requested features
6632 * then turn off the global config parameters to disable the
6633 * feature in the driver. This is not a fatal error.
6634 */
James Smartbf086112011-08-21 21:48:13 -04006635 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6636 if (phba->cfg_enable_bg) {
6637 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6638 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6639 else
6640 ftr_rsp++;
6641 }
James Smartda0436e2009-05-22 14:51:39 -04006642
6643 if (phba->max_vpi && phba->cfg_enable_npiv &&
6644 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6645 ftr_rsp++;
6646
6647 if (ftr_rsp) {
6648 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6649 "0379 Feature Mismatch Data: x%08x %08x "
6650 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6651 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6652 phba->cfg_enable_npiv, phba->max_vpi);
6653 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6654 phba->cfg_enable_bg = 0;
6655 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6656 phba->cfg_enable_npiv = 0;
6657 }
6658
6659 /* These SLI3 features are assumed in SLI4 */
6660 spin_lock_irq(&phba->hbalock);
6661 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6662 spin_unlock_irq(&phba->hbalock);
6663
James Smart6d368e52011-05-24 11:44:12 -04006664 /*
6665 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6666 * calls depends on these resources to complete port setup.
6667 */
6668 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6669 if (rc) {
6670 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6671 "2920 Failed to alloc Resource IDs "
6672 "rc = x%x\n", rc);
6673 goto out_free_mbox;
6674 }
6675
James Smart61bda8f2016-10-13 15:06:05 -07006676 lpfc_set_host_data(phba, mboxq);
6677
6678 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6679 if (rc) {
6680 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6681 "2134 Failed to set host os driver version %x",
6682 rc);
6683 }
6684
James Smartda0436e2009-05-22 14:51:39 -04006685 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006686 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6687 if (rc) {
6688 phba->link_state = LPFC_HBA_ERROR;
6689 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006690 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006691 }
6692
James Smartda0436e2009-05-22 14:51:39 -04006693 mboxq->vport = vport;
6694 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6695 mp = (struct lpfc_dmabuf *) mboxq->context1;
6696 if (rc == MBX_SUCCESS) {
6697 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6698 rc = 0;
6699 }
6700
6701 /*
6702 * This memory was allocated by the lpfc_read_sparam routine. Release
6703 * it to the mbuf pool.
6704 */
6705 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6706 kfree(mp);
6707 mboxq->context1 = NULL;
6708 if (unlikely(rc)) {
6709 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6710 "0382 READ_SPARAM command failed "
6711 "status %d, mbxStatus x%x\n",
6712 rc, bf_get(lpfc_mqe_status, mqe));
6713 phba->link_state = LPFC_HBA_ERROR;
6714 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006715 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006716 }
6717
James Smart05580562011-05-24 11:40:48 -04006718 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006719
6720 /* Update the fc_host data structures with new wwn. */
6721 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6722 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6723
James Smart895427b2017-02-12 13:52:30 -08006724 /* Create all the SLI4 queues */
6725 rc = lpfc_sli4_queue_create(phba);
6726 if (rc) {
6727 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6728 "3089 Failed to allocate queues\n");
6729 rc = -ENODEV;
6730 goto out_free_mbox;
6731 }
6732 /* Set up all the queues to the device */
6733 rc = lpfc_sli4_queue_setup(phba);
6734 if (unlikely(rc)) {
6735 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6736 "0381 Error %d during queue setup.\n ", rc);
6737 goto out_stop_timers;
6738 }
6739 /* Initialize the driver internal SLI layer lists. */
6740 lpfc_sli4_setup(phba);
6741 lpfc_sli4_queue_init(phba);
6742
6743 /* update host els xri-sgl sizes and mappings */
6744 rc = lpfc_sli4_els_sgl_update(phba);
James Smart8a9d2e82012-05-09 21:16:12 -04006745 if (unlikely(rc)) {
6746 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6747 "1400 Failed to update xri-sgl size and "
6748 "mapping: %d\n", rc);
James Smart895427b2017-02-12 13:52:30 -08006749 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006750 }
6751
James Smart8a9d2e82012-05-09 21:16:12 -04006752 /* register the els sgl pool to the port */
James Smart895427b2017-02-12 13:52:30 -08006753 rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
6754 phba->sli4_hba.els_xri_cnt);
6755 if (unlikely(rc < 0)) {
James Smart8a9d2e82012-05-09 21:16:12 -04006756 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6757 "0582 Error %d during els sgl post "
6758 "operation\n", rc);
6759 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08006760 goto out_destroy_queue;
6761 }
6762 phba->sli4_hba.els_xri_cnt = rc;
6763
6764 if (phba->nvmet_support == 0) {
6765 /* update host scsi xri-sgl sizes and mappings */
6766 rc = lpfc_sli4_scsi_sgl_update(phba);
6767 if (unlikely(rc)) {
6768 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6769 "6309 Failed to update scsi-sgl size "
6770 "and mapping: %d\n", rc);
6771 goto out_destroy_queue;
6772 }
6773
6774 /* update host nvme xri-sgl sizes and mappings */
6775 rc = lpfc_sli4_nvme_sgl_update(phba);
6776 if (unlikely(rc)) {
6777 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6778 "6082 Failed to update nvme-sgl size "
6779 "and mapping: %d\n", rc);
6780 goto out_destroy_queue;
6781 }
James Smart8a9d2e82012-05-09 21:16:12 -04006782 }
6783
James Smart895427b2017-02-12 13:52:30 -08006784 if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
6785 /* register the allocated scsi sgl pool to the port */
6786 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6787 if (unlikely(rc)) {
6788 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6789 "0383 Error %d during scsi sgl post "
6790 "operation\n", rc);
6791 /* Some Scsi buffers were moved to abort scsi list */
6792 /* A pci function reset will repost them */
6793 rc = -ENODEV;
6794 goto out_destroy_queue;
6795 }
James Smartda0436e2009-05-22 14:51:39 -04006796 }
6797
6798 /* Post the rpi header region to the device. */
6799 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6800 if (unlikely(rc)) {
6801 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6802 "0393 Error %d during rpi post operation\n",
6803 rc);
6804 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08006805 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006806 }
James Smart97f2ecf2012-03-01 22:35:23 -05006807 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006808
James Smart895427b2017-02-12 13:52:30 -08006809 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6810 if (phba->nvmet_support == 0) {
6811 /*
6812 * The FC Port needs to register FCFI (index 0)
6813 */
6814 lpfc_reg_fcfi(phba, mboxq);
6815 mboxq->vport = phba->pport;
6816 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6817 if (rc != MBX_SUCCESS)
6818 goto out_unset_queue;
6819 rc = 0;
6820 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6821 &mboxq->u.mqe.un.reg_fcfi);
6822 }
6823 /* Check if the port is configured to be disabled */
6824 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04006825 }
6826
6827 /* Arm the CQs and then EQs on device */
6828 lpfc_sli4_arm_cqeq_intr(phba);
6829
6830 /* Indicate device interrupt mode */
6831 phba->sli4_hba.intr_enable = 1;
6832
6833 /* Allow asynchronous mailbox command to go through */
6834 spin_lock_irq(&phba->hbalock);
6835 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6836 spin_unlock_irq(&phba->hbalock);
6837
6838 /* Post receive buffers to the device */
6839 lpfc_sli4_rb_setup(phba);
6840
James Smartfc2b9892010-02-26 14:15:29 -05006841 /* Reset HBA FCF states after HBA reset */
6842 phba->fcf.fcf_flag = 0;
6843 phba->fcf.current_rec.flag = 0;
6844
James Smartda0436e2009-05-22 14:51:39 -04006845 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006846 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006847 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04006848
6849 /* Start heart beat timer */
6850 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006851 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006852 phba->hb_outstanding = 0;
6853 phba->last_completion_time = jiffies;
6854
6855 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04006856 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07006857 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04006858
James Smart75baf692010-06-08 18:31:21 -04006859 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6860 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6861 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6862 if (!rc) {
6863 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6864 "2829 This device supports "
6865 "Advanced Error Reporting (AER)\n");
6866 spin_lock_irq(&phba->hbalock);
6867 phba->hba_flag |= HBA_AER_ENABLED;
6868 spin_unlock_irq(&phba->hbalock);
6869 } else {
6870 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6871 "2830 This device does not support "
6872 "Advanced Error Reporting (AER)\n");
6873 phba->cfg_aer_support = 0;
6874 }
James Smart0a96e972011-07-22 18:37:28 -04006875 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006876 }
6877
James Smartda0436e2009-05-22 14:51:39 -04006878 /*
6879 * The port is ready, set the host's link state to LINK_DOWN
6880 * in preparation for link interrupts.
6881 */
James Smartda0436e2009-05-22 14:51:39 -04006882 spin_lock_irq(&phba->hbalock);
6883 phba->link_state = LPFC_LINK_DOWN;
6884 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006885 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6886 (phba->hba_flag & LINK_DISABLED)) {
6887 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6888 "3103 Adapter Link is disabled.\n");
6889 lpfc_down_link(phba, mboxq);
6890 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6891 if (rc != MBX_SUCCESS) {
6892 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6893 "3104 Adapter failed to issue "
6894 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6895 goto out_unset_queue;
6896 }
6897 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006898 /* don't perform init_link on SLI4 FC port loopback test */
6899 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6900 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6901 if (rc)
6902 goto out_unset_queue;
6903 }
James Smart5350d872011-10-10 21:33:49 -04006904 }
6905 mempool_free(mboxq, phba->mbox_mem_pool);
6906 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006907out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006908 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006909 lpfc_sli4_queue_unset(phba);
6910out_destroy_queue:
6911 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006912out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006913 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006914out_free_mbox:
6915 mempool_free(mboxq, phba->mbox_mem_pool);
6916 return rc;
6917}
James Smarte59058c2008-08-24 21:49:00 -04006918
6919/**
James Smart3621a712009-04-06 18:47:14 -04006920 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006921 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006922 *
James Smarte59058c2008-08-24 21:49:00 -04006923 * This is the callback function for mailbox timer. The mailbox
6924 * timer is armed when a new mailbox command is issued and the timer
6925 * is deleted when the mailbox complete. The function is called by
6926 * the kernel timer code when a mailbox does not complete within
6927 * expected time. This function wakes up the worker thread to
6928 * process the mailbox timeout and returns. All the processing is
6929 * done by the worker thread function lpfc_mbox_timeout_handler.
6930 **/
dea31012005-04-17 16:05:31 -05006931void
6932lpfc_mbox_timeout(unsigned long ptr)
6933{
James Smart92d7f7b2007-06-17 19:56:38 -05006934 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006935 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006936 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006937
James Smart2e0fef82007-06-17 19:56:36 -05006938 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006939 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006940 if (!tmo_posted)
6941 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6942 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6943
James Smart5e9d9b82008-06-14 22:52:53 -04006944 if (!tmo_posted)
6945 lpfc_worker_wake_up(phba);
6946 return;
dea31012005-04-17 16:05:31 -05006947}
6948
James Smarte8d3c3b2013-10-10 12:21:30 -04006949/**
6950 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6951 * are pending
6952 * @phba: Pointer to HBA context object.
6953 *
6954 * This function checks if any mailbox completions are present on the mailbox
6955 * completion queue.
6956 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04006957static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04006958lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6959{
6960
6961 uint32_t idx;
6962 struct lpfc_queue *mcq;
6963 struct lpfc_mcqe *mcqe;
6964 bool pending_completions = false;
6965
6966 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6967 return false;
6968
6969 /* Check for completions on mailbox completion queue */
6970
6971 mcq = phba->sli4_hba.mbx_cq;
6972 idx = mcq->hba_index;
6973 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6974 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6975 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6976 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6977 pending_completions = true;
6978 break;
6979 }
6980 idx = (idx + 1) % mcq->entry_count;
6981 if (mcq->hba_index == idx)
6982 break;
6983 }
6984 return pending_completions;
6985
6986}
6987
6988/**
6989 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6990 * that were missed.
6991 * @phba: Pointer to HBA context object.
6992 *
6993 * For sli4, it is possible to miss an interrupt. As such mbox completions
6994 * maybe missed causing erroneous mailbox timeouts to occur. This function
6995 * checks to see if mbox completions are on the mailbox completion queue
6996 * and will process all the completions associated with the eq for the
6997 * mailbox completion queue.
6998 **/
6999bool
7000lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7001{
7002
7003 uint32_t eqidx;
7004 struct lpfc_queue *fpeq = NULL;
7005 struct lpfc_eqe *eqe;
7006 bool mbox_pending;
7007
7008 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7009 return false;
7010
7011 /* Find the eq associated with the mcq */
7012
7013 if (phba->sli4_hba.hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007014 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smarte8d3c3b2013-10-10 12:21:30 -04007015 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7016 phba->sli4_hba.mbx_cq->assoc_qid) {
7017 fpeq = phba->sli4_hba.hba_eq[eqidx];
7018 break;
7019 }
7020 if (!fpeq)
7021 return false;
7022
7023 /* Turn off interrupts from this EQ */
7024
7025 lpfc_sli4_eq_clr_intr(fpeq);
7026
7027 /* Check to see if a mbox completion is pending */
7028
7029 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7030
7031 /*
7032 * If a mbox completion is pending, process all the events on EQ
7033 * associated with the mbox completion queue (this could include
7034 * mailbox commands, async events, els commands, receive queue data
7035 * and fcp commands)
7036 */
7037
7038 if (mbox_pending)
7039 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7040 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7041 fpeq->EQ_processed++;
7042 }
7043
7044 /* Always clear and re-arm the EQ */
7045
7046 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7047
7048 return mbox_pending;
7049
7050}
James Smarte59058c2008-08-24 21:49:00 -04007051
7052/**
James Smart3621a712009-04-06 18:47:14 -04007053 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007054 * @phba: Pointer to HBA context object.
7055 *
7056 * This function is called from worker thread when a mailbox command times out.
7057 * The caller is not required to hold any locks. This function will reset the
7058 * HBA and recover all the pending commands.
7059 **/
dea31012005-04-17 16:05:31 -05007060void
7061lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7062{
James Smart2e0fef82007-06-17 19:56:36 -05007063 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007064 MAILBOX_t *mb = NULL;
7065
James Smart1dcb58e2007-04-25 09:51:30 -04007066 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007067
James Smarte8d3c3b2013-10-10 12:21:30 -04007068 /* If the mailbox completed, process the completion and return */
7069 if (lpfc_sli4_process_missed_mbox_completions(phba))
7070 return;
7071
James Smarteb016562014-09-03 12:58:06 -04007072 if (pmbox != NULL)
7073 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007074 /* Check the pmbox pointer first. There is a race condition
7075 * between the mbox timeout handler getting executed in the
7076 * worklist and the mailbox actually completing. When this
7077 * race condition occurs, the mbox_active will be NULL.
7078 */
7079 spin_lock_irq(&phba->hbalock);
7080 if (pmbox == NULL) {
7081 lpfc_printf_log(phba, KERN_WARNING,
7082 LOG_MBOX | LOG_SLI,
7083 "0353 Active Mailbox cleared - mailbox timeout "
7084 "exiting\n");
7085 spin_unlock_irq(&phba->hbalock);
7086 return;
7087 }
7088
dea31012005-04-17 16:05:31 -05007089 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007090 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007091 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007092 mb->mbxCommand,
7093 phba->pport->port_state,
7094 phba->sli.sli_flag,
7095 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007096 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007097
James Smart1dcb58e2007-04-25 09:51:30 -04007098 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7099 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007100 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007101 */
James Smart2e0fef82007-06-17 19:56:36 -05007102 spin_lock_irq(&phba->pport->work_port_lock);
7103 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7104 spin_unlock_irq(&phba->pport->work_port_lock);
7105 spin_lock_irq(&phba->hbalock);
7106 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007107 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007108 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007109
James Smartdb55fba2014-04-04 13:52:02 -04007110 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007111
7112 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007113 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007114
7115 /* Reset the HBA device */
7116 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007117}
7118
James Smarte59058c2008-08-24 21:49:00 -04007119/**
James Smart3772a992009-05-22 14:50:54 -04007120 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007121 * @phba: Pointer to HBA context object.
7122 * @pmbox: Pointer to mailbox object.
7123 * @flag: Flag indicating how the mailbox need to be processed.
7124 *
7125 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007126 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7127 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007128 * The mailbox command can be submitted in polling mode, in which case
7129 * this function will wait in a polling loop for the completion of the
7130 * mailbox.
7131 * If the mailbox is submitted in no_wait mode (not polling) the
7132 * function will submit the command and returns immediately without waiting
7133 * for the mailbox completion. The no_wait is supported only when HBA
7134 * is in SLI2/SLI3 mode - interrupts are enabled.
7135 * The SLI interface allows only one mailbox pending at a time. If the
7136 * mailbox is issued in polling mode and there is already a mailbox
7137 * pending, then the function will return an error. If the mailbox is issued
7138 * in NO_WAIT mode and there is a mailbox pending already, the function
7139 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7140 * The sli layer owns the mailbox object until the completion of mailbox
7141 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7142 * return codes the caller owns the mailbox command after the return of
7143 * the function.
7144 **/
James Smart3772a992009-05-22 14:50:54 -04007145static int
7146lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7147 uint32_t flag)
dea31012005-04-17 16:05:31 -05007148{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007149 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007150 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007151 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007152 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007153 int i;
James Smart09372822008-01-11 01:52:54 -05007154 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007155 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007156 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007157 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007158 int processing_queue = 0;
7159
7160 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7161 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007162 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007163 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007164 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7165 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7166 return MBX_SUCCESS;
7167 }
James Smart58da1ff2008-04-07 10:15:56 -04007168 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007169 pmbox = lpfc_mbox_get(phba);
7170 if (!pmbox) {
7171 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7172 return MBX_SUCCESS;
7173 }
7174 }
dea31012005-04-17 16:05:31 -05007175
James Smarted957682007-06-17 19:56:37 -05007176 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007177 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007178 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007179 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007180 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007181 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007182 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007183 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007184 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007185 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007186 }
7187 }
7188
Linas Vepstas8d63f372007-02-14 14:28:36 -06007189 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007190 if (unlikely(pci_channel_offline(phba->pcidev))) {
7191 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7192 goto out_not_finished;
7193 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007194
James Smarta257bf92009-04-06 18:48:10 -04007195 /* If HBA has a deferred error attention, fail the iocb. */
7196 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7197 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7198 goto out_not_finished;
7199 }
7200
dea31012005-04-17 16:05:31 -05007201 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007202
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007203 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007204 status = MBX_SUCCESS;
7205
James Smart2e0fef82007-06-17 19:56:36 -05007206 if (phba->link_state == LPFC_HBA_ERROR) {
7207 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007208
7209 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007210 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7211 "(%d):0311 Mailbox command x%x cannot "
7212 "issue Data: x%x x%x\n",
7213 pmbox->vport ? pmbox->vport->vpi : 0,
7214 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007215 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007216 }
7217
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007218 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007219 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7220 !(hc_copy & HC_MBINT_ENA)) {
7221 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7222 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007223 "(%d):2528 Mailbox command x%x cannot "
7224 "issue Data: x%x x%x\n",
7225 pmbox->vport ? pmbox->vport->vpi : 0,
7226 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007227 goto out_not_finished;
7228 }
James Smart92908312006-03-07 15:04:13 -05007229 }
7230
dea31012005-04-17 16:05:31 -05007231 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7232 /* Polling for a mbox command when another one is already active
7233 * is not allowed in SLI. Also, the driver must have established
7234 * SLI2 mode to queue and process multiple mbox commands.
7235 */
7236
7237 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007238 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007239
7240 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007241 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7242 "(%d):2529 Mailbox command x%x "
7243 "cannot issue Data: x%x x%x\n",
7244 pmbox->vport ? pmbox->vport->vpi : 0,
7245 pmbox->u.mb.mbxCommand,
7246 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007247 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007248 }
7249
James Smart3772a992009-05-22 14:50:54 -04007250 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007251 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007252 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007253 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7254 "(%d):2530 Mailbox command x%x "
7255 "cannot issue Data: x%x x%x\n",
7256 pmbox->vport ? pmbox->vport->vpi : 0,
7257 pmbox->u.mb.mbxCommand,
7258 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007259 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007260 }
7261
dea31012005-04-17 16:05:31 -05007262 /* Another mailbox command is still being processed, queue this
7263 * command to be processed later.
7264 */
7265 lpfc_mbox_put(phba, pmbox);
7266
7267 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007268 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007269 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007270 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007271 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007272 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007273 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007274
7275 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007276 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007277
James Smart858c9f62007-06-17 19:56:39 -05007278 if (pmbox->vport) {
7279 lpfc_debugfs_disc_trc(pmbox->vport,
7280 LPFC_DISC_TRC_MBOX_VPORT,
7281 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007282 (uint32_t)mbx->mbxCommand,
7283 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007284 }
7285 else {
7286 lpfc_debugfs_disc_trc(phba->pport,
7287 LPFC_DISC_TRC_MBOX,
7288 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007289 (uint32_t)mbx->mbxCommand,
7290 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007291 }
7292
James Smart2e0fef82007-06-17 19:56:36 -05007293 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007294 }
7295
dea31012005-04-17 16:05:31 -05007296 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7297
7298 /* If we are not polling, we MUST be in SLI2 mode */
7299 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007300 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007301 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007302 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007303 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007304 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007305 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7306 "(%d):2531 Mailbox command x%x "
7307 "cannot issue Data: x%x x%x\n",
7308 pmbox->vport ? pmbox->vport->vpi : 0,
7309 pmbox->u.mb.mbxCommand,
7310 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007311 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007312 }
7313 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007314 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7315 1000);
7316 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007317 }
7318
7319 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007320 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007321 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007322 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007323 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007324 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007325 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007326
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007327 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007328 if (pmbox->vport) {
7329 lpfc_debugfs_disc_trc(pmbox->vport,
7330 LPFC_DISC_TRC_MBOX_VPORT,
7331 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007332 (uint32_t)mbx->mbxCommand,
7333 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007334 }
7335 else {
7336 lpfc_debugfs_disc_trc(phba->pport,
7337 LPFC_DISC_TRC_MBOX,
7338 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007339 (uint32_t)mbx->mbxCommand,
7340 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007341 }
7342 }
7343
dea31012005-04-17 16:05:31 -05007344 psli->slistat.mbox_cmd++;
7345 evtctr = psli->slistat.mbox_event;
7346
7347 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007348 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007349
James Smart3772a992009-05-22 14:50:54 -04007350 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007351 /* Populate mbox extension offset word. */
7352 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007353 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007354 = (uint8_t *)phba->mbox_ext
7355 - (uint8_t *)phba->mbox;
7356 }
7357
7358 /* Copy the mailbox extension data */
7359 if (pmbox->in_ext_byte_len && pmbox->context2) {
7360 lpfc_sli_pcimem_bcopy(pmbox->context2,
7361 (uint8_t *)phba->mbox_ext,
7362 pmbox->in_ext_byte_len);
7363 }
7364 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007365 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007366 } else {
James Smart7a470272010-03-15 11:25:20 -04007367 /* Populate mbox extension offset word. */
7368 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007369 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007370 = MAILBOX_HBA_EXT_OFFSET;
7371
7372 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007373 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007374 lpfc_memcpy_to_slim(phba->MBslimaddr +
7375 MAILBOX_HBA_EXT_OFFSET,
7376 pmbox->context2, pmbox->in_ext_byte_len);
7377
James Smart895427b2017-02-12 13:52:30 -08007378 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007379 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007380 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7381 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007382
7383 /* First copy mbox command data to HBA SLIM, skip past first
7384 word */
7385 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007386 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007387 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7388
7389 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007390 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007391 to_slim = phba->MBslimaddr;
7392 writel(ldata, to_slim);
7393 readl(to_slim); /* flush */
7394
James Smart895427b2017-02-12 13:52:30 -08007395 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007396 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007397 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007398 }
7399
7400 wmb();
dea31012005-04-17 16:05:31 -05007401
7402 switch (flag) {
7403 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007404 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007405 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007406 /* Interrupt board to do it */
7407 writel(CA_MBATT, phba->CAregaddr);
7408 readl(phba->CAregaddr); /* flush */
7409 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007410 break;
7411
7412 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007413 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007414 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007415 /* Interrupt board to do it */
7416 writel(CA_MBATT, phba->CAregaddr);
7417 readl(phba->CAregaddr); /* flush */
7418
James Smart3772a992009-05-22 14:50:54 -04007419 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007420 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007421 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007422 word0 = le32_to_cpu(word0);
7423 } else {
7424 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007425 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7426 spin_unlock_irqrestore(&phba->hbalock,
7427 drvr_flag);
7428 goto out_not_finished;
7429 }
dea31012005-04-17 16:05:31 -05007430 }
7431
7432 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007433 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7434 spin_unlock_irqrestore(&phba->hbalock,
7435 drvr_flag);
7436 goto out_not_finished;
7437 }
James Smarta183a152011-10-10 21:32:43 -04007438 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7439 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007440 i = 0;
dea31012005-04-17 16:05:31 -05007441 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007442 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7443 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007444 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007445 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007446 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007447 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007448 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007449 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007450 }
7451
7452 /* Check if we took a mbox interrupt while we were
7453 polling */
7454 if (((word0 & OWN_CHIP) != OWN_CHIP)
7455 && (evtctr != psli->slistat.mbox_event))
7456 break;
7457
James Smart09372822008-01-11 01:52:54 -05007458 if (i++ > 10) {
7459 spin_unlock_irqrestore(&phba->hbalock,
7460 drvr_flag);
7461 msleep(1);
7462 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7463 }
dea31012005-04-17 16:05:31 -05007464
James Smart3772a992009-05-22 14:50:54 -04007465 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007466 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007467 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007468 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007469 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007470 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007471 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007472 /* Check real SLIM for any errors */
7473 slimword0 = readl(phba->MBslimaddr);
7474 slimmb = (MAILBOX_t *) & slimword0;
7475 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7476 && slimmb->mbxStatus) {
7477 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007478 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007479 word0 = slimword0;
7480 }
7481 }
7482 } else {
7483 /* First copy command data */
7484 word0 = readl(phba->MBslimaddr);
7485 }
7486 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007487 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7488 spin_unlock_irqrestore(&phba->hbalock,
7489 drvr_flag);
7490 goto out_not_finished;
7491 }
dea31012005-04-17 16:05:31 -05007492 }
7493
James Smart3772a992009-05-22 14:50:54 -04007494 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007495 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007496 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7497 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007498 /* Copy the mailbox extension data */
7499 if (pmbox->out_ext_byte_len && pmbox->context2) {
7500 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7501 pmbox->context2,
7502 pmbox->out_ext_byte_len);
7503 }
dea31012005-04-17 16:05:31 -05007504 } else {
7505 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007506 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007507 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007508 /* Copy the mailbox extension data */
7509 if (pmbox->out_ext_byte_len && pmbox->context2) {
7510 lpfc_memcpy_from_slim(pmbox->context2,
7511 phba->MBslimaddr +
7512 MAILBOX_HBA_EXT_OFFSET,
7513 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007514 }
7515 }
7516
7517 writel(HA_MBATT, phba->HAregaddr);
7518 readl(phba->HAregaddr); /* flush */
7519
7520 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007521 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007522 }
7523
James Smart2e0fef82007-06-17 19:56:36 -05007524 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7525 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007526
7527out_not_finished:
7528 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007529 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007530 lpfc_mbox_cmpl_put(phba, pmbox);
7531 }
7532 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007533}
7534
James Smarte59058c2008-08-24 21:49:00 -04007535/**
James Smartf1126682009-06-10 17:22:44 -04007536 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7537 * @phba: Pointer to HBA context object.
7538 *
7539 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7540 * the driver internal pending mailbox queue. It will then try to wait out the
7541 * possible outstanding mailbox command before return.
7542 *
7543 * Returns:
7544 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7545 * the outstanding mailbox command timed out.
7546 **/
7547static int
7548lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7549{
7550 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007551 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007552 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007553
7554 /* Mark the asynchronous mailbox command posting as blocked */
7555 spin_lock_irq(&phba->hbalock);
7556 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007557 /* Determine how long we might wait for the active mailbox
7558 * command to be gracefully completed by firmware.
7559 */
James Smarta183a152011-10-10 21:32:43 -04007560 if (phba->sli.mbox_active)
7561 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7562 phba->sli.mbox_active) *
7563 1000) + jiffies;
7564 spin_unlock_irq(&phba->hbalock);
7565
James Smarte8d3c3b2013-10-10 12:21:30 -04007566 /* Make sure the mailbox is really active */
7567 if (timeout)
7568 lpfc_sli4_process_missed_mbox_completions(phba);
7569
James Smartf1126682009-06-10 17:22:44 -04007570 /* Wait for the outstnading mailbox command to complete */
7571 while (phba->sli.mbox_active) {
7572 /* Check active mailbox complete status every 2ms */
7573 msleep(2);
7574 if (time_after(jiffies, timeout)) {
7575 /* Timeout, marked the outstanding cmd not complete */
7576 rc = 1;
7577 break;
7578 }
7579 }
7580
7581 /* Can not cleanly block async mailbox command, fails it */
7582 if (rc) {
7583 spin_lock_irq(&phba->hbalock);
7584 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7585 spin_unlock_irq(&phba->hbalock);
7586 }
7587 return rc;
7588}
7589
7590/**
7591 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7592 * @phba: Pointer to HBA context object.
7593 *
7594 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7595 * commands from the driver internal pending mailbox queue. It makes sure
7596 * that there is no outstanding mailbox command before resuming posting
7597 * asynchronous mailbox commands. If, for any reason, there is outstanding
7598 * mailbox command, it will try to wait it out before resuming asynchronous
7599 * mailbox command posting.
7600 **/
7601static void
7602lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7603{
7604 struct lpfc_sli *psli = &phba->sli;
7605
7606 spin_lock_irq(&phba->hbalock);
7607 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7608 /* Asynchronous mailbox posting is not blocked, do nothing */
7609 spin_unlock_irq(&phba->hbalock);
7610 return;
7611 }
7612
7613 /* Outstanding synchronous mailbox command is guaranteed to be done,
7614 * successful or timeout, after timing-out the outstanding mailbox
7615 * command shall always be removed, so just unblock posting async
7616 * mailbox command and resume
7617 */
7618 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7619 spin_unlock_irq(&phba->hbalock);
7620
7621 /* wake up worker thread to post asynchronlous mailbox command */
7622 lpfc_worker_wake_up(phba);
7623}
7624
7625/**
James Smart2d843ed2012-09-29 11:29:06 -04007626 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7627 * @phba: Pointer to HBA context object.
7628 * @mboxq: Pointer to mailbox object.
7629 *
7630 * The function waits for the bootstrap mailbox register ready bit from
7631 * port for twice the regular mailbox command timeout value.
7632 *
7633 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7634 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7635 **/
7636static int
7637lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7638{
7639 uint32_t db_ready;
7640 unsigned long timeout;
7641 struct lpfc_register bmbx_reg;
7642
7643 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7644 * 1000) + jiffies;
7645
7646 do {
7647 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7648 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7649 if (!db_ready)
7650 msleep(2);
7651
7652 if (time_after(jiffies, timeout))
7653 return MBXERR_ERROR;
7654 } while (!db_ready);
7655
7656 return 0;
7657}
7658
7659/**
James Smartda0436e2009-05-22 14:51:39 -04007660 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7661 * @phba: Pointer to HBA context object.
7662 * @mboxq: Pointer to mailbox object.
7663 *
7664 * The function posts a mailbox to the port. The mailbox is expected
7665 * to be comletely filled in and ready for the port to operate on it.
7666 * This routine executes a synchronous completion operation on the
7667 * mailbox by polling for its completion.
7668 *
7669 * The caller must not be holding any locks when calling this routine.
7670 *
7671 * Returns:
7672 * MBX_SUCCESS - mailbox posted successfully
7673 * Any of the MBX error values.
7674 **/
7675static int
7676lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7677{
7678 int rc = MBX_SUCCESS;
7679 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007680 uint32_t mcqe_status;
7681 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007682 struct lpfc_sli *psli = &phba->sli;
7683 struct lpfc_mqe *mb = &mboxq->u.mqe;
7684 struct lpfc_bmbx_create *mbox_rgn;
7685 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007686
7687 /*
7688 * Only one mailbox can be active to the bootstrap mailbox region
7689 * at a time and there is no queueing provided.
7690 */
7691 spin_lock_irqsave(&phba->hbalock, iflag);
7692 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7693 spin_unlock_irqrestore(&phba->hbalock, iflag);
7694 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007695 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007696 "cannot issue Data: x%x x%x\n",
7697 mboxq->vport ? mboxq->vport->vpi : 0,
7698 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007699 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7700 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007701 psli->sli_flag, MBX_POLL);
7702 return MBXERR_ERROR;
7703 }
7704 /* The server grabs the token and owns it until release */
7705 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7706 phba->sli.mbox_active = mboxq;
7707 spin_unlock_irqrestore(&phba->hbalock, iflag);
7708
James Smart2d843ed2012-09-29 11:29:06 -04007709 /* wait for bootstrap mbox register for readyness */
7710 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7711 if (rc)
7712 goto exit;
7713
James Smartda0436e2009-05-22 14:51:39 -04007714 /*
7715 * Initialize the bootstrap memory region to avoid stale data areas
7716 * in the mailbox post. Then copy the caller's mailbox contents to
7717 * the bmbx mailbox region.
7718 */
7719 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7720 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7721 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7722 sizeof(struct lpfc_mqe));
7723
7724 /* Post the high mailbox dma address to the port and wait for ready. */
7725 dma_address = &phba->sli4_hba.bmbx.dma_address;
7726 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7727
James Smart2d843ed2012-09-29 11:29:06 -04007728 /* wait for bootstrap mbox register for hi-address write done */
7729 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7730 if (rc)
7731 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007732
7733 /* Post the low mailbox dma address to the port. */
7734 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007735
James Smart2d843ed2012-09-29 11:29:06 -04007736 /* wait for bootstrap mbox register for low address write done */
7737 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7738 if (rc)
7739 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007740
7741 /*
7742 * Read the CQ to ensure the mailbox has completed.
7743 * If so, update the mailbox status so that the upper layers
7744 * can complete the request normally.
7745 */
7746 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7747 sizeof(struct lpfc_mqe));
7748 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7749 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7750 sizeof(struct lpfc_mcqe));
7751 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007752 /*
7753 * When the CQE status indicates a failure and the mailbox status
7754 * indicates success then copy the CQE status into the mailbox status
7755 * (and prefix it with x4000).
7756 */
James Smartda0436e2009-05-22 14:51:39 -04007757 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007758 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7759 bf_set(lpfc_mqe_status, mb,
7760 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007761 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007762 } else
7763 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007764
7765 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007766 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007767 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7768 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007769 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7770 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7771 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007772 bf_get(lpfc_mqe_status, mb),
7773 mb->un.mb_words[0], mb->un.mb_words[1],
7774 mb->un.mb_words[2], mb->un.mb_words[3],
7775 mb->un.mb_words[4], mb->un.mb_words[5],
7776 mb->un.mb_words[6], mb->un.mb_words[7],
7777 mb->un.mb_words[8], mb->un.mb_words[9],
7778 mb->un.mb_words[10], mb->un.mb_words[11],
7779 mb->un.mb_words[12], mboxq->mcqe.word0,
7780 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7781 mboxq->mcqe.trailer);
7782exit:
7783 /* We are holding the token, no needed for lock when release */
7784 spin_lock_irqsave(&phba->hbalock, iflag);
7785 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7786 phba->sli.mbox_active = NULL;
7787 spin_unlock_irqrestore(&phba->hbalock, iflag);
7788 return rc;
7789}
7790
7791/**
7792 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7793 * @phba: Pointer to HBA context object.
7794 * @pmbox: Pointer to mailbox object.
7795 * @flag: Flag indicating how the mailbox need to be processed.
7796 *
7797 * This function is called by discovery code and HBA management code to submit
7798 * a mailbox command to firmware with SLI-4 interface spec.
7799 *
7800 * Return codes the caller owns the mailbox command after the return of the
7801 * function.
7802 **/
7803static int
7804lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7805 uint32_t flag)
7806{
7807 struct lpfc_sli *psli = &phba->sli;
7808 unsigned long iflags;
7809 int rc;
7810
James Smartb76f2dc2011-07-22 18:37:42 -04007811 /* dump from issue mailbox command if setup */
7812 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7813
James Smart8fa38512009-07-19 10:01:03 -04007814 rc = lpfc_mbox_dev_check(phba);
7815 if (unlikely(rc)) {
7816 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007817 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007818 "cannot issue Data: x%x x%x\n",
7819 mboxq->vport ? mboxq->vport->vpi : 0,
7820 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007821 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7822 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007823 psli->sli_flag, flag);
7824 goto out_not_finished;
7825 }
7826
James Smartda0436e2009-05-22 14:51:39 -04007827 /* Detect polling mode and jump to a handler */
7828 if (!phba->sli4_hba.intr_enable) {
7829 if (flag == MBX_POLL)
7830 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7831 else
7832 rc = -EIO;
7833 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007834 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007835 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007836 "(x%x/x%x) failure: "
7837 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7838 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007839 mboxq->vport ? mboxq->vport->vpi : 0,
7840 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007841 lpfc_sli_config_mbox_subsys_get(phba,
7842 mboxq),
7843 lpfc_sli_config_mbox_opcode_get(phba,
7844 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007845 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7846 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7847 bf_get(lpfc_mcqe_ext_status,
7848 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007849 psli->sli_flag, flag);
7850 return rc;
7851 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007852 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7853 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007854 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007855 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007856 mboxq->vport ? mboxq->vport->vpi : 0,
7857 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007858 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7859 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007860 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007861 /* Try to block the asynchronous mailbox posting */
7862 rc = lpfc_sli4_async_mbox_block(phba);
7863 if (!rc) {
7864 /* Successfully blocked, now issue sync mbox cmd */
7865 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7866 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007867 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007868 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007869 "(%d):2597 Sync Mailbox command "
7870 "x%x (x%x/x%x) failure: "
7871 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7872 "Data: x%x x%x\n,",
7873 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007874 mboxq->u.mb.mbxCommand,
7875 lpfc_sli_config_mbox_subsys_get(phba,
7876 mboxq),
7877 lpfc_sli_config_mbox_opcode_get(phba,
7878 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007879 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7880 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7881 bf_get(lpfc_mcqe_ext_status,
7882 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007883 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007884 /* Unblock the async mailbox posting afterward */
7885 lpfc_sli4_async_mbox_unblock(phba);
7886 }
7887 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007888 }
7889
7890 /* Now, interrupt mode asynchrous mailbox command */
7891 rc = lpfc_mbox_cmd_check(phba, mboxq);
7892 if (rc) {
7893 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007894 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007895 "cannot issue Data: x%x x%x\n",
7896 mboxq->vport ? mboxq->vport->vpi : 0,
7897 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007898 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7899 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007900 psli->sli_flag, flag);
7901 goto out_not_finished;
7902 }
James Smartda0436e2009-05-22 14:51:39 -04007903
7904 /* Put the mailbox command to the driver internal FIFO */
7905 psli->slistat.mbox_busy++;
7906 spin_lock_irqsave(&phba->hbalock, iflags);
7907 lpfc_mbox_put(phba, mboxq);
7908 spin_unlock_irqrestore(&phba->hbalock, iflags);
7909 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7910 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007911 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007912 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7913 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007914 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7915 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007916 phba->pport->port_state,
7917 psli->sli_flag, MBX_NOWAIT);
7918 /* Wake up worker thread to transport mailbox command from head */
7919 lpfc_worker_wake_up(phba);
7920
7921 return MBX_BUSY;
7922
7923out_not_finished:
7924 return MBX_NOT_FINISHED;
7925}
7926
7927/**
7928 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7929 * @phba: Pointer to HBA context object.
7930 *
7931 * This function is called by worker thread to send a mailbox command to
7932 * SLI4 HBA firmware.
7933 *
7934 **/
7935int
7936lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7937{
7938 struct lpfc_sli *psli = &phba->sli;
7939 LPFC_MBOXQ_t *mboxq;
7940 int rc = MBX_SUCCESS;
7941 unsigned long iflags;
7942 struct lpfc_mqe *mqe;
7943 uint32_t mbx_cmnd;
7944
7945 /* Check interrupt mode before post async mailbox command */
7946 if (unlikely(!phba->sli4_hba.intr_enable))
7947 return MBX_NOT_FINISHED;
7948
7949 /* Check for mailbox command service token */
7950 spin_lock_irqsave(&phba->hbalock, iflags);
7951 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7952 spin_unlock_irqrestore(&phba->hbalock, iflags);
7953 return MBX_NOT_FINISHED;
7954 }
7955 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7956 spin_unlock_irqrestore(&phba->hbalock, iflags);
7957 return MBX_NOT_FINISHED;
7958 }
7959 if (unlikely(phba->sli.mbox_active)) {
7960 spin_unlock_irqrestore(&phba->hbalock, iflags);
7961 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7962 "0384 There is pending active mailbox cmd\n");
7963 return MBX_NOT_FINISHED;
7964 }
7965 /* Take the mailbox command service token */
7966 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7967
7968 /* Get the next mailbox command from head of queue */
7969 mboxq = lpfc_mbox_get(phba);
7970
7971 /* If no more mailbox command waiting for post, we're done */
7972 if (!mboxq) {
7973 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7974 spin_unlock_irqrestore(&phba->hbalock, iflags);
7975 return MBX_SUCCESS;
7976 }
7977 phba->sli.mbox_active = mboxq;
7978 spin_unlock_irqrestore(&phba->hbalock, iflags);
7979
7980 /* Check device readiness for posting mailbox command */
7981 rc = lpfc_mbox_dev_check(phba);
7982 if (unlikely(rc))
7983 /* Driver clean routine will clean up pending mailbox */
7984 goto out_not_finished;
7985
7986 /* Prepare the mbox command to be posted */
7987 mqe = &mboxq->u.mqe;
7988 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7989
7990 /* Start timer for the mbox_tmo and log some mailbox post messages */
7991 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04007992 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007993
7994 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007995 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007996 "x%x x%x\n",
7997 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007998 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7999 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008000 phba->pport->port_state, psli->sli_flag);
8001
8002 if (mbx_cmnd != MBX_HEARTBEAT) {
8003 if (mboxq->vport) {
8004 lpfc_debugfs_disc_trc(mboxq->vport,
8005 LPFC_DISC_TRC_MBOX_VPORT,
8006 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8007 mbx_cmnd, mqe->un.mb_words[0],
8008 mqe->un.mb_words[1]);
8009 } else {
8010 lpfc_debugfs_disc_trc(phba->pport,
8011 LPFC_DISC_TRC_MBOX,
8012 "MBOX Send: cmd:x%x mb:x%x x%x",
8013 mbx_cmnd, mqe->un.mb_words[0],
8014 mqe->un.mb_words[1]);
8015 }
8016 }
8017 psli->slistat.mbox_cmd++;
8018
8019 /* Post the mailbox command to the port */
8020 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8021 if (rc != MBX_SUCCESS) {
8022 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008023 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008024 "cannot issue Data: x%x x%x\n",
8025 mboxq->vport ? mboxq->vport->vpi : 0,
8026 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008027 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8028 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008029 psli->sli_flag, MBX_NOWAIT);
8030 goto out_not_finished;
8031 }
8032
8033 return rc;
8034
8035out_not_finished:
8036 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008037 if (phba->sli.mbox_active) {
8038 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8039 __lpfc_mbox_cmpl_put(phba, mboxq);
8040 /* Release the token */
8041 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8042 phba->sli.mbox_active = NULL;
8043 }
James Smartda0436e2009-05-22 14:51:39 -04008044 spin_unlock_irqrestore(&phba->hbalock, iflags);
8045
8046 return MBX_NOT_FINISHED;
8047}
8048
8049/**
8050 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8051 * @phba: Pointer to HBA context object.
8052 * @pmbox: Pointer to mailbox object.
8053 * @flag: Flag indicating how the mailbox need to be processed.
8054 *
8055 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8056 * the API jump table function pointer from the lpfc_hba struct.
8057 *
8058 * Return codes the caller owns the mailbox command after the return of the
8059 * function.
8060 **/
8061int
8062lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8063{
8064 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8065}
8066
8067/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008068 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008069 * @phba: The hba struct for which this call is being executed.
8070 * @dev_grp: The HBA PCI-Device group number.
8071 *
8072 * This routine sets up the mbox interface API function jump table in @phba
8073 * struct.
8074 * Returns: 0 - success, -ENODEV - failure.
8075 **/
8076int
8077lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8078{
8079
8080 switch (dev_grp) {
8081 case LPFC_PCI_DEV_LP:
8082 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8083 phba->lpfc_sli_handle_slow_ring_event =
8084 lpfc_sli_handle_slow_ring_event_s3;
8085 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8086 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8087 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8088 break;
8089 case LPFC_PCI_DEV_OC:
8090 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8091 phba->lpfc_sli_handle_slow_ring_event =
8092 lpfc_sli_handle_slow_ring_event_s4;
8093 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8094 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8095 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8096 break;
8097 default:
8098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8099 "1420 Invalid HBA PCI-device group: 0x%x\n",
8100 dev_grp);
8101 return -ENODEV;
8102 break;
8103 }
8104 return 0;
8105}
8106
8107/**
James Smart3621a712009-04-06 18:47:14 -04008108 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008109 * @phba: Pointer to HBA context object.
8110 * @pring: Pointer to driver SLI ring object.
8111 * @piocb: Pointer to address of newly added command iocb.
8112 *
8113 * This function is called with hbalock held to add a command
8114 * iocb to the txq when SLI layer cannot submit the command iocb
8115 * to the ring.
8116 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008117void
James Smart92d7f7b2007-06-17 19:56:38 -05008118__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008119 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008120{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008121 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008122 /* Insert the caller's iocb in the txq tail for later processing. */
8123 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008124}
8125
James Smarte59058c2008-08-24 21:49:00 -04008126/**
James Smart3621a712009-04-06 18:47:14 -04008127 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008128 * @phba: Pointer to HBA context object.
8129 * @pring: Pointer to driver SLI ring object.
8130 * @piocb: Pointer to address of newly added command iocb.
8131 *
8132 * This function is called with hbalock held before a new
8133 * iocb is submitted to the firmware. This function checks
8134 * txq to flush the iocbs in txq to Firmware before
8135 * submitting new iocbs to the Firmware.
8136 * If there are iocbs in the txq which need to be submitted
8137 * to firmware, lpfc_sli_next_iocb returns the first element
8138 * of the txq after dequeuing it from txq.
8139 * If there is no iocb in the txq then the function will return
8140 * *piocb and *piocb is set to NULL. Caller needs to check
8141 * *piocb to find if there are more commands in the txq.
8142 **/
dea31012005-04-17 16:05:31 -05008143static struct lpfc_iocbq *
8144lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008145 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008146{
8147 struct lpfc_iocbq * nextiocb;
8148
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008149 lockdep_assert_held(&phba->hbalock);
8150
dea31012005-04-17 16:05:31 -05008151 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8152 if (!nextiocb) {
8153 nextiocb = *piocb;
8154 *piocb = NULL;
8155 }
8156
8157 return nextiocb;
8158}
8159
James Smarte59058c2008-08-24 21:49:00 -04008160/**
James Smart3772a992009-05-22 14:50:54 -04008161 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008162 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008163 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008164 * @piocb: Pointer to command iocb.
8165 * @flag: Flag indicating if this command can be put into txq.
8166 *
James Smart3772a992009-05-22 14:50:54 -04008167 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8168 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8169 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8170 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8171 * this function allows only iocbs for posting buffers. This function finds
8172 * next available slot in the command ring and posts the command to the
8173 * available slot and writes the port attention register to request HBA start
8174 * processing new iocb. If there is no slot available in the ring and
8175 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8176 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008177 *
James Smart3772a992009-05-22 14:50:54 -04008178 * This function is called with hbalock held. The function will return success
8179 * after it successfully submit the iocb to firmware or after adding to the
8180 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008181 **/
James Smart98c9ea52007-10-27 13:37:33 -04008182static int
James Smart3772a992009-05-22 14:50:54 -04008183__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008184 struct lpfc_iocbq *piocb, uint32_t flag)
8185{
8186 struct lpfc_iocbq *nextiocb;
8187 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008188 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008189
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008190 lockdep_assert_held(&phba->hbalock);
8191
James Smart92d7f7b2007-06-17 19:56:38 -05008192 if (piocb->iocb_cmpl && (!piocb->vport) &&
8193 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8194 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8195 lpfc_printf_log(phba, KERN_ERR,
8196 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008197 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008198 piocb->iocb.ulpCommand);
8199 dump_stack();
8200 return IOCB_ERROR;
8201 }
8202
8203
Linas Vepstas8d63f372007-02-14 14:28:36 -06008204 /* If the PCI channel is in offline state, do not post iocbs. */
8205 if (unlikely(pci_channel_offline(phba->pcidev)))
8206 return IOCB_ERROR;
8207
James Smarta257bf92009-04-06 18:48:10 -04008208 /* If HBA has a deferred error attention, fail the iocb. */
8209 if (unlikely(phba->hba_flag & DEFER_ERATT))
8210 return IOCB_ERROR;
8211
dea31012005-04-17 16:05:31 -05008212 /*
8213 * We should never get an IOCB if we are in a < LINK_DOWN state
8214 */
James Smart2e0fef82007-06-17 19:56:36 -05008215 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008216 return IOCB_ERROR;
8217
8218 /*
8219 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008220 * outstanding event.
dea31012005-04-17 16:05:31 -05008221 */
James Smart0b727fe2007-10-27 13:37:25 -04008222 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008223 goto iocb_busy;
8224
James Smart2e0fef82007-06-17 19:56:36 -05008225 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008226 /*
James Smart2680eea2007-04-25 09:52:55 -04008227 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008228 * can be issued if the link is not up.
8229 */
8230 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008231 case CMD_GEN_REQUEST64_CR:
8232 case CMD_GEN_REQUEST64_CX:
8233 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8234 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008235 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008236 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8237 MENLO_TRANSPORT_TYPE))
8238
8239 goto iocb_busy;
8240 break;
dea31012005-04-17 16:05:31 -05008241 case CMD_QUE_RING_BUF_CN:
8242 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008243 /*
8244 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8245 * completion, iocb_cmpl MUST be 0.
8246 */
8247 if (piocb->iocb_cmpl)
8248 piocb->iocb_cmpl = NULL;
8249 /*FALLTHROUGH*/
8250 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008251 case CMD_CLOSE_XRI_CN:
8252 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008253 break;
8254 default:
8255 goto iocb_busy;
8256 }
8257
8258 /*
8259 * For FCP commands, we must be in a state where we can process link
8260 * attention events.
8261 */
James Smart895427b2017-02-12 13:52:30 -08008262 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008263 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008264 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008265 }
dea31012005-04-17 16:05:31 -05008266
dea31012005-04-17 16:05:31 -05008267 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8268 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8269 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8270
8271 if (iocb)
8272 lpfc_sli_update_ring(phba, pring);
8273 else
8274 lpfc_sli_update_full_ring(phba, pring);
8275
8276 if (!piocb)
8277 return IOCB_SUCCESS;
8278
8279 goto out_busy;
8280
8281 iocb_busy:
8282 pring->stats.iocb_cmd_delay++;
8283
8284 out_busy:
8285
8286 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008287 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008288 return IOCB_SUCCESS;
8289 }
8290
8291 return IOCB_BUSY;
8292}
8293
James Smart3772a992009-05-22 14:50:54 -04008294/**
James Smart4f774512009-05-22 14:52:35 -04008295 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8296 * @phba: Pointer to HBA context object.
8297 * @piocb: Pointer to command iocb.
8298 * @sglq: Pointer to the scatter gather queue object.
8299 *
8300 * This routine converts the bpl or bde that is in the IOCB
8301 * to a sgl list for the sli4 hardware. The physical address
8302 * of the bpl/bde is converted back to a virtual address.
8303 * If the IOCB contains a BPL then the list of BDE's is
8304 * converted to sli4_sge's. If the IOCB contains a single
8305 * BDE then it is converted to a single sli_sge.
8306 * The IOCB is still in cpu endianess so the contents of
8307 * the bpl can be used without byte swapping.
8308 *
8309 * Returns valid XRI = Success, NO_XRI = Failure.
8310**/
8311static uint16_t
8312lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8313 struct lpfc_sglq *sglq)
8314{
8315 uint16_t xritag = NO_XRI;
8316 struct ulp_bde64 *bpl = NULL;
8317 struct ulp_bde64 bde;
8318 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008319 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008320 IOCB_t *icmd;
8321 int numBdes = 0;
8322 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008323 uint32_t offset = 0; /* accumulated offset in the sg request list */
8324 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008325
8326 if (!piocbq || !sglq)
8327 return xritag;
8328
8329 sgl = (struct sli4_sge *)sglq->sgl;
8330 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008331 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8332 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008333 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8334 numBdes = icmd->un.genreq64.bdl.bdeSize /
8335 sizeof(struct ulp_bde64);
8336 /* The addrHigh and addrLow fields within the IOCB
8337 * have not been byteswapped yet so there is no
8338 * need to swap them back.
8339 */
James Smart1b511972011-12-13 13:23:09 -05008340 if (piocbq->context3)
8341 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8342 else
8343 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008344
James Smart1b511972011-12-13 13:23:09 -05008345 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008346 if (!bpl)
8347 return xritag;
8348
8349 for (i = 0; i < numBdes; i++) {
8350 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008351 sgl->addr_hi = bpl->addrHigh;
8352 sgl->addr_lo = bpl->addrLow;
8353
James Smart05580562011-05-24 11:40:48 -04008354 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008355 if ((i+1) == numBdes)
8356 bf_set(lpfc_sli4_sge_last, sgl, 1);
8357 else
8358 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008359 /* swap the size field back to the cpu so we
8360 * can assign it to the sgl.
8361 */
8362 bde.tus.w = le32_to_cpu(bpl->tus.w);
8363 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008364 /* The offsets in the sgl need to be accumulated
8365 * separately for the request and reply lists.
8366 * The request is always first, the reply follows.
8367 */
8368 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8369 /* add up the reply sg entries */
8370 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8371 inbound++;
8372 /* first inbound? reset the offset */
8373 if (inbound == 1)
8374 offset = 0;
8375 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008376 bf_set(lpfc_sli4_sge_type, sgl,
8377 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008378 offset += bde.tus.f.bdeSize;
8379 }
James Smart546fc852011-03-11 16:06:29 -05008380 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008381 bpl++;
8382 sgl++;
8383 }
8384 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8385 /* The addrHigh and addrLow fields of the BDE have not
8386 * been byteswapped yet so they need to be swapped
8387 * before putting them in the sgl.
8388 */
8389 sgl->addr_hi =
8390 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8391 sgl->addr_lo =
8392 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008393 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008394 bf_set(lpfc_sli4_sge_last, sgl, 1);
8395 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008396 sgl->sge_len =
8397 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008398 }
8399 return sglq->sli4_xritag;
8400}
8401
8402/**
James Smart4f774512009-05-22 14:52:35 -04008403 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8404 * @phba: Pointer to HBA context object.
8405 * @piocb: Pointer to command iocb.
8406 * @wqe: Pointer to the work queue entry.
8407 *
8408 * This routine converts the iocb command to its Work Queue Entry
8409 * equivalent. The wqe pointer should not have any fields set when
8410 * this routine is called because it will memcpy over them.
8411 * This routine does not set the CQ_ID or the WQEC bits in the
8412 * wqe.
8413 *
8414 * Returns: 0 = Success, IOCB_ERROR = Failure.
8415 **/
8416static int
8417lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8418 union lpfc_wqe *wqe)
8419{
James Smart5ffc2662009-11-18 15:39:44 -05008420 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008421 uint8_t ct = 0;
8422 uint32_t fip;
8423 uint32_t abort_tag;
8424 uint8_t command_type = ELS_COMMAND_NON_FIP;
8425 uint8_t cmnd;
8426 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008427 uint16_t abrt_iotag;
8428 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008429 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008430 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008431 int numBdes, i;
8432 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008433 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008434 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008435 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008436
James Smart45ed1192009-10-02 15:17:02 -04008437 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008438 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008439 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008440 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008441 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008442 command_type = ELS_COMMAND_FIP;
8443 else
8444 command_type = ELS_COMMAND_NON_FIP;
8445
James Smartb5c53952016-03-31 14:12:30 -07008446 if (phba->fcp_embed_io)
8447 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008448 /* Some of the fields are in the right position already */
8449 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartf0d9bcc2010-10-22 11:07:09 -04008450 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart28d7f3d2014-05-21 08:05:28 -04008451 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07008452
8453 abort_tag = (uint32_t) iocbq->iotag;
8454 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008455 /* words0-2 bpl convert bde */
8456 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008457 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8458 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008459 bpl = (struct ulp_bde64 *)
8460 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8461 if (!bpl)
8462 return IOCB_ERROR;
8463
8464 /* Should already be byte swapped. */
8465 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8466 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8467 /* swap the size field back to the cpu so we
8468 * can assign it to the sgl.
8469 */
8470 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008471 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8472 total_len = 0;
8473 for (i = 0; i < numBdes; i++) {
8474 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8475 total_len += bde.tus.f.bdeSize;
8476 }
James Smart4f774512009-05-22 14:52:35 -04008477 } else
James Smart5ffc2662009-11-18 15:39:44 -05008478 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008479
8480 iocbq->iocb.ulpIoTag = iocbq->iotag;
8481 cmnd = iocbq->iocb.ulpCommand;
8482
8483 switch (iocbq->iocb.ulpCommand) {
8484 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008485 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8486 ndlp = iocbq->context_un.ndlp;
8487 else
8488 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008489 if (!iocbq->iocb.ulpLe) {
8490 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8491 "2007 Only Limited Edition cmd Format"
8492 " supported 0x%x\n",
8493 iocbq->iocb.ulpCommand);
8494 return IOCB_ERROR;
8495 }
James Smartff78d8f2011-12-13 13:21:35 -05008496
James Smart5ffc2662009-11-18 15:39:44 -05008497 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008498 /* Els_reguest64 has a TMO */
8499 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8500 iocbq->iocb.ulpTimeout);
8501 /* Need a VF for word 4 set the vf bit*/
8502 bf_set(els_req64_vf, &wqe->els_req, 0);
8503 /* And a VFID for word 12 */
8504 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008505 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008506 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8507 iocbq->iocb.ulpContext);
8508 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8509 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008510 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008511 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008512 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8513 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008514 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8515 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008516 if_type = bf_get(lpfc_sli_intf_if_type,
8517 &phba->sli4_hba.sli_intf);
8518 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008519 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008520 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008521 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008522 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008523 *pcmd == ELS_CMD_PLOGI)) {
8524 bf_set(els_req64_sp, &wqe->els_req, 1);
8525 bf_set(els_req64_sid, &wqe->els_req,
8526 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008527 if ((*pcmd == ELS_CMD_FLOGI) &&
8528 !(phba->fc_topology ==
8529 LPFC_TOPOLOGY_LOOP))
8530 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008531 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8532 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008533 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008534 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008535 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8536 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8537 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8538 }
James Smartc8685952009-11-18 15:39:16 -05008539 }
James Smart6d368e52011-05-24 11:44:12 -04008540 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8541 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008542 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8543 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8544 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8545 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8546 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8547 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008548 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008549 break;
James Smart5ffc2662009-11-18 15:39:44 -05008550 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008551 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8552 iocbq->iocb.un.ulpWord[3]);
8553 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008554 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008555 /* The entire sequence is transmitted for this IOCB */
8556 xmit_len = total_len;
8557 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008558 if (phba->link_flag & LS_LOOPBACK_MODE)
8559 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008560 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008561 /* word3 iocb=io_tag32 wqe=reserved */
8562 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008563 /* word4 relative_offset memcpy */
8564 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008565 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8566 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8567 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8568 LPFC_WQE_IOD_WRITE);
8569 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8570 LPFC_WQE_LENLOC_WORD12);
8571 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008572 wqe->xmit_sequence.xmit_len = xmit_len;
8573 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008574 break;
James Smart4f774512009-05-22 14:52:35 -04008575 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008576 /* word3 iocb=iotag32 wqe=seq_payload_len */
8577 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008578 /* word4 iocb=rsvd wqe=rsvd */
8579 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8580 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008581 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008582 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008583 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8584 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8585 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8586 LPFC_WQE_LENLOC_WORD3);
8587 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008588 break;
James Smart4f774512009-05-22 14:52:35 -04008589 case CMD_FCP_IWRITE64_CR:
8590 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008591 /* word3 iocb=iotag wqe=payload_offset_len */
8592 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008593 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8594 xmit_len + sizeof(struct fcp_rsp));
8595 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8596 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008597 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8598 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8599 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8600 iocbq->iocb.ulpFCP2Rcvy);
8601 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8602 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008603 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8604 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8605 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008606 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008607 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008608 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8609 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008610 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8611 if (iocbq->priority) {
8612 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8613 (iocbq->priority << 1));
8614 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008615 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8616 (phba->cfg_XLanePriority << 1));
8617 }
8618 }
James Smartb5c53952016-03-31 14:12:30 -07008619 /* Note, word 10 is already initialized to 0 */
8620
8621 if (phba->fcp_embed_io) {
8622 struct lpfc_scsi_buf *lpfc_cmd;
8623 struct sli4_sge *sgl;
8624 union lpfc_wqe128 *wqe128;
8625 struct fcp_cmnd *fcp_cmnd;
8626 uint32_t *ptr;
8627
8628 /* 128 byte wqe support here */
8629 wqe128 = (union lpfc_wqe128 *)wqe;
8630
8631 lpfc_cmd = iocbq->context1;
8632 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8633 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8634
8635 /* Word 0-2 - FCP_CMND */
8636 wqe128->generic.bde.tus.f.bdeFlags =
8637 BUFF_TYPE_BDE_IMMED;
8638 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8639 wqe128->generic.bde.addrHigh = 0;
8640 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8641
8642 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8643
8644 /* Word 22-29 FCP CMND Payload */
8645 ptr = &wqe128->words[22];
8646 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8647 }
James Smart7851fe22011-07-22 18:36:52 -04008648 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008649 case CMD_FCP_IREAD64_CR:
8650 /* word3 iocb=iotag wqe=payload_offset_len */
8651 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008652 bf_set(payload_offset_len, &wqe->fcp_iread,
8653 xmit_len + sizeof(struct fcp_rsp));
8654 bf_set(cmd_buff_len, &wqe->fcp_iread,
8655 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008656 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8657 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8658 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8659 iocbq->iocb.ulpFCP2Rcvy);
8660 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008661 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008662 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8663 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8664 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008665 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008666 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008667 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8668 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008669 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8670 if (iocbq->priority) {
8671 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8672 (iocbq->priority << 1));
8673 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008674 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8675 (phba->cfg_XLanePriority << 1));
8676 }
8677 }
James Smartb5c53952016-03-31 14:12:30 -07008678 /* Note, word 10 is already initialized to 0 */
8679
8680 if (phba->fcp_embed_io) {
8681 struct lpfc_scsi_buf *lpfc_cmd;
8682 struct sli4_sge *sgl;
8683 union lpfc_wqe128 *wqe128;
8684 struct fcp_cmnd *fcp_cmnd;
8685 uint32_t *ptr;
8686
8687 /* 128 byte wqe support here */
8688 wqe128 = (union lpfc_wqe128 *)wqe;
8689
8690 lpfc_cmd = iocbq->context1;
8691 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8692 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8693
8694 /* Word 0-2 - FCP_CMND */
8695 wqe128->generic.bde.tus.f.bdeFlags =
8696 BUFF_TYPE_BDE_IMMED;
8697 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8698 wqe128->generic.bde.addrHigh = 0;
8699 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8700
8701 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8702
8703 /* Word 22-29 FCP CMND Payload */
8704 ptr = &wqe128->words[22];
8705 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8706 }
James Smart7851fe22011-07-22 18:36:52 -04008707 break;
James Smartf1126682009-06-10 17:22:44 -04008708 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008709 /* word3 iocb=iotag wqe=payload_offset_len */
8710 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8711 bf_set(payload_offset_len, &wqe->fcp_icmd,
8712 xmit_len + sizeof(struct fcp_rsp));
8713 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8714 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008715 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008716 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008717 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008718 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8719 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8720 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8721 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8722 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008723 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8724 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008725 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8726 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008727 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8728 if (iocbq->priority) {
8729 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8730 (iocbq->priority << 1));
8731 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008732 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8733 (phba->cfg_XLanePriority << 1));
8734 }
8735 }
James Smartb5c53952016-03-31 14:12:30 -07008736 /* Note, word 10 is already initialized to 0 */
8737
8738 if (phba->fcp_embed_io) {
8739 struct lpfc_scsi_buf *lpfc_cmd;
8740 struct sli4_sge *sgl;
8741 union lpfc_wqe128 *wqe128;
8742 struct fcp_cmnd *fcp_cmnd;
8743 uint32_t *ptr;
8744
8745 /* 128 byte wqe support here */
8746 wqe128 = (union lpfc_wqe128 *)wqe;
8747
8748 lpfc_cmd = iocbq->context1;
8749 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8750 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8751
8752 /* Word 0-2 - FCP_CMND */
8753 wqe128->generic.bde.tus.f.bdeFlags =
8754 BUFF_TYPE_BDE_IMMED;
8755 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8756 wqe128->generic.bde.addrHigh = 0;
8757 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8758
8759 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8760
8761 /* Word 22-29 FCP CMND Payload */
8762 ptr = &wqe128->words[22];
8763 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8764 }
James Smart7851fe22011-07-22 18:36:52 -04008765 break;
James Smart4f774512009-05-22 14:52:35 -04008766 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008767 /* For this command calculate the xmit length of the
8768 * request bde.
8769 */
8770 xmit_len = 0;
8771 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8772 sizeof(struct ulp_bde64);
8773 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008774 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008775 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8776 break;
James Smart63e801c2010-11-20 23:14:19 -05008777 xmit_len += bde.tus.f.bdeSize;
8778 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008779 /* word3 iocb=IO_TAG wqe=request_payload_len */
8780 wqe->gen_req.request_payload_len = xmit_len;
8781 /* word4 iocb=parameter wqe=relative_offset memcpy */
8782 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008783 /* word6 context tag copied in memcpy */
8784 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8785 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8786 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8787 "2015 Invalid CT %x command 0x%x\n",
8788 ct, iocbq->iocb.ulpCommand);
8789 return IOCB_ERROR;
8790 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008791 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8792 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8793 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8794 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8795 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8796 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8797 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8798 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008799 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008800 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008801 break;
James Smart4f774512009-05-22 14:52:35 -04008802 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008803 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008804 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008805 /* word3 iocb=iotag32 wqe=response_payload_len */
8806 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008807 /* word4 */
8808 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008809 /* word5 iocb=rsvd wge=did */
8810 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008811 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8812
8813 if_type = bf_get(lpfc_sli_intf_if_type,
8814 &phba->sli4_hba.sli_intf);
8815 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8816 if (iocbq->vport->fc_flag & FC_PT2PT) {
8817 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8818 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8819 iocbq->vport->fc_myDID);
8820 if (iocbq->vport->fc_myDID == Fabric_DID) {
8821 bf_set(wqe_els_did,
8822 &wqe->xmit_els_rsp.wqe_dest, 0);
8823 }
8824 }
8825 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008826 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8827 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8828 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8829 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008830 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008831 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008832 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008833 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008834 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8835 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8836 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8837 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8838 LPFC_WQE_LENLOC_WORD3);
8839 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008840 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8841 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008842 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8843 iocbq->context2)->virt);
8844 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008845 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8846 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008847 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008848 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8849 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008850 phba->vpi_ids[phba->pport->vpi]);
8851 }
James Smart4f774512009-05-22 14:52:35 -04008852 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008853 break;
James Smart4f774512009-05-22 14:52:35 -04008854 case CMD_CLOSE_XRI_CN:
8855 case CMD_ABORT_XRI_CN:
8856 case CMD_ABORT_XRI_CX:
8857 /* words 0-2 memcpy should be 0 rserved */
8858 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008859 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8860 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8861 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8862 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8863 } else
8864 fip = 0;
8865
8866 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008867 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008868 * The link is down, or the command was ELS_FIP
8869 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008870 * on the wire.
8871 */
8872 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8873 else
8874 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8875 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008876 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8877 wqe->abort_cmd.rsrvd5 = 0;
8878 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008879 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8880 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008881 /*
8882 * The abort handler will send us CMD_ABORT_XRI_CN or
8883 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8884 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008885 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8886 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8887 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8888 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008889 cmnd = CMD_ABORT_XRI_CX;
8890 command_type = OTHER_COMMAND;
8891 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008892 break;
James Smart6669f9b2009-10-02 15:16:45 -04008893 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008894 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008895 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008896 * we re-construct this WQE here based on information in
8897 * iocbq from scratch.
8898 */
8899 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008900 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008901 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008902 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8903 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008904 LPFC_ABTS_UNSOL_INT) {
8905 /* ABTS sent by initiator to CT exchange, the
8906 * RX_ID field will be filled with the newly
8907 * allocated responder XRI.
8908 */
8909 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8910 iocbq->sli4_xritag);
8911 } else {
8912 /* ABTS sent by responder to CT exchange, the
8913 * RX_ID field will be filled with the responder
8914 * RX_ID from ABTS.
8915 */
8916 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008917 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008918 }
James Smart6669f9b2009-10-02 15:16:45 -04008919 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8920 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008921
8922 /* Use CT=VPI */
8923 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8924 ndlp->nlp_DID);
8925 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8926 iocbq->iocb.ulpContext);
8927 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008928 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008929 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008930 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8931 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8932 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008933 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8934 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008935 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8936 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8937 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8938 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8939 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8940 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8941 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8942 }
8943
James Smart7851fe22011-07-22 18:36:52 -04008944 break;
James Smart4f774512009-05-22 14:52:35 -04008945 case CMD_XRI_ABORTED_CX:
8946 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008947 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8948 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8949 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8950 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8951 default:
8952 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8953 "2014 Invalid command 0x%x\n",
8954 iocbq->iocb.ulpCommand);
8955 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008956 break;
James Smart4f774512009-05-22 14:52:35 -04008957 }
James Smart6d368e52011-05-24 11:44:12 -04008958
James Smart8012cc32012-10-31 14:44:49 -04008959 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8960 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8961 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8962 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8963 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8964 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8965 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8966 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008967 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8968 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8969 wqe->generic.wqe_com.abort_tag = abort_tag;
8970 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8971 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8972 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8973 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008974 return 0;
8975}
8976
8977/**
8978 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8979 * @phba: Pointer to HBA context object.
8980 * @ring_number: SLI ring number to issue iocb on.
8981 * @piocb: Pointer to command iocb.
8982 * @flag: Flag indicating if this command can be put into txq.
8983 *
8984 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8985 * an iocb command to an HBA with SLI-4 interface spec.
8986 *
8987 * This function is called with hbalock held. The function will return success
8988 * after it successfully submit the iocb to firmware or after adding to the
8989 * txq.
8990 **/
8991static int
8992__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8993 struct lpfc_iocbq *piocb, uint32_t flag)
8994{
8995 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07008996 union lpfc_wqe *wqe;
8997 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05008998 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08008999 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009000
James Smart895427b2017-02-12 13:52:30 -08009001 /* Get the WQ */
9002 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9003 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9004 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9005 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9006 else
9007 wq = phba->sli4_hba.oas_wq;
9008 } else {
9009 wq = phba->sli4_hba.els_wq;
9010 }
9011
9012 /* Get corresponding ring */
9013 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009014
James Smartb5c53952016-03-31 14:12:30 -07009015 /*
9016 * The WQE can be either 64 or 128 bytes,
9017 * so allocate space on the stack assuming the largest.
9018 */
9019 wqe = (union lpfc_wqe *)&wqe128;
9020
James Smart895427b2017-02-12 13:52:30 -08009021 lockdep_assert_held(&phba->hbalock);
9022
James Smart4f774512009-05-22 14:52:35 -04009023 if (piocb->sli4_xritag == NO_XRI) {
9024 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009025 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009026 sglq = NULL;
9027 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009028 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009029 if (!(flag & SLI_IOCB_RET_IOCB)) {
9030 __lpfc_sli_ringtx_put(phba,
9031 pring, piocb);
9032 return IOCB_SUCCESS;
9033 } else {
9034 return IOCB_BUSY;
9035 }
9036 } else {
James Smart895427b2017-02-12 13:52:30 -08009037 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009038 if (!sglq) {
9039 if (!(flag & SLI_IOCB_RET_IOCB)) {
9040 __lpfc_sli_ringtx_put(phba,
9041 pring,
9042 piocb);
9043 return IOCB_SUCCESS;
9044 } else
9045 return IOCB_BUSY;
9046 }
9047 }
James Smart4f774512009-05-22 14:52:35 -04009048 }
James Smart2ea259e2017-02-12 13:52:27 -08009049 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009050 /* These IO's already have an XRI and a mapped sgl. */
9051 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009052 else {
James Smart6d368e52011-05-24 11:44:12 -04009053 /*
9054 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009055 * sglq is on the active list
9056 */
James Smartedccdc12013-01-03 15:43:45 -05009057 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009058 if (!sglq)
9059 return IOCB_ERROR;
9060 }
9061
9062 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009063 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009064 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009065 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009066 return IOCB_ERROR;
9067 }
9068
James Smartb5c53952016-03-31 14:12:30 -07009069 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04009070 return IOCB_ERROR;
9071
James Smart895427b2017-02-12 13:52:30 -08009072 if (lpfc_sli4_wq_put(wq, wqe))
9073 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009074 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9075
9076 return 0;
9077}
9078
9079/**
James Smart3772a992009-05-22 14:50:54 -04009080 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9081 *
9082 * This routine wraps the actual lockless version for issusing IOCB function
9083 * pointer from the lpfc_hba struct.
9084 *
9085 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009086 * IOCB_ERROR - Error
9087 * IOCB_SUCCESS - Success
9088 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009089 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009090int
James Smart3772a992009-05-22 14:50:54 -04009091__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9092 struct lpfc_iocbq *piocb, uint32_t flag)
9093{
9094 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9095}
9096
9097/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009098 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009099 * @phba: The hba struct for which this call is being executed.
9100 * @dev_grp: The HBA PCI-Device group number.
9101 *
9102 * This routine sets up the SLI interface API function jump table in @phba
9103 * struct.
9104 * Returns: 0 - success, -ENODEV - failure.
9105 **/
9106int
9107lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9108{
9109
9110 switch (dev_grp) {
9111 case LPFC_PCI_DEV_LP:
9112 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9113 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9114 break;
James Smart4f774512009-05-22 14:52:35 -04009115 case LPFC_PCI_DEV_OC:
9116 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9117 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9118 break;
James Smart3772a992009-05-22 14:50:54 -04009119 default:
9120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9121 "1419 Invalid HBA PCI-device group: 0x%x\n",
9122 dev_grp);
9123 return -ENODEV;
9124 break;
9125 }
9126 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9127 return 0;
9128}
James Smart92d7f7b2007-06-17 19:56:38 -05009129
James Smarta1efe162015-05-21 13:55:20 -04009130/**
James Smart895427b2017-02-12 13:52:30 -08009131 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009132 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009133 * @piocb: Pointer to command iocb.
9134 *
James Smart895427b2017-02-12 13:52:30 -08009135 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9136 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009137 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009138 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009139 */
James Smart895427b2017-02-12 13:52:30 -08009140struct lpfc_sli_ring *
9141lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009142{
James Smart895427b2017-02-12 13:52:30 -08009143 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009144 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009145 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009146 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009147 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009148 /*
James Smart895427b2017-02-12 13:52:30 -08009149 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009150 * be setup based on what work queue we used.
9151 */
9152 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
James Smart895427b2017-02-12 13:52:30 -08009153 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009154 lpfc_sli4_scmd_to_wqidx_distr(phba,
9155 piocb->context1);
James Smart895427b2017-02-12 13:52:30 -08009156 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009157 } else {
9158 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009159 return NULL;
9160 piocb->hba_wqidx = 0;
9161 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009162 }
James Smart895427b2017-02-12 13:52:30 -08009163 } else {
9164 if (unlikely(!phba->sli4_hba.els_wq))
9165 return NULL;
9166 piocb->hba_wqidx = 0;
9167 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009168 }
James Smart9bd2bff52014-09-03 12:57:30 -04009169}
9170
James Smarte59058c2008-08-24 21:49:00 -04009171/**
James Smart3621a712009-04-06 18:47:14 -04009172 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009173 * @phba: Pointer to HBA context object.
9174 * @pring: Pointer to driver SLI ring object.
9175 * @piocb: Pointer to command iocb.
9176 * @flag: Flag indicating if this command can be put into txq.
9177 *
9178 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9179 * function. This function gets the hbalock and calls
9180 * __lpfc_sli_issue_iocb function and will return the error returned
9181 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9182 * functions which do not hold hbalock.
9183 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009184int
James Smart3772a992009-05-22 14:50:54 -04009185lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009186 struct lpfc_iocbq *piocb, uint32_t flag)
9187{
James Smart895427b2017-02-12 13:52:30 -08009188 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009189 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009190 struct lpfc_queue *fpeq;
9191 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009192 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009193 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009194
James Smart7e56aa22012-08-03 12:35:34 -04009195 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009196 pring = lpfc_sli4_calc_ring(phba, piocb);
9197 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009198 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009199
James Smart9bd2bff52014-09-03 12:57:30 -04009200 spin_lock_irqsave(&pring->ring_lock, iflags);
9201 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9202 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009203
James Smart9bd2bff52014-09-03 12:57:30 -04009204 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009205 idx = piocb->hba_wqidx;
9206 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009207
James Smart895427b2017-02-12 13:52:30 -08009208 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009209
James Smart9bd2bff52014-09-03 12:57:30 -04009210 /* Get associated EQ with this index */
9211 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009212
James Smart9bd2bff52014-09-03 12:57:30 -04009213 /* Turn off interrupts from this EQ */
9214 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009215
James Smart9bd2bff52014-09-03 12:57:30 -04009216 /*
9217 * Process all the events on FCP EQ
9218 */
9219 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9220 lpfc_sli4_hba_handle_eqe(phba,
9221 eqe, idx);
9222 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009223 }
James Smartba20c852012-08-03 12:36:52 -04009224
James Smart9bd2bff52014-09-03 12:57:30 -04009225 /* Always clear and re-arm the EQ */
9226 lpfc_sli4_eq_release(fpeq,
9227 LPFC_QUEUE_REARM);
9228 }
James Smart895427b2017-02-12 13:52:30 -08009229 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009230 }
James Smart7e56aa22012-08-03 12:35:34 -04009231 } else {
9232 /* For now, SLI2/3 will still use hbalock */
9233 spin_lock_irqsave(&phba->hbalock, iflags);
9234 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9235 spin_unlock_irqrestore(&phba->hbalock, iflags);
9236 }
James Smart92d7f7b2007-06-17 19:56:38 -05009237 return rc;
9238}
9239
James Smarte59058c2008-08-24 21:49:00 -04009240/**
James Smart3621a712009-04-06 18:47:14 -04009241 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009242 * @phba: Pointer to HBA context object.
9243 *
9244 * This function is called while driver attaches with the
9245 * HBA to setup the extra ring. The extra ring is used
9246 * only when driver needs to support target mode functionality
9247 * or IP over FC functionalities.
9248 *
James Smart895427b2017-02-12 13:52:30 -08009249 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009250 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009251static int
9252lpfc_extra_ring_setup( struct lpfc_hba *phba)
9253{
9254 struct lpfc_sli *psli;
9255 struct lpfc_sli_ring *pring;
9256
9257 psli = &phba->sli;
9258
9259 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009260
9261 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009262 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009263 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9264 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9265 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9266 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009267
James Smarta4bc3372006-12-02 13:34:16 -05009268 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009269 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009270
James Smart7e56aa22012-08-03 12:35:34 -04009271 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9272 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9273 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9274 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009275
9276 /* Setup default profile for this ring */
9277 pring->iotag_max = 4096;
9278 pring->num_mask = 1;
9279 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009280 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9281 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009282 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9283 return 0;
9284}
9285
James Smartcb69f7d2011-12-13 13:21:57 -05009286/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9287 * @phba: Pointer to HBA context object.
9288 * @iocbq: Pointer to iocb object.
9289 *
9290 * The async_event handler calls this routine when it receives
9291 * an ASYNC_STATUS_CN event from the port. The port generates
9292 * this event when an Abort Sequence request to an rport fails
9293 * twice in succession. The abort could be originated by the
9294 * driver or by the port. The ABTS could have been for an ELS
9295 * or FCP IO. The port only generates this event when an ABTS
9296 * fails to complete after one retry.
9297 */
9298static void
9299lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9300 struct lpfc_iocbq *iocbq)
9301{
9302 struct lpfc_nodelist *ndlp = NULL;
9303 uint16_t rpi = 0, vpi = 0;
9304 struct lpfc_vport *vport = NULL;
9305
9306 /* The rpi in the ulpContext is vport-sensitive. */
9307 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9308 rpi = iocbq->iocb.ulpContext;
9309
9310 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9311 "3092 Port generated ABTS async event "
9312 "on vpi %d rpi %d status 0x%x\n",
9313 vpi, rpi, iocbq->iocb.ulpStatus);
9314
9315 vport = lpfc_find_vport_by_vpid(phba, vpi);
9316 if (!vport)
9317 goto err_exit;
9318 ndlp = lpfc_findnode_rpi(vport, rpi);
9319 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9320 goto err_exit;
9321
9322 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9323 lpfc_sli_abts_recover_port(vport, ndlp);
9324 return;
9325
9326 err_exit:
9327 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9328 "3095 Event Context not found, no "
9329 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9330 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9331 vpi, rpi);
9332}
9333
9334/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9335 * @phba: pointer to HBA context object.
9336 * @ndlp: nodelist pointer for the impacted rport.
9337 * @axri: pointer to the wcqe containing the failed exchange.
9338 *
9339 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9340 * port. The port generates this event when an abort exchange request to an
9341 * rport fails twice in succession with no reply. The abort could be originated
9342 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9343 */
9344void
9345lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9346 struct lpfc_nodelist *ndlp,
9347 struct sli4_wcqe_xri_aborted *axri)
9348{
9349 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009350 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009351
James Smart6b5151f2012-01-18 16:24:06 -05009352 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009353 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9354 "3115 Node Context not found, driver "
9355 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009356 return;
9357 }
9358
James Smartcb69f7d2011-12-13 13:21:57 -05009359 vport = ndlp->vport;
9360 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9361 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009362 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009363 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009364 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009365 bf_get(lpfc_wcqe_xa_status, axri),
9366 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009367
James Smart5c1db2a2012-03-01 22:34:36 -05009368 /*
9369 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9370 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9371 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9372 */
James Smarte3d2b802012-08-14 14:25:43 -04009373 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009374 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9375 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009376 lpfc_sli_abts_recover_port(vport, ndlp);
9377}
9378
James Smarte59058c2008-08-24 21:49:00 -04009379/**
James Smart3621a712009-04-06 18:47:14 -04009380 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009381 * @phba: Pointer to HBA context object.
9382 * @pring: Pointer to driver SLI ring object.
9383 * @iocbq: Pointer to iocb object.
9384 *
9385 * This function is called by the slow ring event handler
9386 * function when there is an ASYNC event iocb in the ring.
9387 * This function is called with no lock held.
9388 * Currently this function handles only temperature related
9389 * ASYNC events. The function decodes the temperature sensor
9390 * event message and posts events for the management applications.
9391 **/
James Smart98c9ea52007-10-27 13:37:33 -04009392static void
James Smart57127f12007-10-27 13:37:05 -04009393lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9394 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9395{
9396 IOCB_t *icmd;
9397 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009398 struct temp_event temp_event_data;
9399 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009400 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009401
9402 icmd = &iocbq->iocb;
9403 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009404
James Smartcb69f7d2011-12-13 13:21:57 -05009405 switch (evt_code) {
9406 case ASYNC_TEMP_WARN:
9407 case ASYNC_TEMP_SAFE:
9408 temp_event_data.data = (uint32_t) icmd->ulpContext;
9409 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9410 if (evt_code == ASYNC_TEMP_WARN) {
9411 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9412 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9413 "0347 Adapter is very hot, please take "
9414 "corrective action. temperature : %d Celsius\n",
9415 (uint32_t) icmd->ulpContext);
9416 } else {
9417 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9418 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9419 "0340 Adapter temperature is OK now. "
9420 "temperature : %d Celsius\n",
9421 (uint32_t) icmd->ulpContext);
9422 }
9423
9424 /* Send temperature change event to applications */
9425 shost = lpfc_shost_from_vport(phba->pport);
9426 fc_host_post_vendor_event(shost, fc_get_event_number(),
9427 sizeof(temp_event_data), (char *) &temp_event_data,
9428 LPFC_NL_VENDOR_ID);
9429 break;
9430 case ASYNC_STATUS_CN:
9431 lpfc_sli_abts_err_handler(phba, iocbq);
9432 break;
9433 default:
James Smarta257bf92009-04-06 18:48:10 -04009434 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009435 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009436 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009437 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009438 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9439 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9440 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9441 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009442 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009443 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9444 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9445 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9446 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9447
James Smartcb69f7d2011-12-13 13:21:57 -05009448 break;
James Smart57127f12007-10-27 13:37:05 -04009449 }
James Smart57127f12007-10-27 13:37:05 -04009450}
9451
9452
James Smarte59058c2008-08-24 21:49:00 -04009453/**
James Smart895427b2017-02-12 13:52:30 -08009454 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009455 * @phba: Pointer to HBA context object.
9456 *
9457 * lpfc_sli_setup sets up rings of the SLI interface with
9458 * number of iocbs per ring and iotags. This function is
9459 * called while driver attach to the HBA and before the
9460 * interrupts are enabled. So there is no need for locking.
9461 *
9462 * This function always returns 0.
9463 **/
dea31012005-04-17 16:05:31 -05009464int
James Smart895427b2017-02-12 13:52:30 -08009465lpfc_sli4_setup(struct lpfc_hba *phba)
9466{
9467 struct lpfc_sli_ring *pring;
9468
9469 pring = phba->sli4_hba.els_wq->pring;
9470 pring->num_mask = LPFC_MAX_RING_MASK;
9471 pring->prt[0].profile = 0; /* Mask 0 */
9472 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9473 pring->prt[0].type = FC_TYPE_ELS;
9474 pring->prt[0].lpfc_sli_rcv_unsol_event =
9475 lpfc_els_unsol_event;
9476 pring->prt[1].profile = 0; /* Mask 1 */
9477 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9478 pring->prt[1].type = FC_TYPE_ELS;
9479 pring->prt[1].lpfc_sli_rcv_unsol_event =
9480 lpfc_els_unsol_event;
9481 pring->prt[2].profile = 0; /* Mask 2 */
9482 /* NameServer Inquiry */
9483 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9484 /* NameServer */
9485 pring->prt[2].type = FC_TYPE_CT;
9486 pring->prt[2].lpfc_sli_rcv_unsol_event =
9487 lpfc_ct_unsol_event;
9488 pring->prt[3].profile = 0; /* Mask 3 */
9489 /* NameServer response */
9490 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9491 /* NameServer */
9492 pring->prt[3].type = FC_TYPE_CT;
9493 pring->prt[3].lpfc_sli_rcv_unsol_event =
9494 lpfc_ct_unsol_event;
9495 return 0;
9496}
9497
9498/**
9499 * lpfc_sli_setup - SLI ring setup function
9500 * @phba: Pointer to HBA context object.
9501 *
9502 * lpfc_sli_setup sets up rings of the SLI interface with
9503 * number of iocbs per ring and iotags. This function is
9504 * called while driver attach to the HBA and before the
9505 * interrupts are enabled. So there is no need for locking.
9506 *
9507 * This function always returns 0. SLI3 only.
9508 **/
9509int
dea31012005-04-17 16:05:31 -05009510lpfc_sli_setup(struct lpfc_hba *phba)
9511{
James Smarted957682007-06-17 19:56:37 -05009512 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009513 struct lpfc_sli *psli = &phba->sli;
9514 struct lpfc_sli_ring *pring;
9515
James Smart2a76a282012-08-03 12:35:54 -04009516 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -05009517 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -05009518
James Bottomley604a3e32005-10-29 10:28:33 -05009519 psli->iocbq_lookup = NULL;
9520 psli->iocbq_lookup_len = 0;
9521 psli->last_iotag = 0;
9522
dea31012005-04-17 16:05:31 -05009523 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009524 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009525 switch (i) {
9526 case LPFC_FCP_RING: /* ring 0 - FCP */
9527 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009528 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9529 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9530 pring->sli.sli3.numCiocb +=
9531 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9532 pring->sli.sli3.numRiocb +=
9533 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9534 pring->sli.sli3.numCiocb +=
9535 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9536 pring->sli.sli3.numRiocb +=
9537 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9538 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009539 SLI3_IOCB_CMD_SIZE :
9540 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009541 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009542 SLI3_IOCB_RSP_SIZE :
9543 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009544 pring->iotag_ctr = 0;
9545 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009546 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009547 pring->fast_iotag = pring->iotag_max;
9548 pring->num_mask = 0;
9549 break;
James Smarta4bc3372006-12-02 13:34:16 -05009550 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009551 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009552 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9553 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9554 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009555 SLI3_IOCB_CMD_SIZE :
9556 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009557 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009558 SLI3_IOCB_RSP_SIZE :
9559 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009560 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009561 pring->num_mask = 0;
9562 break;
9563 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9564 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009565 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9566 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9567 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009568 SLI3_IOCB_CMD_SIZE :
9569 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009570 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009571 SLI3_IOCB_RSP_SIZE :
9572 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009573 pring->fast_iotag = 0;
9574 pring->iotag_ctr = 0;
9575 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009576 pring->lpfc_sli_rcv_async_status =
9577 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009578 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009579 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009580 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9581 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009582 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009583 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009584 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009585 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9586 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009587 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009588 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009589 pring->prt[2].profile = 0; /* Mask 2 */
9590 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009591 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009592 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009593 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009594 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009595 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009596 pring->prt[3].profile = 0; /* Mask 3 */
9597 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009598 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009599 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009600 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009601 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009602 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009603 break;
9604 }
James Smart7e56aa22012-08-03 12:35:34 -04009605 totiocbsize += (pring->sli.sli3.numCiocb *
9606 pring->sli.sli3.sizeCiocb) +
9607 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009608 }
James Smarted957682007-06-17 19:56:37 -05009609 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009610 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009611 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9612 "SLI2 SLIM Data: x%x x%lx\n",
9613 phba->brd_no, totiocbsize,
9614 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009615 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009616 if (phba->cfg_multi_ring_support == 2)
9617 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009618
9619 return 0;
9620}
9621
James Smarte59058c2008-08-24 21:49:00 -04009622/**
James Smart895427b2017-02-12 13:52:30 -08009623 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009624 * @phba: Pointer to HBA context object.
9625 *
James Smart895427b2017-02-12 13:52:30 -08009626 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -04009627 * ring. This function also initializes ring indices of each ring.
9628 * This function is called during the initialization of the SLI
9629 * interface of an HBA.
9630 * This function is called with no lock held and always returns
9631 * 1.
9632 **/
James Smart895427b2017-02-12 13:52:30 -08009633void
9634lpfc_sli4_queue_init(struct lpfc_hba *phba)
9635{
9636 struct lpfc_sli *psli;
9637 struct lpfc_sli_ring *pring;
9638 int i;
9639
9640 psli = &phba->sli;
9641 spin_lock_irq(&phba->hbalock);
9642 INIT_LIST_HEAD(&psli->mboxq);
9643 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9644 /* Initialize list headers for txq and txcmplq as double linked lists */
9645 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9646 pring = phba->sli4_hba.fcp_wq[i]->pring;
9647 pring->flag = 0;
9648 pring->ringno = LPFC_FCP_RING;
9649 INIT_LIST_HEAD(&pring->txq);
9650 INIT_LIST_HEAD(&pring->txcmplq);
9651 INIT_LIST_HEAD(&pring->iocb_continueq);
9652 spin_lock_init(&pring->ring_lock);
9653 }
9654 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9655 pring = phba->sli4_hba.nvme_wq[i]->pring;
9656 pring->flag = 0;
9657 pring->ringno = LPFC_FCP_RING;
9658 INIT_LIST_HEAD(&pring->txq);
9659 INIT_LIST_HEAD(&pring->txcmplq);
9660 INIT_LIST_HEAD(&pring->iocb_continueq);
9661 spin_lock_init(&pring->ring_lock);
9662 }
9663 pring = phba->sli4_hba.els_wq->pring;
9664 pring->flag = 0;
9665 pring->ringno = LPFC_ELS_RING;
9666 INIT_LIST_HEAD(&pring->txq);
9667 INIT_LIST_HEAD(&pring->txcmplq);
9668 INIT_LIST_HEAD(&pring->iocb_continueq);
9669 spin_lock_init(&pring->ring_lock);
9670
9671 if (phba->cfg_nvme_io_channel) {
9672 pring = phba->sli4_hba.nvmels_wq->pring;
9673 pring->flag = 0;
9674 pring->ringno = LPFC_ELS_RING;
9675 INIT_LIST_HEAD(&pring->txq);
9676 INIT_LIST_HEAD(&pring->txcmplq);
9677 INIT_LIST_HEAD(&pring->iocb_continueq);
9678 spin_lock_init(&pring->ring_lock);
9679 }
9680
9681 if (phba->cfg_fof) {
9682 pring = phba->sli4_hba.oas_wq->pring;
9683 pring->flag = 0;
9684 pring->ringno = LPFC_FCP_RING;
9685 INIT_LIST_HEAD(&pring->txq);
9686 INIT_LIST_HEAD(&pring->txcmplq);
9687 INIT_LIST_HEAD(&pring->iocb_continueq);
9688 spin_lock_init(&pring->ring_lock);
9689 }
9690
9691 spin_unlock_irq(&phba->hbalock);
9692}
9693
9694/**
9695 * lpfc_sli_queue_init - Queue initialization function
9696 * @phba: Pointer to HBA context object.
9697 *
9698 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9699 * ring. This function also initializes ring indices of each ring.
9700 * This function is called during the initialization of the SLI
9701 * interface of an HBA.
9702 * This function is called with no lock held and always returns
9703 * 1.
9704 **/
9705void
9706lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009707{
9708 struct lpfc_sli *psli;
9709 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009710 int i;
dea31012005-04-17 16:05:31 -05009711
9712 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009713 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009714 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009715 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009716 /* Initialize list headers for txq and txcmplq as double linked lists */
9717 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009718 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009719 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009720 pring->sli.sli3.next_cmdidx = 0;
9721 pring->sli.sli3.local_getidx = 0;
9722 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05009723 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009724 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009725 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -08009726 pring->flag = 0;
9727 INIT_LIST_HEAD(&pring->txq);
9728 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -04009729 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009730 }
James Smart2e0fef82007-06-17 19:56:36 -05009731 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009732}
9733
James Smarte59058c2008-08-24 21:49:00 -04009734/**
James Smart04c68492009-05-22 14:52:52 -04009735 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9736 * @phba: Pointer to HBA context object.
9737 *
9738 * This routine flushes the mailbox command subsystem. It will unconditionally
9739 * flush all the mailbox commands in the three possible stages in the mailbox
9740 * command sub-system: pending mailbox command queue; the outstanding mailbox
9741 * command; and completed mailbox command queue. It is caller's responsibility
9742 * to make sure that the driver is in the proper state to flush the mailbox
9743 * command sub-system. Namely, the posting of mailbox commands into the
9744 * pending mailbox command queue from the various clients must be stopped;
9745 * either the HBA is in a state that it will never works on the outstanding
9746 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9747 * mailbox command has been completed.
9748 **/
9749static void
9750lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9751{
9752 LIST_HEAD(completions);
9753 struct lpfc_sli *psli = &phba->sli;
9754 LPFC_MBOXQ_t *pmb;
9755 unsigned long iflag;
9756
9757 /* Flush all the mailbox commands in the mbox system */
9758 spin_lock_irqsave(&phba->hbalock, iflag);
9759 /* The pending mailbox command queue */
9760 list_splice_init(&phba->sli.mboxq, &completions);
9761 /* The outstanding active mailbox command */
9762 if (psli->mbox_active) {
9763 list_add_tail(&psli->mbox_active->list, &completions);
9764 psli->mbox_active = NULL;
9765 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9766 }
9767 /* The completed mailbox command queue */
9768 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9769 spin_unlock_irqrestore(&phba->hbalock, iflag);
9770
9771 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9772 while (!list_empty(&completions)) {
9773 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9774 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9775 if (pmb->mbox_cmpl)
9776 pmb->mbox_cmpl(phba, pmb);
9777 }
9778}
9779
9780/**
James Smart3621a712009-04-06 18:47:14 -04009781 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009782 * @vport: Pointer to virtual port object.
9783 *
9784 * lpfc_sli_host_down is called to clean up the resources
9785 * associated with a vport before destroying virtual
9786 * port data structures.
9787 * This function does following operations:
9788 * - Free discovery resources associated with this virtual
9789 * port.
9790 * - Free iocbs associated with this virtual port in
9791 * the txq.
9792 * - Send abort for all iocb commands associated with this
9793 * vport in txcmplq.
9794 *
9795 * This function is called with no lock held and always returns 1.
9796 **/
dea31012005-04-17 16:05:31 -05009797int
James Smart92d7f7b2007-06-17 19:56:38 -05009798lpfc_sli_host_down(struct lpfc_vport *vport)
9799{
James Smart858c9f62007-06-17 19:56:39 -05009800 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009801 struct lpfc_hba *phba = vport->phba;
9802 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08009803 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -05009804 struct lpfc_sli_ring *pring;
9805 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009806 int i;
9807 unsigned long flags = 0;
9808 uint16_t prev_pring_flag;
9809
9810 lpfc_cleanup_discovery_resources(vport);
9811
9812 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009813
James Smart895427b2017-02-12 13:52:30 -08009814 /*
9815 * Error everything on the txq since these iocbs
9816 * have not been given to the FW yet.
9817 * Also issue ABTS for everything on the txcmplq
9818 */
9819 if (phba->sli_rev != LPFC_SLI_REV4) {
9820 for (i = 0; i < psli->num_rings; i++) {
9821 pring = &psli->sli3_ring[i];
9822 prev_pring_flag = pring->flag;
9823 /* Only slow rings */
9824 if (pring->ringno == LPFC_ELS_RING) {
9825 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9826 /* Set the lpfc data pending flag */
9827 set_bit(LPFC_DATA_READY, &phba->data_flags);
9828 }
9829 list_for_each_entry_safe(iocb, next_iocb,
9830 &pring->txq, list) {
9831 if (iocb->vport != vport)
9832 continue;
9833 list_move_tail(&iocb->list, &completions);
9834 }
9835 list_for_each_entry_safe(iocb, next_iocb,
9836 &pring->txcmplq, list) {
9837 if (iocb->vport != vport)
9838 continue;
9839 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9840 }
9841 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -05009842 }
James Smart895427b2017-02-12 13:52:30 -08009843 } else {
9844 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
9845 pring = qp->pring;
9846 if (!pring)
9847 continue;
9848 if (pring == phba->sli4_hba.els_wq->pring) {
9849 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9850 /* Set the lpfc data pending flag */
9851 set_bit(LPFC_DATA_READY, &phba->data_flags);
9852 }
9853 prev_pring_flag = pring->flag;
9854 spin_lock_irq(&pring->ring_lock);
9855 list_for_each_entry_safe(iocb, next_iocb,
9856 &pring->txq, list) {
9857 if (iocb->vport != vport)
9858 continue;
9859 list_move_tail(&iocb->list, &completions);
9860 }
9861 spin_unlock_irq(&pring->ring_lock);
9862 list_for_each_entry_safe(iocb, next_iocb,
9863 &pring->txcmplq, list) {
9864 if (iocb->vport != vport)
9865 continue;
9866 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9867 }
9868 pring->flag = prev_pring_flag;
9869 }
James Smart92d7f7b2007-06-17 19:56:38 -05009870 }
James Smart92d7f7b2007-06-17 19:56:38 -05009871 spin_unlock_irqrestore(&phba->hbalock, flags);
9872
James Smarta257bf92009-04-06 18:48:10 -04009873 /* Cancel all the IOCBs from the completions list */
9874 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9875 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009876 return 1;
9877}
9878
James Smarte59058c2008-08-24 21:49:00 -04009879/**
James Smart3621a712009-04-06 18:47:14 -04009880 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009881 * @phba: Pointer to HBA context object.
9882 *
9883 * This function cleans up all iocb, buffers, mailbox commands
9884 * while shutting down the HBA. This function is called with no
9885 * lock held and always returns 1.
9886 * This function does the following to cleanup driver resources:
9887 * - Free discovery resources for each virtual port
9888 * - Cleanup any pending fabric iocbs
9889 * - Iterate through the iocb txq and free each entry
9890 * in the list.
9891 * - Free up any buffer posted to the HBA
9892 * - Free mailbox commands in the mailbox queue.
9893 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009894int
James Smart2e0fef82007-06-17 19:56:36 -05009895lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009896{
James Smart2534ba72007-04-25 09:52:20 -04009897 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009898 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08009899 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -05009900 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009901 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009902 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009903 int i;
9904
9905 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009906 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009907
dea31012005-04-17 16:05:31 -05009908 lpfc_hba_down_prep(phba);
9909
James Smart92d7f7b2007-06-17 19:56:38 -05009910 lpfc_fabric_abort_hba(phba);
9911
James Smart2e0fef82007-06-17 19:56:36 -05009912 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009913
James Smart895427b2017-02-12 13:52:30 -08009914 /*
9915 * Error everything on the txq since these iocbs
9916 * have not been given to the FW yet.
9917 */
9918 if (phba->sli_rev != LPFC_SLI_REV4) {
9919 for (i = 0; i < psli->num_rings; i++) {
9920 pring = &psli->sli3_ring[i];
9921 /* Only slow rings */
9922 if (pring->ringno == LPFC_ELS_RING) {
9923 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9924 /* Set the lpfc data pending flag */
9925 set_bit(LPFC_DATA_READY, &phba->data_flags);
9926 }
9927 list_splice_init(&pring->txq, &completions);
9928 }
9929 } else {
9930 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
9931 pring = qp->pring;
9932 if (!pring)
9933 continue;
9934 spin_lock_irq(&pring->ring_lock);
9935 list_splice_init(&pring->txq, &completions);
9936 spin_unlock_irq(&pring->ring_lock);
9937 if (pring == phba->sli4_hba.els_wq->pring) {
9938 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9939 /* Set the lpfc data pending flag */
9940 set_bit(LPFC_DATA_READY, &phba->data_flags);
9941 }
9942 }
dea31012005-04-17 16:05:31 -05009943 }
James Smart2e0fef82007-06-17 19:56:36 -05009944 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009945
James Smarta257bf92009-04-06 18:48:10 -04009946 /* Cancel all the IOCBs from the completions list */
9947 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9948 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009949
James Smart0ff10d42008-01-11 01:52:36 -05009950 spin_lock_irqsave(&phba->hbalock, flags);
9951 list_splice_init(&phba->elsbuf, &completions);
9952 phba->elsbuf_cnt = 0;
9953 phba->elsbuf_prev_cnt = 0;
9954 spin_unlock_irqrestore(&phba->hbalock, flags);
9955
9956 while (!list_empty(&completions)) {
9957 list_remove_head(&completions, buf_ptr,
9958 struct lpfc_dmabuf, list);
9959 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9960 kfree(buf_ptr);
9961 }
9962
dea31012005-04-17 16:05:31 -05009963 /* Return any active mbox cmds */
9964 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009965
James Smartda0436e2009-05-22 14:51:39 -04009966 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009967 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009968 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009969
James Smartda0436e2009-05-22 14:51:39 -04009970 return 1;
9971}
James Smart92d7f7b2007-06-17 19:56:38 -05009972
James Smartda0436e2009-05-22 14:51:39 -04009973/**
James Smart3621a712009-04-06 18:47:14 -04009974 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009975 * @srcp: Source memory pointer.
9976 * @destp: Destination memory pointer.
9977 * @cnt: Number of words required to be copied.
9978 *
9979 * This function is used for copying data between driver memory
9980 * and the SLI memory. This function also changes the endianness
9981 * of each word if native endianness is different from SLI
9982 * endianness. This function can be called with or without
9983 * lock.
9984 **/
dea31012005-04-17 16:05:31 -05009985void
9986lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9987{
9988 uint32_t *src = srcp;
9989 uint32_t *dest = destp;
9990 uint32_t ldata;
9991 int i;
9992
9993 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9994 ldata = *src;
9995 ldata = le32_to_cpu(ldata);
9996 *dest = ldata;
9997 src++;
9998 dest++;
9999 }
10000}
10001
James Smarte59058c2008-08-24 21:49:00 -040010002
10003/**
James Smarta0c87cb2009-07-19 10:01:10 -040010004 * lpfc_sli_bemem_bcopy - SLI memory copy function
10005 * @srcp: Source memory pointer.
10006 * @destp: Destination memory pointer.
10007 * @cnt: Number of words required to be copied.
10008 *
10009 * This function is used for copying data between a data structure
10010 * with big endian representation to local endianness.
10011 * This function can be called with or without lock.
10012 **/
10013void
10014lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10015{
10016 uint32_t *src = srcp;
10017 uint32_t *dest = destp;
10018 uint32_t ldata;
10019 int i;
10020
10021 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10022 ldata = *src;
10023 ldata = be32_to_cpu(ldata);
10024 *dest = ldata;
10025 src++;
10026 dest++;
10027 }
10028}
10029
10030/**
James Smart3621a712009-04-06 18:47:14 -040010031 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010032 * @phba: Pointer to HBA context object.
10033 * @pring: Pointer to driver SLI ring object.
10034 * @mp: Pointer to driver buffer object.
10035 *
10036 * This function is called with no lock held.
10037 * It always return zero after adding the buffer to the postbufq
10038 * buffer list.
10039 **/
dea31012005-04-17 16:05:31 -050010040int
James Smart2e0fef82007-06-17 19:56:36 -050010041lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10042 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010043{
10044 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10045 later */
James Smart2e0fef82007-06-17 19:56:36 -050010046 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010047 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010048 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010049 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010050 return 0;
10051}
10052
James Smarte59058c2008-08-24 21:49:00 -040010053/**
James Smart3621a712009-04-06 18:47:14 -040010054 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010055 * @phba: Pointer to HBA context object.
10056 *
10057 * When HBQ is enabled, buffers are searched based on tags. This function
10058 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10059 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10060 * does not conflict with tags of buffer posted for unsolicited events.
10061 * The function returns the allocated tag. The function is called with
10062 * no locks held.
10063 **/
James Smart76bb24e2007-10-27 13:38:00 -040010064uint32_t
10065lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10066{
10067 spin_lock_irq(&phba->hbalock);
10068 phba->buffer_tag_count++;
10069 /*
10070 * Always set the QUE_BUFTAG_BIT to distiguish between
10071 * a tag assigned by HBQ.
10072 */
10073 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10074 spin_unlock_irq(&phba->hbalock);
10075 return phba->buffer_tag_count;
10076}
10077
James Smarte59058c2008-08-24 21:49:00 -040010078/**
James Smart3621a712009-04-06 18:47:14 -040010079 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010080 * @phba: Pointer to HBA context object.
10081 * @pring: Pointer to driver SLI ring object.
10082 * @tag: Buffer tag.
10083 *
10084 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10085 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10086 * iocb is posted to the response ring with the tag of the buffer.
10087 * This function searches the pring->postbufq list using the tag
10088 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10089 * iocb. If the buffer is found then lpfc_dmabuf object of the
10090 * buffer is returned to the caller else NULL is returned.
10091 * This function is called with no lock held.
10092 **/
James Smart76bb24e2007-10-27 13:38:00 -040010093struct lpfc_dmabuf *
10094lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10095 uint32_t tag)
10096{
10097 struct lpfc_dmabuf *mp, *next_mp;
10098 struct list_head *slp = &pring->postbufq;
10099
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010100 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010101 spin_lock_irq(&phba->hbalock);
10102 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10103 if (mp->buffer_tag == tag) {
10104 list_del_init(&mp->list);
10105 pring->postbufq_cnt--;
10106 spin_unlock_irq(&phba->hbalock);
10107 return mp;
10108 }
10109 }
10110
10111 spin_unlock_irq(&phba->hbalock);
10112 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010113 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010114 "ring %d Data x%lx x%p x%p x%x\n",
10115 pring->ringno, (unsigned long) tag,
10116 slp->next, slp->prev, pring->postbufq_cnt);
10117
10118 return NULL;
10119}
dea31012005-04-17 16:05:31 -050010120
James Smarte59058c2008-08-24 21:49:00 -040010121/**
James Smart3621a712009-04-06 18:47:14 -040010122 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010123 * @phba: Pointer to HBA context object.
10124 * @pring: Pointer to driver SLI ring object.
10125 * @phys: DMA address of the buffer.
10126 *
10127 * This function searches the buffer list using the dma_address
10128 * of unsolicited event to find the driver's lpfc_dmabuf object
10129 * corresponding to the dma_address. The function returns the
10130 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10131 * This function is called by the ct and els unsolicited event
10132 * handlers to get the buffer associated with the unsolicited
10133 * event.
10134 *
10135 * This function is called with no lock held.
10136 **/
dea31012005-04-17 16:05:31 -050010137struct lpfc_dmabuf *
10138lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10139 dma_addr_t phys)
10140{
10141 struct lpfc_dmabuf *mp, *next_mp;
10142 struct list_head *slp = &pring->postbufq;
10143
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010144 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010145 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010146 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10147 if (mp->phys == phys) {
10148 list_del_init(&mp->list);
10149 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010150 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010151 return mp;
10152 }
10153 }
10154
James Smart2e0fef82007-06-17 19:56:36 -050010155 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010156 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010157 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010158 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010159 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010160 slp->next, slp->prev, pring->postbufq_cnt);
10161 return NULL;
10162}
10163
James Smarte59058c2008-08-24 21:49:00 -040010164/**
James Smart3621a712009-04-06 18:47:14 -040010165 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010166 * @phba: Pointer to HBA context object.
10167 * @cmdiocb: Pointer to driver command iocb object.
10168 * @rspiocb: Pointer to driver response iocb object.
10169 *
10170 * This function is the completion handler for the abort iocbs for
10171 * ELS commands. This function is called from the ELS ring event
10172 * handler with no lock held. This function frees memory resources
10173 * associated with the abort iocb.
10174 **/
dea31012005-04-17 16:05:31 -050010175static void
James Smart2e0fef82007-06-17 19:56:36 -050010176lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10177 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010178{
James Smart2e0fef82007-06-17 19:56:36 -050010179 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010180 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010181 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010182
10183 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010184
10185 /*
10186 * Assume that the port already completed and returned, or
10187 * will return the iocb. Just Log the message.
10188 */
James Smart2680eea2007-04-25 09:52:55 -040010189 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10190 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10191
James Smart2e0fef82007-06-17 19:56:36 -050010192 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010193 if (phba->sli_rev < LPFC_SLI_REV4) {
10194 if (abort_iotag != 0 &&
10195 abort_iotag <= phba->sli.last_iotag)
10196 abort_iocb =
10197 phba->sli.iocbq_lookup[abort_iotag];
10198 } else
10199 /* For sli4 the abort_tag is the XRI,
10200 * so the abort routine puts the iotag of the iocb
10201 * being aborted in the context field of the abort
10202 * IOCB.
10203 */
10204 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010205
James Smart2a9bf3d2010-06-07 15:24:45 -040010206 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10207 "0327 Cannot abort els iocb %p "
10208 "with tag %x context %x, abort status %x, "
10209 "abort code %x\n",
10210 abort_iocb, abort_iotag, abort_context,
10211 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010212
James Smartff78d8f2011-12-13 13:21:35 -050010213 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010214 }
James Bottomley604a3e32005-10-29 10:28:33 -050010215 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010216 return;
10217}
10218
James Smarte59058c2008-08-24 21:49:00 -040010219/**
James Smart3621a712009-04-06 18:47:14 -040010220 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010221 * @phba: Pointer to HBA context object.
10222 * @cmdiocb: Pointer to driver command iocb object.
10223 * @rspiocb: Pointer to driver response iocb object.
10224 *
10225 * The function is called from SLI ring event handler with no
10226 * lock held. This function is the completion handler for ELS commands
10227 * which are aborted. The function frees memory resources used for
10228 * the aborted ELS commands.
10229 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010230static void
10231lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10232 struct lpfc_iocbq *rspiocb)
10233{
10234 IOCB_t *irsp = &rspiocb->iocb;
10235
10236 /* ELS cmd tag <ulpIoTag> completes */
10237 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010238 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010239 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010240 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010241 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010242 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10243 lpfc_ct_free_iocb(phba, cmdiocb);
10244 else
10245 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010246 return;
10247}
10248
James Smarte59058c2008-08-24 21:49:00 -040010249/**
James Smart5af5eee2010-10-22 11:06:38 -040010250 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010251 * @phba: Pointer to HBA context object.
10252 * @pring: Pointer to driver SLI ring object.
10253 * @cmdiocb: Pointer to driver command iocb object.
10254 *
James Smart5af5eee2010-10-22 11:06:38 -040010255 * This function issues an abort iocb for the provided command iocb down to
10256 * the port. Other than the case the outstanding command iocb is an abort
10257 * request, this function issues abort out unconditionally. This function is
10258 * called with hbalock held. The function returns 0 when it fails due to
10259 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010260 **/
James Smart5af5eee2010-10-22 11:06:38 -040010261static int
10262lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010263 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010264{
James Smart2e0fef82007-06-17 19:56:36 -050010265 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010266 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010267 IOCB_t *icmd = NULL;
10268 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010269 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010270 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010271
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010272 lockdep_assert_held(&phba->hbalock);
10273
James Smart92d7f7b2007-06-17 19:56:38 -050010274 /*
10275 * There are certain command types we don't want to abort. And we
10276 * don't want to abort commands that are already in the process of
10277 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010278 */
10279 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010280 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010281 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10282 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010283 return 0;
10284
dea31012005-04-17 16:05:31 -050010285 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010286 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010287 if (abtsiocbp == NULL)
10288 return 0;
dea31012005-04-17 16:05:31 -050010289
James Smart07951072007-04-25 09:51:38 -040010290 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010291 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010292 */
10293 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10294
dea31012005-04-17 16:05:31 -050010295 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010296 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10297 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010298 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010299 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010300 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10301 }
James Smartda0436e2009-05-22 14:51:39 -040010302 else
10303 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010304 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010305 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010306
James Smart5ffc2662009-11-18 15:39:44 -050010307 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010308 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010309 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10310 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010311 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10312 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010313
James Smart2e0fef82007-06-17 19:56:36 -050010314 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010315 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10316 else
10317 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10318
10319 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010320 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010321
James Smarte8b62012007-08-02 11:10:09 -040010322 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10323 "0339 Abort xri x%x, original iotag x%x, "
10324 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010325 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010326 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010327 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010328
10329 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010330 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10331 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010332 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010333 /* Note: both hbalock and ring_lock need to be set here */
10334 spin_lock_irqsave(&pring->ring_lock, iflags);
10335 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10336 abtsiocbp, 0);
10337 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10338 } else {
10339 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10340 abtsiocbp, 0);
10341 }
James Smart07951072007-04-25 09:51:38 -040010342
James Smartd7c255b2008-08-24 21:50:00 -040010343 if (retval)
10344 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010345
10346 /*
10347 * Caller to this routine should check for IOCB_ERROR
10348 * and handle it properly. This routine no longer removes
10349 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10350 */
10351 return retval;
10352}
10353
10354/**
10355 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10356 * @phba: Pointer to HBA context object.
10357 * @pring: Pointer to driver SLI ring object.
10358 * @cmdiocb: Pointer to driver command iocb object.
10359 *
10360 * This function issues an abort iocb for the provided command iocb. In case
10361 * of unloading, the abort iocb will not be issued to commands on the ELS
10362 * ring. Instead, the callback function shall be changed to those commands
10363 * so that nothing happens when them finishes. This function is called with
10364 * hbalock held. The function returns 0 when the command iocb is an abort
10365 * request.
10366 **/
10367int
10368lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10369 struct lpfc_iocbq *cmdiocb)
10370{
10371 struct lpfc_vport *vport = cmdiocb->vport;
10372 int retval = IOCB_ERROR;
10373 IOCB_t *icmd = NULL;
10374
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010375 lockdep_assert_held(&phba->hbalock);
10376
James Smart5af5eee2010-10-22 11:06:38 -040010377 /*
10378 * There are certain command types we don't want to abort. And we
10379 * don't want to abort commands that are already in the process of
10380 * being aborted.
10381 */
10382 icmd = &cmdiocb->iocb;
10383 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10384 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10385 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10386 return 0;
10387
10388 /*
10389 * If we're unloading, don't abort iocb on the ELS ring, but change
10390 * the callback so that nothing happens when it finishes.
10391 */
10392 if ((vport->load_flag & FC_UNLOADING) &&
10393 (pring->ringno == LPFC_ELS_RING)) {
10394 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10395 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10396 else
10397 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10398 goto abort_iotag_exit;
10399 }
10400
10401 /* Now, we try to issue the abort to the cmdiocb out */
10402 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10403
James Smart07951072007-04-25 09:51:38 -040010404abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010405 /*
10406 * Caller to this routine should check for IOCB_ERROR
10407 * and handle it properly. This routine no longer removes
10408 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010409 */
James Smart2e0fef82007-06-17 19:56:36 -050010410 return retval;
dea31012005-04-17 16:05:31 -050010411}
10412
James Smarte59058c2008-08-24 21:49:00 -040010413/**
James Smart895427b2017-02-12 13:52:30 -080010414 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10415 * @phba: Pointer to HBA context object.
10416 * @pring: Pointer to driver SLI ring object.
10417 * @cmdiocb: Pointer to driver command iocb object.
10418 *
10419 * This function issues an abort iocb for the provided command iocb down to
10420 * the port. Other than the case the outstanding command iocb is an abort
10421 * request, this function issues abort out unconditionally. This function is
10422 * called with hbalock held. The function returns 0 when it fails due to
10423 * memory allocation failure or when the command iocb is an abort request.
10424 **/
10425static int
10426lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10427 struct lpfc_iocbq *cmdiocb)
10428{
10429 struct lpfc_vport *vport = cmdiocb->vport;
10430 struct lpfc_iocbq *abtsiocbp;
10431 union lpfc_wqe *abts_wqe;
10432 int retval;
10433
10434 /*
10435 * There are certain command types we don't want to abort. And we
10436 * don't want to abort commands that are already in the process of
10437 * being aborted.
10438 */
10439 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10440 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10441 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10442 return 0;
10443
10444 /* issue ABTS for this io based on iotag */
10445 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10446 if (abtsiocbp == NULL)
10447 return 0;
10448
10449 /* This signals the response to set the correct status
10450 * before calling the completion handler
10451 */
10452 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10453
10454 /* Complete prepping the abort wqe and issue to the FW. */
10455 abts_wqe = &abtsiocbp->wqe;
10456 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10457 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10458
10459 /* Explicitly set reserved fields to zero.*/
10460 abts_wqe->abort_cmd.rsrvd4 = 0;
10461 abts_wqe->abort_cmd.rsrvd5 = 0;
10462
10463 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10464 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10465 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10466
10467 /* word 7 */
10468 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10469 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10470 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10471 cmdiocb->iocb.ulpClass);
10472
10473 /* word 8 - tell the FW to abort the IO associated with this
10474 * outstanding exchange ID.
10475 */
10476 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10477
10478 /* word 9 - this is the iotag for the abts_wqe completion. */
10479 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10480 abtsiocbp->iotag);
10481
10482 /* word 10 */
10483 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10484 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10485 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10486
10487 /* word 11 */
10488 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10489 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10490 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10491
10492 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10493 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10494 abtsiocbp->vport = vport;
10495 /* todo: assign wqe_cmpl to lpfc_nvme_abort_fcreq_cmpl
10496 * subsequent patch will add routine. For now, just skip assignment
10497 * as won't ever be called.
10498 */
10499 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
10500 if (retval == IOCB_ERROR) {
10501 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10502 "6147 Failed abts issue_wqe with status x%x "
10503 "for oxid x%x\n",
10504 retval, cmdiocb->sli4_xritag);
10505 lpfc_sli_release_iocbq(phba, abtsiocbp);
10506 return retval;
10507 }
10508
10509 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10510 "6148 Drv Abort NVME Request Issued for "
10511 "ox_id x%x on reqtag x%x\n",
10512 cmdiocb->sli4_xritag,
10513 abtsiocbp->iotag);
10514
10515 return retval;
10516}
10517
10518/**
James Smart5af5eee2010-10-22 11:06:38 -040010519 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10520 * @phba: pointer to lpfc HBA data structure.
10521 *
10522 * This routine will abort all pending and outstanding iocbs to an HBA.
10523 **/
10524void
10525lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10526{
10527 struct lpfc_sli *psli = &phba->sli;
10528 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080010529 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010530 int i;
10531
James Smart895427b2017-02-12 13:52:30 -080010532 if (phba->sli_rev != LPFC_SLI_REV4) {
10533 for (i = 0; i < psli->num_rings; i++) {
10534 pring = &psli->sli3_ring[i];
10535 lpfc_sli_abort_iocb_ring(phba, pring);
10536 }
10537 return;
10538 }
10539 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10540 pring = qp->pring;
10541 if (!pring)
10542 continue;
James Smartdb55fba2014-04-04 13:52:02 -040010543 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010544 }
10545}
10546
10547/**
James Smart3621a712009-04-06 18:47:14 -040010548 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010549 * @iocbq: Pointer to driver iocb object.
10550 * @vport: Pointer to driver virtual port object.
10551 * @tgt_id: SCSI ID of the target.
10552 * @lun_id: LUN ID of the scsi device.
10553 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10554 *
James Smart3621a712009-04-06 18:47:14 -040010555 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010556 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10557 * 0 if the filtering criteria is met for the given iocb and will return
10558 * 1 if the filtering criteria is not met.
10559 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10560 * given iocb is for the SCSI device specified by vport, tgt_id and
10561 * lun_id parameter.
10562 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10563 * given iocb is for the SCSI target specified by vport and tgt_id
10564 * parameters.
10565 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10566 * given iocb is for the SCSI host associated with the given vport.
10567 * This function is called with no locks held.
10568 **/
dea31012005-04-17 16:05:31 -050010569static int
James Smart51ef4c22007-08-02 11:10:31 -040010570lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10571 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010572 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010573{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010574 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010575 int rc = 1;
10576
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010577 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10578 return rc;
10579
James Smart51ef4c22007-08-02 11:10:31 -040010580 if (iocbq->vport != vport)
10581 return rc;
10582
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010583 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010584
James Smart495a7142008-06-14 22:52:59 -040010585 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010586 return rc;
10587
10588 switch (ctx_cmd) {
10589 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010590 if ((lpfc_cmd->rdata->pnode) &&
10591 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10592 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010593 rc = 0;
10594 break;
10595 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010596 if ((lpfc_cmd->rdata->pnode) &&
10597 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010598 rc = 0;
10599 break;
dea31012005-04-17 16:05:31 -050010600 case LPFC_CTX_HOST:
10601 rc = 0;
10602 break;
10603 default:
10604 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010605 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010606 break;
10607 }
10608
10609 return rc;
10610}
10611
James Smarte59058c2008-08-24 21:49:00 -040010612/**
James Smart3621a712009-04-06 18:47:14 -040010613 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010614 * @vport: Pointer to virtual port.
10615 * @tgt_id: SCSI ID of the target.
10616 * @lun_id: LUN ID of the scsi device.
10617 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10618 *
10619 * This function returns number of FCP commands pending for the vport.
10620 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10621 * commands pending on the vport associated with SCSI device specified
10622 * by tgt_id and lun_id parameters.
10623 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10624 * commands pending on the vport associated with SCSI target specified
10625 * by tgt_id parameter.
10626 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10627 * commands pending on the vport.
10628 * This function returns the number of iocbs which satisfy the filter.
10629 * This function is called without any lock held.
10630 **/
dea31012005-04-17 16:05:31 -050010631int
James Smart51ef4c22007-08-02 11:10:31 -040010632lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10633 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010634{
James Smart51ef4c22007-08-02 11:10:31 -040010635 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010636 struct lpfc_iocbq *iocbq;
10637 int sum, i;
dea31012005-04-17 16:05:31 -050010638
Johannes Thumshirn31979002016-07-18 16:06:03 +020010639 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010640 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10641 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010642
James Smart51ef4c22007-08-02 11:10:31 -040010643 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10644 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010645 sum++;
dea31012005-04-17 16:05:31 -050010646 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010647 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010648
dea31012005-04-17 16:05:31 -050010649 return sum;
10650}
10651
James Smarte59058c2008-08-24 21:49:00 -040010652/**
James Smart3621a712009-04-06 18:47:14 -040010653 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010654 * @phba: Pointer to HBA context object
10655 * @cmdiocb: Pointer to command iocb object.
10656 * @rspiocb: Pointer to response iocb object.
10657 *
10658 * This function is called when an aborted FCP iocb completes. This
10659 * function is called by the ring event handler with no lock held.
10660 * This function frees the iocb.
10661 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010662void
James Smart2e0fef82007-06-17 19:56:36 -050010663lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10664 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010665{
James Smartcb69f7d2011-12-13 13:21:57 -050010666 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010667 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010668 "original iotag x%x, abort cmd iotag x%x "
10669 "status 0x%x, reason 0x%x\n",
10670 cmdiocb->iocb.un.acxri.abortContextTag,
10671 cmdiocb->iocb.un.acxri.abortIoTag,
10672 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10673 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010674 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010675 return;
10676}
10677
James Smarte59058c2008-08-24 21:49:00 -040010678/**
James Smart3621a712009-04-06 18:47:14 -040010679 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010680 * @vport: Pointer to virtual port.
10681 * @pring: Pointer to driver SLI ring object.
10682 * @tgt_id: SCSI ID of the target.
10683 * @lun_id: LUN ID of the scsi device.
10684 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10685 *
10686 * This function sends an abort command for every SCSI command
10687 * associated with the given virtual port pending on the ring
10688 * filtered by lpfc_sli_validate_fcp_iocb function.
10689 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10690 * FCP iocbs associated with lun specified by tgt_id and lun_id
10691 * parameters
10692 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10693 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10694 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10695 * FCP iocbs associated with virtual port.
10696 * This function returns number of iocbs it failed to abort.
10697 * This function is called with no locks held.
10698 **/
dea31012005-04-17 16:05:31 -050010699int
James Smart51ef4c22007-08-02 11:10:31 -040010700lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10701 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010702{
James Smart51ef4c22007-08-02 11:10:31 -040010703 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010704 struct lpfc_iocbq *iocbq;
10705 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010706 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010707 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010708 int i;
dea31012005-04-17 16:05:31 -050010709
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010710 for (i = 1; i <= phba->sli.last_iotag; i++) {
10711 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010712
James Smart51ef4c22007-08-02 11:10:31 -040010713 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010714 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010715 continue;
10716
James Smartafbd8d82013-09-06 12:22:13 -040010717 /*
10718 * If the iocbq is already being aborted, don't take a second
10719 * action, but do count it.
10720 */
10721 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10722 continue;
10723
dea31012005-04-17 16:05:31 -050010724 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010725 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010726 if (abtsiocb == NULL) {
10727 errcnt++;
10728 continue;
10729 }
dea31012005-04-17 16:05:31 -050010730
James Smartafbd8d82013-09-06 12:22:13 -040010731 /* indicate the IO is being aborted by the driver. */
10732 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10733
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010734 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010735 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10736 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010737 if (phba->sli_rev == LPFC_SLI_REV4)
10738 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10739 else
10740 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010741 abtsiocb->iocb.ulpLe = 1;
10742 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010743 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010744
James Smart5ffc2662009-11-18 15:39:44 -050010745 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010746 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010747 if (iocbq->iocb_flag & LPFC_IO_FCP)
10748 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010749 if (iocbq->iocb_flag & LPFC_IO_FOF)
10750 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010751
James Smart2e0fef82007-06-17 19:56:36 -050010752 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010753 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10754 else
10755 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10756
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010757 /* Setup callback routine and issue the command. */
10758 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010759 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10760 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010761 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010762 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010763 errcnt++;
10764 continue;
10765 }
10766 }
10767
10768 return errcnt;
10769}
10770
James Smarte59058c2008-08-24 21:49:00 -040010771/**
James Smart98912dda2014-04-04 13:52:31 -040010772 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10773 * @vport: Pointer to virtual port.
10774 * @pring: Pointer to driver SLI ring object.
10775 * @tgt_id: SCSI ID of the target.
10776 * @lun_id: LUN ID of the scsi device.
10777 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10778 *
10779 * This function sends an abort command for every SCSI command
10780 * associated with the given virtual port pending on the ring
10781 * filtered by lpfc_sli_validate_fcp_iocb function.
10782 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10783 * FCP iocbs associated with lun specified by tgt_id and lun_id
10784 * parameters
10785 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10786 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10787 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10788 * FCP iocbs associated with virtual port.
10789 * This function returns number of iocbs it aborted .
10790 * This function is called with no locks held right after a taskmgmt
10791 * command is sent.
10792 **/
10793int
10794lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10795 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10796{
10797 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040010798 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040010799 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040010800 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040010801 struct lpfc_iocbq *iocbq;
10802 IOCB_t *icmd;
10803 int sum, i, ret_val;
10804 unsigned long iflags;
10805 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040010806
10807 spin_lock_irq(&phba->hbalock);
10808
10809 /* all I/Os are in process of being flushed */
10810 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10811 spin_unlock_irq(&phba->hbalock);
10812 return 0;
10813 }
10814 sum = 0;
10815
10816 for (i = 1; i <= phba->sli.last_iotag; i++) {
10817 iocbq = phba->sli.iocbq_lookup[i];
10818
10819 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10820 cmd) != 0)
10821 continue;
10822
10823 /*
10824 * If the iocbq is already being aborted, don't take a second
10825 * action, but do count it.
10826 */
10827 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10828 continue;
10829
10830 /* issue ABTS for this IOCB based on iotag */
10831 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10832 if (abtsiocbq == NULL)
10833 continue;
10834
10835 icmd = &iocbq->iocb;
10836 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10837 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10838 if (phba->sli_rev == LPFC_SLI_REV4)
10839 abtsiocbq->iocb.un.acxri.abortIoTag =
10840 iocbq->sli4_xritag;
10841 else
10842 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10843 abtsiocbq->iocb.ulpLe = 1;
10844 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10845 abtsiocbq->vport = vport;
10846
10847 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010848 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040010849 if (iocbq->iocb_flag & LPFC_IO_FCP)
10850 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010851 if (iocbq->iocb_flag & LPFC_IO_FOF)
10852 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040010853
James Smart8c50d252014-09-03 12:58:16 -040010854 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10855 ndlp = lpfc_cmd->rdata->pnode;
10856
10857 if (lpfc_is_link_up(phba) &&
10858 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040010859 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10860 else
10861 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10862
10863 /* Setup callback routine and issue the command. */
10864 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10865
10866 /*
10867 * Indicate the IO is being aborted by the driver and set
10868 * the caller's flag into the aborted IO.
10869 */
10870 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10871
10872 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010873 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
10874 if (pring_s4 == NULL)
10875 continue;
James Smart98912dda2014-04-04 13:52:31 -040010876 /* Note: both hbalock and ring_lock must be set here */
10877 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10878 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10879 abtsiocbq, 0);
10880 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10881 } else {
10882 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10883 abtsiocbq, 0);
10884 }
10885
10886
10887 if (ret_val == IOCB_ERROR)
10888 __lpfc_sli_release_iocbq(phba, abtsiocbq);
10889 else
10890 sum++;
10891 }
10892 spin_unlock_irq(&phba->hbalock);
10893 return sum;
10894}
10895
10896/**
James Smart3621a712009-04-06 18:47:14 -040010897 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040010898 * @phba: Pointer to HBA context object.
10899 * @cmdiocbq: Pointer to command iocb.
10900 * @rspiocbq: Pointer to response iocb.
10901 *
10902 * This function is the completion handler for iocbs issued using
10903 * lpfc_sli_issue_iocb_wait function. This function is called by the
10904 * ring event handler function without any lock held. This function
10905 * can be called from both worker thread context and interrupt
10906 * context. This function also can be called from other thread which
10907 * cleans up the SLI layer objects.
10908 * This function copy the contents of the response iocb to the
10909 * response iocb memory object provided by the caller of
10910 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10911 * sleeps for the iocb completion.
10912 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010913static void
10914lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10915 struct lpfc_iocbq *cmdiocbq,
10916 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050010917{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010918 wait_queue_head_t *pdone_q;
10919 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050010920 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010921
James Smart2e0fef82007-06-17 19:56:36 -050010922 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040010923 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10924
10925 /*
10926 * A time out has occurred for the iocb. If a time out
10927 * completion handler has been supplied, call it. Otherwise,
10928 * just free the iocbq.
10929 */
10930
10931 spin_unlock_irqrestore(&phba->hbalock, iflags);
10932 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10933 cmdiocbq->wait_iocb_cmpl = NULL;
10934 if (cmdiocbq->iocb_cmpl)
10935 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10936 else
10937 lpfc_sli_release_iocbq(phba, cmdiocbq);
10938 return;
10939 }
10940
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010941 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10942 if (cmdiocbq->context2 && rspiocbq)
10943 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10944 &rspiocbq->iocb, sizeof(IOCB_t));
10945
James Smart0f65ff62010-02-26 14:14:23 -050010946 /* Set the exchange busy flag for task management commands */
10947 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10948 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10949 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10950 cur_iocbq);
10951 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10952 }
10953
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010954 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010955 if (pdone_q)
10956 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050010957 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050010958 return;
10959}
10960
James Smarte59058c2008-08-24 21:49:00 -040010961/**
James Smartd11e31d2009-06-10 17:23:06 -040010962 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10963 * @phba: Pointer to HBA context object..
10964 * @piocbq: Pointer to command iocb.
10965 * @flag: Flag to test.
10966 *
10967 * This routine grabs the hbalock and then test the iocb_flag to
10968 * see if the passed in flag is set.
10969 * Returns:
10970 * 1 if flag is set.
10971 * 0 if flag is not set.
10972 **/
10973static int
10974lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10975 struct lpfc_iocbq *piocbq, uint32_t flag)
10976{
10977 unsigned long iflags;
10978 int ret;
10979
10980 spin_lock_irqsave(&phba->hbalock, iflags);
10981 ret = piocbq->iocb_flag & flag;
10982 spin_unlock_irqrestore(&phba->hbalock, iflags);
10983 return ret;
10984
10985}
10986
10987/**
James Smart3621a712009-04-06 18:47:14 -040010988 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040010989 * @phba: Pointer to HBA context object..
10990 * @pring: Pointer to sli ring.
10991 * @piocb: Pointer to command iocb.
10992 * @prspiocbq: Pointer to response iocb.
10993 * @timeout: Timeout in number of seconds.
10994 *
10995 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040010996 * iocb to complete. The iocb_cmpl field of the shall be used
10997 * to handle iocbs which time out. If the field is NULL, the
10998 * function shall free the iocbq structure. If more clean up is
10999 * needed, the caller is expected to provide a completion function
11000 * that will provide the needed clean up. If the iocb command is
11001 * not completed within timeout seconds, the function will either
11002 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11003 * completion function set in the iocb_cmpl field and then return
11004 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11005 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011006 * The function waits for the iocb completion using an
11007 * non-interruptible wait.
11008 * This function will sleep while waiting for iocb completion.
11009 * So, this function should not be called from any context which
11010 * does not allow sleeping. Due to the same reason, this function
11011 * cannot be called with interrupt disabled.
11012 * This function assumes that the iocb completions occur while
11013 * this function sleep. So, this function cannot be called from
11014 * the thread which process iocb completion for this ring.
11015 * This function clears the iocb_flag of the iocb object before
11016 * issuing the iocb and the iocb completion handler sets this
11017 * flag and wakes this thread when the iocb completes.
11018 * The contents of the response iocb will be copied to prspiocbq
11019 * by the completion handler when the command completes.
11020 * This function returns IOCB_SUCCESS when success.
11021 * This function is called with no lock held.
11022 **/
dea31012005-04-17 16:05:31 -050011023int
James Smart2e0fef82007-06-17 19:56:36 -050011024lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011025 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011026 struct lpfc_iocbq *piocb,
11027 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011028 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011029{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011030 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011031 long timeleft, timeout_req = 0;
11032 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011033 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011034 struct lpfc_iocbq *iocb;
11035 int txq_cnt = 0;
11036 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011037 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011038 unsigned long iflags;
11039 bool iocb_completed = true;
11040
James Smart895427b2017-02-12 13:52:30 -080011041 if (phba->sli_rev >= LPFC_SLI_REV4)
11042 pring = lpfc_sli4_calc_ring(phba, piocb);
11043 else
11044 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011045 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011046 * If the caller has provided a response iocbq buffer, then context2
11047 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011048 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011049 if (prspiocbq) {
11050 if (piocb->context2)
11051 return IOCB_ERROR;
11052 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011053 }
11054
James Smart5a0916b2013-07-15 18:31:42 -040011055 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011056 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11057 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011058 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011059
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011060 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011061 if (lpfc_readl(phba->HCregaddr, &creg_val))
11062 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011063 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11064 writel(creg_val, phba->HCregaddr);
11065 readl(phba->HCregaddr); /* flush */
11066 }
11067
James Smart2a9bf3d2010-06-07 15:24:45 -040011068 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11069 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011070 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011071 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011072 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011073 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011074 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011075 spin_lock_irqsave(&phba->hbalock, iflags);
11076 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011077
James Smart5a0916b2013-07-15 18:31:42 -040011078 /*
11079 * IOCB timed out. Inform the wake iocb wait
11080 * completion function and set local status
11081 */
11082
11083 iocb_completed = false;
11084 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11085 }
11086 spin_unlock_irqrestore(&phba->hbalock, iflags);
11087 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011088 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011089 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011090 /* Note: we are not indicating if the IOCB has a success
11091 * status or not - that's for the caller to check.
11092 * IOCB_SUCCESS means just that the command was sent and
11093 * completed. Not that it completed successfully.
11094 * */
James Smart7054a602007-04-25 09:52:34 -040011095 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011096 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011097 "0338 IOCB wait timeout error - no "
11098 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011099 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011100 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011101 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011102 "0330 IOCB wake NOT set, "
11103 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011104 timeout, (timeleft / jiffies));
11105 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011106 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011107 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011108 if (phba->cfg_log_verbose & LOG_SLI) {
11109 list_for_each_entry(iocb, &pring->txq, list) {
11110 txq_cnt++;
11111 }
11112 list_for_each_entry(iocb, &pring->txcmplq, list) {
11113 txcmplq_cnt++;
11114 }
11115 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11116 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11117 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11118 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011119 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011120 } else {
11121 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011122 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011123 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011124 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011125 }
11126
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011127 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011128 if (lpfc_readl(phba->HCregaddr, &creg_val))
11129 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011130 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11131 writel(creg_val, phba->HCregaddr);
11132 readl(phba->HCregaddr); /* flush */
11133 }
11134
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011135 if (prspiocbq)
11136 piocb->context2 = NULL;
11137
11138 piocb->context_un.wait_queue = NULL;
11139 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011140 return retval;
11141}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011142
James Smarte59058c2008-08-24 21:49:00 -040011143/**
James Smart3621a712009-04-06 18:47:14 -040011144 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011145 * @phba: Pointer to HBA context object.
11146 * @pmboxq: Pointer to driver mailbox object.
11147 * @timeout: Timeout in number of seconds.
11148 *
11149 * This function issues the mailbox to firmware and waits for the
11150 * mailbox command to complete. If the mailbox command is not
11151 * completed within timeout seconds, it returns MBX_TIMEOUT.
11152 * The function waits for the mailbox completion using an
11153 * interruptible wait. If the thread is woken up due to a
11154 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11155 * should not free the mailbox resources, if this function returns
11156 * MBX_TIMEOUT.
11157 * This function will sleep while waiting for mailbox completion.
11158 * So, this function should not be called from any context which
11159 * does not allow sleeping. Due to the same reason, this function
11160 * cannot be called with interrupt disabled.
11161 * This function assumes that the mailbox completion occurs while
11162 * this function sleep. So, this function cannot be called from
11163 * the worker thread which processes mailbox completion.
11164 * This function is called in the context of HBA management
11165 * applications.
11166 * This function returns MBX_SUCCESS when successful.
11167 * This function is called with no lock held.
11168 **/
dea31012005-04-17 16:05:31 -050011169int
James Smart2e0fef82007-06-17 19:56:36 -050011170lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011171 uint32_t timeout)
11172{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011173 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040011174 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050011175 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011176 unsigned long flag;
dea31012005-04-17 16:05:31 -050011177
James Smartb230b8a2013-05-31 17:05:27 -040011178 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040011179 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040011180 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050011181
James Smart495a7142008-06-14 22:52:59 -040011182 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011183 /* setup wake call as IOCB callback */
11184 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11185 /* setup context field to pass wait_queue pointer to wake function */
11186 pmboxq->context1 = &done_q;
11187
dea31012005-04-17 16:05:31 -050011188 /* now issue the command */
11189 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011190 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040011191 wait_event_interruptible_timeout(done_q,
11192 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040011193 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011194
James Smart858c9f62007-06-17 19:56:39 -050011195 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011196 /* restore the possible extended buffer for free resource */
11197 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040011198 /*
11199 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11200 * else do not free the resources.
11201 */
James Smartd7c47992010-06-08 18:31:54 -040011202 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011203 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011204 } else {
James Smart7054a602007-04-25 09:52:34 -040011205 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011206 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11207 }
11208 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011209 } else {
11210 /* restore the possible extended buffer for free resource */
11211 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050011212 }
11213
dea31012005-04-17 16:05:31 -050011214 return retval;
11215}
11216
James Smarte59058c2008-08-24 21:49:00 -040011217/**
James Smart3772a992009-05-22 14:50:54 -040011218 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011219 * @phba: Pointer to HBA context.
11220 *
James Smart3772a992009-05-22 14:50:54 -040011221 * This function is called to shutdown the driver's mailbox sub-system.
11222 * It first marks the mailbox sub-system is in a block state to prevent
11223 * the asynchronous mailbox command from issued off the pending mailbox
11224 * command queue. If the mailbox command sub-system shutdown is due to
11225 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11226 * the mailbox sub-system flush routine to forcefully bring down the
11227 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11228 * as with offline or HBA function reset), this routine will wait for the
11229 * outstanding mailbox command to complete before invoking the mailbox
11230 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011231 **/
James Smart3772a992009-05-22 14:50:54 -040011232void
James Smart618a5232012-06-12 13:54:36 -040011233lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011234{
James Smart3772a992009-05-22 14:50:54 -040011235 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011236 unsigned long timeout;
11237
James Smart618a5232012-06-12 13:54:36 -040011238 if (mbx_action == LPFC_MBX_NO_WAIT) {
11239 /* delay 100ms for port state */
11240 msleep(100);
11241 lpfc_sli_mbox_sys_flush(phba);
11242 return;
11243 }
James Smarta183a152011-10-10 21:32:43 -040011244 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011245
James Smart3772a992009-05-22 14:50:54 -040011246 spin_lock_irq(&phba->hbalock);
11247 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011248
11249 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011250 /* Determine how long we might wait for the active mailbox
11251 * command to be gracefully completed by firmware.
11252 */
James Smarta183a152011-10-10 21:32:43 -040011253 if (phba->sli.mbox_active)
11254 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11255 phba->sli.mbox_active) *
11256 1000) + jiffies;
11257 spin_unlock_irq(&phba->hbalock);
11258
James Smart3772a992009-05-22 14:50:54 -040011259 while (phba->sli.mbox_active) {
11260 /* Check active mailbox complete status every 2ms */
11261 msleep(2);
11262 if (time_after(jiffies, timeout))
11263 /* Timeout, let the mailbox flush routine to
11264 * forcefully release active mailbox command
11265 */
11266 break;
11267 }
James Smartd7069f02012-03-01 22:36:29 -050011268 } else
11269 spin_unlock_irq(&phba->hbalock);
11270
James Smart3772a992009-05-22 14:50:54 -040011271 lpfc_sli_mbox_sys_flush(phba);
11272}
11273
11274/**
11275 * lpfc_sli_eratt_read - read sli-3 error attention events
11276 * @phba: Pointer to HBA context.
11277 *
11278 * This function is called to read the SLI3 device error attention registers
11279 * for possible error attention events. The caller must hold the hostlock
11280 * with spin_lock_irq().
11281 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011282 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011283 * Register and returns 0 otherwise.
11284 **/
11285static int
11286lpfc_sli_eratt_read(struct lpfc_hba *phba)
11287{
James Smarted957682007-06-17 19:56:37 -050011288 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011289
James Smart3772a992009-05-22 14:50:54 -040011290 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011291 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11292 goto unplug_err;
11293
James Smart3772a992009-05-22 14:50:54 -040011294 if (ha_copy & HA_ERATT) {
11295 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011296 if (lpfc_sli_read_hs(phba))
11297 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011298
James Smart3772a992009-05-22 14:50:54 -040011299 /* Check if there is a deferred error condition is active */
11300 if ((HS_FFER1 & phba->work_hs) &&
11301 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011302 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011303 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011304 /* Clear all interrupt enable conditions */
11305 writel(0, phba->HCregaddr);
11306 readl(phba->HCregaddr);
11307 }
11308
11309 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011310 phba->work_ha |= HA_ERATT;
11311 /* Indicate polling handles this ERATT */
11312 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011313 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011314 }
James Smart3772a992009-05-22 14:50:54 -040011315 return 0;
James Smart9940b972011-03-11 16:06:12 -050011316
11317unplug_err:
11318 /* Set the driver HS work bitmap */
11319 phba->work_hs |= UNPLUG_ERR;
11320 /* Set the driver HA work bitmap */
11321 phba->work_ha |= HA_ERATT;
11322 /* Indicate polling handles this ERATT */
11323 phba->hba_flag |= HBA_ERATT_HANDLED;
11324 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011325}
11326
James Smarte59058c2008-08-24 21:49:00 -040011327/**
James Smartda0436e2009-05-22 14:51:39 -040011328 * lpfc_sli4_eratt_read - read sli-4 error attention events
11329 * @phba: Pointer to HBA context.
11330 *
11331 * This function is called to read the SLI4 device error attention registers
11332 * for possible error attention events. The caller must hold the hostlock
11333 * with spin_lock_irq().
11334 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011335 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011336 * Register and returns 0 otherwise.
11337 **/
11338static int
11339lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11340{
11341 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011342 uint32_t if_type, portsmphr;
11343 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011344
James Smart2fcee4b2010-12-15 17:57:46 -050011345 /*
11346 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011347 * registers for error attention. This can be changed later.
11348 */
James Smart2fcee4b2010-12-15 17:57:46 -050011349 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11350 switch (if_type) {
11351 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011352 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11353 &uerr_sta_lo) ||
11354 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11355 &uerr_sta_hi)) {
11356 phba->work_hs |= UNPLUG_ERR;
11357 phba->work_ha |= HA_ERATT;
11358 phba->hba_flag |= HBA_ERATT_HANDLED;
11359 return 1;
11360 }
James Smart2fcee4b2010-12-15 17:57:46 -050011361 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11362 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11364 "1423 HBA Unrecoverable error: "
11365 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11366 "ue_mask_lo_reg=0x%x, "
11367 "ue_mask_hi_reg=0x%x\n",
11368 uerr_sta_lo, uerr_sta_hi,
11369 phba->sli4_hba.ue_mask_lo,
11370 phba->sli4_hba.ue_mask_hi);
11371 phba->work_status[0] = uerr_sta_lo;
11372 phba->work_status[1] = uerr_sta_hi;
11373 phba->work_ha |= HA_ERATT;
11374 phba->hba_flag |= HBA_ERATT_HANDLED;
11375 return 1;
11376 }
11377 break;
11378 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050011379 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11380 &portstat_reg.word0) ||
11381 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11382 &portsmphr)){
11383 phba->work_hs |= UNPLUG_ERR;
11384 phba->work_ha |= HA_ERATT;
11385 phba->hba_flag |= HBA_ERATT_HANDLED;
11386 return 1;
11387 }
James Smart2fcee4b2010-12-15 17:57:46 -050011388 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11389 phba->work_status[0] =
11390 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11391 phba->work_status[1] =
11392 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11393 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011394 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011395 "port status reg 0x%x, "
11396 "port smphr reg 0x%x, "
11397 "error 1=0x%x, error 2=0x%x\n",
11398 portstat_reg.word0,
11399 portsmphr,
11400 phba->work_status[0],
11401 phba->work_status[1]);
11402 phba->work_ha |= HA_ERATT;
11403 phba->hba_flag |= HBA_ERATT_HANDLED;
11404 return 1;
11405 }
11406 break;
11407 case LPFC_SLI_INTF_IF_TYPE_1:
11408 default:
James Smarta747c9c2009-11-18 15:41:10 -050011409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011410 "2886 HBA Error Attention on unsupported "
11411 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011412 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011413 }
James Smart2fcee4b2010-12-15 17:57:46 -050011414
James Smartda0436e2009-05-22 14:51:39 -040011415 return 0;
11416}
11417
11418/**
James Smart3621a712009-04-06 18:47:14 -040011419 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011420 * @phba: Pointer to HBA context.
11421 *
James Smart3772a992009-05-22 14:50:54 -040011422 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011423 * error attention register bit for error attention events.
11424 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011425 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011426 * Register and returns 0 otherwise.
11427 **/
11428int
11429lpfc_sli_check_eratt(struct lpfc_hba *phba)
11430{
11431 uint32_t ha_copy;
11432
11433 /* If somebody is waiting to handle an eratt, don't process it
11434 * here. The brdkill function will do this.
11435 */
11436 if (phba->link_flag & LS_IGNORE_ERATT)
11437 return 0;
11438
11439 /* Check if interrupt handler handles this ERATT */
11440 spin_lock_irq(&phba->hbalock);
11441 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11442 /* Interrupt handler has handled ERATT */
11443 spin_unlock_irq(&phba->hbalock);
11444 return 0;
11445 }
11446
James Smarta257bf92009-04-06 18:48:10 -040011447 /*
11448 * If there is deferred error attention, do not check for error
11449 * attention
11450 */
11451 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11452 spin_unlock_irq(&phba->hbalock);
11453 return 0;
11454 }
11455
James Smart3772a992009-05-22 14:50:54 -040011456 /* If PCI channel is offline, don't process it */
11457 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011458 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011459 return 0;
11460 }
11461
11462 switch (phba->sli_rev) {
11463 case LPFC_SLI_REV2:
11464 case LPFC_SLI_REV3:
11465 /* Read chip Host Attention (HA) register */
11466 ha_copy = lpfc_sli_eratt_read(phba);
11467 break;
James Smartda0436e2009-05-22 14:51:39 -040011468 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011469 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011470 ha_copy = lpfc_sli4_eratt_read(phba);
11471 break;
James Smart3772a992009-05-22 14:50:54 -040011472 default:
11473 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11474 "0299 Invalid SLI revision (%d)\n",
11475 phba->sli_rev);
11476 ha_copy = 0;
11477 break;
James Smart93996272008-08-24 21:50:30 -040011478 }
11479 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011480
11481 return ha_copy;
11482}
11483
11484/**
11485 * lpfc_intr_state_check - Check device state for interrupt handling
11486 * @phba: Pointer to HBA context.
11487 *
11488 * This inline routine checks whether a device or its PCI slot is in a state
11489 * that the interrupt should be handled.
11490 *
11491 * This function returns 0 if the device or the PCI slot is in a state that
11492 * interrupt should be handled, otherwise -EIO.
11493 */
11494static inline int
11495lpfc_intr_state_check(struct lpfc_hba *phba)
11496{
11497 /* If the pci channel is offline, ignore all the interrupts */
11498 if (unlikely(pci_channel_offline(phba->pcidev)))
11499 return -EIO;
11500
11501 /* Update device level interrupt statistics */
11502 phba->sli.slistat.sli_intr++;
11503
11504 /* Ignore all interrupts during initialization. */
11505 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11506 return -EIO;
11507
James Smart93996272008-08-24 21:50:30 -040011508 return 0;
11509}
11510
11511/**
James Smart3772a992009-05-22 14:50:54 -040011512 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011513 * @irq: Interrupt number.
11514 * @dev_id: The device context pointer.
11515 *
James Smart93996272008-08-24 21:50:30 -040011516 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011517 * service routine when device with SLI-3 interface spec is enabled with
11518 * MSI-X multi-message interrupt mode and there are slow-path events in
11519 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11520 * interrupt mode, this function is called as part of the device-level
11521 * interrupt handler. When the PCI slot is in error recovery or the HBA
11522 * is undergoing initialization, the interrupt handler will not process
11523 * the interrupt. The link attention and ELS ring attention events are
11524 * handled by the worker thread. The interrupt handler signals the worker
11525 * thread and returns for these events. This function is called without
11526 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011527 * structures.
11528 *
11529 * This function returns IRQ_HANDLED when interrupt is handled else it
11530 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011531 **/
dea31012005-04-17 16:05:31 -050011532irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011533lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011534{
James Smart2e0fef82007-06-17 19:56:36 -050011535 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011536 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011537 uint32_t work_ha_copy;
11538 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011539 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011540 uint32_t control;
11541
James Smart92d7f7b2007-06-17 19:56:38 -050011542 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011543 struct lpfc_vport *vport;
11544 struct lpfc_nodelist *ndlp;
11545 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011546 LPFC_MBOXQ_t *pmb;
11547 int rc;
11548
dea31012005-04-17 16:05:31 -050011549 /*
11550 * Get the driver's phba structure from the dev_id and
11551 * assume the HBA is not interrupting.
11552 */
James Smart93996272008-08-24 21:50:30 -040011553 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011554
11555 if (unlikely(!phba))
11556 return IRQ_NONE;
11557
dea31012005-04-17 16:05:31 -050011558 /*
James Smart93996272008-08-24 21:50:30 -040011559 * Stuff needs to be attented to when this function is invoked as an
11560 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011561 */
James Smart93996272008-08-24 21:50:30 -040011562 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011563 /* Check device state for handling interrupt */
11564 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011565 return IRQ_NONE;
11566 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011567 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011568 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11569 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011570 /* If somebody is waiting to handle an eratt don't process it
11571 * here. The brdkill function will do this.
11572 */
11573 if (phba->link_flag & LS_IGNORE_ERATT)
11574 ha_copy &= ~HA_ERATT;
11575 /* Check the need for handling ERATT in interrupt handler */
11576 if (ha_copy & HA_ERATT) {
11577 if (phba->hba_flag & HBA_ERATT_HANDLED)
11578 /* ERATT polling has handled ERATT */
11579 ha_copy &= ~HA_ERATT;
11580 else
11581 /* Indicate interrupt handler handles ERATT */
11582 phba->hba_flag |= HBA_ERATT_HANDLED;
11583 }
James Smarta257bf92009-04-06 18:48:10 -040011584
11585 /*
11586 * If there is deferred error attention, do not check for any
11587 * interrupt.
11588 */
11589 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011590 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011591 return IRQ_NONE;
11592 }
11593
James Smart93996272008-08-24 21:50:30 -040011594 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011595 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11596 goto unplug_error;
11597
James Smarta747c9c2009-11-18 15:41:10 -050011598 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11599 HC_LAINT_ENA | HC_ERINT_ENA),
11600 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011601 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11602 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011603 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011604 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011605 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011606 } else
11607 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011608
dea31012005-04-17 16:05:31 -050011609 work_ha_copy = ha_copy & phba->work_ha_mask;
11610
James Smart93996272008-08-24 21:50:30 -040011611 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011612 if (work_ha_copy & HA_LATT) {
11613 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11614 /*
11615 * Turn off Link Attention interrupts
11616 * until CLEAR_LA done
11617 */
James Smart5b75da22008-12-04 22:39:35 -050011618 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011619 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011620 if (lpfc_readl(phba->HCregaddr, &control))
11621 goto unplug_error;
dea31012005-04-17 16:05:31 -050011622 control &= ~HC_LAINT_ENA;
11623 writel(control, phba->HCregaddr);
11624 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011625 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011626 }
11627 else
11628 work_ha_copy &= ~HA_LATT;
11629 }
11630
James Smart93996272008-08-24 21:50:30 -040011631 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011632 /*
11633 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11634 * the only slow ring.
11635 */
11636 status = (work_ha_copy &
11637 (HA_RXMASK << (4*LPFC_ELS_RING)));
11638 status >>= (4*LPFC_ELS_RING);
11639 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011640 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011641 if (lpfc_readl(phba->HCregaddr, &control))
11642 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011643
11644 lpfc_debugfs_slow_ring_trc(phba,
11645 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11646 control, status,
11647 (uint32_t)phba->sli.slistat.sli_intr);
11648
James Smart858c9f62007-06-17 19:56:39 -050011649 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011650 lpfc_debugfs_slow_ring_trc(phba,
11651 "ISR Disable ring:"
11652 "pwork:x%x hawork:x%x wait:x%x",
11653 phba->work_ha, work_ha_copy,
11654 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011655 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011656
James Smart858c9f62007-06-17 19:56:39 -050011657 control &=
11658 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011659 writel(control, phba->HCregaddr);
11660 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011661 }
James Smarta58cbd52007-08-02 11:09:43 -040011662 else {
11663 lpfc_debugfs_slow_ring_trc(phba,
11664 "ISR slow ring: pwork:"
11665 "x%x hawork:x%x wait:x%x",
11666 phba->work_ha, work_ha_copy,
11667 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011668 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011669 }
James Smart5b75da22008-12-04 22:39:35 -050011670 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011671 }
11672 }
James Smart5b75da22008-12-04 22:39:35 -050011673 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011674 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011675 if (lpfc_sli_read_hs(phba))
11676 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011677 /*
11678 * Check if there is a deferred error condition
11679 * is active
11680 */
11681 if ((HS_FFER1 & phba->work_hs) &&
11682 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011683 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11684 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011685 phba->hba_flag |= DEFER_ERATT;
11686 /* Clear all interrupt enable conditions */
11687 writel(0, phba->HCregaddr);
11688 readl(phba->HCregaddr);
11689 }
11690 }
11691
James Smart93996272008-08-24 21:50:30 -040011692 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011693 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011694 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011695 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011696 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011697
11698 /* First check out the status word */
11699 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11700 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011701 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011702 /*
11703 * Stray Mailbox Interrupt, mbxCommand <cmd>
11704 * mbxStatus <status>
11705 */
James Smart09372822008-01-11 01:52:54 -050011706 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011707 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011708 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011709 "Interrupt mbxCommand x%x "
11710 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011711 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011712 pmbox->mbxCommand,
11713 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011714 /* clear mailbox attention bit */
11715 work_ha_copy &= ~HA_MBATT;
11716 } else {
James Smart97eab632008-04-07 10:16:05 -040011717 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011718 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011719 phba->last_completion_time = jiffies;
11720 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011721 if (pmb->mbox_cmpl) {
11722 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11723 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011724 if (pmb->out_ext_byte_len &&
11725 pmb->context2)
11726 lpfc_sli_pcimem_bcopy(
11727 phba->mbox_ext,
11728 pmb->context2,
11729 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011730 }
James Smart09372822008-01-11 01:52:54 -050011731 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11732 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11733
11734 lpfc_debugfs_disc_trc(vport,
11735 LPFC_DISC_TRC_MBOX_VPORT,
11736 "MBOX dflt rpi: : "
11737 "status:x%x rpi:x%x",
11738 (uint32_t)pmbox->mbxStatus,
11739 pmbox->un.varWords[0], 0);
11740
11741 if (!pmbox->mbxStatus) {
11742 mp = (struct lpfc_dmabuf *)
11743 (pmb->context1);
11744 ndlp = (struct lpfc_nodelist *)
11745 pmb->context2;
11746
11747 /* Reg_LOGIN of dflt RPI was
11748 * successful. new lets get
11749 * rid of the RPI using the
11750 * same mbox buffer.
11751 */
11752 lpfc_unreg_login(phba,
11753 vport->vpi,
11754 pmbox->un.varWords[0],
11755 pmb);
11756 pmb->mbox_cmpl =
11757 lpfc_mbx_cmpl_dflt_rpi;
11758 pmb->context1 = mp;
11759 pmb->context2 = ndlp;
11760 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040011761 rc = lpfc_sli_issue_mbox(phba,
11762 pmb,
11763 MBX_NOWAIT);
11764 if (rc != MBX_BUSY)
11765 lpfc_printf_log(phba,
11766 KERN_ERR,
11767 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011768 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040011769 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040011770 if (rc != MBX_NOT_FINISHED)
11771 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050011772 }
11773 }
James Smart5b75da22008-12-04 22:39:35 -050011774 spin_lock_irqsave(
11775 &phba->pport->work_port_lock,
11776 iflag);
James Smart09372822008-01-11 01:52:54 -050011777 phba->pport->work_port_events &=
11778 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050011779 spin_unlock_irqrestore(
11780 &phba->pport->work_port_lock,
11781 iflag);
James Smart09372822008-01-11 01:52:54 -050011782 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050011783 }
James Smart97eab632008-04-07 10:16:05 -040011784 } else
James Smart5b75da22008-12-04 22:39:35 -050011785 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011786
James Smart92d7f7b2007-06-17 19:56:38 -050011787 if ((work_ha_copy & HA_MBATT) &&
11788 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050011789send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050011790 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040011791 do {
11792 rc = lpfc_sli_issue_mbox(phba, NULL,
11793 MBX_NOWAIT);
11794 } while (rc == MBX_NOT_FINISHED);
11795 if (rc != MBX_SUCCESS)
11796 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11797 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040011798 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050011799 }
11800
James Smart5b75da22008-12-04 22:39:35 -050011801 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011802 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050011803 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040011804 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050011805 }
James Smart93996272008-08-24 21:50:30 -040011806 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050011807unplug_error:
11808 spin_unlock_irqrestore(&phba->hbalock, iflag);
11809 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050011810
James Smart3772a992009-05-22 14:50:54 -040011811} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040011812
11813/**
James Smart3772a992009-05-22 14:50:54 -040011814 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040011815 * @irq: Interrupt number.
11816 * @dev_id: The device context pointer.
11817 *
11818 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011819 * service routine when device with SLI-3 interface spec is enabled with
11820 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11821 * ring event in the HBA. However, when the device is enabled with either
11822 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11823 * device-level interrupt handler. When the PCI slot is in error recovery
11824 * or the HBA is undergoing initialization, the interrupt handler will not
11825 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11826 * the intrrupt context. This function is called without any lock held.
11827 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011828 *
11829 * This function returns IRQ_HANDLED when interrupt is handled else it
11830 * returns IRQ_NONE.
11831 **/
11832irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011833lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011834{
11835 struct lpfc_hba *phba;
11836 uint32_t ha_copy;
11837 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011838 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080011839 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040011840
11841 /* Get the driver's phba structure from the dev_id and
11842 * assume the HBA is not interrupting.
11843 */
11844 phba = (struct lpfc_hba *) dev_id;
11845
11846 if (unlikely(!phba))
11847 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050011848
11849 /*
James Smart93996272008-08-24 21:50:30 -040011850 * Stuff needs to be attented to when this function is invoked as an
11851 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011852 */
James Smart93996272008-08-24 21:50:30 -040011853 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011854 /* Check device state for handling interrupt */
11855 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011856 return IRQ_NONE;
11857 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050011858 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11859 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040011860 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050011861 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011862 /*
11863 * If there is deferred error attention, do not check for
11864 * any interrupt.
11865 */
11866 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011867 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011868 return IRQ_NONE;
11869 }
James Smart93996272008-08-24 21:50:30 -040011870 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11871 phba->HAregaddr);
11872 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011873 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011874 } else
11875 ha_copy = phba->ha_copy;
11876
11877 /*
11878 * Process all events on FCP ring. Take the optimized path for FCP IO.
11879 */
11880 ha_copy &= ~(phba->work_ha_mask);
11881
11882 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050011883 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080011884 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050011885 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080011886 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050011887
11888 if (phba->cfg_multi_ring_support == 2) {
11889 /*
James Smart93996272008-08-24 21:50:30 -040011890 * Process all events on extra ring. Take the optimized path
11891 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050011892 */
James Smart93996272008-08-24 21:50:30 -040011893 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050011894 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050011895 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050011896 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080011897 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050011898 status);
11899 }
11900 }
dea31012005-04-17 16:05:31 -050011901 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011902} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050011903
James Smart93996272008-08-24 21:50:30 -040011904/**
James Smart3772a992009-05-22 14:50:54 -040011905 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040011906 * @irq: Interrupt number.
11907 * @dev_id: The device context pointer.
11908 *
James Smart3772a992009-05-22 14:50:54 -040011909 * This function is the HBA device-level interrupt handler to device with
11910 * SLI-3 interface spec, called from the PCI layer when either MSI or
11911 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11912 * requires driver attention. This function invokes the slow-path interrupt
11913 * attention handling function and fast-path interrupt attention handling
11914 * function in turn to process the relevant HBA attention events. This
11915 * function is called without any lock held. It gets the hbalock to access
11916 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011917 *
11918 * This function returns IRQ_HANDLED when interrupt is handled, else it
11919 * returns IRQ_NONE.
11920 **/
11921irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011922lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011923{
11924 struct lpfc_hba *phba;
11925 irqreturn_t sp_irq_rc, fp_irq_rc;
11926 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050011927 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040011928
11929 /*
11930 * Get the driver's phba structure from the dev_id and
11931 * assume the HBA is not interrupting.
11932 */
11933 phba = (struct lpfc_hba *) dev_id;
11934
11935 if (unlikely(!phba))
11936 return IRQ_NONE;
11937
James Smart3772a992009-05-22 14:50:54 -040011938 /* Check device state for handling interrupt */
11939 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011940 return IRQ_NONE;
11941
11942 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050011943 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11944 spin_unlock(&phba->hbalock);
11945 return IRQ_HANDLED;
11946 }
11947
James Smart93996272008-08-24 21:50:30 -040011948 if (unlikely(!phba->ha_copy)) {
11949 spin_unlock(&phba->hbalock);
11950 return IRQ_NONE;
11951 } else if (phba->ha_copy & HA_ERATT) {
11952 if (phba->hba_flag & HBA_ERATT_HANDLED)
11953 /* ERATT polling has handled ERATT */
11954 phba->ha_copy &= ~HA_ERATT;
11955 else
11956 /* Indicate interrupt handler handles ERATT */
11957 phba->hba_flag |= HBA_ERATT_HANDLED;
11958 }
11959
James Smarta257bf92009-04-06 18:48:10 -040011960 /*
11961 * If there is deferred error attention, do not check for any interrupt.
11962 */
11963 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020011964 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040011965 return IRQ_NONE;
11966 }
11967
James Smart93996272008-08-24 21:50:30 -040011968 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050011969 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11970 spin_unlock(&phba->hbalock);
11971 return IRQ_HANDLED;
11972 }
James Smarta747c9c2009-11-18 15:41:10 -050011973 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11974 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11975 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011976 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011977 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011978 readl(phba->HAregaddr); /* flush */
11979 spin_unlock(&phba->hbalock);
11980
11981 /*
11982 * Invokes slow-path host attention interrupt handling as appropriate.
11983 */
11984
11985 /* status of events with mailbox and link attention */
11986 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11987
11988 /* status of events with ELS ring */
11989 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11990 status2 >>= (4*LPFC_ELS_RING);
11991
11992 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011993 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011994 else
11995 sp_irq_rc = IRQ_NONE;
11996
11997 /*
11998 * Invoke fast-path host attention interrupt handling as appropriate.
11999 */
12000
12001 /* status of events with FCP ring */
12002 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12003 status1 >>= (4*LPFC_FCP_RING);
12004
12005 /* status of events with extra ring */
12006 if (phba->cfg_multi_ring_support == 2) {
12007 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12008 status2 >>= (4*LPFC_EXTRA_RING);
12009 } else
12010 status2 = 0;
12011
12012 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012013 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012014 else
12015 fp_irq_rc = IRQ_NONE;
12016
12017 /* Return device-level interrupt handling status */
12018 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012019} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012020
12021/**
12022 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12023 * @phba: pointer to lpfc hba data structure.
12024 *
12025 * This routine is invoked by the worker thread to process all the pending
12026 * SLI4 FCP abort XRI events.
12027 **/
12028void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12029{
12030 struct lpfc_cq_event *cq_event;
12031
12032 /* First, declare the fcp xri abort event has been handled */
12033 spin_lock_irq(&phba->hbalock);
12034 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12035 spin_unlock_irq(&phba->hbalock);
12036 /* Now, handle all the fcp xri abort events */
12037 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12038 /* Get the first event from the head of the event queue */
12039 spin_lock_irq(&phba->hbalock);
12040 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12041 cq_event, struct lpfc_cq_event, list);
12042 spin_unlock_irq(&phba->hbalock);
12043 /* Notify aborted XRI for FCP work queue */
12044 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12045 /* Free the event processed back to the free pool */
12046 lpfc_sli4_cq_event_release(phba, cq_event);
12047 }
12048}
12049
12050/**
12051 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12052 * @phba: pointer to lpfc hba data structure.
12053 *
12054 * This routine is invoked by the worker thread to process all the pending
12055 * SLI4 els abort xri events.
12056 **/
12057void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12058{
12059 struct lpfc_cq_event *cq_event;
12060
12061 /* First, declare the els xri abort event has been handled */
12062 spin_lock_irq(&phba->hbalock);
12063 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12064 spin_unlock_irq(&phba->hbalock);
12065 /* Now, handle all the els xri abort events */
12066 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12067 /* Get the first event from the head of the event queue */
12068 spin_lock_irq(&phba->hbalock);
12069 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12070 cq_event, struct lpfc_cq_event, list);
12071 spin_unlock_irq(&phba->hbalock);
12072 /* Notify aborted XRI for ELS work queue */
12073 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12074 /* Free the event processed back to the free pool */
12075 lpfc_sli4_cq_event_release(phba, cq_event);
12076 }
12077}
12078
James Smart341af102010-01-26 23:07:37 -050012079/**
12080 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12081 * @phba: pointer to lpfc hba data structure
12082 * @pIocbIn: pointer to the rspiocbq
12083 * @pIocbOut: pointer to the cmdiocbq
12084 * @wcqe: pointer to the complete wcqe
12085 *
12086 * This routine transfers the fields of a command iocbq to a response iocbq
12087 * by copying all the IOCB fields from command iocbq and transferring the
12088 * completion status information from the complete wcqe.
12089 **/
James Smart4f774512009-05-22 14:52:35 -040012090static void
James Smart341af102010-01-26 23:07:37 -050012091lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12092 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012093 struct lpfc_iocbq *pIocbOut,
12094 struct lpfc_wcqe_complete *wcqe)
12095{
James Smartaf227412013-10-10 12:23:10 -040012096 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012097 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012098 uint32_t status, max_response;
12099 struct lpfc_dmabuf *dmabuf;
12100 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012101 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12102
12103 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12104 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012105 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012106 status = bf_get(lpfc_wcqe_c_status, wcqe);
12107 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012108 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12109 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12110 pIocbIn->iocb.un.fcpi.fcpi_parm =
12111 pIocbOut->iocb.un.fcpi.fcpi_parm -
12112 wcqe->total_data_placed;
12113 else
12114 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012115 else {
James Smart4f774512009-05-22 14:52:35 -040012116 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012117 switch (pIocbOut->iocb.ulpCommand) {
12118 case CMD_ELS_REQUEST64_CR:
12119 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12120 bpl = (struct ulp_bde64 *)dmabuf->virt;
12121 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12122 max_response = bde.tus.f.bdeSize;
12123 break;
12124 case CMD_GEN_REQUEST64_CR:
12125 max_response = 0;
12126 if (!pIocbOut->context3)
12127 break;
12128 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12129 sizeof(struct ulp_bde64);
12130 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12131 bpl = (struct ulp_bde64 *)dmabuf->virt;
12132 for (i = 0; i < numBdes; i++) {
12133 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12134 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12135 max_response += bde.tus.f.bdeSize;
12136 }
12137 break;
12138 default:
12139 max_response = wcqe->total_data_placed;
12140 break;
12141 }
12142 if (max_response < wcqe->total_data_placed)
12143 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12144 else
12145 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12146 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012147 }
James Smart341af102010-01-26 23:07:37 -050012148
James Smartacd68592012-01-18 16:25:09 -050012149 /* Convert BG errors for completion status */
12150 if (status == CQE_STATUS_DI_ERROR) {
12151 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12152
12153 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12154 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12155 else
12156 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12157
12158 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12159 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12160 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12161 BGS_GUARD_ERR_MASK;
12162 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12163 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12164 BGS_APPTAG_ERR_MASK;
12165 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12166 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12167 BGS_REFTAG_ERR_MASK;
12168
12169 /* Check to see if there was any good data before the error */
12170 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12171 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12172 BGS_HI_WATER_MARK_PRESENT_MASK;
12173 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12174 wcqe->total_data_placed;
12175 }
12176
12177 /*
12178 * Set ALL the error bits to indicate we don't know what
12179 * type of error it is.
12180 */
12181 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12182 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12183 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12184 BGS_GUARD_ERR_MASK);
12185 }
12186
James Smart341af102010-01-26 23:07:37 -050012187 /* Pick up HBA exchange busy condition */
12188 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12189 spin_lock_irqsave(&phba->hbalock, iflags);
12190 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12191 spin_unlock_irqrestore(&phba->hbalock, iflags);
12192 }
James Smart4f774512009-05-22 14:52:35 -040012193}
12194
12195/**
James Smart45ed1192009-10-02 15:17:02 -040012196 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12197 * @phba: Pointer to HBA context object.
12198 * @wcqe: Pointer to work-queue completion queue entry.
12199 *
12200 * This routine handles an ELS work-queue completion event and construct
12201 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12202 * discovery engine to handle.
12203 *
12204 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12205 **/
12206static struct lpfc_iocbq *
12207lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12208 struct lpfc_iocbq *irspiocbq)
12209{
James Smart895427b2017-02-12 13:52:30 -080012210 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012211 struct lpfc_iocbq *cmdiocbq;
12212 struct lpfc_wcqe_complete *wcqe;
12213 unsigned long iflags;
12214
James Smart895427b2017-02-12 13:52:30 -080012215 pring = lpfc_phba_elsring(phba);
12216
James Smart45ed1192009-10-02 15:17:02 -040012217 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012218 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012219 pring->stats.iocb_event++;
12220 /* Look up the ELS command IOCB and create pseudo response IOCB */
12221 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12222 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart89533e92016-10-13 15:06:15 -070012223 /* Put the iocb back on the txcmplq */
12224 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
James Smart7e56aa22012-08-03 12:35:34 -040012225 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012226
12227 if (unlikely(!cmdiocbq)) {
12228 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12229 "0386 ELS complete with no corresponding "
12230 "cmdiocb: iotag (%d)\n",
12231 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12232 lpfc_sli_release_iocbq(phba, irspiocbq);
12233 return NULL;
12234 }
12235
12236 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012237 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012238
12239 return irspiocbq;
12240}
12241
12242/**
James Smart04c68492009-05-22 14:52:52 -040012243 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12244 * @phba: Pointer to HBA context object.
12245 * @cqe: Pointer to mailbox completion queue entry.
12246 *
12247 * This routine process a mailbox completion queue entry with asynchrous
12248 * event.
12249 *
12250 * Return: true if work posted to worker thread, otherwise false.
12251 **/
12252static bool
12253lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12254{
12255 struct lpfc_cq_event *cq_event;
12256 unsigned long iflags;
12257
12258 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12259 "0392 Async Event: word0:x%x, word1:x%x, "
12260 "word2:x%x, word3:x%x\n", mcqe->word0,
12261 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12262
12263 /* Allocate a new internal CQ_EVENT entry */
12264 cq_event = lpfc_sli4_cq_event_alloc(phba);
12265 if (!cq_event) {
12266 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12267 "0394 Failed to allocate CQ_EVENT entry\n");
12268 return false;
12269 }
12270
12271 /* Move the CQE into an asynchronous event entry */
12272 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
12273 spin_lock_irqsave(&phba->hbalock, iflags);
12274 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12275 /* Set the async event flag */
12276 phba->hba_flag |= ASYNC_EVENT;
12277 spin_unlock_irqrestore(&phba->hbalock, iflags);
12278
12279 return true;
12280}
12281
12282/**
12283 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12284 * @phba: Pointer to HBA context object.
12285 * @cqe: Pointer to mailbox completion queue entry.
12286 *
12287 * This routine process a mailbox completion queue entry with mailbox
12288 * completion event.
12289 *
12290 * Return: true if work posted to worker thread, otherwise false.
12291 **/
12292static bool
12293lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12294{
12295 uint32_t mcqe_status;
12296 MAILBOX_t *mbox, *pmbox;
12297 struct lpfc_mqe *mqe;
12298 struct lpfc_vport *vport;
12299 struct lpfc_nodelist *ndlp;
12300 struct lpfc_dmabuf *mp;
12301 unsigned long iflags;
12302 LPFC_MBOXQ_t *pmb;
12303 bool workposted = false;
12304 int rc;
12305
12306 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12307 if (!bf_get(lpfc_trailer_completed, mcqe))
12308 goto out_no_mqe_complete;
12309
12310 /* Get the reference to the active mbox command */
12311 spin_lock_irqsave(&phba->hbalock, iflags);
12312 pmb = phba->sli.mbox_active;
12313 if (unlikely(!pmb)) {
12314 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12315 "1832 No pending MBOX command to handle\n");
12316 spin_unlock_irqrestore(&phba->hbalock, iflags);
12317 goto out_no_mqe_complete;
12318 }
12319 spin_unlock_irqrestore(&phba->hbalock, iflags);
12320 mqe = &pmb->u.mqe;
12321 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12322 mbox = phba->mbox;
12323 vport = pmb->vport;
12324
12325 /* Reset heartbeat timer */
12326 phba->last_completion_time = jiffies;
12327 del_timer(&phba->sli.mbox_tmo);
12328
12329 /* Move mbox data to caller's mailbox region, do endian swapping */
12330 if (pmb->mbox_cmpl && mbox)
12331 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012332
James Smart73d91e52011-10-10 21:32:10 -040012333 /*
12334 * For mcqe errors, conditionally move a modified error code to
12335 * the mbox so that the error will not be missed.
12336 */
12337 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12338 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12339 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12340 bf_set(lpfc_mqe_status, mqe,
12341 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12342 }
James Smart04c68492009-05-22 14:52:52 -040012343 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12344 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12345 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12346 "MBOX dflt rpi: status:x%x rpi:x%x",
12347 mcqe_status,
12348 pmbox->un.varWords[0], 0);
12349 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12350 mp = (struct lpfc_dmabuf *)(pmb->context1);
12351 ndlp = (struct lpfc_nodelist *)pmb->context2;
12352 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12353 * RID of the PPI using the same mbox buffer.
12354 */
12355 lpfc_unreg_login(phba, vport->vpi,
12356 pmbox->un.varWords[0], pmb);
12357 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12358 pmb->context1 = mp;
12359 pmb->context2 = ndlp;
12360 pmb->vport = vport;
12361 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12362 if (rc != MBX_BUSY)
12363 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12364 LOG_SLI, "0385 rc should "
12365 "have been MBX_BUSY\n");
12366 if (rc != MBX_NOT_FINISHED)
12367 goto send_current_mbox;
12368 }
12369 }
12370 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12371 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12372 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12373
12374 /* There is mailbox completion work to do */
12375 spin_lock_irqsave(&phba->hbalock, iflags);
12376 __lpfc_mbox_cmpl_put(phba, pmb);
12377 phba->work_ha |= HA_MBATT;
12378 spin_unlock_irqrestore(&phba->hbalock, iflags);
12379 workposted = true;
12380
12381send_current_mbox:
12382 spin_lock_irqsave(&phba->hbalock, iflags);
12383 /* Release the mailbox command posting token */
12384 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12385 /* Setting active mailbox pointer need to be in sync to flag clear */
12386 phba->sli.mbox_active = NULL;
12387 spin_unlock_irqrestore(&phba->hbalock, iflags);
12388 /* Wake up worker thread to post the next pending mailbox command */
12389 lpfc_worker_wake_up(phba);
12390out_no_mqe_complete:
12391 if (bf_get(lpfc_trailer_consumed, mcqe))
12392 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12393 return workposted;
12394}
12395
12396/**
12397 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12398 * @phba: Pointer to HBA context object.
12399 * @cqe: Pointer to mailbox completion queue entry.
12400 *
12401 * This routine process a mailbox completion queue entry, it invokes the
12402 * proper mailbox complete handling or asynchrous event handling routine
12403 * according to the MCQE's async bit.
12404 *
12405 * Return: true if work posted to worker thread, otherwise false.
12406 **/
12407static bool
12408lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12409{
12410 struct lpfc_mcqe mcqe;
12411 bool workposted;
12412
12413 /* Copy the mailbox MCQE and convert endian order as needed */
12414 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12415
12416 /* Invoke the proper event handling routine */
12417 if (!bf_get(lpfc_trailer_async, &mcqe))
12418 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12419 else
12420 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12421 return workposted;
12422}
12423
12424/**
James Smart4f774512009-05-22 14:52:35 -040012425 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12426 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012427 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012428 * @wcqe: Pointer to work-queue completion queue entry.
12429 *
12430 * This routine handles an ELS work-queue completion event.
12431 *
12432 * Return: true if work posted to worker thread, otherwise false.
12433 **/
12434static bool
James Smart2a76a282012-08-03 12:35:54 -040012435lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012436 struct lpfc_wcqe_complete *wcqe)
12437{
James Smart4f774512009-05-22 14:52:35 -040012438 struct lpfc_iocbq *irspiocbq;
12439 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012440 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012441 int txq_cnt = 0;
12442 int txcmplq_cnt = 0;
12443 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012444
James Smart45ed1192009-10-02 15:17:02 -040012445 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012446 irspiocbq = lpfc_sli_get_iocbq(phba);
12447 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012448 if (!list_empty(&pring->txq))
12449 txq_cnt++;
12450 if (!list_empty(&pring->txcmplq))
12451 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012452 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012453 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12454 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012455 txq_cnt, phba->iocb_cnt,
12456 fcp_txcmplq_cnt,
12457 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012458 return false;
James Smart4f774512009-05-22 14:52:35 -040012459 }
James Smart4f774512009-05-22 14:52:35 -040012460
James Smart45ed1192009-10-02 15:17:02 -040012461 /* Save off the slow-path queue event for work thread to process */
12462 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012463 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012464 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012465 &phba->sli4_hba.sp_queue_event);
12466 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012467 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012468
James Smart45ed1192009-10-02 15:17:02 -040012469 return true;
James Smart4f774512009-05-22 14:52:35 -040012470}
12471
12472/**
12473 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12474 * @phba: Pointer to HBA context object.
12475 * @wcqe: Pointer to work-queue completion queue entry.
12476 *
12477 * This routine handles slow-path WQ entry comsumed event by invoking the
12478 * proper WQ release routine to the slow-path WQ.
12479 **/
12480static void
12481lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12482 struct lpfc_wcqe_release *wcqe)
12483{
James Smart2e90f4b2011-12-13 13:22:37 -050012484 /* sanity check on queue memory */
12485 if (unlikely(!phba->sli4_hba.els_wq))
12486 return;
James Smart4f774512009-05-22 14:52:35 -040012487 /* Check for the slow-path ELS work queue */
12488 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12489 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12490 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12491 else
12492 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12493 "2579 Slow-path wqe consume event carries "
12494 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12495 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12496 phba->sli4_hba.els_wq->queue_id);
12497}
12498
12499/**
12500 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12501 * @phba: Pointer to HBA context object.
12502 * @cq: Pointer to a WQ completion queue.
12503 * @wcqe: Pointer to work-queue completion queue entry.
12504 *
12505 * This routine handles an XRI abort event.
12506 *
12507 * Return: true if work posted to worker thread, otherwise false.
12508 **/
12509static bool
12510lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12511 struct lpfc_queue *cq,
12512 struct sli4_wcqe_xri_aborted *wcqe)
12513{
12514 bool workposted = false;
12515 struct lpfc_cq_event *cq_event;
12516 unsigned long iflags;
12517
12518 /* Allocate a new internal CQ_EVENT entry */
12519 cq_event = lpfc_sli4_cq_event_alloc(phba);
12520 if (!cq_event) {
12521 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12522 "0602 Failed to allocate CQ_EVENT entry\n");
12523 return false;
12524 }
12525
12526 /* Move the CQE into the proper xri abort event list */
12527 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12528 switch (cq->subtype) {
12529 case LPFC_FCP:
12530 spin_lock_irqsave(&phba->hbalock, iflags);
12531 list_add_tail(&cq_event->list,
12532 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12533 /* Set the fcp xri abort event flag */
12534 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12535 spin_unlock_irqrestore(&phba->hbalock, iflags);
12536 workposted = true;
12537 break;
12538 case LPFC_ELS:
12539 spin_lock_irqsave(&phba->hbalock, iflags);
12540 list_add_tail(&cq_event->list,
12541 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12542 /* Set the els xri abort event flag */
12543 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12544 spin_unlock_irqrestore(&phba->hbalock, iflags);
12545 workposted = true;
12546 break;
12547 default:
12548 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12549 "0603 Invalid work queue CQE subtype (x%x)\n",
12550 cq->subtype);
12551 workposted = false;
12552 break;
12553 }
12554 return workposted;
12555}
12556
12557/**
James Smart4d9ab992009-10-02 15:16:39 -040012558 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012559 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012560 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012561 *
James Smart4d9ab992009-10-02 15:16:39 -040012562 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012563 *
12564 * Return: true if work posted to worker thread, otherwise false.
12565 **/
12566static bool
James Smart4d9ab992009-10-02 15:16:39 -040012567lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12568{
12569 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080012570 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040012571 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12572 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12573 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012574 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012575 unsigned long iflags;
12576
James Smart2e90f4b2011-12-13 13:22:37 -050012577 /* sanity check on queue memory */
12578 if (unlikely(!hrq) || unlikely(!drq))
12579 return workposted;
12580
James Smart7851fe22011-07-22 18:36:52 -040012581 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12582 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12583 else
12584 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12585 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012586 goto out;
12587
12588 status = bf_get(lpfc_rcqe_status, rcqe);
12589 switch (status) {
12590 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12591 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12592 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040012593 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040012594 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012595 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012596 spin_lock_irqsave(&phba->hbalock, iflags);
12597 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12598 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012599 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012600 spin_unlock_irqrestore(&phba->hbalock, iflags);
12601 goto out;
12602 }
James Smartb84daac2012-08-03 12:35:13 -040012603 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012604 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080012605
12606 /* If a NVME LS event (type 0x28), treat it as Fast path */
12607 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12608
James Smart4d9ab992009-10-02 15:16:39 -040012609 /* save off the frame for the word thread to process */
12610 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012611 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012612 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012613 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012614 spin_unlock_irqrestore(&phba->hbalock, iflags);
12615 workposted = true;
12616 break;
12617 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12618 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040012619 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012620 /* Post more buffers if possible */
12621 spin_lock_irqsave(&phba->hbalock, iflags);
12622 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12623 spin_unlock_irqrestore(&phba->hbalock, iflags);
12624 workposted = true;
12625 break;
12626 }
12627out:
12628 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012629}
12630
12631/**
12632 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12633 * @phba: Pointer to HBA context object.
12634 * @cq: Pointer to the completion queue.
12635 * @wcqe: Pointer to a completion queue entry.
12636 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012637 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012638 * entry.
12639 *
12640 * Return: true if work posted to worker thread, otherwise false.
12641 **/
12642static bool
12643lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012644 struct lpfc_cqe *cqe)
12645{
James Smart45ed1192009-10-02 15:17:02 -040012646 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012647 bool workposted = false;
12648
12649 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012650 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012651
12652 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012653 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012654 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012655 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012656 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012657 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012658 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012659 break;
12660 case CQE_CODE_RELEASE_WQE:
12661 /* Process the WQ release event */
12662 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012663 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012664 break;
12665 case CQE_CODE_XRI_ABORTED:
12666 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012667 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012668 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012669 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012670 break;
James Smart4d9ab992009-10-02 15:16:39 -040012671 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012672 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012673 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012674 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012675 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012676 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012677 break;
James Smart4f774512009-05-22 14:52:35 -040012678 default:
12679 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12680 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012681 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012682 break;
12683 }
12684 return workposted;
12685}
12686
12687/**
James Smart4f774512009-05-22 14:52:35 -040012688 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12689 * @phba: Pointer to HBA context object.
12690 * @eqe: Pointer to fast-path event queue entry.
12691 *
12692 * This routine process a event queue entry from the slow-path event queue.
12693 * It will check the MajorCode and MinorCode to determine this is for a
12694 * completion event on a completion queue, if not, an error shall be logged
12695 * and just return. Otherwise, it will get to the corresponding completion
12696 * queue and process all the entries on that completion queue, rearm the
12697 * completion queue, and then return.
12698 *
12699 **/
12700static void
James Smart67d12732012-08-03 12:36:13 -040012701lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12702 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040012703{
James Smart67d12732012-08-03 12:36:13 -040012704 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040012705 struct lpfc_cqe *cqe;
12706 bool workposted = false;
12707 int ecount = 0;
12708 uint16_t cqid;
12709
James Smart4f774512009-05-22 14:52:35 -040012710 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040012711 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040012712
James Smart4f774512009-05-22 14:52:35 -040012713 list_for_each_entry(childq, &speq->child_list, list) {
12714 if (childq->queue_id == cqid) {
12715 cq = childq;
12716 break;
12717 }
12718 }
12719 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012720 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12721 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12722 "0365 Slow-path CQ identifier "
12723 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040012724 return;
12725 }
12726
James Smart895427b2017-02-12 13:52:30 -080012727 /* Save EQ associated with this CQ */
12728 cq->assoc_qp = speq;
12729
James Smart4f774512009-05-22 14:52:35 -040012730 /* Process all the entries to the CQ */
12731 switch (cq->type) {
12732 case LPFC_MCQ:
12733 while ((cqe = lpfc_sli4_cq_get(cq))) {
12734 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012735 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012736 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012737 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040012738 }
12739 break;
12740 case LPFC_WCQ:
12741 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080012742 if ((cq->subtype == LPFC_FCP) ||
12743 (cq->subtype == LPFC_NVME))
12744 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040012745 cqe);
12746 else
12747 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12748 cqe);
James Smart73d91e52011-10-10 21:32:10 -040012749 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012750 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12751 }
James Smartb84daac2012-08-03 12:35:13 -040012752
12753 /* Track the max number of CQEs processed in 1 EQ */
12754 if (ecount > cq->CQ_max_cqe)
12755 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040012756 break;
12757 default:
12758 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12759 "0370 Invalid completion queue type (%d)\n",
12760 cq->type);
12761 return;
12762 }
12763
12764 /* Catch the no cq entry condition, log an error */
12765 if (unlikely(ecount == 0))
12766 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12767 "0371 No entry from the CQ: identifier "
12768 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12769
12770 /* In any case, flash and re-arm the RCQ */
12771 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12772
12773 /* wake up worker thread if there are works to be done */
12774 if (workposted)
12775 lpfc_worker_wake_up(phba);
12776}
12777
12778/**
12779 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040012780 * @phba: Pointer to HBA context object.
12781 * @cq: Pointer to associated CQ
12782 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012783 *
12784 * This routine process a fast-path work queue completion entry from fast-path
12785 * event queue for FCP command response completion.
12786 **/
12787static void
James Smart2a76a282012-08-03 12:35:54 -040012788lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012789 struct lpfc_wcqe_complete *wcqe)
12790{
James Smart2a76a282012-08-03 12:35:54 -040012791 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040012792 struct lpfc_iocbq *cmdiocbq;
12793 struct lpfc_iocbq irspiocbq;
12794 unsigned long iflags;
12795
James Smart4f774512009-05-22 14:52:35 -040012796 /* Check for response status */
12797 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12798 /* If resource errors reported from HBA, reduce queue
12799 * depth of the SCSI device.
12800 */
James Smarte3d2b802012-08-14 14:25:43 -040012801 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12802 IOSTAT_LOCAL_REJECT)) &&
12803 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12804 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040012805 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040012806
James Smart4f774512009-05-22 14:52:35 -040012807 /* Log the error status */
12808 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12809 "0373 FCP complete error: status=x%x, "
12810 "hw_status=x%x, total_data_specified=%d, "
12811 "parameter=x%x, word3=x%x\n",
12812 bf_get(lpfc_wcqe_c_status, wcqe),
12813 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12814 wcqe->total_data_placed, wcqe->parameter,
12815 wcqe->word3);
12816 }
12817
12818 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040012819 spin_lock_irqsave(&pring->ring_lock, iflags);
12820 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040012821 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12822 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040012823 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012824 if (unlikely(!cmdiocbq)) {
12825 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12826 "0374 FCP complete with no corresponding "
12827 "cmdiocb: iotag (%d)\n",
12828 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12829 return;
12830 }
James Smart895427b2017-02-12 13:52:30 -080012831
12832 if (cq->assoc_qp)
12833 cmdiocbq->isr_timestamp =
12834 cq->assoc_qp->isr_timestamp;
12835
12836 if (cmdiocbq->iocb_cmpl == NULL) {
12837 if (cmdiocbq->wqe_cmpl) {
12838 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12839 spin_lock_irqsave(&phba->hbalock, iflags);
12840 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12841 spin_unlock_irqrestore(&phba->hbalock, iflags);
12842 }
12843
12844 /* Pass the cmd_iocb and the wcqe to the upper layer */
12845 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
12846 return;
12847 }
James Smart4f774512009-05-22 14:52:35 -040012848 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12849 "0375 FCP cmdiocb not callback function "
12850 "iotag: (%d)\n",
12851 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12852 return;
12853 }
12854
12855 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012856 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040012857
James Smart0f65ff62010-02-26 14:14:23 -050012858 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12859 spin_lock_irqsave(&phba->hbalock, iflags);
12860 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12861 spin_unlock_irqrestore(&phba->hbalock, iflags);
12862 }
12863
James Smart4f774512009-05-22 14:52:35 -040012864 /* Pass the cmd_iocb and the rsp state to the upper layer */
12865 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12866}
12867
12868/**
12869 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12870 * @phba: Pointer to HBA context object.
12871 * @cq: Pointer to completion queue.
12872 * @wcqe: Pointer to work-queue completion queue entry.
12873 *
12874 * This routine handles an fast-path WQ entry comsumed event by invoking the
12875 * proper WQ release routine to the slow-path WQ.
12876 **/
12877static void
12878lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12879 struct lpfc_wcqe_release *wcqe)
12880{
12881 struct lpfc_queue *childwq;
12882 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080012883 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040012884
12885 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080012886 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040012887 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080012888 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040012889 lpfc_sli4_wq_release(childwq,
12890 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12891 wqid_matched = true;
12892 break;
12893 }
12894 }
12895 /* Report warning log message if no match found */
12896 if (wqid_matched != true)
12897 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12898 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080012899 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040012900}
12901
12902/**
James Smart895427b2017-02-12 13:52:30 -080012903 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040012904 * @cq: Pointer to the completion queue.
12905 * @eqe: Pointer to fast-path completion queue entry.
12906 *
12907 * This routine process a fast-path work queue completion entry from fast-path
12908 * event queue for FCP command response completion.
12909 **/
12910static int
James Smart895427b2017-02-12 13:52:30 -080012911lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012912 struct lpfc_cqe *cqe)
12913{
12914 struct lpfc_wcqe_release wcqe;
12915 bool workposted = false;
12916
12917 /* Copy the work queue CQE and convert endian order if needed */
12918 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12919
12920 /* Check and process for different type of WCQE and dispatch */
12921 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12922 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080012923 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040012924 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040012925 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040012926 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080012927 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
12928 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
12929 (struct lpfc_wcqe_complete *)&wcqe);
12930 if (cq->subtype == LPFC_NVME_LS)
12931 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040012932 (struct lpfc_wcqe_complete *)&wcqe);
12933 break;
12934 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012935 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040012936 /* Process the WQ release event */
12937 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12938 (struct lpfc_wcqe_release *)&wcqe);
12939 break;
12940 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040012941 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040012942 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012943 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012944 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12945 (struct sli4_wcqe_xri_aborted *)&wcqe);
12946 break;
James Smart895427b2017-02-12 13:52:30 -080012947 case CQE_CODE_RECEIVE_V1:
12948 case CQE_CODE_RECEIVE:
12949 phba->last_completion_time = jiffies;
12950 break;
James Smart4f774512009-05-22 14:52:35 -040012951 default:
12952 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080012953 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040012954 bf_get(lpfc_wcqe_c_code, &wcqe));
12955 break;
12956 }
12957 return workposted;
12958}
12959
12960/**
James Smart67d12732012-08-03 12:36:13 -040012961 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040012962 * @phba: Pointer to HBA context object.
12963 * @eqe: Pointer to fast-path event queue entry.
12964 *
12965 * This routine process a event queue entry from the fast-path event queue.
12966 * It will check the MajorCode and MinorCode to determine this is for a
12967 * completion event on a completion queue, if not, an error shall be logged
12968 * and just return. Otherwise, it will get to the corresponding completion
12969 * queue and process all the entries on the completion queue, rearm the
12970 * completion queue, and then return.
12971 **/
12972static void
James Smart67d12732012-08-03 12:36:13 -040012973lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12974 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040012975{
James Smart895427b2017-02-12 13:52:30 -080012976 struct lpfc_queue *cq = NULL;
James Smart4f774512009-05-22 14:52:35 -040012977 struct lpfc_cqe *cqe;
12978 bool workposted = false;
12979 uint16_t cqid;
12980 int ecount = 0;
12981
James Smartcb5172e2010-03-15 11:25:07 -040012982 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040012983 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040012984 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040012985 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040012986 bf_get_le32(lpfc_eqe_major_code, eqe),
12987 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040012988 return;
12989 }
12990
James Smart67d12732012-08-03 12:36:13 -040012991 /* Get the reference to the corresponding CQ */
12992 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12993
James Smart895427b2017-02-12 13:52:30 -080012994 if (phba->sli4_hba.nvme_cq_map &&
12995 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
12996 /* Process NVME command completion */
12997 cq = phba->sli4_hba.nvme_cq[qidx];
12998 goto process_cq;
12999 }
13000
13001 if (phba->sli4_hba.fcp_cq_map &&
13002 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13003 /* Process FCP command completion */
13004 cq = phba->sli4_hba.fcp_cq[qidx];
13005 goto process_cq;
13006 }
13007
13008 if (phba->sli4_hba.nvmels_cq &&
13009 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13010 /* Process NVME unsol rcv */
13011 cq = phba->sli4_hba.nvmels_cq;
13012 }
13013
13014 /* Otherwise this is a Slow path event */
13015 if (cq == NULL) {
13016 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013017 return;
13018 }
13019
James Smart895427b2017-02-12 13:52:30 -080013020process_cq:
James Smart4f774512009-05-22 14:52:35 -040013021 if (unlikely(cqid != cq->queue_id)) {
13022 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13023 "0368 Miss-matched fast-path completion "
13024 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13025 cqid, cq->queue_id);
13026 return;
13027 }
13028
James Smart895427b2017-02-12 13:52:30 -080013029 /* Save EQ associated with this CQ */
13030 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13031
James Smart4f774512009-05-22 14:52:35 -040013032 /* Process all the entries to the CQ */
13033 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013034 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040013035 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040013036 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13037 }
13038
James Smartb84daac2012-08-03 12:35:13 -040013039 /* Track the max number of CQEs processed in 1 EQ */
13040 if (ecount > cq->CQ_max_cqe)
13041 cq->CQ_max_cqe = ecount;
13042
James Smart4f774512009-05-22 14:52:35 -040013043 /* Catch the no cq entry condition */
13044 if (unlikely(ecount == 0))
13045 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13046 "0369 No entry from fast-path completion "
13047 "queue fcpcqid=%d\n", cq->queue_id);
13048
13049 /* In any case, flash and re-arm the CQ */
13050 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13051
13052 /* wake up worker thread if there are works to be done */
13053 if (workposted)
13054 lpfc_worker_wake_up(phba);
13055}
13056
13057static void
13058lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13059{
13060 struct lpfc_eqe *eqe;
13061
13062 /* walk all the EQ entries and drop on the floor */
13063 while ((eqe = lpfc_sli4_eq_get(eq)))
13064 ;
13065
13066 /* Clear and re-arm the EQ */
13067 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13068}
13069
James Smart1ba981f2014-02-20 09:56:45 -050013070
13071/**
13072 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13073 * entry
13074 * @phba: Pointer to HBA context object.
13075 * @eqe: Pointer to fast-path event queue entry.
13076 *
13077 * This routine process a event queue entry from the Flash Optimized Fabric
13078 * event queue. It will check the MajorCode and MinorCode to determine this
13079 * is for a completion event on a completion queue, if not, an error shall be
13080 * logged and just return. Otherwise, it will get to the corresponding
13081 * completion queue and process all the entries on the completion queue, rearm
13082 * the completion queue, and then return.
13083 **/
13084static void
13085lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13086{
13087 struct lpfc_queue *cq;
13088 struct lpfc_cqe *cqe;
13089 bool workposted = false;
13090 uint16_t cqid;
13091 int ecount = 0;
13092
13093 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13094 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13095 "9147 Not a valid completion "
13096 "event: majorcode=x%x, minorcode=x%x\n",
13097 bf_get_le32(lpfc_eqe_major_code, eqe),
13098 bf_get_le32(lpfc_eqe_minor_code, eqe));
13099 return;
13100 }
13101
13102 /* Get the reference to the corresponding CQ */
13103 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13104
13105 /* Next check for OAS */
13106 cq = phba->sli4_hba.oas_cq;
13107 if (unlikely(!cq)) {
13108 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13109 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13110 "9148 OAS completion queue "
13111 "does not exist\n");
13112 return;
13113 }
13114
13115 if (unlikely(cqid != cq->queue_id)) {
13116 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13117 "9149 Miss-matched fast-path compl "
13118 "queue id: eqcqid=%d, fcpcqid=%d\n",
13119 cqid, cq->queue_id);
13120 return;
13121 }
13122
13123 /* Process all the entries to the OAS CQ */
13124 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013125 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart1ba981f2014-02-20 09:56:45 -050013126 if (!(++ecount % cq->entry_repost))
13127 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13128 }
13129
13130 /* Track the max number of CQEs processed in 1 EQ */
13131 if (ecount > cq->CQ_max_cqe)
13132 cq->CQ_max_cqe = ecount;
13133
13134 /* Catch the no cq entry condition */
13135 if (unlikely(ecount == 0))
13136 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13137 "9153 No entry from fast-path completion "
13138 "queue fcpcqid=%d\n", cq->queue_id);
13139
13140 /* In any case, flash and re-arm the CQ */
13141 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13142
13143 /* wake up worker thread if there are works to be done */
13144 if (workposted)
13145 lpfc_worker_wake_up(phba);
13146}
13147
13148/**
13149 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13150 * @irq: Interrupt number.
13151 * @dev_id: The device context pointer.
13152 *
13153 * This function is directly called from the PCI layer as an interrupt
13154 * service routine when device with SLI-4 interface spec is enabled with
13155 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13156 * IOCB ring event in the HBA. However, when the device is enabled with either
13157 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13158 * device-level interrupt handler. When the PCI slot is in error recovery
13159 * or the HBA is undergoing initialization, the interrupt handler will not
13160 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13161 * the intrrupt context. This function is called without any lock held.
13162 * It gets the hbalock to access and update SLI data structures. Note that,
13163 * the EQ to CQ are one-to-one map such that the EQ index is
13164 * equal to that of CQ index.
13165 *
13166 * This function returns IRQ_HANDLED when interrupt is handled else it
13167 * returns IRQ_NONE.
13168 **/
13169irqreturn_t
13170lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13171{
13172 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013173 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013174 struct lpfc_queue *eq;
13175 struct lpfc_eqe *eqe;
13176 unsigned long iflag;
13177 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013178
13179 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013180 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13181 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013182
13183 if (unlikely(!phba))
13184 return IRQ_NONE;
13185
13186 /* Get to the EQ struct associated with this vector */
13187 eq = phba->sli4_hba.fof_eq;
13188 if (unlikely(!eq))
13189 return IRQ_NONE;
13190
13191 /* Check device state for handling interrupt */
13192 if (unlikely(lpfc_intr_state_check(phba))) {
13193 eq->EQ_badstate++;
13194 /* Check again for link_state with lock held */
13195 spin_lock_irqsave(&phba->hbalock, iflag);
13196 if (phba->link_state < LPFC_LINK_DOWN)
13197 /* Flush, clear interrupt, and rearm the EQ */
13198 lpfc_sli4_eq_flush(phba, eq);
13199 spin_unlock_irqrestore(&phba->hbalock, iflag);
13200 return IRQ_NONE;
13201 }
13202
13203 /*
13204 * Process all the event on FCP fast-path EQ
13205 */
13206 while ((eqe = lpfc_sli4_eq_get(eq))) {
13207 lpfc_sli4_fof_handle_eqe(phba, eqe);
13208 if (!(++ecount % eq->entry_repost))
13209 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
13210 eq->EQ_processed++;
13211 }
13212
13213 /* Track the max number of EQEs processed in 1 intr */
13214 if (ecount > eq->EQ_max_eqe)
13215 eq->EQ_max_eqe = ecount;
13216
13217
13218 if (unlikely(ecount == 0)) {
13219 eq->EQ_no_entry++;
13220
13221 if (phba->intr_type == MSIX)
13222 /* MSI-X treated interrupt served as no EQ share INT */
13223 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13224 "9145 MSI-X interrupt with no EQE\n");
13225 else {
13226 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13227 "9146 ISR interrupt with no EQE\n");
13228 /* Non MSI-X treated on interrupt as EQ share INT */
13229 return IRQ_NONE;
13230 }
13231 }
13232 /* Always clear and re-arm the fast-path EQ */
13233 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13234 return IRQ_HANDLED;
13235}
13236
James Smart4f774512009-05-22 14:52:35 -040013237/**
James Smart67d12732012-08-03 12:36:13 -040013238 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013239 * @irq: Interrupt number.
13240 * @dev_id: The device context pointer.
13241 *
13242 * This function is directly called from the PCI layer as an interrupt
13243 * service routine when device with SLI-4 interface spec is enabled with
13244 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13245 * ring event in the HBA. However, when the device is enabled with either
13246 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13247 * device-level interrupt handler. When the PCI slot is in error recovery
13248 * or the HBA is undergoing initialization, the interrupt handler will not
13249 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13250 * the intrrupt context. This function is called without any lock held.
13251 * It gets the hbalock to access and update SLI data structures. Note that,
13252 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13253 * equal to that of FCP CQ index.
13254 *
James Smart67d12732012-08-03 12:36:13 -040013255 * The link attention and ELS ring attention events are handled
13256 * by the worker thread. The interrupt handler signals the worker thread
13257 * and returns for these events. This function is called without any lock
13258 * held. It gets the hbalock to access and update SLI data structures.
13259 *
James Smart4f774512009-05-22 14:52:35 -040013260 * This function returns IRQ_HANDLED when interrupt is handled else it
13261 * returns IRQ_NONE.
13262 **/
13263irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013264lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013265{
13266 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013267 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013268 struct lpfc_queue *fpeq;
13269 struct lpfc_eqe *eqe;
13270 unsigned long iflag;
13271 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013272 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013273
13274 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013275 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13276 phba = hba_eq_hdl->phba;
13277 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040013278
13279 if (unlikely(!phba))
13280 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040013281 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040013282 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013283
13284 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080013285 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050013286 if (unlikely(!fpeq))
13287 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013288
James Smartba20c852012-08-03 12:36:52 -040013289 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013290 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartba20c852012-08-03 12:36:52 -040013291 lpfc_sli4_eq_clr_intr(fpeq);
13292 else {
James Smart895427b2017-02-12 13:52:30 -080013293 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013294 return IRQ_NONE;
13295 }
13296 }
13297
James Smart4f774512009-05-22 14:52:35 -040013298 /* Check device state for handling interrupt */
13299 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040013300 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040013301 /* Check again for link_state with lock held */
13302 spin_lock_irqsave(&phba->hbalock, iflag);
13303 if (phba->link_state < LPFC_LINK_DOWN)
13304 /* Flush, clear interrupt, and rearm the EQ */
13305 lpfc_sli4_eq_flush(phba, fpeq);
13306 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040013307 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013308 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040013309 return IRQ_NONE;
13310 }
13311
13312 /*
13313 * Process all the event on FCP fast-path EQ
13314 */
13315 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040013316 if (eqe == NULL)
13317 break;
13318
James Smart895427b2017-02-12 13:52:30 -080013319 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040013320 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040013321 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040013322 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040013323 }
13324
James Smartb84daac2012-08-03 12:35:13 -040013325 /* Track the max number of EQEs processed in 1 intr */
13326 if (ecount > fpeq->EQ_max_eqe)
13327 fpeq->EQ_max_eqe = ecount;
13328
James Smart4f774512009-05-22 14:52:35 -040013329 /* Always clear and re-arm the fast-path EQ */
13330 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13331
13332 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040013333 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040013334
13335 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013336 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013337 return IRQ_NONE;
13338 }
13339
James Smart4f774512009-05-22 14:52:35 -040013340 if (phba->intr_type == MSIX)
13341 /* MSI-X treated interrupt served as no EQ share INT */
13342 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13343 "0358 MSI-X interrupt with no EQE\n");
13344 else
13345 /* Non MSI-X treated on interrupt as EQ share INT */
13346 return IRQ_NONE;
13347 }
13348
James Smartba20c852012-08-03 12:36:52 -040013349 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013350 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13351
James Smart4f774512009-05-22 14:52:35 -040013352 return IRQ_HANDLED;
13353} /* lpfc_sli4_fp_intr_handler */
13354
13355/**
13356 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13357 * @irq: Interrupt number.
13358 * @dev_id: The device context pointer.
13359 *
13360 * This function is the device-level interrupt handler to device with SLI-4
13361 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13362 * interrupt mode is enabled and there is an event in the HBA which requires
13363 * driver attention. This function invokes the slow-path interrupt attention
13364 * handling function and fast-path interrupt attention handling function in
13365 * turn to process the relevant HBA attention events. This function is called
13366 * without any lock held. It gets the hbalock to access and update SLI data
13367 * structures.
13368 *
13369 * This function returns IRQ_HANDLED when interrupt is handled, else it
13370 * returns IRQ_NONE.
13371 **/
13372irqreturn_t
13373lpfc_sli4_intr_handler(int irq, void *dev_id)
13374{
13375 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040013376 irqreturn_t hba_irq_rc;
13377 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080013378 int qidx;
James Smart4f774512009-05-22 14:52:35 -040013379
13380 /* Get the driver's phba structure from the dev_id */
13381 phba = (struct lpfc_hba *)dev_id;
13382
13383 if (unlikely(!phba))
13384 return IRQ_NONE;
13385
13386 /*
James Smart4f774512009-05-22 14:52:35 -040013387 * Invoke fast-path host attention interrupt handling as appropriate.
13388 */
James Smart895427b2017-02-12 13:52:30 -080013389 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040013390 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013391 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013392 if (hba_irq_rc == IRQ_HANDLED)
13393 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040013394 }
13395
James Smart1ba981f2014-02-20 09:56:45 -050013396 if (phba->cfg_fof) {
13397 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013398 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050013399 if (hba_irq_rc == IRQ_HANDLED)
13400 hba_handled |= true;
13401 }
13402
James Smart67d12732012-08-03 12:36:13 -040013403 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013404} /* lpfc_sli4_intr_handler */
13405
13406/**
13407 * lpfc_sli4_queue_free - free a queue structure and associated memory
13408 * @queue: The queue structure to free.
13409 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040013410 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040013411 * the host resident queue. This function must be called after destroying the
13412 * queue on the HBA.
13413 **/
13414void
13415lpfc_sli4_queue_free(struct lpfc_queue *queue)
13416{
13417 struct lpfc_dmabuf *dmabuf;
13418
13419 if (!queue)
13420 return;
13421
13422 while (!list_empty(&queue->page_list)) {
13423 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13424 list);
James Smart49198b32010-04-06 15:04:33 -040013425 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040013426 dmabuf->virt, dmabuf->phys);
13427 kfree(dmabuf);
13428 }
James Smart895427b2017-02-12 13:52:30 -080013429 if (queue->rqbp) {
13430 lpfc_free_rq_buffer(queue->phba, queue);
13431 kfree(queue->rqbp);
13432 }
13433 kfree(queue->pring);
James Smart4f774512009-05-22 14:52:35 -040013434 kfree(queue);
13435 return;
13436}
13437
13438/**
13439 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13440 * @phba: The HBA that this queue is being created on.
13441 * @entry_size: The size of each queue entry for this queue.
13442 * @entry count: The number of entries that this queue will handle.
13443 *
13444 * This function allocates a queue structure and the DMAable memory used for
13445 * the host resident queue. This function must be called before creating the
13446 * queue on the HBA.
13447 **/
13448struct lpfc_queue *
13449lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
13450 uint32_t entry_count)
13451{
13452 struct lpfc_queue *queue;
13453 struct lpfc_dmabuf *dmabuf;
13454 int x, total_qe_count;
13455 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040013456 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040013457
James Smartcb5172e2010-03-15 11:25:07 -040013458 if (!phba->sli4_hba.pc_sli4_params.supported)
13459 hw_page_size = SLI4_PAGE_SIZE;
13460
James Smart4f774512009-05-22 14:52:35 -040013461 queue = kzalloc(sizeof(struct lpfc_queue) +
13462 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13463 if (!queue)
13464 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040013465 queue->page_count = (ALIGN(entry_size * entry_count,
13466 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080013467
13468 /* If needed, Adjust page count to match the max the adapter supports */
13469 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13470 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13471
James Smart4f774512009-05-22 14:52:35 -040013472 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080013473 INIT_LIST_HEAD(&queue->wq_list);
James Smart4f774512009-05-22 14:52:35 -040013474 INIT_LIST_HEAD(&queue->page_list);
13475 INIT_LIST_HEAD(&queue->child_list);
13476 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13477 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13478 if (!dmabuf)
13479 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040013480 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13481 hw_page_size, &dmabuf->phys,
13482 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013483 if (!dmabuf->virt) {
13484 kfree(dmabuf);
13485 goto out_fail;
13486 }
13487 dmabuf->buffer_tag = x;
13488 list_add_tail(&dmabuf->list, &queue->page_list);
13489 /* initialize queue's entry array */
13490 dma_pointer = dmabuf->virt;
13491 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040013492 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040013493 total_qe_count++, dma_pointer += entry_size) {
13494 queue->qe[total_qe_count].address = dma_pointer;
13495 }
13496 }
13497 queue->entry_size = entry_size;
13498 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040013499
13500 /*
13501 * entry_repost is calculated based on the number of entries in the
13502 * queue. This works out except for RQs. If buffers are NOT initially
13503 * posted for every RQE, entry_repost should be adjusted accordingly.
13504 */
13505 queue->entry_repost = (entry_count >> 3);
13506 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13507 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040013508 queue->phba = phba;
13509
13510 return queue;
13511out_fail:
13512 lpfc_sli4_queue_free(queue);
13513 return NULL;
13514}
13515
13516/**
James Smart962bc512013-01-03 15:44:00 -050013517 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13518 * @phba: HBA structure that indicates port to create a queue on.
13519 * @pci_barset: PCI BAR set flag.
13520 *
13521 * This function shall perform iomap of the specified PCI BAR address to host
13522 * memory address if not already done so and return it. The returned host
13523 * memory address can be NULL.
13524 */
13525static void __iomem *
13526lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13527{
James Smart962bc512013-01-03 15:44:00 -050013528 if (!phba->pcidev)
13529 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013530
13531 switch (pci_barset) {
13532 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013533 return phba->pci_bar0_memmap_p;
13534 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013535 return phba->pci_bar2_memmap_p;
13536 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013537 return phba->pci_bar4_memmap_p;
13538 default:
13539 break;
13540 }
13541 return NULL;
13542}
13543
13544/**
James Smart895427b2017-02-12 13:52:30 -080013545 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040013546 * @phba: HBA structure that indicates port to create a queue on.
13547 * @startq: The starting FCP EQ to modify
13548 *
13549 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13550 *
13551 * The @phba struct is used to send mailbox command to HBA. The @startq
13552 * is used to get the starting FCP EQ to change.
13553 * This function is asynchronous and will wait for the mailbox
13554 * command to finish before continuing.
13555 *
13556 * On success this function will return a zero. If unable to allocate enough
13557 * memory this function will return -ENOMEM. If the queue create mailbox command
13558 * fails this function will return -ENXIO.
13559 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013560int
James Smart895427b2017-02-12 13:52:30 -080013561lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013562{
13563 struct lpfc_mbx_modify_eq_delay *eq_delay;
13564 LPFC_MBOXQ_t *mbox;
13565 struct lpfc_queue *eq;
13566 int cnt, rc, length, status = 0;
13567 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013568 uint32_t result;
James Smart895427b2017-02-12 13:52:30 -080013569 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040013570 union lpfc_sli4_cfg_shdr *shdr;
13571 uint16_t dmult;
13572
James Smart895427b2017-02-12 13:52:30 -080013573 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040013574 return 0;
13575
13576 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13577 if (!mbox)
13578 return -ENOMEM;
13579 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13580 sizeof(struct lpfc_sli4_cfg_mhdr));
13581 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13582 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13583 length, LPFC_SLI4_MBX_EMBED);
13584 eq_delay = &mbox->u.mqe.un.eq_delay;
13585
13586 /* Calculate delay multiper from maximum interrupt per second */
James Smart895427b2017-02-12 13:52:30 -080013587 result = phba->cfg_fcp_imax / phba->io_channel_irqs;
13588 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040013589 dmult = 0;
13590 else
13591 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040013592
13593 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080013594 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
13595 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040013596 if (!eq)
13597 continue;
13598 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13599 eq_delay->u.request.eq[cnt].phase = 0;
13600 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13601 cnt++;
13602 if (cnt >= LPFC_MAX_EQ_DELAY)
13603 break;
13604 }
13605 eq_delay->u.request.num_eq = cnt;
13606
13607 mbox->vport = phba->pport;
13608 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13609 mbox->context1 = NULL;
13610 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13611 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13612 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13613 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13614 if (shdr_status || shdr_add_status || rc) {
13615 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13616 "2512 MODIFY_EQ_DELAY mailbox failed with "
13617 "status x%x add_status x%x, mbx status x%x\n",
13618 shdr_status, shdr_add_status, rc);
13619 status = -ENXIO;
13620 }
13621 mempool_free(mbox, phba->mbox_mem_pool);
13622 return status;
13623}
13624
13625/**
James Smart4f774512009-05-22 14:52:35 -040013626 * lpfc_eq_create - Create an Event Queue on the HBA
13627 * @phba: HBA structure that indicates port to create a queue on.
13628 * @eq: The queue structure to use to create the event queue.
13629 * @imax: The maximum interrupt per second limit.
13630 *
13631 * This function creates an event queue, as detailed in @eq, on a port,
13632 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13633 *
13634 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13635 * is used to get the entry count and entry size that are necessary to
13636 * determine the number of pages to allocate and use for this queue. This
13637 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13638 * event queue. This function is asynchronous and will wait for the mailbox
13639 * command to finish before continuing.
13640 *
13641 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013642 * memory this function will return -ENOMEM. If the queue create mailbox command
13643 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013644 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013645int
James Smartee020062012-09-29 11:28:52 -040013646lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040013647{
13648 struct lpfc_mbx_eq_create *eq_create;
13649 LPFC_MBOXQ_t *mbox;
13650 int rc, length, status = 0;
13651 struct lpfc_dmabuf *dmabuf;
13652 uint32_t shdr_status, shdr_add_status;
13653 union lpfc_sli4_cfg_shdr *shdr;
13654 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040013655 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13656
James Smart2e90f4b2011-12-13 13:22:37 -050013657 /* sanity check on queue memory */
13658 if (!eq)
13659 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013660 if (!phba->sli4_hba.pc_sli4_params.supported)
13661 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013662
13663 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13664 if (!mbox)
13665 return -ENOMEM;
13666 length = (sizeof(struct lpfc_mbx_eq_create) -
13667 sizeof(struct lpfc_sli4_cfg_mhdr));
13668 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13669 LPFC_MBOX_OPCODE_EQ_CREATE,
13670 length, LPFC_SLI4_MBX_EMBED);
13671 eq_create = &mbox->u.mqe.un.eq_create;
13672 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13673 eq->page_count);
13674 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13675 LPFC_EQE_SIZE);
13676 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040013677 /* don't setup delay multiplier using EQ_CREATE */
13678 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040013679 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13680 dmult);
13681 switch (eq->entry_count) {
13682 default:
13683 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13684 "0360 Unsupported EQ count. (%d)\n",
13685 eq->entry_count);
13686 if (eq->entry_count < 256)
13687 return -EINVAL;
13688 /* otherwise default to smallest count (drop through) */
13689 case 256:
13690 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13691 LPFC_EQ_CNT_256);
13692 break;
13693 case 512:
13694 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13695 LPFC_EQ_CNT_512);
13696 break;
13697 case 1024:
13698 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13699 LPFC_EQ_CNT_1024);
13700 break;
13701 case 2048:
13702 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13703 LPFC_EQ_CNT_2048);
13704 break;
13705 case 4096:
13706 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13707 LPFC_EQ_CNT_4096);
13708 break;
13709 }
13710 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013711 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013712 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13713 putPaddrLow(dmabuf->phys);
13714 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13715 putPaddrHigh(dmabuf->phys);
13716 }
13717 mbox->vport = phba->pport;
13718 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13719 mbox->context1 = NULL;
13720 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13721 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13722 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13723 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13724 if (shdr_status || shdr_add_status || rc) {
13725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13726 "2500 EQ_CREATE mailbox failed with "
13727 "status x%x add_status x%x, mbx status x%x\n",
13728 shdr_status, shdr_add_status, rc);
13729 status = -ENXIO;
13730 }
13731 eq->type = LPFC_EQ;
13732 eq->subtype = LPFC_NONE;
13733 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13734 if (eq->queue_id == 0xFFFF)
13735 status = -ENXIO;
13736 eq->host_index = 0;
13737 eq->hba_index = 0;
13738
James Smart8fa38512009-07-19 10:01:03 -040013739 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013740 return status;
13741}
13742
13743/**
13744 * lpfc_cq_create - Create a Completion Queue on the HBA
13745 * @phba: HBA structure that indicates port to create a queue on.
13746 * @cq: The queue structure to use to create the completion queue.
13747 * @eq: The event queue to bind this completion queue to.
13748 *
13749 * This function creates a completion queue, as detailed in @wq, on a port,
13750 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13751 *
13752 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13753 * is used to get the entry count and entry size that are necessary to
13754 * determine the number of pages to allocate and use for this queue. The @eq
13755 * is used to indicate which event queue to bind this completion queue to. This
13756 * function will send the CQ_CREATE mailbox command to the HBA to setup the
13757 * completion queue. This function is asynchronous and will wait for the mailbox
13758 * command to finish before continuing.
13759 *
13760 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013761 * memory this function will return -ENOMEM. If the queue create mailbox command
13762 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013763 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013764int
James Smart4f774512009-05-22 14:52:35 -040013765lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13766 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13767{
13768 struct lpfc_mbx_cq_create *cq_create;
13769 struct lpfc_dmabuf *dmabuf;
13770 LPFC_MBOXQ_t *mbox;
13771 int rc, length, status = 0;
13772 uint32_t shdr_status, shdr_add_status;
13773 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013774 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13775
James Smart2e90f4b2011-12-13 13:22:37 -050013776 /* sanity check on queue memory */
13777 if (!cq || !eq)
13778 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013779 if (!phba->sli4_hba.pc_sli4_params.supported)
13780 hw_page_size = SLI4_PAGE_SIZE;
13781
James Smart4f774512009-05-22 14:52:35 -040013782 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13783 if (!mbox)
13784 return -ENOMEM;
13785 length = (sizeof(struct lpfc_mbx_cq_create) -
13786 sizeof(struct lpfc_sli4_cfg_mhdr));
13787 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13788 LPFC_MBOX_OPCODE_CQ_CREATE,
13789 length, LPFC_SLI4_MBX_EMBED);
13790 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013791 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013792 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13793 cq->page_count);
13794 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13795 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013796 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13797 phba->sli4_hba.pc_sli4_params.cqv);
13798 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040013799 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13800 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013801 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13802 eq->queue_id);
13803 } else {
13804 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13805 eq->queue_id);
13806 }
James Smart4f774512009-05-22 14:52:35 -040013807 switch (cq->entry_count) {
13808 default:
13809 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080013810 "0361 Unsupported CQ count: "
13811 "entry cnt %d sz %d pg cnt %d repost %d\n",
13812 cq->entry_count, cq->entry_size,
13813 cq->page_count, cq->entry_repost);
James Smart4f4c1862012-06-12 13:54:02 -040013814 if (cq->entry_count < 256) {
13815 status = -EINVAL;
13816 goto out;
13817 }
James Smart4f774512009-05-22 14:52:35 -040013818 /* otherwise default to smallest count (drop through) */
13819 case 256:
13820 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13821 LPFC_CQ_CNT_256);
13822 break;
13823 case 512:
13824 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13825 LPFC_CQ_CNT_512);
13826 break;
13827 case 1024:
13828 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13829 LPFC_CQ_CNT_1024);
13830 break;
13831 }
13832 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013833 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013834 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13835 putPaddrLow(dmabuf->phys);
13836 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13837 putPaddrHigh(dmabuf->phys);
13838 }
13839 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13840
13841 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013842 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13843 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13844 if (shdr_status || shdr_add_status || rc) {
13845 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13846 "2501 CQ_CREATE mailbox failed with "
13847 "status x%x add_status x%x, mbx status x%x\n",
13848 shdr_status, shdr_add_status, rc);
13849 status = -ENXIO;
13850 goto out;
13851 }
13852 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13853 if (cq->queue_id == 0xFFFF) {
13854 status = -ENXIO;
13855 goto out;
13856 }
13857 /* link the cq onto the parent eq child list */
13858 list_add_tail(&cq->list, &eq->child_list);
13859 /* Set up completion queue's type and subtype */
13860 cq->type = type;
13861 cq->subtype = subtype;
13862 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050013863 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013864 cq->host_index = 0;
13865 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040013866
James Smart8fa38512009-07-19 10:01:03 -040013867out:
13868 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013869 return status;
13870}
13871
13872/**
James Smartb19a0612010-04-06 14:48:51 -040013873 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040013874 * @phba: HBA structure that indicates port to create a queue on.
13875 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040013876 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13877 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040013878 *
James Smartb19a0612010-04-06 14:48:51 -040013879 * This function provides failback (fb) functionality when the
13880 * mq_create_ext fails on older FW generations. It's purpose is identical
13881 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040013882 *
James Smartb19a0612010-04-06 14:48:51 -040013883 * This routine cannot fail as all attributes were previously accessed and
13884 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040013885 **/
James Smartb19a0612010-04-06 14:48:51 -040013886static void
13887lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13888 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040013889{
13890 struct lpfc_mbx_mq_create *mq_create;
13891 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040013892 int length;
James Smart04c68492009-05-22 14:52:52 -040013893
James Smart04c68492009-05-22 14:52:52 -040013894 length = (sizeof(struct lpfc_mbx_mq_create) -
13895 sizeof(struct lpfc_sli4_cfg_mhdr));
13896 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13897 LPFC_MBOX_OPCODE_MQ_CREATE,
13898 length, LPFC_SLI4_MBX_EMBED);
13899 mq_create = &mbox->u.mqe.un.mq_create;
13900 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040013901 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040013902 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040013903 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040013904 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13905 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040013906 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013907 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13908 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040013909 break;
13910 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013911 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13912 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040013913 break;
13914 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013915 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13916 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040013917 break;
13918 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013919 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13920 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040013921 break;
13922 }
13923 list_for_each_entry(dmabuf, &mq->page_list, list) {
13924 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040013925 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040013926 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040013927 putPaddrHigh(dmabuf->phys);
13928 }
13929}
13930
13931/**
13932 * lpfc_mq_create - Create a mailbox Queue on the HBA
13933 * @phba: HBA structure that indicates port to create a queue on.
13934 * @mq: The queue structure to use to create the mailbox queue.
13935 * @cq: The completion queue to associate with this cq.
13936 * @subtype: The queue's subtype.
13937 *
13938 * This function creates a mailbox queue, as detailed in @mq, on a port,
13939 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13940 *
13941 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13942 * is used to get the entry count and entry size that are necessary to
13943 * determine the number of pages to allocate and use for this queue. This
13944 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13945 * mailbox queue. This function is asynchronous and will wait for the mailbox
13946 * command to finish before continuing.
13947 *
13948 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013949 * memory this function will return -ENOMEM. If the queue create mailbox command
13950 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040013951 **/
13952int32_t
13953lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13954 struct lpfc_queue *cq, uint32_t subtype)
13955{
13956 struct lpfc_mbx_mq_create *mq_create;
13957 struct lpfc_mbx_mq_create_ext *mq_create_ext;
13958 struct lpfc_dmabuf *dmabuf;
13959 LPFC_MBOXQ_t *mbox;
13960 int rc, length, status = 0;
13961 uint32_t shdr_status, shdr_add_status;
13962 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013963 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040013964
James Smart2e90f4b2011-12-13 13:22:37 -050013965 /* sanity check on queue memory */
13966 if (!mq || !cq)
13967 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013968 if (!phba->sli4_hba.pc_sli4_params.supported)
13969 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040013970
13971 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13972 if (!mbox)
13973 return -ENOMEM;
13974 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13975 sizeof(struct lpfc_sli4_cfg_mhdr));
13976 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13977 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13978 length, LPFC_SLI4_MBX_EMBED);
13979
13980 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050013981 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050013982 bf_set(lpfc_mbx_mq_create_ext_num_pages,
13983 &mq_create_ext->u.request, mq->page_count);
13984 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13985 &mq_create_ext->u.request, 1);
13986 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040013987 &mq_create_ext->u.request, 1);
13988 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13989 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050013990 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13991 &mq_create_ext->u.request, 1);
13992 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13993 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040013994 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013995 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13996 phba->sli4_hba.pc_sli4_params.mqv);
13997 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13998 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13999 cq->queue_id);
14000 else
14001 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14002 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040014003 switch (mq->entry_count) {
14004 default:
14005 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14006 "0362 Unsupported MQ count. (%d)\n",
14007 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014008 if (mq->entry_count < 16) {
14009 status = -EINVAL;
14010 goto out;
14011 }
James Smartb19a0612010-04-06 14:48:51 -040014012 /* otherwise default to smallest count (drop through) */
14013 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014014 bf_set(lpfc_mq_context_ring_size,
14015 &mq_create_ext->u.request.context,
14016 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040014017 break;
14018 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014019 bf_set(lpfc_mq_context_ring_size,
14020 &mq_create_ext->u.request.context,
14021 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040014022 break;
14023 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014024 bf_set(lpfc_mq_context_ring_size,
14025 &mq_create_ext->u.request.context,
14026 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040014027 break;
14028 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014029 bf_set(lpfc_mq_context_ring_size,
14030 &mq_create_ext->u.request.context,
14031 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040014032 break;
14033 }
14034 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014035 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040014036 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14037 putPaddrLow(dmabuf->phys);
14038 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040014039 putPaddrHigh(dmabuf->phys);
14040 }
14041 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040014042 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14043 &mq_create_ext->u.response);
14044 if (rc != MBX_SUCCESS) {
14045 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14046 "2795 MQ_CREATE_EXT failed with "
14047 "status x%x. Failback to MQ_CREATE.\n",
14048 rc);
14049 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14050 mq_create = &mbox->u.mqe.un.mq_create;
14051 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14052 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14053 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14054 &mq_create->u.response);
14055 }
14056
James Smart04c68492009-05-22 14:52:52 -040014057 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040014058 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14059 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14060 if (shdr_status || shdr_add_status || rc) {
14061 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14062 "2502 MQ_CREATE mailbox failed with "
14063 "status x%x add_status x%x, mbx status x%x\n",
14064 shdr_status, shdr_add_status, rc);
14065 status = -ENXIO;
14066 goto out;
14067 }
James Smart04c68492009-05-22 14:52:52 -040014068 if (mq->queue_id == 0xFFFF) {
14069 status = -ENXIO;
14070 goto out;
14071 }
14072 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050014073 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040014074 mq->subtype = subtype;
14075 mq->host_index = 0;
14076 mq->hba_index = 0;
14077
14078 /* link the mq onto the parent cq child list */
14079 list_add_tail(&mq->list, &cq->child_list);
14080out:
James Smart8fa38512009-07-19 10:01:03 -040014081 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014082 return status;
14083}
14084
14085/**
James Smart4f774512009-05-22 14:52:35 -040014086 * lpfc_wq_create - Create a Work Queue on the HBA
14087 * @phba: HBA structure that indicates port to create a queue on.
14088 * @wq: The queue structure to use to create the work queue.
14089 * @cq: The completion queue to bind this work queue to.
14090 * @subtype: The subtype of the work queue indicating its functionality.
14091 *
14092 * This function creates a work queue, as detailed in @wq, on a port, described
14093 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14094 *
14095 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14096 * is used to get the entry count and entry size that are necessary to
14097 * determine the number of pages to allocate and use for this queue. The @cq
14098 * is used to indicate which completion queue to bind this work queue to. This
14099 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14100 * work queue. This function is asynchronous and will wait for the mailbox
14101 * command to finish before continuing.
14102 *
14103 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014104 * memory this function will return -ENOMEM. If the queue create mailbox command
14105 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014106 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014107int
James Smart4f774512009-05-22 14:52:35 -040014108lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14109 struct lpfc_queue *cq, uint32_t subtype)
14110{
14111 struct lpfc_mbx_wq_create *wq_create;
14112 struct lpfc_dmabuf *dmabuf;
14113 LPFC_MBOXQ_t *mbox;
14114 int rc, length, status = 0;
14115 uint32_t shdr_status, shdr_add_status;
14116 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014117 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050014118 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050014119 void __iomem *bar_memmap_p;
14120 uint32_t db_offset;
14121 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040014122
James Smart2e90f4b2011-12-13 13:22:37 -050014123 /* sanity check on queue memory */
14124 if (!wq || !cq)
14125 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014126 if (!phba->sli4_hba.pc_sli4_params.supported)
14127 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014128
14129 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14130 if (!mbox)
14131 return -ENOMEM;
14132 length = (sizeof(struct lpfc_mbx_wq_create) -
14133 sizeof(struct lpfc_sli4_cfg_mhdr));
14134 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14135 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14136 length, LPFC_SLI4_MBX_EMBED);
14137 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014138 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014139 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14140 wq->page_count);
14141 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14142 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040014143
14144 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050014145 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14146 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050014147
James Smart0c651872013-07-15 18:33:23 -040014148 switch (phba->sli4_hba.pc_sli4_params.wqv) {
14149 case LPFC_Q_CREATE_VERSION_0:
14150 switch (wq->entry_size) {
14151 default:
14152 case 64:
14153 /* Nothing to do, version 0 ONLY supports 64 byte */
14154 page = wq_create->u.request.page;
14155 break;
14156 case 128:
14157 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14158 LPFC_WQ_SZ128_SUPPORT)) {
14159 status = -ERANGE;
14160 goto out;
14161 }
14162 /* If we get here the HBA MUST also support V1 and
14163 * we MUST use it
14164 */
14165 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14166 LPFC_Q_CREATE_VERSION_1);
14167
14168 bf_set(lpfc_mbx_wq_create_wqe_count,
14169 &wq_create->u.request_1, wq->entry_count);
14170 bf_set(lpfc_mbx_wq_create_wqe_size,
14171 &wq_create->u.request_1,
14172 LPFC_WQ_WQE_SIZE_128);
14173 bf_set(lpfc_mbx_wq_create_page_size,
14174 &wq_create->u.request_1,
James Smart8ea73db2017-02-12 13:52:25 -080014175 LPFC_WQ_PAGE_SIZE_4096);
James Smart0c651872013-07-15 18:33:23 -040014176 page = wq_create->u.request_1.page;
14177 break;
14178 }
14179 break;
14180 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050014181 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14182 wq->entry_count);
14183 switch (wq->entry_size) {
14184 default:
14185 case 64:
14186 bf_set(lpfc_mbx_wq_create_wqe_size,
14187 &wq_create->u.request_1,
14188 LPFC_WQ_WQE_SIZE_64);
14189 break;
14190 case 128:
James Smart0c651872013-07-15 18:33:23 -040014191 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14192 LPFC_WQ_SZ128_SUPPORT)) {
14193 status = -ERANGE;
14194 goto out;
14195 }
James Smart5a6f1332011-03-11 16:05:35 -050014196 bf_set(lpfc_mbx_wq_create_wqe_size,
14197 &wq_create->u.request_1,
14198 LPFC_WQ_WQE_SIZE_128);
14199 break;
14200 }
James Smart8ea73db2017-02-12 13:52:25 -080014201 bf_set(lpfc_mbx_wq_create_page_size,
14202 &wq_create->u.request_1,
14203 LPFC_WQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050014204 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040014205 break;
14206 default:
14207 status = -ERANGE;
14208 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050014209 }
James Smart0c651872013-07-15 18:33:23 -040014210
James Smart4f774512009-05-22 14:52:35 -040014211 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014212 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050014213 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14214 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040014215 }
James Smart962bc512013-01-03 15:44:00 -050014216
14217 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14218 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14219
James Smart4f774512009-05-22 14:52:35 -040014220 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14221 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014222 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14223 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14224 if (shdr_status || shdr_add_status || rc) {
14225 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14226 "2503 WQ_CREATE mailbox failed with "
14227 "status x%x add_status x%x, mbx status x%x\n",
14228 shdr_status, shdr_add_status, rc);
14229 status = -ENXIO;
14230 goto out;
14231 }
14232 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
14233 if (wq->queue_id == 0xFFFF) {
14234 status = -ENXIO;
14235 goto out;
14236 }
James Smart962bc512013-01-03 15:44:00 -050014237 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14238 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
14239 &wq_create->u.response);
14240 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
14241 (wq->db_format != LPFC_DB_RING_FORMAT)) {
14242 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14243 "3265 WQ[%d] doorbell format not "
14244 "supported: x%x\n", wq->queue_id,
14245 wq->db_format);
14246 status = -EINVAL;
14247 goto out;
14248 }
14249 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
14250 &wq_create->u.response);
14251 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14252 if (!bar_memmap_p) {
14253 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14254 "3263 WQ[%d] failed to memmap pci "
14255 "barset:x%x\n", wq->queue_id,
14256 pci_barset);
14257 status = -ENOMEM;
14258 goto out;
14259 }
14260 db_offset = wq_create->u.response.doorbell_offset;
14261 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
14262 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
14263 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14264 "3252 WQ[%d] doorbell offset not "
14265 "supported: x%x\n", wq->queue_id,
14266 db_offset);
14267 status = -EINVAL;
14268 goto out;
14269 }
14270 wq->db_regaddr = bar_memmap_p + db_offset;
14271 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040014272 "3264 WQ[%d]: barset:x%x, offset:x%x, "
14273 "format:x%x\n", wq->queue_id, pci_barset,
14274 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014275 } else {
14276 wq->db_format = LPFC_DB_LIST_FORMAT;
14277 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
14278 }
James Smart895427b2017-02-12 13:52:30 -080014279 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
14280 if (wq->pring == NULL) {
14281 status = -ENOMEM;
14282 goto out;
14283 }
James Smart4f774512009-05-22 14:52:35 -040014284 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050014285 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014286 wq->subtype = subtype;
14287 wq->host_index = 0;
14288 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050014289 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040014290
14291 /* link the wq onto the parent cq child list */
14292 list_add_tail(&wq->list, &cq->child_list);
14293out:
James Smart8fa38512009-07-19 10:01:03 -040014294 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014295 return status;
14296}
14297
14298/**
James Smart73d91e52011-10-10 21:32:10 -040014299 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
14300 * @phba: HBA structure that indicates port to create a queue on.
14301 * @rq: The queue structure to use for the receive queue.
14302 * @qno: The associated HBQ number
14303 *
14304 *
14305 * For SLI4 we need to adjust the RQ repost value based on
14306 * the number of buffers that are initially posted to the RQ.
14307 */
14308void
14309lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
14310{
14311 uint32_t cnt;
14312
James Smart2e90f4b2011-12-13 13:22:37 -050014313 /* sanity check on queue memory */
14314 if (!rq)
14315 return;
James Smart73d91e52011-10-10 21:32:10 -040014316 cnt = lpfc_hbq_defs[qno]->entry_count;
14317
14318 /* Recalc repost for RQs based on buffers initially posted */
14319 cnt = (cnt >> 3);
14320 if (cnt < LPFC_QUEUE_MIN_REPOST)
14321 cnt = LPFC_QUEUE_MIN_REPOST;
14322
14323 rq->entry_repost = cnt;
14324}
14325
14326/**
James Smart4f774512009-05-22 14:52:35 -040014327 * lpfc_rq_create - Create a Receive Queue on the HBA
14328 * @phba: HBA structure that indicates port to create a queue on.
14329 * @hrq: The queue structure to use to create the header receive queue.
14330 * @drq: The queue structure to use to create the data receive queue.
14331 * @cq: The completion queue to bind this work queue to.
14332 *
14333 * This function creates a receive buffer queue pair , as detailed in @hrq and
14334 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
14335 * to the HBA.
14336 *
14337 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
14338 * struct is used to get the entry count that is necessary to determine the
14339 * number of pages to use for this queue. The @cq is used to indicate which
14340 * completion queue to bind received buffers that are posted to these queues to.
14341 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
14342 * receive queue pair. This function is asynchronous and will wait for the
14343 * mailbox command to finish before continuing.
14344 *
14345 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014346 * memory this function will return -ENOMEM. If the queue create mailbox command
14347 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014348 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014349int
James Smart4f774512009-05-22 14:52:35 -040014350lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14351 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
14352{
14353 struct lpfc_mbx_rq_create *rq_create;
14354 struct lpfc_dmabuf *dmabuf;
14355 LPFC_MBOXQ_t *mbox;
14356 int rc, length, status = 0;
14357 uint32_t shdr_status, shdr_add_status;
14358 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014359 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050014360 void __iomem *bar_memmap_p;
14361 uint32_t db_offset;
14362 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040014363
James Smart2e90f4b2011-12-13 13:22:37 -050014364 /* sanity check on queue memory */
14365 if (!hrq || !drq || !cq)
14366 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014367 if (!phba->sli4_hba.pc_sli4_params.supported)
14368 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014369
14370 if (hrq->entry_count != drq->entry_count)
14371 return -EINVAL;
14372 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14373 if (!mbox)
14374 return -ENOMEM;
14375 length = (sizeof(struct lpfc_mbx_rq_create) -
14376 sizeof(struct lpfc_sli4_cfg_mhdr));
14377 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14378 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14379 length, LPFC_SLI4_MBX_EMBED);
14380 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014381 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14382 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14383 phba->sli4_hba.pc_sli4_params.rqv);
14384 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14385 bf_set(lpfc_rq_context_rqe_count_1,
14386 &rq_create->u.request.context,
14387 hrq->entry_count);
14388 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040014389 bf_set(lpfc_rq_context_rqe_size,
14390 &rq_create->u.request.context,
14391 LPFC_RQE_SIZE_8);
14392 bf_set(lpfc_rq_context_page_size,
14393 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080014394 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050014395 } else {
14396 switch (hrq->entry_count) {
14397 default:
14398 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14399 "2535 Unsupported RQ count. (%d)\n",
14400 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014401 if (hrq->entry_count < 512) {
14402 status = -EINVAL;
14403 goto out;
14404 }
James Smart5a6f1332011-03-11 16:05:35 -050014405 /* otherwise default to smallest count (drop through) */
14406 case 512:
14407 bf_set(lpfc_rq_context_rqe_count,
14408 &rq_create->u.request.context,
14409 LPFC_RQ_RING_SIZE_512);
14410 break;
14411 case 1024:
14412 bf_set(lpfc_rq_context_rqe_count,
14413 &rq_create->u.request.context,
14414 LPFC_RQ_RING_SIZE_1024);
14415 break;
14416 case 2048:
14417 bf_set(lpfc_rq_context_rqe_count,
14418 &rq_create->u.request.context,
14419 LPFC_RQ_RING_SIZE_2048);
14420 break;
14421 case 4096:
14422 bf_set(lpfc_rq_context_rqe_count,
14423 &rq_create->u.request.context,
14424 LPFC_RQ_RING_SIZE_4096);
14425 break;
14426 }
14427 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14428 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040014429 }
14430 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14431 cq->queue_id);
14432 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14433 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040014434 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014435 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014436 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14437 putPaddrLow(dmabuf->phys);
14438 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14439 putPaddrHigh(dmabuf->phys);
14440 }
James Smart962bc512013-01-03 15:44:00 -050014441 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14442 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
14443
James Smart4f774512009-05-22 14:52:35 -040014444 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14445 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014446 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14447 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14448 if (shdr_status || shdr_add_status || rc) {
14449 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14450 "2504 RQ_CREATE mailbox failed with "
14451 "status x%x add_status x%x, mbx status x%x\n",
14452 shdr_status, shdr_add_status, rc);
14453 status = -ENXIO;
14454 goto out;
14455 }
14456 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14457 if (hrq->queue_id == 0xFFFF) {
14458 status = -ENXIO;
14459 goto out;
14460 }
James Smart962bc512013-01-03 15:44:00 -050014461
14462 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14463 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
14464 &rq_create->u.response);
14465 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
14466 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
14467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14468 "3262 RQ [%d] doorbell format not "
14469 "supported: x%x\n", hrq->queue_id,
14470 hrq->db_format);
14471 status = -EINVAL;
14472 goto out;
14473 }
14474
14475 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
14476 &rq_create->u.response);
14477 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14478 if (!bar_memmap_p) {
14479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14480 "3269 RQ[%d] failed to memmap pci "
14481 "barset:x%x\n", hrq->queue_id,
14482 pci_barset);
14483 status = -ENOMEM;
14484 goto out;
14485 }
14486
14487 db_offset = rq_create->u.response.doorbell_offset;
14488 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
14489 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
14490 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14491 "3270 RQ[%d] doorbell offset not "
14492 "supported: x%x\n", hrq->queue_id,
14493 db_offset);
14494 status = -EINVAL;
14495 goto out;
14496 }
14497 hrq->db_regaddr = bar_memmap_p + db_offset;
14498 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040014499 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
14500 "format:x%x\n", hrq->queue_id, pci_barset,
14501 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014502 } else {
14503 hrq->db_format = LPFC_DB_RING_FORMAT;
14504 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
14505 }
James Smart4f774512009-05-22 14:52:35 -040014506 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014507 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014508 hrq->subtype = subtype;
14509 hrq->host_index = 0;
14510 hrq->hba_index = 0;
14511
14512 /* now create the data queue */
14513 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14514 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14515 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050014516 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14517 phba->sli4_hba.pc_sli4_params.rqv);
14518 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14519 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040014520 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050014521 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040014522 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
14523 LPFC_RQE_SIZE_8);
14524 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
14525 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014526 } else {
14527 switch (drq->entry_count) {
14528 default:
14529 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14530 "2536 Unsupported RQ count. (%d)\n",
14531 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014532 if (drq->entry_count < 512) {
14533 status = -EINVAL;
14534 goto out;
14535 }
James Smart5a6f1332011-03-11 16:05:35 -050014536 /* otherwise default to smallest count (drop through) */
14537 case 512:
14538 bf_set(lpfc_rq_context_rqe_count,
14539 &rq_create->u.request.context,
14540 LPFC_RQ_RING_SIZE_512);
14541 break;
14542 case 1024:
14543 bf_set(lpfc_rq_context_rqe_count,
14544 &rq_create->u.request.context,
14545 LPFC_RQ_RING_SIZE_1024);
14546 break;
14547 case 2048:
14548 bf_set(lpfc_rq_context_rqe_count,
14549 &rq_create->u.request.context,
14550 LPFC_RQ_RING_SIZE_2048);
14551 break;
14552 case 4096:
14553 bf_set(lpfc_rq_context_rqe_count,
14554 &rq_create->u.request.context,
14555 LPFC_RQ_RING_SIZE_4096);
14556 break;
14557 }
14558 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14559 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040014560 }
14561 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14562 cq->queue_id);
14563 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14564 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040014565 list_for_each_entry(dmabuf, &drq->page_list, list) {
14566 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14567 putPaddrLow(dmabuf->phys);
14568 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14569 putPaddrHigh(dmabuf->phys);
14570 }
James Smart962bc512013-01-03 15:44:00 -050014571 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14572 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040014573 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14574 /* The IOCTL status is embedded in the mailbox subheader. */
14575 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14576 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14577 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14578 if (shdr_status || shdr_add_status || rc) {
14579 status = -ENXIO;
14580 goto out;
14581 }
14582 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14583 if (drq->queue_id == 0xFFFF) {
14584 status = -ENXIO;
14585 goto out;
14586 }
14587 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014588 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014589 drq->subtype = subtype;
14590 drq->host_index = 0;
14591 drq->hba_index = 0;
14592
14593 /* link the header and data RQs onto the parent cq child list */
14594 list_add_tail(&hrq->list, &cq->child_list);
14595 list_add_tail(&drq->list, &cq->child_list);
14596
14597out:
James Smart8fa38512009-07-19 10:01:03 -040014598 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014599 return status;
14600}
14601
14602/**
14603 * lpfc_eq_destroy - Destroy an event Queue on the HBA
14604 * @eq: The queue structure associated with the queue to destroy.
14605 *
14606 * This function destroys a queue, as detailed in @eq by sending an mailbox
14607 * command, specific to the type of queue, to the HBA.
14608 *
14609 * The @eq struct is used to get the queue ID of the queue to destroy.
14610 *
14611 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014612 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014613 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014614int
James Smart4f774512009-05-22 14:52:35 -040014615lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
14616{
14617 LPFC_MBOXQ_t *mbox;
14618 int rc, length, status = 0;
14619 uint32_t shdr_status, shdr_add_status;
14620 union lpfc_sli4_cfg_shdr *shdr;
14621
James Smart2e90f4b2011-12-13 13:22:37 -050014622 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014623 if (!eq)
14624 return -ENODEV;
14625 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
14626 if (!mbox)
14627 return -ENOMEM;
14628 length = (sizeof(struct lpfc_mbx_eq_destroy) -
14629 sizeof(struct lpfc_sli4_cfg_mhdr));
14630 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14631 LPFC_MBOX_OPCODE_EQ_DESTROY,
14632 length, LPFC_SLI4_MBX_EMBED);
14633 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
14634 eq->queue_id);
14635 mbox->vport = eq->phba->pport;
14636 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14637
14638 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
14639 /* The IOCTL status is embedded in the mailbox subheader. */
14640 shdr = (union lpfc_sli4_cfg_shdr *)
14641 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
14642 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14643 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14644 if (shdr_status || shdr_add_status || rc) {
14645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14646 "2505 EQ_DESTROY mailbox failed with "
14647 "status x%x add_status x%x, mbx status x%x\n",
14648 shdr_status, shdr_add_status, rc);
14649 status = -ENXIO;
14650 }
14651
14652 /* Remove eq from any list */
14653 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040014654 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014655 return status;
14656}
14657
14658/**
14659 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
14660 * @cq: The queue structure associated with the queue to destroy.
14661 *
14662 * This function destroys a queue, as detailed in @cq by sending an mailbox
14663 * command, specific to the type of queue, to the HBA.
14664 *
14665 * The @cq struct is used to get the queue ID of the queue to destroy.
14666 *
14667 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014668 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014669 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014670int
James Smart4f774512009-05-22 14:52:35 -040014671lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14672{
14673 LPFC_MBOXQ_t *mbox;
14674 int rc, length, status = 0;
14675 uint32_t shdr_status, shdr_add_status;
14676 union lpfc_sli4_cfg_shdr *shdr;
14677
James Smart2e90f4b2011-12-13 13:22:37 -050014678 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014679 if (!cq)
14680 return -ENODEV;
14681 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14682 if (!mbox)
14683 return -ENOMEM;
14684 length = (sizeof(struct lpfc_mbx_cq_destroy) -
14685 sizeof(struct lpfc_sli4_cfg_mhdr));
14686 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14687 LPFC_MBOX_OPCODE_CQ_DESTROY,
14688 length, LPFC_SLI4_MBX_EMBED);
14689 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14690 cq->queue_id);
14691 mbox->vport = cq->phba->pport;
14692 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14693 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14694 /* The IOCTL status is embedded in the mailbox subheader. */
14695 shdr = (union lpfc_sli4_cfg_shdr *)
14696 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14697 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14698 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14699 if (shdr_status || shdr_add_status || rc) {
14700 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14701 "2506 CQ_DESTROY mailbox failed with "
14702 "status x%x add_status x%x, mbx status x%x\n",
14703 shdr_status, shdr_add_status, rc);
14704 status = -ENXIO;
14705 }
14706 /* Remove cq from any list */
14707 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040014708 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014709 return status;
14710}
14711
14712/**
James Smart04c68492009-05-22 14:52:52 -040014713 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14714 * @qm: The queue structure associated with the queue to destroy.
14715 *
14716 * This function destroys a queue, as detailed in @mq by sending an mailbox
14717 * command, specific to the type of queue, to the HBA.
14718 *
14719 * The @mq struct is used to get the queue ID of the queue to destroy.
14720 *
14721 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014722 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040014723 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014724int
James Smart04c68492009-05-22 14:52:52 -040014725lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14726{
14727 LPFC_MBOXQ_t *mbox;
14728 int rc, length, status = 0;
14729 uint32_t shdr_status, shdr_add_status;
14730 union lpfc_sli4_cfg_shdr *shdr;
14731
James Smart2e90f4b2011-12-13 13:22:37 -050014732 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040014733 if (!mq)
14734 return -ENODEV;
14735 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14736 if (!mbox)
14737 return -ENOMEM;
14738 length = (sizeof(struct lpfc_mbx_mq_destroy) -
14739 sizeof(struct lpfc_sli4_cfg_mhdr));
14740 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14741 LPFC_MBOX_OPCODE_MQ_DESTROY,
14742 length, LPFC_SLI4_MBX_EMBED);
14743 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14744 mq->queue_id);
14745 mbox->vport = mq->phba->pport;
14746 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14747 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14748 /* The IOCTL status is embedded in the mailbox subheader. */
14749 shdr = (union lpfc_sli4_cfg_shdr *)
14750 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14751 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14752 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14753 if (shdr_status || shdr_add_status || rc) {
14754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14755 "2507 MQ_DESTROY mailbox failed with "
14756 "status x%x add_status x%x, mbx status x%x\n",
14757 shdr_status, shdr_add_status, rc);
14758 status = -ENXIO;
14759 }
14760 /* Remove mq from any list */
14761 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040014762 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014763 return status;
14764}
14765
14766/**
James Smart4f774512009-05-22 14:52:35 -040014767 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14768 * @wq: The queue structure associated with the queue to destroy.
14769 *
14770 * This function destroys a queue, as detailed in @wq by sending an mailbox
14771 * command, specific to the type of queue, to the HBA.
14772 *
14773 * The @wq struct is used to get the queue ID of the queue to destroy.
14774 *
14775 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014776 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014777 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014778int
James Smart4f774512009-05-22 14:52:35 -040014779lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14780{
14781 LPFC_MBOXQ_t *mbox;
14782 int rc, length, status = 0;
14783 uint32_t shdr_status, shdr_add_status;
14784 union lpfc_sli4_cfg_shdr *shdr;
14785
James Smart2e90f4b2011-12-13 13:22:37 -050014786 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014787 if (!wq)
14788 return -ENODEV;
14789 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14790 if (!mbox)
14791 return -ENOMEM;
14792 length = (sizeof(struct lpfc_mbx_wq_destroy) -
14793 sizeof(struct lpfc_sli4_cfg_mhdr));
14794 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14795 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14796 length, LPFC_SLI4_MBX_EMBED);
14797 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14798 wq->queue_id);
14799 mbox->vport = wq->phba->pport;
14800 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14801 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14802 shdr = (union lpfc_sli4_cfg_shdr *)
14803 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14804 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14805 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14806 if (shdr_status || shdr_add_status || rc) {
14807 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14808 "2508 WQ_DESTROY mailbox failed with "
14809 "status x%x add_status x%x, mbx status x%x\n",
14810 shdr_status, shdr_add_status, rc);
14811 status = -ENXIO;
14812 }
14813 /* Remove wq from any list */
14814 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040014815 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014816 return status;
14817}
14818
14819/**
14820 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14821 * @rq: The queue structure associated with the queue to destroy.
14822 *
14823 * This function destroys a queue, as detailed in @rq by sending an mailbox
14824 * command, specific to the type of queue, to the HBA.
14825 *
14826 * The @rq struct is used to get the queue ID of the queue to destroy.
14827 *
14828 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014829 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014830 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014831int
James Smart4f774512009-05-22 14:52:35 -040014832lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14833 struct lpfc_queue *drq)
14834{
14835 LPFC_MBOXQ_t *mbox;
14836 int rc, length, status = 0;
14837 uint32_t shdr_status, shdr_add_status;
14838 union lpfc_sli4_cfg_shdr *shdr;
14839
James Smart2e90f4b2011-12-13 13:22:37 -050014840 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014841 if (!hrq || !drq)
14842 return -ENODEV;
14843 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14844 if (!mbox)
14845 return -ENOMEM;
14846 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050014847 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040014848 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14849 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14850 length, LPFC_SLI4_MBX_EMBED);
14851 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14852 hrq->queue_id);
14853 mbox->vport = hrq->phba->pport;
14854 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14855 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14856 /* The IOCTL status is embedded in the mailbox subheader. */
14857 shdr = (union lpfc_sli4_cfg_shdr *)
14858 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14859 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14860 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14861 if (shdr_status || shdr_add_status || rc) {
14862 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14863 "2509 RQ_DESTROY mailbox failed with "
14864 "status x%x add_status x%x, mbx status x%x\n",
14865 shdr_status, shdr_add_status, rc);
14866 if (rc != MBX_TIMEOUT)
14867 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14868 return -ENXIO;
14869 }
14870 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14871 drq->queue_id);
14872 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14873 shdr = (union lpfc_sli4_cfg_shdr *)
14874 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14875 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14876 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14877 if (shdr_status || shdr_add_status || rc) {
14878 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14879 "2510 RQ_DESTROY mailbox failed with "
14880 "status x%x add_status x%x, mbx status x%x\n",
14881 shdr_status, shdr_add_status, rc);
14882 status = -ENXIO;
14883 }
14884 list_del_init(&hrq->list);
14885 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040014886 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014887 return status;
14888}
14889
14890/**
14891 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14892 * @phba: The virtual port for which this call being executed.
14893 * @pdma_phys_addr0: Physical address of the 1st SGL page.
14894 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14895 * @xritag: the xritag that ties this io to the SGL pages.
14896 *
14897 * This routine will post the sgl pages for the IO that has the xritag
14898 * that is in the iocbq structure. The xritag is assigned during iocbq
14899 * creation and persists for as long as the driver is loaded.
14900 * if the caller has fewer than 256 scatter gather segments to map then
14901 * pdma_phys_addr1 should be 0.
14902 * If the caller needs to map more than 256 scatter gather segment then
14903 * pdma_phys_addr1 should be a valid physical address.
14904 * physical address for SGLs must be 64 byte aligned.
14905 * If you are going to map 2 SGL's then the first one must have 256 entries
14906 * the second sgl can have between 1 and 256 entries.
14907 *
14908 * Return codes:
14909 * 0 - Success
14910 * -ENXIO, -ENOMEM - Failure
14911 **/
14912int
14913lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14914 dma_addr_t pdma_phys_addr0,
14915 dma_addr_t pdma_phys_addr1,
14916 uint16_t xritag)
14917{
14918 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14919 LPFC_MBOXQ_t *mbox;
14920 int rc;
14921 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040014922 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040014923 union lpfc_sli4_cfg_shdr *shdr;
14924
14925 if (xritag == NO_XRI) {
14926 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14927 "0364 Invalid param:\n");
14928 return -EINVAL;
14929 }
14930
14931 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14932 if (!mbox)
14933 return -ENOMEM;
14934
14935 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14936 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14937 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050014938 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040014939
14940 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14941 &mbox->u.mqe.un.post_sgl_pages;
14942 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14943 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14944
14945 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14946 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14947 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14948 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14949
14950 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14951 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14952 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14953 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14954 if (!phba->sli4_hba.intr_enable)
14955 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040014956 else {
James Smarta183a152011-10-10 21:32:43 -040014957 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040014958 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14959 }
James Smart4f774512009-05-22 14:52:35 -040014960 /* The IOCTL status is embedded in the mailbox subheader. */
14961 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14962 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14963 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14964 if (rc != MBX_TIMEOUT)
14965 mempool_free(mbox, phba->mbox_mem_pool);
14966 if (shdr_status || shdr_add_status || rc) {
14967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14968 "2511 POST_SGL mailbox failed with "
14969 "status x%x add_status x%x, mbx status x%x\n",
14970 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040014971 }
14972 return 0;
14973}
James Smart4f774512009-05-22 14:52:35 -040014974
14975/**
James Smart88a2cfb2011-07-22 18:36:33 -040014976 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040014977 * @phba: pointer to lpfc hba data structure.
14978 *
14979 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040014980 * HBA consistent with the SLI-4 interface spec. This routine
14981 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14982 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040014983 *
James Smart88a2cfb2011-07-22 18:36:33 -040014984 * Returns
14985 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14986 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14987 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014988static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014989lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14990{
14991 unsigned long xri;
14992
14993 /*
14994 * Fetch the next logical xri. Because this index is logical,
14995 * the driver starts at 0 each time.
14996 */
14997 spin_lock_irq(&phba->hbalock);
14998 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14999 phba->sli4_hba.max_cfg_param.max_xri, 0);
15000 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15001 spin_unlock_irq(&phba->hbalock);
15002 return NO_XRI;
15003 } else {
15004 set_bit(xri, phba->sli4_hba.xri_bmask);
15005 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040015006 }
James Smart6d368e52011-05-24 11:44:12 -040015007 spin_unlock_irq(&phba->hbalock);
15008 return xri;
15009}
15010
15011/**
15012 * lpfc_sli4_free_xri - Release an xri for reuse.
15013 * @phba: pointer to lpfc hba data structure.
15014 *
15015 * This routine is invoked to release an xri to the pool of
15016 * available rpis maintained by the driver.
15017 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015018static void
James Smart6d368e52011-05-24 11:44:12 -040015019__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15020{
15021 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040015022 phba->sli4_hba.max_cfg_param.xri_used--;
15023 }
15024}
15025
15026/**
15027 * lpfc_sli4_free_xri - Release an xri for reuse.
15028 * @phba: pointer to lpfc hba data structure.
15029 *
15030 * This routine is invoked to release an xri to the pool of
15031 * available rpis maintained by the driver.
15032 **/
15033void
15034lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15035{
15036 spin_lock_irq(&phba->hbalock);
15037 __lpfc_sli4_free_xri(phba, xri);
15038 spin_unlock_irq(&phba->hbalock);
15039}
15040
15041/**
James Smart4f774512009-05-22 14:52:35 -040015042 * lpfc_sli4_next_xritag - Get an xritag for the io
15043 * @phba: Pointer to HBA context object.
15044 *
15045 * This function gets an xritag for the iocb. If there is no unused xritag
15046 * it will return 0xffff.
15047 * The function returns the allocated xritag if successful, else returns zero.
15048 * Zero is not a valid xritag.
15049 * The caller is not required to hold any lock.
15050 **/
15051uint16_t
15052lpfc_sli4_next_xritag(struct lpfc_hba *phba)
15053{
James Smart6d368e52011-05-24 11:44:12 -040015054 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040015055
James Smart6d368e52011-05-24 11:44:12 -040015056 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040015057 if (xri_index == NO_XRI)
15058 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15059 "2004 Failed to allocate XRI.last XRITAG is %d"
15060 " Max XRI is %d, Used XRI is %d\n",
15061 xri_index,
15062 phba->sli4_hba.max_cfg_param.max_xri,
15063 phba->sli4_hba.max_cfg_param.xri_used);
15064 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040015065}
15066
15067/**
James Smart895427b2017-02-12 13:52:30 -080015068 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040015069 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040015070 * @post_sgl_list: pointer to els sgl entry list.
15071 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040015072 *
15073 * This routine is invoked to post a block of driver's sgl pages to the
15074 * HBA using non-embedded mailbox command. No Lock is held. This routine
15075 * is only called when the driver is loading and after all IO has been
15076 * stopped.
15077 **/
James Smart8a9d2e82012-05-09 21:16:12 -040015078static int
James Smart895427b2017-02-12 13:52:30 -080015079lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040015080 struct list_head *post_sgl_list,
15081 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040015082{
James Smart8a9d2e82012-05-09 21:16:12 -040015083 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040015084 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15085 struct sgl_page_pairs *sgl_pg_pairs;
15086 void *viraddr;
15087 LPFC_MBOXQ_t *mbox;
15088 uint32_t reqlen, alloclen, pg_pairs;
15089 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040015090 uint16_t xritag_start = 0;
15091 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040015092 uint32_t shdr_status, shdr_add_status;
15093 union lpfc_sli4_cfg_shdr *shdr;
15094
James Smart895427b2017-02-12 13:52:30 -080015095 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040015096 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040015097 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080015098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040015099 "2559 Block sgl registration required DMA "
15100 "size (%d) great than a page\n", reqlen);
15101 return -ENOMEM;
15102 }
James Smart895427b2017-02-12 13:52:30 -080015103
James Smart4f774512009-05-22 14:52:35 -040015104 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040015105 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040015106 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040015107
15108 /* Allocate DMA memory and set up the non-embedded mailbox command */
15109 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15110 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15111 LPFC_SLI4_MBX_NEMBED);
15112
15113 if (alloclen < reqlen) {
15114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15115 "0285 Allocated DMA memory size (%d) is "
15116 "less than the requested DMA memory "
15117 "size (%d)\n", alloclen, reqlen);
15118 lpfc_sli4_mbox_cmd_free(phba, mbox);
15119 return -ENOMEM;
15120 }
James Smart4f774512009-05-22 14:52:35 -040015121 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040015122 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040015123 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15124 sgl_pg_pairs = &sgl->sgl_pg_pairs;
15125
James Smart8a9d2e82012-05-09 21:16:12 -040015126 pg_pairs = 0;
15127 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040015128 /* Set up the sge entry */
15129 sgl_pg_pairs->sgl_pg0_addr_lo =
15130 cpu_to_le32(putPaddrLow(sglq_entry->phys));
15131 sgl_pg_pairs->sgl_pg0_addr_hi =
15132 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
15133 sgl_pg_pairs->sgl_pg1_addr_lo =
15134 cpu_to_le32(putPaddrLow(0));
15135 sgl_pg_pairs->sgl_pg1_addr_hi =
15136 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040015137
James Smart4f774512009-05-22 14:52:35 -040015138 /* Keep the first xritag on the list */
15139 if (pg_pairs == 0)
15140 xritag_start = sglq_entry->sli4_xritag;
15141 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040015142 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040015143 }
James Smart6d368e52011-05-24 11:44:12 -040015144
15145 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040015146 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080015147 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040015148 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080015149
James Smart4f774512009-05-22 14:52:35 -040015150 if (!phba->sli4_hba.intr_enable)
15151 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15152 else {
James Smarta183a152011-10-10 21:32:43 -040015153 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040015154 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15155 }
15156 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
15157 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15158 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15159 if (rc != MBX_TIMEOUT)
15160 lpfc_sli4_mbox_cmd_free(phba, mbox);
15161 if (shdr_status || shdr_add_status || rc) {
15162 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15163 "2513 POST_SGL_BLOCK mailbox command failed "
15164 "status x%x add_status x%x mbx status x%x\n",
15165 shdr_status, shdr_add_status, rc);
15166 rc = -ENXIO;
15167 }
15168 return rc;
15169}
15170
15171/**
15172 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
15173 * @phba: pointer to lpfc hba data structure.
15174 * @sblist: pointer to scsi buffer list.
15175 * @count: number of scsi buffers on the list.
15176 *
15177 * This routine is invoked to post a block of @count scsi sgl pages from a
15178 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
15179 * No Lock is held.
15180 *
15181 **/
15182int
James Smart8a9d2e82012-05-09 21:16:12 -040015183lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
15184 struct list_head *sblist,
15185 int count)
James Smart4f774512009-05-22 14:52:35 -040015186{
15187 struct lpfc_scsi_buf *psb;
15188 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15189 struct sgl_page_pairs *sgl_pg_pairs;
15190 void *viraddr;
15191 LPFC_MBOXQ_t *mbox;
15192 uint32_t reqlen, alloclen, pg_pairs;
15193 uint32_t mbox_tmo;
15194 uint16_t xritag_start = 0;
15195 int rc = 0;
15196 uint32_t shdr_status, shdr_add_status;
15197 dma_addr_t pdma_phys_bpl1;
15198 union lpfc_sli4_cfg_shdr *shdr;
15199
15200 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040015201 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040015202 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040015203 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040015204 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
15205 "0217 Block sgl registration required DMA "
15206 "size (%d) great than a page\n", reqlen);
15207 return -ENOMEM;
15208 }
15209 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15210 if (!mbox) {
15211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15212 "0283 Failed to allocate mbox cmd memory\n");
15213 return -ENOMEM;
15214 }
15215
15216 /* Allocate DMA memory and set up the non-embedded mailbox command */
15217 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15218 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15219 LPFC_SLI4_MBX_NEMBED);
15220
15221 if (alloclen < reqlen) {
15222 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15223 "2561 Allocated DMA memory size (%d) is "
15224 "less than the requested DMA memory "
15225 "size (%d)\n", alloclen, reqlen);
15226 lpfc_sli4_mbox_cmd_free(phba, mbox);
15227 return -ENOMEM;
15228 }
James Smart6d368e52011-05-24 11:44:12 -040015229
James Smart4f774512009-05-22 14:52:35 -040015230 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040015231 viraddr = mbox->sge_array->addr[0];
15232
15233 /* Set up the SGL pages in the non-embedded DMA pages */
15234 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15235 sgl_pg_pairs = &sgl->sgl_pg_pairs;
15236
15237 pg_pairs = 0;
15238 list_for_each_entry(psb, sblist, list) {
15239 /* Set up the sge entry */
15240 sgl_pg_pairs->sgl_pg0_addr_lo =
15241 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
15242 sgl_pg_pairs->sgl_pg0_addr_hi =
15243 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
15244 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
15245 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
15246 else
15247 pdma_phys_bpl1 = 0;
15248 sgl_pg_pairs->sgl_pg1_addr_lo =
15249 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
15250 sgl_pg_pairs->sgl_pg1_addr_hi =
15251 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
15252 /* Keep the first xritag on the list */
15253 if (pg_pairs == 0)
15254 xritag_start = psb->cur_iocbq.sli4_xritag;
15255 sgl_pg_pairs++;
15256 pg_pairs++;
15257 }
15258 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
15259 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
15260 /* Perform endian conversion if necessary */
15261 sgl->word0 = cpu_to_le32(sgl->word0);
15262
15263 if (!phba->sli4_hba.intr_enable)
15264 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15265 else {
James Smarta183a152011-10-10 21:32:43 -040015266 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040015267 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15268 }
15269 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
15270 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15271 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15272 if (rc != MBX_TIMEOUT)
15273 lpfc_sli4_mbox_cmd_free(phba, mbox);
15274 if (shdr_status || shdr_add_status || rc) {
15275 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15276 "2564 POST_SGL_BLOCK mailbox command failed "
15277 "status x%x add_status x%x mbx status x%x\n",
15278 shdr_status, shdr_add_status, rc);
15279 rc = -ENXIO;
15280 }
15281 return rc;
15282}
15283
James Smart2ea259e2017-02-12 13:52:27 -080015284static char *lpfc_rctl_names[] = FC_RCTL_NAMES_INIT;
15285static char *lpfc_type_names[] = FC_TYPE_NAMES_INIT;
15286
James Smart4f774512009-05-22 14:52:35 -040015287/**
15288 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
15289 * @phba: pointer to lpfc_hba struct that the frame was received on
15290 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
15291 *
15292 * This function checks the fields in the @fc_hdr to see if the FC frame is a
15293 * valid type of frame that the LPFC driver will handle. This function will
15294 * return a zero if the frame is a valid frame or a non zero value when the
15295 * frame does not pass the check.
15296 **/
15297static int
15298lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
15299{
Tomas Henzl474ffb72010-12-22 16:52:40 +010015300 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040015301 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050015302 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040015303
15304 switch (fc_hdr->fh_r_ctl) {
15305 case FC_RCTL_DD_UNCAT: /* uncategorized information */
15306 case FC_RCTL_DD_SOL_DATA: /* solicited data */
15307 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
15308 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
15309 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
15310 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
15311 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
15312 case FC_RCTL_DD_CMD_STATUS: /* command status */
15313 case FC_RCTL_ELS_REQ: /* extended link services request */
15314 case FC_RCTL_ELS_REP: /* extended link services reply */
15315 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
15316 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
15317 case FC_RCTL_BA_NOP: /* basic link service NOP */
15318 case FC_RCTL_BA_ABTS: /* basic link service abort */
15319 case FC_RCTL_BA_RMC: /* remove connection */
15320 case FC_RCTL_BA_ACC: /* basic accept */
15321 case FC_RCTL_BA_RJT: /* basic reject */
15322 case FC_RCTL_BA_PRMT:
15323 case FC_RCTL_ACK_1: /* acknowledge_1 */
15324 case FC_RCTL_ACK_0: /* acknowledge_0 */
15325 case FC_RCTL_P_RJT: /* port reject */
15326 case FC_RCTL_F_RJT: /* fabric reject */
15327 case FC_RCTL_P_BSY: /* port busy */
15328 case FC_RCTL_F_BSY: /* fabric busy to data frame */
15329 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
15330 case FC_RCTL_LCR: /* link credit reset */
15331 case FC_RCTL_END: /* end */
15332 break;
15333 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
15334 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
15335 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
15336 return lpfc_fc_frame_check(phba, fc_hdr);
15337 default:
15338 goto drop;
15339 }
15340 switch (fc_hdr->fh_type) {
15341 case FC_TYPE_BLS:
15342 case FC_TYPE_ELS:
15343 case FC_TYPE_FCP:
15344 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080015345 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040015346 break;
15347 case FC_TYPE_IP:
15348 case FC_TYPE_ILS:
15349 default:
15350 goto drop;
15351 }
James Smart546fc852011-03-11 16:06:29 -050015352
James Smart4f774512009-05-22 14:52:35 -040015353 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040015354 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
15355 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart2ea259e2017-02-12 13:52:27 -080015356 lpfc_rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
15357 lpfc_type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050015358 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
15359 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040015360 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
15361 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040015362 return 0;
15363drop:
15364 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
15365 "2539 Dropped frame rctl:%s type:%s\n",
James Smart2ea259e2017-02-12 13:52:27 -080015366 lpfc_rctl_names[fc_hdr->fh_r_ctl],
15367 lpfc_type_names[fc_hdr->fh_type]);
James Smart4f774512009-05-22 14:52:35 -040015368 return 1;
15369}
15370
15371/**
15372 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
15373 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
15374 *
15375 * This function processes the FC header to retrieve the VFI from the VF
15376 * header, if one exists. This function will return the VFI if one exists
15377 * or 0 if no VSAN Header exists.
15378 **/
15379static uint32_t
15380lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
15381{
15382 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
15383
15384 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
15385 return 0;
15386 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
15387}
15388
15389/**
15390 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
15391 * @phba: Pointer to the HBA structure to search for the vport on
15392 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
15393 * @fcfi: The FC Fabric ID that the frame came from
15394 *
15395 * This function searches the @phba for a vport that matches the content of the
15396 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
15397 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
15398 * returns the matching vport pointer or NULL if unable to match frame to a
15399 * vport.
15400 **/
15401static struct lpfc_vport *
15402lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080015403 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040015404{
15405 struct lpfc_vport **vports;
15406 struct lpfc_vport *vport = NULL;
15407 int i;
James Smart939723a2012-05-09 21:19:03 -040015408
James Smartbf086112011-08-21 21:48:13 -040015409 if (did == Fabric_DID)
15410 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040015411 if ((phba->pport->fc_flag & FC_PT2PT) &&
15412 !(phba->link_state == LPFC_HBA_READY))
15413 return phba->pport;
15414
James Smart4f774512009-05-22 14:52:35 -040015415 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080015416 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040015417 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
15418 if (phba->fcf.fcfi == fcfi &&
15419 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
15420 vports[i]->fc_myDID == did) {
15421 vport = vports[i];
15422 break;
15423 }
15424 }
James Smart895427b2017-02-12 13:52:30 -080015425 }
James Smart4f774512009-05-22 14:52:35 -040015426 lpfc_destroy_vport_work_array(phba, vports);
15427 return vport;
15428}
15429
15430/**
James Smart45ed1192009-10-02 15:17:02 -040015431 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
15432 * @vport: The vport to work on.
15433 *
15434 * This function updates the receive sequence time stamp for this vport. The
15435 * receive sequence time stamp indicates the time that the last frame of the
15436 * the sequence that has been idle for the longest amount of time was received.
15437 * the driver uses this time stamp to indicate if any received sequences have
15438 * timed out.
15439 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015440static void
James Smart45ed1192009-10-02 15:17:02 -040015441lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
15442{
15443 struct lpfc_dmabuf *h_buf;
15444 struct hbq_dmabuf *dmabuf = NULL;
15445
15446 /* get the oldest sequence on the rcv list */
15447 h_buf = list_get_first(&vport->rcv_buffer_list,
15448 struct lpfc_dmabuf, list);
15449 if (!h_buf)
15450 return;
15451 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15452 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
15453}
15454
15455/**
15456 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
15457 * @vport: The vport that the received sequences were sent to.
15458 *
15459 * This function cleans up all outstanding received sequences. This is called
15460 * by the driver when a link event or user action invalidates all the received
15461 * sequences.
15462 **/
15463void
15464lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
15465{
15466 struct lpfc_dmabuf *h_buf, *hnext;
15467 struct lpfc_dmabuf *d_buf, *dnext;
15468 struct hbq_dmabuf *dmabuf = NULL;
15469
15470 /* start with the oldest sequence on the rcv list */
15471 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15472 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15473 list_del_init(&dmabuf->hbuf.list);
15474 list_for_each_entry_safe(d_buf, dnext,
15475 &dmabuf->dbuf.list, list) {
15476 list_del_init(&d_buf->list);
15477 lpfc_in_buf_free(vport->phba, d_buf);
15478 }
15479 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15480 }
15481}
15482
15483/**
15484 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
15485 * @vport: The vport that the received sequences were sent to.
15486 *
15487 * This function determines whether any received sequences have timed out by
15488 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
15489 * indicates that there is at least one timed out sequence this routine will
15490 * go through the received sequences one at a time from most inactive to most
15491 * active to determine which ones need to be cleaned up. Once it has determined
15492 * that a sequence needs to be cleaned up it will simply free up the resources
15493 * without sending an abort.
15494 **/
15495void
15496lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
15497{
15498 struct lpfc_dmabuf *h_buf, *hnext;
15499 struct lpfc_dmabuf *d_buf, *dnext;
15500 struct hbq_dmabuf *dmabuf = NULL;
15501 unsigned long timeout;
15502 int abort_count = 0;
15503
15504 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15505 vport->rcv_buffer_time_stamp);
15506 if (list_empty(&vport->rcv_buffer_list) ||
15507 time_before(jiffies, timeout))
15508 return;
15509 /* start with the oldest sequence on the rcv list */
15510 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15511 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15512 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15513 dmabuf->time_stamp);
15514 if (time_before(jiffies, timeout))
15515 break;
15516 abort_count++;
15517 list_del_init(&dmabuf->hbuf.list);
15518 list_for_each_entry_safe(d_buf, dnext,
15519 &dmabuf->dbuf.list, list) {
15520 list_del_init(&d_buf->list);
15521 lpfc_in_buf_free(vport->phba, d_buf);
15522 }
15523 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15524 }
15525 if (abort_count)
15526 lpfc_update_rcv_time_stamp(vport);
15527}
15528
15529/**
James Smart4f774512009-05-22 14:52:35 -040015530 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
15531 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
15532 *
15533 * This function searches through the existing incomplete sequences that have
15534 * been sent to this @vport. If the frame matches one of the incomplete
15535 * sequences then the dbuf in the @dmabuf is added to the list of frames that
15536 * make up that sequence. If no sequence is found that matches this frame then
15537 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
15538 * This function returns a pointer to the first dmabuf in the sequence list that
15539 * the frame was linked to.
15540 **/
15541static struct hbq_dmabuf *
15542lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15543{
15544 struct fc_frame_header *new_hdr;
15545 struct fc_frame_header *temp_hdr;
15546 struct lpfc_dmabuf *d_buf;
15547 struct lpfc_dmabuf *h_buf;
15548 struct hbq_dmabuf *seq_dmabuf = NULL;
15549 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050015550 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040015551
James Smart4d9ab992009-10-02 15:16:39 -040015552 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015553 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040015554 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050015555
James Smart4f774512009-05-22 14:52:35 -040015556 /* Use the hdr_buf to find the sequence that this frame belongs to */
15557 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15558 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15559 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15560 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15561 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15562 continue;
15563 /* found a pending sequence that matches this frame */
15564 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15565 break;
15566 }
15567 if (!seq_dmabuf) {
15568 /*
15569 * This indicates first frame received for this sequence.
15570 * Queue the buffer on the vport's rcv_buffer_list.
15571 */
15572 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040015573 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015574 return dmabuf;
15575 }
15576 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050015577 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
15578 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040015579 list_del_init(&seq_dmabuf->hbuf.list);
15580 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15581 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015582 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015583 return dmabuf;
15584 }
James Smart45ed1192009-10-02 15:17:02 -040015585 /* move this sequence to the tail to indicate a young sequence */
15586 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
15587 seq_dmabuf->time_stamp = jiffies;
15588 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050015589 if (list_empty(&seq_dmabuf->dbuf.list)) {
15590 temp_hdr = dmabuf->hbuf.virt;
15591 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15592 return seq_dmabuf;
15593 }
James Smart4f774512009-05-22 14:52:35 -040015594 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050015595 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
15596 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040015597 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15598 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
15599 /*
15600 * If the frame's sequence count is greater than the frame on
15601 * the list then insert the frame right after this frame
15602 */
James Smarteeead812009-12-21 17:01:23 -050015603 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
15604 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040015605 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050015606 found = 1;
15607 break;
James Smart4f774512009-05-22 14:52:35 -040015608 }
James Smart4360ca92015-12-16 18:12:04 -050015609
15610 if (&d_buf->list == &seq_dmabuf->dbuf.list)
15611 break;
15612 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040015613 }
James Smart4360ca92015-12-16 18:12:04 -050015614
15615 if (found)
15616 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015617 return NULL;
15618}
15619
15620/**
James Smart6669f9b2009-10-02 15:16:45 -040015621 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
15622 * @vport: pointer to a vitural port
15623 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15624 *
15625 * This function tries to abort from the partially assembed sequence, described
15626 * by the information from basic abbort @dmabuf. It checks to see whether such
15627 * partially assembled sequence held by the driver. If so, it shall free up all
15628 * the frames from the partially assembled sequence.
15629 *
15630 * Return
15631 * true -- if there is matching partially assembled sequence present and all
15632 * the frames freed with the sequence;
15633 * false -- if there is no matching partially assembled sequence present so
15634 * nothing got aborted in the lower layer driver
15635 **/
15636static bool
15637lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
15638 struct hbq_dmabuf *dmabuf)
15639{
15640 struct fc_frame_header *new_hdr;
15641 struct fc_frame_header *temp_hdr;
15642 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
15643 struct hbq_dmabuf *seq_dmabuf = NULL;
15644
15645 /* Use the hdr_buf to find the sequence that matches this frame */
15646 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15647 INIT_LIST_HEAD(&dmabuf->hbuf.list);
15648 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15649 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15650 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15651 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15652 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15653 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15654 continue;
15655 /* found a pending sequence that matches this frame */
15656 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15657 break;
15658 }
15659
15660 /* Free up all the frames from the partially assembled sequence */
15661 if (seq_dmabuf) {
15662 list_for_each_entry_safe(d_buf, n_buf,
15663 &seq_dmabuf->dbuf.list, list) {
15664 list_del_init(&d_buf->list);
15665 lpfc_in_buf_free(vport->phba, d_buf);
15666 }
15667 return true;
15668 }
15669 return false;
15670}
15671
15672/**
James Smart6dd9e312013-01-03 15:43:37 -050015673 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15674 * @vport: pointer to a vitural port
15675 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15676 *
15677 * This function tries to abort from the assembed sequence from upper level
15678 * protocol, described by the information from basic abbort @dmabuf. It
15679 * checks to see whether such pending context exists at upper level protocol.
15680 * If so, it shall clean up the pending context.
15681 *
15682 * Return
15683 * true -- if there is matching pending context of the sequence cleaned
15684 * at ulp;
15685 * false -- if there is no matching pending context of the sequence present
15686 * at ulp.
15687 **/
15688static bool
15689lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15690{
15691 struct lpfc_hba *phba = vport->phba;
15692 int handled;
15693
15694 /* Accepting abort at ulp with SLI4 only */
15695 if (phba->sli_rev < LPFC_SLI_REV4)
15696 return false;
15697
15698 /* Register all caring upper level protocols to attend abort */
15699 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15700 if (handled)
15701 return true;
15702
15703 return false;
15704}
15705
15706/**
James Smart546fc852011-03-11 16:06:29 -050015707 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040015708 * @phba: Pointer to HBA context object.
15709 * @cmd_iocbq: pointer to the command iocbq structure.
15710 * @rsp_iocbq: pointer to the response iocbq structure.
15711 *
James Smart546fc852011-03-11 16:06:29 -050015712 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040015713 * event. It properly releases the memory allocated to the sequence abort
15714 * accept iocb.
15715 **/
15716static void
James Smart546fc852011-03-11 16:06:29 -050015717lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040015718 struct lpfc_iocbq *cmd_iocbq,
15719 struct lpfc_iocbq *rsp_iocbq)
15720{
James Smart6dd9e312013-01-03 15:43:37 -050015721 struct lpfc_nodelist *ndlp;
15722
15723 if (cmd_iocbq) {
15724 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15725 lpfc_nlp_put(ndlp);
15726 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015727 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050015728 }
James Smart6b5151f2012-01-18 16:24:06 -050015729
15730 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15731 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15732 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15733 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
15734 rsp_iocbq->iocb.ulpStatus,
15735 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040015736}
15737
15738/**
James Smart6d368e52011-05-24 11:44:12 -040015739 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15740 * @phba: Pointer to HBA context object.
15741 * @xri: xri id in transaction.
15742 *
15743 * This function validates the xri maps to the known range of XRIs allocated an
15744 * used by the driver.
15745 **/
James Smart7851fe22011-07-22 18:36:52 -040015746uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015747lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15748 uint16_t xri)
15749{
James Smarta2fc4aef2014-09-03 12:57:55 -040015750 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040015751
15752 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15753 if (xri == phba->sli4_hba.xri_ids[i])
15754 return i;
15755 }
15756 return NO_XRI;
15757}
15758
James Smart6d368e52011-05-24 11:44:12 -040015759/**
James Smart546fc852011-03-11 16:06:29 -050015760 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015761 * @phba: Pointer to HBA context object.
15762 * @fc_hdr: pointer to a FC frame header.
15763 *
James Smart546fc852011-03-11 16:06:29 -050015764 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015765 * event after aborting the sequence handling.
15766 **/
15767static void
James Smart6dd9e312013-01-03 15:43:37 -050015768lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15769 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040015770{
James Smart6dd9e312013-01-03 15:43:37 -050015771 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040015772 struct lpfc_iocbq *ctiocb = NULL;
15773 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040015774 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050015775 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040015776 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050015777 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040015778
15779 if (!lpfc_is_link_up(phba))
15780 return;
15781
15782 sid = sli4_sid_from_fc_hdr(fc_hdr);
15783 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050015784 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040015785
James Smart6dd9e312013-01-03 15:43:37 -050015786 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040015787 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050015788 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15789 if (!ndlp) {
15790 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15791 "1268 Failed to allocate ndlp for "
15792 "oxid:x%x SID:x%x\n", oxid, sid);
15793 return;
15794 }
15795 lpfc_nlp_init(vport, ndlp, sid);
15796 /* Put ndlp onto pport node list */
15797 lpfc_enqueue_node(vport, ndlp);
15798 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15799 /* re-setup ndlp without removing from node list */
15800 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15801 if (!ndlp) {
15802 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15803 "3275 Failed to active ndlp found "
15804 "for oxid:x%x SID:x%x\n", oxid, sid);
15805 return;
15806 }
James Smart6669f9b2009-10-02 15:16:45 -040015807 }
15808
James Smart546fc852011-03-11 16:06:29 -050015809 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040015810 ctiocb = lpfc_sli_get_iocbq(phba);
15811 if (!ctiocb)
15812 return;
15813
James Smart5ffc2662009-11-18 15:39:44 -050015814 /* Extract the F_CTL field from FC_HDR */
15815 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15816
James Smart6669f9b2009-10-02 15:16:45 -040015817 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040015818 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050015819 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040015820 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15821 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15822 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15823
15824 /* Fill in the rest of iocb fields */
15825 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15826 icmd->ulpBdeCount = 0;
15827 icmd->ulpLe = 1;
15828 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040015829 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050015830 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015831
James Smart6669f9b2009-10-02 15:16:45 -040015832 ctiocb->iocb_cmpl = NULL;
15833 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050015834 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040015835 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050015836 ctiocb->sli4_xritag = NO_XRI;
15837
James Smartee0f4fe2012-05-09 21:19:14 -040015838 if (fctl & FC_FC_EX_CTX)
15839 /* Exchange responder sent the abort so we
15840 * own the oxid.
15841 */
15842 xri = oxid;
15843 else
15844 xri = rxid;
15845 lxri = lpfc_sli4_xri_inrange(phba, xri);
15846 if (lxri != NO_XRI)
15847 lpfc_set_rrq_active(phba, ndlp, lxri,
15848 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050015849 /* For BA_ABTS from exchange responder, if the logical xri with
15850 * the oxid maps to the FCP XRI range, the port no longer has
15851 * that exchange context, send a BLS_RJT. Override the IOCB for
15852 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050015853 */
James Smart6dd9e312013-01-03 15:43:37 -050015854 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080015855 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050015856 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15857 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15858 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15859 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15860 }
15861
15862 /* If BA_ABTS failed to abort a partially assembled receive sequence,
15863 * the driver no longer has that exchange, send a BLS_RJT. Override
15864 * the IOCB for a BA_RJT.
15865 */
15866 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050015867 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15868 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15869 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15870 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15871 }
James Smart6669f9b2009-10-02 15:16:45 -040015872
James Smart5ffc2662009-11-18 15:39:44 -050015873 if (fctl & FC_FC_EX_CTX) {
15874 /* ABTS sent by responder to CT exchange, construction
15875 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15876 * field and RX_ID from ABTS for RX_ID field.
15877 */
James Smart546fc852011-03-11 16:06:29 -050015878 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050015879 } else {
15880 /* ABTS sent by initiator to CT exchange, construction
15881 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050015882 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050015883 */
James Smart546fc852011-03-11 16:06:29 -050015884 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050015885 }
James Smartf09c3ac2012-03-01 22:33:29 -050015886 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050015887 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050015888
James Smart546fc852011-03-11 16:06:29 -050015889 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050015890 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15891 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15892 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050015893
15894 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15895 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050015896 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15897 "2925 Failed to issue CT ABTS RSP x%x on "
15898 "xri x%x, Data x%x\n",
15899 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15900 phba->link_state);
15901 lpfc_nlp_put(ndlp);
15902 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050015903 lpfc_sli_release_iocbq(phba, ctiocb);
15904 }
James Smart6669f9b2009-10-02 15:16:45 -040015905}
15906
15907/**
15908 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15909 * @vport: Pointer to the vport on which this sequence was received
15910 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15911 *
15912 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15913 * receive sequence is only partially assembed by the driver, it shall abort
15914 * the partially assembled frames for the sequence. Otherwise, if the
15915 * unsolicited receive sequence has been completely assembled and passed to
15916 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15917 * unsolicited sequence has been aborted. After that, it will issue a basic
15918 * accept to accept the abort.
15919 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015920static void
James Smart6669f9b2009-10-02 15:16:45 -040015921lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15922 struct hbq_dmabuf *dmabuf)
15923{
15924 struct lpfc_hba *phba = vport->phba;
15925 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050015926 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050015927 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040015928
James Smart6669f9b2009-10-02 15:16:45 -040015929 /* Make a copy of fc_hdr before the dmabuf being released */
15930 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050015931 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040015932
James Smart5ffc2662009-11-18 15:39:44 -050015933 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050015934 /* ABTS by responder to exchange, no cleanup needed */
15935 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050015936 } else {
James Smart6dd9e312013-01-03 15:43:37 -050015937 /* ABTS by initiator to exchange, need to do cleanup */
15938 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15939 if (aborted == false)
15940 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050015941 }
James Smart6dd9e312013-01-03 15:43:37 -050015942 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15943
15944 /* Respond with BA_ACC or BA_RJT accordingly */
15945 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040015946}
15947
15948/**
James Smart4f774512009-05-22 14:52:35 -040015949 * lpfc_seq_complete - Indicates if a sequence is complete
15950 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15951 *
15952 * This function checks the sequence, starting with the frame described by
15953 * @dmabuf, to see if all the frames associated with this sequence are present.
15954 * the frames associated with this sequence are linked to the @dmabuf using the
15955 * dbuf list. This function looks for two major things. 1) That the first frame
15956 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15957 * set. 3) That there are no holes in the sequence count. The function will
15958 * return 1 when the sequence is complete, otherwise it will return 0.
15959 **/
15960static int
15961lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15962{
15963 struct fc_frame_header *hdr;
15964 struct lpfc_dmabuf *d_buf;
15965 struct hbq_dmabuf *seq_dmabuf;
15966 uint32_t fctl;
15967 int seq_count = 0;
15968
15969 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15970 /* make sure first fame of sequence has a sequence count of zero */
15971 if (hdr->fh_seq_cnt != seq_count)
15972 return 0;
15973 fctl = (hdr->fh_f_ctl[0] << 16 |
15974 hdr->fh_f_ctl[1] << 8 |
15975 hdr->fh_f_ctl[2]);
15976 /* If last frame of sequence we can return success. */
15977 if (fctl & FC_FC_END_SEQ)
15978 return 1;
15979 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15980 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15981 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15982 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050015983 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040015984 return 0;
15985 fctl = (hdr->fh_f_ctl[0] << 16 |
15986 hdr->fh_f_ctl[1] << 8 |
15987 hdr->fh_f_ctl[2]);
15988 /* If last frame of sequence we can return success. */
15989 if (fctl & FC_FC_END_SEQ)
15990 return 1;
15991 }
15992 return 0;
15993}
15994
15995/**
15996 * lpfc_prep_seq - Prep sequence for ULP processing
15997 * @vport: Pointer to the vport on which this sequence was received
15998 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15999 *
16000 * This function takes a sequence, described by a list of frames, and creates
16001 * a list of iocbq structures to describe the sequence. This iocbq list will be
16002 * used to issue to the generic unsolicited sequence handler. This routine
16003 * returns a pointer to the first iocbq in the list. If the function is unable
16004 * to allocate an iocbq then it throw out the received frames that were not
16005 * able to be described and return a pointer to the first iocbq. If unable to
16006 * allocate any iocbqs (including the first) this function will return NULL.
16007 **/
16008static struct lpfc_iocbq *
16009lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16010{
James Smart7851fe22011-07-22 18:36:52 -040016011 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040016012 struct lpfc_dmabuf *d_buf, *n_buf;
16013 struct lpfc_iocbq *first_iocbq, *iocbq;
16014 struct fc_frame_header *fc_hdr;
16015 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040016016 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050016017 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040016018
16019 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16020 /* remove from receive buffer list */
16021 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016022 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016023 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040016024 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040016025 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016026 /* Get an iocbq struct to fill in. */
16027 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16028 if (first_iocbq) {
16029 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040016030 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016031 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080016032 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040016033
16034 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16035 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16036 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
16037 first_iocbq->iocb.un.rcvels.parmRo =
16038 sli4_did_from_fc_hdr(fc_hdr);
16039 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
16040 } else
16041 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040016042 first_iocbq->iocb.ulpContext = NO_XRI;
16043 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
16044 be16_to_cpu(fc_hdr->fh_ox_id);
16045 /* iocbq is prepped for internal consumption. Physical vpi. */
16046 first_iocbq->iocb.unsli3.rcvsli3.vpi =
16047 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040016048 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040016049 tot_len = bf_get(lpfc_rcqe_length,
16050 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
16051
James Smart4f774512009-05-22 14:52:35 -040016052 first_iocbq->context2 = &seq_dmabuf->dbuf;
16053 first_iocbq->context3 = NULL;
16054 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040016055 if (tot_len > LPFC_DATA_BUF_SIZE)
16056 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040016057 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040016058 else
16059 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
16060
James Smart4f774512009-05-22 14:52:35 -040016061 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040016062
James Smart7851fe22011-07-22 18:36:52 -040016063 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040016064 }
16065 iocbq = first_iocbq;
16066 /*
16067 * Each IOCBq can have two Buffers assigned, so go through the list
16068 * of buffers for this sequence and save two buffers in each IOCBq
16069 */
16070 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
16071 if (!iocbq) {
16072 lpfc_in_buf_free(vport->phba, d_buf);
16073 continue;
16074 }
16075 if (!iocbq->context3) {
16076 iocbq->context3 = d_buf;
16077 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040016078 /* We need to get the size out of the right CQE */
16079 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16080 len = bf_get(lpfc_rcqe_length,
16081 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016082 pbde = (struct ulp_bde64 *)
16083 &iocbq->iocb.unsli3.sli3Words[4];
16084 if (len > LPFC_DATA_BUF_SIZE)
16085 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
16086 else
16087 pbde->tus.f.bdeSize = len;
16088
James Smart7851fe22011-07-22 18:36:52 -040016089 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
16090 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040016091 } else {
16092 iocbq = lpfc_sli_get_iocbq(vport->phba);
16093 if (!iocbq) {
16094 if (first_iocbq) {
16095 first_iocbq->iocb.ulpStatus =
16096 IOSTAT_FCP_RSP_ERROR;
16097 first_iocbq->iocb.un.ulpWord[4] =
16098 IOERR_NO_RESOURCES;
16099 }
16100 lpfc_in_buf_free(vport->phba, d_buf);
16101 continue;
16102 }
James Smart7851fe22011-07-22 18:36:52 -040016103 /* We need to get the size out of the right CQE */
16104 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16105 len = bf_get(lpfc_rcqe_length,
16106 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016107 iocbq->context2 = d_buf;
16108 iocbq->context3 = NULL;
16109 iocbq->iocb.ulpBdeCount = 1;
16110 if (len > LPFC_DATA_BUF_SIZE)
16111 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
16112 LPFC_DATA_BUF_SIZE;
16113 else
16114 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
16115
James Smart7851fe22011-07-22 18:36:52 -040016116 tot_len += len;
16117 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
16118
James Smart4f774512009-05-22 14:52:35 -040016119 iocbq->iocb.un.rcvels.remoteID = sid;
16120 list_add_tail(&iocbq->list, &first_iocbq->list);
16121 }
16122 }
16123 return first_iocbq;
16124}
16125
James Smart6669f9b2009-10-02 15:16:45 -040016126static void
16127lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
16128 struct hbq_dmabuf *seq_dmabuf)
16129{
16130 struct fc_frame_header *fc_hdr;
16131 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
16132 struct lpfc_hba *phba = vport->phba;
16133
16134 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16135 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
16136 if (!iocbq) {
16137 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16138 "2707 Ring %d handler: Failed to allocate "
16139 "iocb Rctl x%x Type x%x received\n",
16140 LPFC_ELS_RING,
16141 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16142 return;
16143 }
16144 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080016145 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040016146 iocbq, fc_hdr->fh_r_ctl,
16147 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040016148 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040016149 "2540 Ring %d handler: unexpected Rctl "
16150 "x%x Type x%x received\n",
16151 LPFC_ELS_RING,
16152 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16153
16154 /* Free iocb created in lpfc_prep_seq */
16155 list_for_each_entry_safe(curr_iocb, next_iocb,
16156 &iocbq->list, list) {
16157 list_del_init(&curr_iocb->list);
16158 lpfc_sli_release_iocbq(phba, curr_iocb);
16159 }
16160 lpfc_sli_release_iocbq(phba, iocbq);
16161}
16162
James Smart4f774512009-05-22 14:52:35 -040016163/**
16164 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
16165 * @phba: Pointer to HBA context object.
16166 *
16167 * This function is called with no lock held. This function processes all
16168 * the received buffers and gives it to upper layers when a received buffer
16169 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080016170 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040016171 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
16172 * appropriate receive function when the final frame in a sequence is received.
16173 **/
James Smart4d9ab992009-10-02 15:16:39 -040016174void
16175lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
16176 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040016177{
James Smart4d9ab992009-10-02 15:16:39 -040016178 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016179 struct fc_frame_header *fc_hdr;
16180 struct lpfc_vport *vport;
16181 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040016182 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040016183
James Smart4f774512009-05-22 14:52:35 -040016184 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040016185 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080016186
James Smart4d9ab992009-10-02 15:16:39 -040016187 /* check to see if this a valid type of frame */
16188 if (lpfc_fc_frame_check(phba, fc_hdr)) {
16189 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16190 return;
16191 }
James Smart2ea259e2017-02-12 13:52:27 -080016192
James Smart7851fe22011-07-22 18:36:52 -040016193 if ((bf_get(lpfc_cqe_code,
16194 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
16195 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
16196 &dmabuf->cq_event.cqe.rcqe_cmpl);
16197 else
16198 fcfi = bf_get(lpfc_rcqe_fcf_id,
16199 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040016200
James Smart895427b2017-02-12 13:52:30 -080016201 /* d_id this frame is directed to */
16202 did = sli4_did_from_fc_hdr(fc_hdr);
16203
16204 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040016205 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040016206 /* throw out the frame */
16207 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16208 return;
16209 }
James Smart939723a2012-05-09 21:19:03 -040016210
James Smart939723a2012-05-09 21:19:03 -040016211 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
16212 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
16213 (did != Fabric_DID)) {
16214 /*
16215 * Throw out the frame if we are not pt2pt.
16216 * The pt2pt protocol allows for discovery frames
16217 * to be received without a registered VPI.
16218 */
16219 if (!(vport->fc_flag & FC_PT2PT) ||
16220 (phba->link_state == LPFC_HBA_READY)) {
16221 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16222 return;
16223 }
16224 }
16225
James Smart6669f9b2009-10-02 15:16:45 -040016226 /* Handle the basic abort sequence (BA_ABTS) event */
16227 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
16228 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
16229 return;
16230 }
16231
James Smart4d9ab992009-10-02 15:16:39 -040016232 /* Link this frame */
16233 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
16234 if (!seq_dmabuf) {
16235 /* unable to add frame to vport - throw it out */
16236 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16237 return;
16238 }
16239 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050016240 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040016241 return;
James Smartdef9c7a2009-12-21 17:02:28 -050016242
James Smart6669f9b2009-10-02 15:16:45 -040016243 /* Send the complete sequence to the upper layer protocol */
16244 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040016245}
James Smart6fb120a2009-05-22 14:52:59 -040016246
16247/**
16248 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
16249 * @phba: pointer to lpfc hba data structure.
16250 *
16251 * This routine is invoked to post rpi header templates to the
16252 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040016253 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16254 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040016255 *
16256 * This routine does not require any locks. It's usage is expected
16257 * to be driver load or reset recovery when the driver is
16258 * sequential.
16259 *
16260 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020016261 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040016262 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040016263 * When this error occurs, the driver is not guaranteed
16264 * to have any rpi regions posted to the device and
16265 * must either attempt to repost the regions or take a
16266 * fatal error.
16267 **/
16268int
16269lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
16270{
16271 struct lpfc_rpi_hdr *rpi_page;
16272 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040016273 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016274
James Smart6d368e52011-05-24 11:44:12 -040016275 /* SLI4 ports that support extents do not require RPI headers. */
16276 if (!phba->sli4_hba.rpi_hdrs_in_use)
16277 goto exit;
16278 if (phba->sli4_hba.extents_in_use)
16279 return -EIO;
16280
James Smart6fb120a2009-05-22 14:52:59 -040016281 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040016282 /*
16283 * Assign the rpi headers a physical rpi only if the driver
16284 * has not initialized those resources. A port reset only
16285 * needs the headers posted.
16286 */
16287 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
16288 LPFC_RPI_RSRC_RDY)
16289 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
16290
James Smart6fb120a2009-05-22 14:52:59 -040016291 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
16292 if (rc != MBX_SUCCESS) {
16293 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16294 "2008 Error %d posting all rpi "
16295 "headers\n", rc);
16296 rc = -EIO;
16297 break;
16298 }
16299 }
16300
James Smart6d368e52011-05-24 11:44:12 -040016301 exit:
16302 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
16303 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040016304 return rc;
16305}
16306
16307/**
16308 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
16309 * @phba: pointer to lpfc hba data structure.
16310 * @rpi_page: pointer to the rpi memory region.
16311 *
16312 * This routine is invoked to post a single rpi header to the
16313 * HBA consistent with the SLI-4 interface spec. This memory region
16314 * maps up to 64 rpi context regions.
16315 *
16316 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020016317 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040016318 * -ENOMEM - No available memory
16319 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040016320 **/
16321int
16322lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
16323{
16324 LPFC_MBOXQ_t *mboxq;
16325 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
16326 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016327 uint32_t shdr_status, shdr_add_status;
16328 union lpfc_sli4_cfg_shdr *shdr;
16329
James Smart6d368e52011-05-24 11:44:12 -040016330 /* SLI4 ports that support extents do not require RPI headers. */
16331 if (!phba->sli4_hba.rpi_hdrs_in_use)
16332 return rc;
16333 if (phba->sli4_hba.extents_in_use)
16334 return -EIO;
16335
James Smart6fb120a2009-05-22 14:52:59 -040016336 /* The port is notified of the header region via a mailbox command. */
16337 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16338 if (!mboxq) {
16339 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16340 "2001 Unable to allocate memory for issuing "
16341 "SLI_CONFIG_SPECIAL mailbox command\n");
16342 return -ENOMEM;
16343 }
16344
16345 /* Post all rpi memory regions to the port. */
16346 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040016347 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16348 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
16349 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050016350 sizeof(struct lpfc_sli4_cfg_mhdr),
16351 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040016352
16353
16354 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040016355 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
16356 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040016357 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
16358 hdr_tmpl, rpi_page->page_count);
16359
James Smart6fb120a2009-05-22 14:52:59 -040016360 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
16361 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040016362 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040016363 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
16364 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16365 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16366 if (rc != MBX_TIMEOUT)
16367 mempool_free(mboxq, phba->mbox_mem_pool);
16368 if (shdr_status || shdr_add_status || rc) {
16369 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16370 "2514 POST_RPI_HDR mailbox failed with "
16371 "status x%x add_status x%x, mbx status x%x\n",
16372 shdr_status, shdr_add_status, rc);
16373 rc = -ENXIO;
16374 }
16375 return rc;
16376}
16377
16378/**
16379 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
16380 * @phba: pointer to lpfc hba data structure.
16381 *
16382 * This routine is invoked to post rpi header templates to the
16383 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040016384 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16385 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040016386 *
16387 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020016388 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040016389 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16390 **/
16391int
16392lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
16393{
James Smart6d368e52011-05-24 11:44:12 -040016394 unsigned long rpi;
16395 uint16_t max_rpi, rpi_limit;
16396 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016397 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040016398 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040016399
James Smart6fb120a2009-05-22 14:52:59 -040016400 /*
James Smart6d368e52011-05-24 11:44:12 -040016401 * Fetch the next logical rpi. Because this index is logical,
16402 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040016403 */
James Smart4902b382013-10-10 12:20:35 -040016404 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040016405 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
16406 rpi_limit = phba->sli4_hba.next_rpi;
16407
James Smart6d368e52011-05-24 11:44:12 -040016408 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
16409 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040016410 rpi = LPFC_RPI_ALLOC_ERROR;
16411 else {
16412 set_bit(rpi, phba->sli4_hba.rpi_bmask);
16413 phba->sli4_hba.max_cfg_param.rpi_used++;
16414 phba->sli4_hba.rpi_count++;
16415 }
James Smartbe6bb942015-04-07 15:07:22 -040016416 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
16417 "0001 rpi:%x max:%x lim:%x\n",
16418 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040016419
16420 /*
16421 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040016422 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040016423 */
16424 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
16425 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040016426 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040016427 return rpi;
16428 }
16429
16430 /*
James Smart6d368e52011-05-24 11:44:12 -040016431 * RPI header postings are not required for SLI4 ports capable of
16432 * extents.
16433 */
16434 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040016435 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040016436 return rpi;
16437 }
16438
16439 /*
James Smart6fb120a2009-05-22 14:52:59 -040016440 * If the driver is running low on rpi resources, allocate another
16441 * page now. Note that the next_rpi value is used because
16442 * it represents how many are actually in use whereas max_rpi notes
16443 * how many are supported max by the device.
16444 */
James Smart6d368e52011-05-24 11:44:12 -040016445 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040016446 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040016447 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
16448 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
16449 if (!rpi_hdr) {
16450 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16451 "2002 Error Could not grow rpi "
16452 "count\n");
16453 } else {
James Smart6d368e52011-05-24 11:44:12 -040016454 lrpi = rpi_hdr->start_rpi;
16455 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040016456 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
16457 }
16458 }
16459
16460 return rpi;
16461}
16462
16463/**
16464 * lpfc_sli4_free_rpi - Release an rpi for reuse.
16465 * @phba: pointer to lpfc hba data structure.
16466 *
16467 * This routine is invoked to release an rpi to the pool of
16468 * available rpis maintained by the driver.
16469 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016470static void
James Smartd7c47992010-06-08 18:31:54 -040016471__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16472{
16473 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
16474 phba->sli4_hba.rpi_count--;
16475 phba->sli4_hba.max_cfg_param.rpi_used--;
16476 }
16477}
16478
16479/**
16480 * lpfc_sli4_free_rpi - Release an rpi for reuse.
16481 * @phba: pointer to lpfc hba data structure.
16482 *
16483 * This routine is invoked to release an rpi to the pool of
16484 * available rpis maintained by the driver.
16485 **/
16486void
James Smart6fb120a2009-05-22 14:52:59 -040016487lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16488{
16489 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040016490 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040016491 spin_unlock_irq(&phba->hbalock);
16492}
16493
16494/**
16495 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
16496 * @phba: pointer to lpfc hba data structure.
16497 *
16498 * This routine is invoked to remove the memory region that
16499 * provided rpi via a bitmask.
16500 **/
16501void
16502lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
16503{
16504 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040016505 kfree(phba->sli4_hba.rpi_ids);
16506 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040016507}
16508
16509/**
16510 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
16511 * @phba: pointer to lpfc hba data structure.
16512 *
16513 * This routine is invoked to remove the memory region that
16514 * provided rpi via a bitmask.
16515 **/
16516int
James Smart6b5151f2012-01-18 16:24:06 -050016517lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
16518 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040016519{
16520 LPFC_MBOXQ_t *mboxq;
16521 struct lpfc_hba *phba = ndlp->phba;
16522 int rc;
16523
16524 /* The port is notified of the header region via a mailbox command. */
16525 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16526 if (!mboxq)
16527 return -ENOMEM;
16528
16529 /* Post all rpi memory regions to the port. */
16530 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050016531 if (cmpl) {
16532 mboxq->mbox_cmpl = cmpl;
16533 mboxq->context1 = arg;
16534 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050016535 } else
16536 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050016537 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040016538 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16539 if (rc == MBX_NOT_FINISHED) {
16540 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16541 "2010 Resume RPI Mailbox failed "
16542 "status %d, mbxStatus x%x\n", rc,
16543 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16544 mempool_free(mboxq, phba->mbox_mem_pool);
16545 return -EIO;
16546 }
16547 return 0;
16548}
16549
16550/**
16551 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050016552 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040016553 *
James Smart76a95d72010-11-20 23:11:48 -050016554 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040016555 *
16556 * Returns:
16557 * 0 success
16558 * -Evalue otherwise
16559 **/
16560int
James Smart76a95d72010-11-20 23:11:48 -050016561lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040016562{
16563 LPFC_MBOXQ_t *mboxq;
16564 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040016565 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040016566 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050016567 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040016568 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16569 if (!mboxq)
16570 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050016571 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040016572 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040016573 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040016574 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050016575 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040016576 "2022 INIT VPI Mailbox failed "
16577 "status %d, mbxStatus x%x\n", rc,
16578 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040016579 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040016580 }
James Smart6a9c52c2009-10-02 15:16:51 -040016581 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050016582 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040016583
16584 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040016585}
16586
16587/**
16588 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
16589 * @phba: pointer to lpfc hba data structure.
16590 * @mboxq: Pointer to mailbox object.
16591 *
16592 * This routine is invoked to manually add a single FCF record. The caller
16593 * must pass a completely initialized FCF_Record. This routine takes
16594 * care of the nonembedded mailbox operations.
16595 **/
16596static void
16597lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
16598{
16599 void *virt_addr;
16600 union lpfc_sli4_cfg_shdr *shdr;
16601 uint32_t shdr_status, shdr_add_status;
16602
16603 virt_addr = mboxq->sge_array->addr[0];
16604 /* The IOCTL status is embedded in the mailbox subheader. */
16605 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
16606 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16607 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16608
16609 if ((shdr_status || shdr_add_status) &&
16610 (shdr_status != STATUS_FCF_IN_USE))
16611 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16612 "2558 ADD_FCF_RECORD mailbox failed with "
16613 "status x%x add_status x%x\n",
16614 shdr_status, shdr_add_status);
16615
16616 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16617}
16618
16619/**
16620 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
16621 * @phba: pointer to lpfc hba data structure.
16622 * @fcf_record: pointer to the initialized fcf record to add.
16623 *
16624 * This routine is invoked to manually add a single FCF record. The caller
16625 * must pass a completely initialized FCF_Record. This routine takes
16626 * care of the nonembedded mailbox operations.
16627 **/
16628int
16629lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
16630{
16631 int rc = 0;
16632 LPFC_MBOXQ_t *mboxq;
16633 uint8_t *bytep;
16634 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040016635 struct lpfc_mbx_sge sge;
16636 uint32_t alloc_len, req_len;
16637 uint32_t fcfindex;
16638
16639 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16640 if (!mboxq) {
16641 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16642 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
16643 return -ENOMEM;
16644 }
16645
16646 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
16647 sizeof(uint32_t);
16648
16649 /* Allocate DMA memory and set up the non-embedded mailbox command */
16650 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16651 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
16652 req_len, LPFC_SLI4_MBX_NEMBED);
16653 if (alloc_len < req_len) {
16654 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16655 "2523 Allocated DMA memory size (x%x) is "
16656 "less than the requested DMA memory "
16657 "size (x%x)\n", alloc_len, req_len);
16658 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16659 return -ENOMEM;
16660 }
16661
16662 /*
16663 * Get the first SGE entry from the non-embedded DMA memory. This
16664 * routine only uses a single SGE.
16665 */
16666 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040016667 virt_addr = mboxq->sge_array->addr[0];
16668 /*
16669 * Configure the FCF record for FCFI 0. This is the driver's
16670 * hardcoded default and gets used in nonFIP mode.
16671 */
16672 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16673 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16674 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16675
16676 /*
16677 * Copy the fcf_index and the FCF Record Data. The data starts after
16678 * the FCoE header plus word10. The data copy needs to be endian
16679 * correct.
16680 */
16681 bytep += sizeof(uint32_t);
16682 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16683 mboxq->vport = phba->pport;
16684 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16685 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16686 if (rc == MBX_NOT_FINISHED) {
16687 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16688 "2515 ADD_FCF_RECORD mailbox failed with "
16689 "status 0x%x\n", rc);
16690 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16691 rc = -EIO;
16692 } else
16693 rc = 0;
16694
16695 return rc;
16696}
16697
16698/**
16699 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16700 * @phba: pointer to lpfc hba data structure.
16701 * @fcf_record: pointer to the fcf record to write the default data.
16702 * @fcf_index: FCF table entry index.
16703 *
16704 * This routine is invoked to build the driver's default FCF record. The
16705 * values used are hardcoded. This routine handles memory initialization.
16706 *
16707 **/
16708void
16709lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16710 struct fcf_record *fcf_record,
16711 uint16_t fcf_index)
16712{
16713 memset(fcf_record, 0, sizeof(struct fcf_record));
16714 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16715 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16716 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16717 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16718 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16719 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16720 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16721 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16722 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16723 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16724 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16725 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16726 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040016727 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040016728 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16729 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16730 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16731 /* Set the VLAN bit map */
16732 if (phba->valid_vlan) {
16733 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16734 = 1 << (phba->vlan_id % 8);
16735 }
16736}
16737
16738/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016739 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040016740 * @phba: pointer to lpfc hba data structure.
16741 * @fcf_index: FCF table entry offset.
16742 *
James Smart0c9ab6f2010-02-26 14:15:57 -050016743 * This routine is invoked to scan the entire FCF table by reading FCF
16744 * record and processing it one at a time starting from the @fcf_index
16745 * for initial FCF discovery or fast FCF failover rediscovery.
16746 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016747 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016748 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040016749 **/
16750int
James Smart0c9ab6f2010-02-26 14:15:57 -050016751lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040016752{
16753 int rc = 0, error;
16754 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040016755
James Smart32b97932009-07-19 10:01:21 -040016756 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050016757 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040016758 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16759 if (!mboxq) {
16760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16761 "2000 Failed to allocate mbox for "
16762 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040016763 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050016764 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016765 }
James Smartecfd03c2010-02-12 14:41:27 -050016766 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050016767 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050016768 if (rc) {
16769 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050016770 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016771 }
James Smartecfd03c2010-02-12 14:41:27 -050016772 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040016773 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050016774 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040016775
16776 spin_lock_irq(&phba->hbalock);
16777 phba->hba_flag |= FCF_TS_INPROG;
16778 spin_unlock_irq(&phba->hbalock);
16779
James Smart6fb120a2009-05-22 14:52:59 -040016780 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050016781 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040016782 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050016783 else {
James Smart38b92ef2010-08-04 16:11:39 -040016784 /* Reset eligible FCF count for new scan */
16785 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040016786 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016787 error = 0;
James Smart32b97932009-07-19 10:01:21 -040016788 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016789fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040016790 if (error) {
16791 if (mboxq)
16792 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040016793 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040016794 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040016795 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040016796 spin_unlock_irq(&phba->hbalock);
16797 }
James Smart6fb120a2009-05-22 14:52:59 -040016798 return error;
16799}
James Smarta0c87cb2009-07-19 10:01:10 -040016800
16801/**
James Smarta93ff372010-10-22 11:06:08 -040016802 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050016803 * @phba: pointer to lpfc hba data structure.
16804 * @fcf_index: FCF table entry offset.
16805 *
16806 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040016807 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050016808 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016809 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016810 * otherwise.
16811 **/
16812int
16813lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16814{
16815 int rc = 0, error;
16816 LPFC_MBOXQ_t *mboxq;
16817
16818 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16819 if (!mboxq) {
16820 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16821 "2763 Failed to allocate mbox for "
16822 "READ_FCF cmd\n");
16823 error = -ENOMEM;
16824 goto fail_fcf_read;
16825 }
16826 /* Construct the read FCF record mailbox command */
16827 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16828 if (rc) {
16829 error = -EINVAL;
16830 goto fail_fcf_read;
16831 }
16832 /* Issue the mailbox command asynchronously */
16833 mboxq->vport = phba->pport;
16834 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16835 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16836 if (rc == MBX_NOT_FINISHED)
16837 error = -EIO;
16838 else
16839 error = 0;
16840
16841fail_fcf_read:
16842 if (error && mboxq)
16843 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16844 return error;
16845}
16846
16847/**
16848 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16849 * @phba: pointer to lpfc hba data structure.
16850 * @fcf_index: FCF table entry offset.
16851 *
16852 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040016853 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050016854 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016855 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016856 * otherwise.
16857 **/
16858int
16859lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16860{
16861 int rc = 0, error;
16862 LPFC_MBOXQ_t *mboxq;
16863
16864 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16865 if (!mboxq) {
16866 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16867 "2758 Failed to allocate mbox for "
16868 "READ_FCF cmd\n");
16869 error = -ENOMEM;
16870 goto fail_fcf_read;
16871 }
16872 /* Construct the read FCF record mailbox command */
16873 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16874 if (rc) {
16875 error = -EINVAL;
16876 goto fail_fcf_read;
16877 }
16878 /* Issue the mailbox command asynchronously */
16879 mboxq->vport = phba->pport;
16880 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16881 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16882 if (rc == MBX_NOT_FINISHED)
16883 error = -EIO;
16884 else
16885 error = 0;
16886
16887fail_fcf_read:
16888 if (error && mboxq)
16889 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16890 return error;
16891}
16892
16893/**
James Smartf5cb5302015-12-16 18:11:52 -050016894 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040016895 * phba pointer to the lpfc_hba struct for this port.
16896 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16897 * routine when the rr_bmask is empty. The FCF indecies are put into the
16898 * rr_bmask based on their priority level. Starting from the highest priority
16899 * to the lowest. The most likely FCF candidate will be in the highest
16900 * priority group. When this routine is called it searches the fcf_pri list for
16901 * next lowest priority group and repopulates the rr_bmask with only those
16902 * fcf_indexes.
16903 * returns:
16904 * 1=success 0=failure
16905 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016906static int
James Smart7d791df2011-07-22 18:37:52 -040016907lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16908{
16909 uint16_t next_fcf_pri;
16910 uint16_t last_index;
16911 struct lpfc_fcf_pri *fcf_pri;
16912 int rc;
16913 int ret = 0;
16914
16915 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16916 LPFC_SLI4_FCF_TBL_INDX_MAX);
16917 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16918 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050016919
16920 /* Verify the priority list has 2 or more entries */
16921 spin_lock_irq(&phba->hbalock);
16922 if (list_empty(&phba->fcf.fcf_pri_list) ||
16923 list_is_singular(&phba->fcf.fcf_pri_list)) {
16924 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040016925 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16926 "3061 Last IDX %d\n", last_index);
16927 return 0; /* Empty rr list */
16928 }
James Smart25626692013-03-01 16:36:54 -050016929 spin_unlock_irq(&phba->hbalock);
16930
James Smart7d791df2011-07-22 18:37:52 -040016931 next_fcf_pri = 0;
16932 /*
16933 * Clear the rr_bmask and set all of the bits that are at this
16934 * priority.
16935 */
16936 memset(phba->fcf.fcf_rr_bmask, 0,
16937 sizeof(*phba->fcf.fcf_rr_bmask));
16938 spin_lock_irq(&phba->hbalock);
16939 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16940 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16941 continue;
16942 /*
16943 * the 1st priority that has not FLOGI failed
16944 * will be the highest.
16945 */
16946 if (!next_fcf_pri)
16947 next_fcf_pri = fcf_pri->fcf_rec.priority;
16948 spin_unlock_irq(&phba->hbalock);
16949 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16950 rc = lpfc_sli4_fcf_rr_index_set(phba,
16951 fcf_pri->fcf_rec.fcf_index);
16952 if (rc)
16953 return 0;
16954 }
16955 spin_lock_irq(&phba->hbalock);
16956 }
16957 /*
16958 * if next_fcf_pri was not set above and the list is not empty then
16959 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070016960 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040016961 */
16962 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16963 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16964 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16965 /*
16966 * the 1st priority that has not FLOGI failed
16967 * will be the highest.
16968 */
16969 if (!next_fcf_pri)
16970 next_fcf_pri = fcf_pri->fcf_rec.priority;
16971 spin_unlock_irq(&phba->hbalock);
16972 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16973 rc = lpfc_sli4_fcf_rr_index_set(phba,
16974 fcf_pri->fcf_rec.fcf_index);
16975 if (rc)
16976 return 0;
16977 }
16978 spin_lock_irq(&phba->hbalock);
16979 }
16980 } else
16981 ret = 1;
16982 spin_unlock_irq(&phba->hbalock);
16983
16984 return ret;
16985}
16986/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016987 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16988 * @phba: pointer to lpfc hba data structure.
16989 *
16990 * This routine is to get the next eligible FCF record index in a round
16991 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040016992 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050016993 * shall be returned, otherwise, the next eligible FCF record's index
16994 * shall be returned.
16995 **/
16996uint16_t
16997lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16998{
16999 uint16_t next_fcf_index;
17000
James Smart421c6622013-01-03 15:44:16 -050017001initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040017002 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050017003 next_fcf_index = phba->fcf.current_rec.fcf_indx;
17004
James Smart7d791df2011-07-22 18:37:52 -040017005next_priority:
James Smart421c6622013-01-03 15:44:16 -050017006 /* Determine the next fcf index to check */
17007 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050017008 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17009 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040017010 next_fcf_index);
17011
James Smart0c9ab6f2010-02-26 14:15:57 -050017012 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040017013 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17014 /*
17015 * If we have wrapped then we need to clear the bits that
17016 * have been tested so that we can detect when we should
17017 * change the priority level.
17018 */
James Smart0c9ab6f2010-02-26 14:15:57 -050017019 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17020 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040017021 }
17022
James Smart0c9ab6f2010-02-26 14:15:57 -050017023
James Smart3804dc82010-07-14 15:31:37 -040017024 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040017025 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
17026 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
17027 /*
17028 * If next fcf index is not found check if there are lower
17029 * Priority level fcf's in the fcf_priority list.
17030 * Set up the rr_bmask with all of the avaiable fcf bits
17031 * at that level and continue the selection process.
17032 */
17033 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050017034 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040017035 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17036 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040017037 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
17038 return LPFC_FCOE_FCF_NEXT_NONE;
17039 else {
17040 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17041 "3063 Only FCF available idx %d, flag %x\n",
17042 next_fcf_index,
17043 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
17044 return next_fcf_index;
17045 }
James Smart3804dc82010-07-14 15:31:37 -040017046 }
17047
James Smart7d791df2011-07-22 18:37:52 -040017048 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
17049 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050017050 LPFC_FCF_FLOGI_FAILED) {
17051 if (list_is_singular(&phba->fcf.fcf_pri_list))
17052 return LPFC_FCOE_FCF_NEXT_NONE;
17053
James Smart7d791df2011-07-22 18:37:52 -040017054 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050017055 }
James Smart7d791df2011-07-22 18:37:52 -040017056
James Smart3804dc82010-07-14 15:31:37 -040017057 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017058 "2845 Get next roundrobin failover FCF (x%x)\n",
17059 next_fcf_index);
17060
James Smart0c9ab6f2010-02-26 14:15:57 -050017061 return next_fcf_index;
17062}
17063
17064/**
17065 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
17066 * @phba: pointer to lpfc hba data structure.
17067 *
17068 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040017069 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050017070 * does not go beyond the range of the driver allocated bmask dimension
17071 * before setting the bit.
17072 *
17073 * Returns 0 if the index bit successfully set, otherwise, it returns
17074 * -EINVAL.
17075 **/
17076int
17077lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
17078{
17079 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17080 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017081 "2610 FCF (x%x) reached driver's book "
17082 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050017083 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17084 return -EINVAL;
17085 }
17086 /* Set the eligible FCF record index bmask */
17087 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
17088
James Smart3804dc82010-07-14 15:31:37 -040017089 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017090 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040017091 "bmask\n", fcf_index);
17092
James Smart0c9ab6f2010-02-26 14:15:57 -050017093 return 0;
17094}
17095
17096/**
James Smart3804dc82010-07-14 15:31:37 -040017097 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050017098 * @phba: pointer to lpfc hba data structure.
17099 *
17100 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040017101 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050017102 * does not go beyond the range of the driver allocated bmask dimension
17103 * before clearing the bit.
17104 **/
17105void
17106lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
17107{
James Smart9a803a72013-09-06 12:17:56 -040017108 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050017109 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17110 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017111 "2762 FCF (x%x) reached driver's book "
17112 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050017113 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17114 return;
17115 }
17116 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040017117 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040017118 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
17119 list) {
James Smart7d791df2011-07-22 18:37:52 -040017120 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
17121 list_del_init(&fcf_pri->list);
17122 break;
17123 }
17124 }
17125 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050017126 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040017127
17128 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017129 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040017130 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050017131}
17132
17133/**
James Smartecfd03c2010-02-12 14:41:27 -050017134 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
17135 * @phba: pointer to lpfc hba data structure.
17136 *
17137 * This routine is the completion routine for the rediscover FCF table mailbox
17138 * command. If the mailbox command returned failure, it will try to stop the
17139 * FCF rediscover wait timer.
17140 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017141static void
James Smartecfd03c2010-02-12 14:41:27 -050017142lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
17143{
17144 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17145 uint32_t shdr_status, shdr_add_status;
17146
17147 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17148
17149 shdr_status = bf_get(lpfc_mbox_hdr_status,
17150 &redisc_fcf->header.cfg_shdr.response);
17151 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
17152 &redisc_fcf->header.cfg_shdr.response);
17153 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050017154 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050017155 "2746 Requesting for FCF rediscovery failed "
17156 "status x%x add_status x%x\n",
17157 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050017158 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050017159 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050017160 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050017161 spin_unlock_irq(&phba->hbalock);
17162 /*
17163 * CVL event triggered FCF rediscover request failed,
17164 * last resort to re-try current registered FCF entry.
17165 */
17166 lpfc_retry_pport_discovery(phba);
17167 } else {
17168 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050017169 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050017170 spin_unlock_irq(&phba->hbalock);
17171 /*
17172 * DEAD FCF event triggered FCF rediscover request
17173 * failed, last resort to fail over as a link down
17174 * to FCF registration.
17175 */
17176 lpfc_sli4_fcf_dead_failthrough(phba);
17177 }
James Smart0c9ab6f2010-02-26 14:15:57 -050017178 } else {
17179 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017180 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050017181 /*
17182 * Start FCF rediscovery wait timer for pending FCF
17183 * before rescan FCF record table.
17184 */
17185 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050017186 }
James Smartecfd03c2010-02-12 14:41:27 -050017187
17188 mempool_free(mbox, phba->mbox_mem_pool);
17189}
17190
17191/**
James Smart3804dc82010-07-14 15:31:37 -040017192 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050017193 * @phba: pointer to lpfc hba data structure.
17194 *
17195 * This routine is invoked to request for rediscovery of the entire FCF table
17196 * by the port.
17197 **/
17198int
17199lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
17200{
17201 LPFC_MBOXQ_t *mbox;
17202 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17203 int rc, length;
17204
James Smart0c9ab6f2010-02-26 14:15:57 -050017205 /* Cancel retry delay timers to all vports before FCF rediscover */
17206 lpfc_cancel_all_vport_retry_delay_timer(phba);
17207
James Smartecfd03c2010-02-12 14:41:27 -050017208 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17209 if (!mbox) {
17210 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17211 "2745 Failed to allocate mbox for "
17212 "requesting FCF rediscover.\n");
17213 return -ENOMEM;
17214 }
17215
17216 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
17217 sizeof(struct lpfc_sli4_cfg_mhdr));
17218 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17219 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
17220 length, LPFC_SLI4_MBX_EMBED);
17221
17222 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17223 /* Set count to 0 for invalidating the entire FCF database */
17224 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
17225
17226 /* Issue the mailbox command asynchronously */
17227 mbox->vport = phba->pport;
17228 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
17229 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
17230
17231 if (rc == MBX_NOT_FINISHED) {
17232 mempool_free(mbox, phba->mbox_mem_pool);
17233 return -EIO;
17234 }
17235 return 0;
17236}
17237
17238/**
James Smartfc2b9892010-02-26 14:15:29 -050017239 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
17240 * @phba: pointer to lpfc hba data structure.
17241 *
17242 * This function is the failover routine as a last resort to the FCF DEAD
17243 * event when driver failed to perform fast FCF failover.
17244 **/
17245void
17246lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
17247{
17248 uint32_t link_state;
17249
17250 /*
17251 * Last resort as FCF DEAD event failover will treat this as
17252 * a link down, but save the link state because we don't want
17253 * it to be changed to Link Down unless it is already down.
17254 */
17255 link_state = phba->link_state;
17256 lpfc_linkdown(phba);
17257 phba->link_state = link_state;
17258
17259 /* Unregister FCF if no devices connected to it */
17260 lpfc_unregister_unused_fcf(phba);
17261}
17262
17263/**
James Smart026abb82011-12-13 13:20:45 -050017264 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040017265 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050017266 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040017267 *
James Smart026abb82011-12-13 13:20:45 -050017268 * This function gets SLI3 port configure region 23 data through memory dump
17269 * mailbox command. When it successfully retrieves data, the size of the data
17270 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040017271 **/
James Smart026abb82011-12-13 13:20:45 -050017272static uint32_t
17273lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040017274{
17275 LPFC_MBOXQ_t *pmb = NULL;
17276 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050017277 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040017278 int rc;
17279
James Smart026abb82011-12-13 13:20:45 -050017280 if (!rgn23_data)
17281 return 0;
17282
James Smarta0c87cb2009-07-19 10:01:10 -040017283 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17284 if (!pmb) {
17285 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050017286 "2600 failed to allocate mailbox memory\n");
17287 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040017288 }
17289 mb = &pmb->u.mb;
17290
James Smarta0c87cb2009-07-19 10:01:10 -040017291 do {
17292 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
17293 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
17294
17295 if (rc != MBX_SUCCESS) {
17296 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050017297 "2601 failed to read config "
17298 "region 23, rc 0x%x Status 0x%x\n",
17299 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040017300 mb->un.varDmp.word_cnt = 0;
17301 }
17302 /*
17303 * dump mem may return a zero when finished or we got a
17304 * mailbox error, either way we are done.
17305 */
17306 if (mb->un.varDmp.word_cnt == 0)
17307 break;
17308 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
17309 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
17310
17311 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050017312 rgn23_data + offset,
17313 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040017314 offset += mb->un.varDmp.word_cnt;
17315 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
17316
James Smart026abb82011-12-13 13:20:45 -050017317 mempool_free(pmb, phba->mbox_mem_pool);
17318 return offset;
17319}
17320
17321/**
17322 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
17323 * @phba: pointer to lpfc hba data structure.
17324 * @rgn23_data: pointer to configure region 23 data.
17325 *
17326 * This function gets SLI4 port configure region 23 data through memory dump
17327 * mailbox command. When it successfully retrieves data, the size of the data
17328 * will be returned, otherwise, 0 will be returned.
17329 **/
17330static uint32_t
17331lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
17332{
17333 LPFC_MBOXQ_t *mboxq = NULL;
17334 struct lpfc_dmabuf *mp = NULL;
17335 struct lpfc_mqe *mqe;
17336 uint32_t data_length = 0;
17337 int rc;
17338
17339 if (!rgn23_data)
17340 return 0;
17341
17342 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17343 if (!mboxq) {
17344 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17345 "3105 failed to allocate mailbox memory\n");
17346 return 0;
17347 }
17348
17349 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
17350 goto out;
17351 mqe = &mboxq->u.mqe;
17352 mp = (struct lpfc_dmabuf *) mboxq->context1;
17353 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
17354 if (rc)
17355 goto out;
17356 data_length = mqe->un.mb_words[5];
17357 if (data_length == 0)
17358 goto out;
17359 if (data_length > DMP_RGN23_SIZE) {
17360 data_length = 0;
17361 goto out;
17362 }
17363 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
17364out:
17365 mempool_free(mboxq, phba->mbox_mem_pool);
17366 if (mp) {
17367 lpfc_mbuf_free(phba, mp->virt, mp->phys);
17368 kfree(mp);
17369 }
17370 return data_length;
17371}
17372
17373/**
17374 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
17375 * @phba: pointer to lpfc hba data structure.
17376 *
17377 * This function read region 23 and parse TLV for port status to
17378 * decide if the user disaled the port. If the TLV indicates the
17379 * port is disabled, the hba_flag is set accordingly.
17380 **/
17381void
17382lpfc_sli_read_link_ste(struct lpfc_hba *phba)
17383{
17384 uint8_t *rgn23_data = NULL;
17385 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
17386 uint32_t offset = 0;
17387
17388 /* Get adapter Region 23 data */
17389 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
17390 if (!rgn23_data)
17391 goto out;
17392
17393 if (phba->sli_rev < LPFC_SLI_REV4)
17394 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
17395 else {
17396 if_type = bf_get(lpfc_sli_intf_if_type,
17397 &phba->sli4_hba.sli_intf);
17398 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
17399 goto out;
17400 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
17401 }
James Smarta0c87cb2009-07-19 10:01:10 -040017402
17403 if (!data_size)
17404 goto out;
17405
17406 /* Check the region signature first */
17407 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
17408 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17409 "2619 Config region 23 has bad signature\n");
17410 goto out;
17411 }
17412 offset += 4;
17413
17414 /* Check the data structure version */
17415 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
17416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17417 "2620 Config region 23 has bad version\n");
17418 goto out;
17419 }
17420 offset += 4;
17421
17422 /* Parse TLV entries in the region */
17423 while (offset < data_size) {
17424 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
17425 break;
17426 /*
17427 * If the TLV is not driver specific TLV or driver id is
17428 * not linux driver id, skip the record.
17429 */
17430 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
17431 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
17432 (rgn23_data[offset + 3] != 0)) {
17433 offset += rgn23_data[offset + 1] * 4 + 4;
17434 continue;
17435 }
17436
17437 /* Driver found a driver specific TLV in the config region */
17438 sub_tlv_len = rgn23_data[offset + 1] * 4;
17439 offset += 4;
17440 tlv_offset = 0;
17441
17442 /*
17443 * Search for configured port state sub-TLV.
17444 */
17445 while ((offset < data_size) &&
17446 (tlv_offset < sub_tlv_len)) {
17447 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
17448 offset += 4;
17449 tlv_offset += 4;
17450 break;
17451 }
17452 if (rgn23_data[offset] != PORT_STE_TYPE) {
17453 offset += rgn23_data[offset + 1] * 4 + 4;
17454 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
17455 continue;
17456 }
17457
17458 /* This HBA contains PORT_STE configured */
17459 if (!rgn23_data[offset + 2])
17460 phba->hba_flag |= LINK_DISABLED;
17461
17462 goto out;
17463 }
17464 }
James Smart026abb82011-12-13 13:20:45 -050017465
James Smarta0c87cb2009-07-19 10:01:10 -040017466out:
James Smarta0c87cb2009-07-19 10:01:10 -040017467 kfree(rgn23_data);
17468 return;
17469}
James Smart695a8142010-01-26 23:08:03 -050017470
17471/**
James Smart52d52442011-05-24 11:42:45 -040017472 * lpfc_wr_object - write an object to the firmware
17473 * @phba: HBA structure that indicates port to create a queue on.
17474 * @dmabuf_list: list of dmabufs to write to the port.
17475 * @size: the total byte value of the objects to write to the port.
17476 * @offset: the current offset to be used to start the transfer.
17477 *
17478 * This routine will create a wr_object mailbox command to send to the port.
17479 * the mailbox command will be constructed using the dma buffers described in
17480 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
17481 * BDEs that the imbedded mailbox can support. The @offset variable will be
17482 * used to indicate the starting offset of the transfer and will also return
17483 * the offset after the write object mailbox has completed. @size is used to
17484 * determine the end of the object and whether the eof bit should be set.
17485 *
17486 * Return 0 is successful and offset will contain the the new offset to use
17487 * for the next write.
17488 * Return negative value for error cases.
17489 **/
17490int
17491lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
17492 uint32_t size, uint32_t *offset)
17493{
17494 struct lpfc_mbx_wr_object *wr_object;
17495 LPFC_MBOXQ_t *mbox;
17496 int rc = 0, i = 0;
17497 uint32_t shdr_status, shdr_add_status;
17498 uint32_t mbox_tmo;
17499 union lpfc_sli4_cfg_shdr *shdr;
17500 struct lpfc_dmabuf *dmabuf;
17501 uint32_t written = 0;
17502
17503 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17504 if (!mbox)
17505 return -ENOMEM;
17506
17507 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17508 LPFC_MBOX_OPCODE_WRITE_OBJECT,
17509 sizeof(struct lpfc_mbx_wr_object) -
17510 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17511
17512 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
17513 wr_object->u.request.write_offset = *offset;
17514 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
17515 wr_object->u.request.object_name[0] =
17516 cpu_to_le32(wr_object->u.request.object_name[0]);
17517 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
17518 list_for_each_entry(dmabuf, dmabuf_list, list) {
17519 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
17520 break;
17521 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
17522 wr_object->u.request.bde[i].addrHigh =
17523 putPaddrHigh(dmabuf->phys);
17524 if (written + SLI4_PAGE_SIZE >= size) {
17525 wr_object->u.request.bde[i].tus.f.bdeSize =
17526 (size - written);
17527 written += (size - written);
17528 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
17529 } else {
17530 wr_object->u.request.bde[i].tus.f.bdeSize =
17531 SLI4_PAGE_SIZE;
17532 written += SLI4_PAGE_SIZE;
17533 }
17534 i++;
17535 }
17536 wr_object->u.request.bde_count = i;
17537 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
17538 if (!phba->sli4_hba.intr_enable)
17539 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17540 else {
James Smarta183a152011-10-10 21:32:43 -040017541 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040017542 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17543 }
17544 /* The IOCTL status is embedded in the mailbox subheader. */
17545 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
17546 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17547 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17548 if (rc != MBX_TIMEOUT)
17549 mempool_free(mbox, phba->mbox_mem_pool);
17550 if (shdr_status || shdr_add_status || rc) {
17551 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17552 "3025 Write Object mailbox failed with "
17553 "status x%x add_status x%x, mbx status x%x\n",
17554 shdr_status, shdr_add_status, rc);
17555 rc = -ENXIO;
17556 } else
17557 *offset += wr_object->u.response.actual_write_length;
17558 return rc;
17559}
17560
17561/**
James Smart695a8142010-01-26 23:08:03 -050017562 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
17563 * @vport: pointer to vport data structure.
17564 *
17565 * This function iterate through the mailboxq and clean up all REG_LOGIN
17566 * and REG_VPI mailbox commands associated with the vport. This function
17567 * is called when driver want to restart discovery of the vport due to
17568 * a Clear Virtual Link event.
17569 **/
17570void
17571lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
17572{
17573 struct lpfc_hba *phba = vport->phba;
17574 LPFC_MBOXQ_t *mb, *nextmb;
17575 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040017576 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040017577 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040017578 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040017579 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050017580 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050017581
James Smartd439d282010-09-29 11:18:45 -040017582 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050017583 spin_lock_irq(&phba->hbalock);
17584 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
17585 if (mb->vport != vport)
17586 continue;
17587
17588 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17589 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17590 continue;
17591
James Smartd439d282010-09-29 11:18:45 -040017592 list_del(&mb->list);
17593 list_add_tail(&mb->list, &mbox_cmd_list);
17594 }
17595 /* Clean up active mailbox command with the vport */
17596 mb = phba->sli.mbox_active;
17597 if (mb && (mb->vport == vport)) {
17598 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
17599 (mb->u.mb.mbxCommand == MBX_REG_VPI))
17600 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17601 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17602 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
17603 /* Put reference count for delayed processing */
17604 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
17605 /* Unregister the RPI when mailbox complete */
17606 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17607 }
17608 }
James Smart63e801c2010-11-20 23:14:19 -050017609 /* Cleanup any mailbox completions which are not yet processed */
17610 do {
17611 restart_loop = 0;
17612 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
17613 /*
17614 * If this mailox is already processed or it is
17615 * for another vport ignore it.
17616 */
17617 if ((mb->vport != vport) ||
17618 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
17619 continue;
17620
17621 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17622 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17623 continue;
17624
17625 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17626 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17627 ndlp = (struct lpfc_nodelist *)mb->context2;
17628 /* Unregister the RPI when mailbox complete */
17629 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17630 restart_loop = 1;
17631 spin_unlock_irq(&phba->hbalock);
17632 spin_lock(shost->host_lock);
17633 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17634 spin_unlock(shost->host_lock);
17635 spin_lock_irq(&phba->hbalock);
17636 break;
17637 }
17638 }
17639 } while (restart_loop);
17640
James Smartd439d282010-09-29 11:18:45 -040017641 spin_unlock_irq(&phba->hbalock);
17642
17643 /* Release the cleaned-up mailbox commands */
17644 while (!list_empty(&mbox_cmd_list)) {
17645 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050017646 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17647 mp = (struct lpfc_dmabuf *) (mb->context1);
17648 if (mp) {
17649 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
17650 kfree(mp);
17651 }
James Smart78730cf2010-04-06 15:06:30 -040017652 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040017653 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040017654 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017655 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040017656 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017657 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040017658 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040017659 }
James Smart695a8142010-01-26 23:08:03 -050017660 }
James Smart695a8142010-01-26 23:08:03 -050017661 mempool_free(mb, phba->mbox_mem_pool);
17662 }
James Smartd439d282010-09-29 11:18:45 -040017663
17664 /* Release the ndlp with the cleaned-up active mailbox command */
17665 if (act_mbx_ndlp) {
17666 spin_lock(shost->host_lock);
17667 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17668 spin_unlock(shost->host_lock);
17669 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050017670 }
James Smart695a8142010-01-26 23:08:03 -050017671}
17672
James Smart2a9bf3d2010-06-07 15:24:45 -040017673/**
17674 * lpfc_drain_txq - Drain the txq
17675 * @phba: Pointer to HBA context object.
17676 *
17677 * This function attempt to submit IOCBs on the txq
17678 * to the adapter. For SLI4 adapters, the txq contains
17679 * ELS IOCBs that have been deferred because the there
17680 * are no SGLs. This congestion can occur with large
17681 * vport counts during node discovery.
17682 **/
17683
17684uint32_t
17685lpfc_drain_txq(struct lpfc_hba *phba)
17686{
17687 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080017688 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090017689 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040017690 unsigned long iflags = 0;
17691 char *fail_msg = NULL;
17692 struct lpfc_sglq *sglq;
James Smart2f077842016-12-19 15:07:29 -080017693 union lpfc_wqe128 wqe128;
17694 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
James Smarta2fc4aef2014-09-03 12:57:55 -040017695 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040017696
James Smart895427b2017-02-12 13:52:30 -080017697 pring = lpfc_phba_elsring(phba);
17698
James Smart398d81c2013-05-31 17:04:19 -040017699 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050017700 list_for_each_entry(piocbq, &pring->txq, list) {
17701 txq_cnt++;
17702 }
17703
17704 if (txq_cnt > pring->txq_max)
17705 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017706
James Smart398d81c2013-05-31 17:04:19 -040017707 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017708
James Smart0e9bb8d2013-03-01 16:35:12 -050017709 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040017710 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017711
James Smart19ca7602010-11-20 23:11:55 -050017712 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040017713 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040017714 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040017715 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17716 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050017717 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040017718 break;
17719 }
James Smart895427b2017-02-12 13:52:30 -080017720 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040017721 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050017722 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040017723 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017724 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040017725 }
James Smart0e9bb8d2013-03-01 16:35:12 -050017726 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040017727
17728 /* The xri and iocb resources secured,
17729 * attempt to issue request
17730 */
James Smart6d368e52011-05-24 11:44:12 -040017731 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040017732 piocbq->sli4_xritag = sglq->sli4_xritag;
17733 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17734 fail_msg = "to convert bpl to sgl";
James Smart2f077842016-12-19 15:07:29 -080017735 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040017736 fail_msg = "to convert iocb to wqe";
James Smart2f077842016-12-19 15:07:29 -080017737 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040017738 fail_msg = " - Wq is full";
17739 else
17740 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17741
17742 if (fail_msg) {
17743 /* Failed means we can't issue and need to cancel */
17744 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17745 "2822 IOCB failed %s iotag 0x%x "
17746 "xri 0x%x\n",
17747 fail_msg,
17748 piocbq->iotag, piocbq->sli4_xritag);
17749 list_add_tail(&piocbq->list, &completions);
17750 }
James Smart398d81c2013-05-31 17:04:19 -040017751 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017752 }
17753
James Smart2a9bf3d2010-06-07 15:24:45 -040017754 /* Cancel all the IOCBs that cannot be issued */
17755 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17756 IOERR_SLI_ABORTED);
17757
James Smart0e9bb8d2013-03-01 16:35:12 -050017758 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017759}
James Smart895427b2017-02-12 13:52:30 -080017760
17761/**
17762 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
17763 * @phba: Pointer to HBA context object.
17764 * @pwqe: Pointer to command WQE.
17765 * @sglq: Pointer to the scatter gather queue object.
17766 *
17767 * This routine converts the bpl or bde that is in the WQE
17768 * to a sgl list for the sli4 hardware. The physical address
17769 * of the bpl/bde is converted back to a virtual address.
17770 * If the WQE contains a BPL then the list of BDE's is
17771 * converted to sli4_sge's. If the WQE contains a single
17772 * BDE then it is converted to a single sli_sge.
17773 * The WQE is still in cpu endianness so the contents of
17774 * the bpl can be used without byte swapping.
17775 *
17776 * Returns valid XRI = Success, NO_XRI = Failure.
17777 */
17778static uint16_t
17779lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
17780 struct lpfc_sglq *sglq)
17781{
17782 uint16_t xritag = NO_XRI;
17783 struct ulp_bde64 *bpl = NULL;
17784 struct ulp_bde64 bde;
17785 struct sli4_sge *sgl = NULL;
17786 struct lpfc_dmabuf *dmabuf;
17787 union lpfc_wqe *wqe;
17788 int numBdes = 0;
17789 int i = 0;
17790 uint32_t offset = 0; /* accumulated offset in the sg request list */
17791 int inbound = 0; /* number of sg reply entries inbound from firmware */
17792 uint32_t cmd;
17793
17794 if (!pwqeq || !sglq)
17795 return xritag;
17796
17797 sgl = (struct sli4_sge *)sglq->sgl;
17798 wqe = &pwqeq->wqe;
17799 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
17800
17801 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
17802 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
17803 return sglq->sli4_xritag;
17804 numBdes = pwqeq->rsvd2;
17805 if (numBdes) {
17806 /* The addrHigh and addrLow fields within the WQE
17807 * have not been byteswapped yet so there is no
17808 * need to swap them back.
17809 */
17810 if (pwqeq->context3)
17811 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
17812 else
17813 return xritag;
17814
17815 bpl = (struct ulp_bde64 *)dmabuf->virt;
17816 if (!bpl)
17817 return xritag;
17818
17819 for (i = 0; i < numBdes; i++) {
17820 /* Should already be byte swapped. */
17821 sgl->addr_hi = bpl->addrHigh;
17822 sgl->addr_lo = bpl->addrLow;
17823
17824 sgl->word2 = le32_to_cpu(sgl->word2);
17825 if ((i+1) == numBdes)
17826 bf_set(lpfc_sli4_sge_last, sgl, 1);
17827 else
17828 bf_set(lpfc_sli4_sge_last, sgl, 0);
17829 /* swap the size field back to the cpu so we
17830 * can assign it to the sgl.
17831 */
17832 bde.tus.w = le32_to_cpu(bpl->tus.w);
17833 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
17834 /* The offsets in the sgl need to be accumulated
17835 * separately for the request and reply lists.
17836 * The request is always first, the reply follows.
17837 */
17838 switch (cmd) {
17839 case CMD_GEN_REQUEST64_WQE:
17840 /* add up the reply sg entries */
17841 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
17842 inbound++;
17843 /* first inbound? reset the offset */
17844 if (inbound == 1)
17845 offset = 0;
17846 bf_set(lpfc_sli4_sge_offset, sgl, offset);
17847 bf_set(lpfc_sli4_sge_type, sgl,
17848 LPFC_SGE_TYPE_DATA);
17849 offset += bde.tus.f.bdeSize;
17850 break;
17851 case CMD_FCP_TRSP64_WQE:
17852 bf_set(lpfc_sli4_sge_offset, sgl, 0);
17853 bf_set(lpfc_sli4_sge_type, sgl,
17854 LPFC_SGE_TYPE_DATA);
17855 break;
17856 case CMD_FCP_TSEND64_WQE:
17857 case CMD_FCP_TRECEIVE64_WQE:
17858 bf_set(lpfc_sli4_sge_type, sgl,
17859 bpl->tus.f.bdeFlags);
17860 if (i < 3)
17861 offset = 0;
17862 else
17863 offset += bde.tus.f.bdeSize;
17864 bf_set(lpfc_sli4_sge_offset, sgl, offset);
17865 break;
17866 }
17867 sgl->word2 = cpu_to_le32(sgl->word2);
17868 bpl++;
17869 sgl++;
17870 }
17871 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
17872 /* The addrHigh and addrLow fields of the BDE have not
17873 * been byteswapped yet so they need to be swapped
17874 * before putting them in the sgl.
17875 */
17876 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
17877 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
17878 sgl->word2 = le32_to_cpu(sgl->word2);
17879 bf_set(lpfc_sli4_sge_last, sgl, 1);
17880 sgl->word2 = cpu_to_le32(sgl->word2);
17881 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
17882 }
17883 return sglq->sli4_xritag;
17884}
17885
17886/**
17887 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
17888 * @phba: Pointer to HBA context object.
17889 * @ring_number: Base sli ring number
17890 * @pwqe: Pointer to command WQE.
17891 **/
17892int
17893lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
17894 struct lpfc_iocbq *pwqe)
17895{
17896 union lpfc_wqe *wqe = &pwqe->wqe;
17897 struct lpfc_queue *wq;
17898 struct lpfc_sglq *sglq;
17899 struct lpfc_sli_ring *pring;
17900 unsigned long iflags;
17901
17902 /* NVME_LS and NVME_LS ABTS requests. */
17903 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
17904 pring = phba->sli4_hba.nvmels_wq->pring;
17905 spin_lock_irqsave(&pring->ring_lock, iflags);
17906 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
17907 if (!sglq) {
17908 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17909 return WQE_BUSY;
17910 }
17911 pwqe->sli4_lxritag = sglq->sli4_lxritag;
17912 pwqe->sli4_xritag = sglq->sli4_xritag;
17913 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
17914 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17915 return WQE_ERROR;
17916 }
17917 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
17918 pwqe->sli4_xritag);
17919 if (lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe)) {
17920 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17921 return WQE_ERROR;
17922 }
17923 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
17924 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17925 return 0;
17926 }
17927
17928 /* NVME_FCREQ and NVME_ABTS requests */
17929 if (pwqe->iocb_flag & LPFC_IO_NVME) {
17930 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
17931 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
17932
17933 spin_lock_irqsave(&pring->ring_lock, iflags);
17934 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
17935 bf_set(wqe_cqid, &wqe->generic.wqe_com,
17936 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
17937 if (lpfc_sli4_wq_put(wq, wqe)) {
17938 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17939 return WQE_ERROR;
17940 }
17941 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
17942 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17943 return 0;
17944 }
17945
17946 return WQE_ERROR;
17947}