blob: 5f6ad67d5a73a19cab578c2eb34a7eecc861508a [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
James Smart01649562017-02-12 13:52:32 -08006798 if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
6799 (phba->nvmet_support == 0)) {
6800
6801 /* register the allocated nvme sgl pool to the port */
6802 rc = lpfc_repost_nvme_sgl_list(phba);
6803 if (unlikely(rc)) {
6804 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6805 "6116 Error %d during nvme sgl post "
6806 "operation\n", rc);
6807 /* Some NVME buffers were moved to abort nvme list */
6808 /* A pci function reset will repost them */
6809 rc = -ENODEV;
6810 goto out_destroy_queue;
6811 }
6812 }
6813
James Smartda0436e2009-05-22 14:51:39 -04006814 /* Post the rpi header region to the device. */
6815 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6816 if (unlikely(rc)) {
6817 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6818 "0393 Error %d during rpi post operation\n",
6819 rc);
6820 rc = -ENODEV;
James Smart895427b2017-02-12 13:52:30 -08006821 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006822 }
James Smart97f2ecf2012-03-01 22:35:23 -05006823 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006824
James Smart895427b2017-02-12 13:52:30 -08006825 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6826 if (phba->nvmet_support == 0) {
6827 /*
6828 * The FC Port needs to register FCFI (index 0)
6829 */
6830 lpfc_reg_fcfi(phba, mboxq);
6831 mboxq->vport = phba->pport;
6832 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6833 if (rc != MBX_SUCCESS)
6834 goto out_unset_queue;
6835 rc = 0;
6836 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6837 &mboxq->u.mqe.un.reg_fcfi);
6838 }
6839 /* Check if the port is configured to be disabled */
6840 lpfc_sli_read_link_ste(phba);
James Smartda0436e2009-05-22 14:51:39 -04006841 }
6842
6843 /* Arm the CQs and then EQs on device */
6844 lpfc_sli4_arm_cqeq_intr(phba);
6845
6846 /* Indicate device interrupt mode */
6847 phba->sli4_hba.intr_enable = 1;
6848
6849 /* Allow asynchronous mailbox command to go through */
6850 spin_lock_irq(&phba->hbalock);
6851 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6852 spin_unlock_irq(&phba->hbalock);
6853
6854 /* Post receive buffers to the device */
6855 lpfc_sli4_rb_setup(phba);
6856
James Smartfc2b9892010-02-26 14:15:29 -05006857 /* Reset HBA FCF states after HBA reset */
6858 phba->fcf.fcf_flag = 0;
6859 phba->fcf.current_rec.flag = 0;
6860
James Smartda0436e2009-05-22 14:51:39 -04006861 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006862 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006863 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04006864
6865 /* Start heart beat timer */
6866 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006867 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006868 phba->hb_outstanding = 0;
6869 phba->last_completion_time = jiffies;
6870
6871 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04006872 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07006873 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04006874
James Smart75baf692010-06-08 18:31:21 -04006875 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6876 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6877 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6878 if (!rc) {
6879 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6880 "2829 This device supports "
6881 "Advanced Error Reporting (AER)\n");
6882 spin_lock_irq(&phba->hbalock);
6883 phba->hba_flag |= HBA_AER_ENABLED;
6884 spin_unlock_irq(&phba->hbalock);
6885 } else {
6886 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6887 "2830 This device does not support "
6888 "Advanced Error Reporting (AER)\n");
6889 phba->cfg_aer_support = 0;
6890 }
James Smart0a96e972011-07-22 18:37:28 -04006891 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006892 }
6893
James Smartda0436e2009-05-22 14:51:39 -04006894 /*
6895 * The port is ready, set the host's link state to LINK_DOWN
6896 * in preparation for link interrupts.
6897 */
James Smartda0436e2009-05-22 14:51:39 -04006898 spin_lock_irq(&phba->hbalock);
6899 phba->link_state = LPFC_LINK_DOWN;
6900 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006901 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6902 (phba->hba_flag & LINK_DISABLED)) {
6903 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6904 "3103 Adapter Link is disabled.\n");
6905 lpfc_down_link(phba, mboxq);
6906 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6907 if (rc != MBX_SUCCESS) {
6908 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6909 "3104 Adapter failed to issue "
6910 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6911 goto out_unset_queue;
6912 }
6913 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006914 /* don't perform init_link on SLI4 FC port loopback test */
6915 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6916 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6917 if (rc)
6918 goto out_unset_queue;
6919 }
James Smart5350d872011-10-10 21:33:49 -04006920 }
6921 mempool_free(mboxq, phba->mbox_mem_pool);
6922 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006923out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006924 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006925 lpfc_sli4_queue_unset(phba);
6926out_destroy_queue:
6927 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006928out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006929 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006930out_free_mbox:
6931 mempool_free(mboxq, phba->mbox_mem_pool);
6932 return rc;
6933}
James Smarte59058c2008-08-24 21:49:00 -04006934
6935/**
James Smart3621a712009-04-06 18:47:14 -04006936 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006937 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006938 *
James Smarte59058c2008-08-24 21:49:00 -04006939 * This is the callback function for mailbox timer. The mailbox
6940 * timer is armed when a new mailbox command is issued and the timer
6941 * is deleted when the mailbox complete. The function is called by
6942 * the kernel timer code when a mailbox does not complete within
6943 * expected time. This function wakes up the worker thread to
6944 * process the mailbox timeout and returns. All the processing is
6945 * done by the worker thread function lpfc_mbox_timeout_handler.
6946 **/
dea31012005-04-17 16:05:31 -05006947void
6948lpfc_mbox_timeout(unsigned long ptr)
6949{
James Smart92d7f7b2007-06-17 19:56:38 -05006950 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006951 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006952 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006953
James Smart2e0fef82007-06-17 19:56:36 -05006954 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006955 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006956 if (!tmo_posted)
6957 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6958 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6959
James Smart5e9d9b82008-06-14 22:52:53 -04006960 if (!tmo_posted)
6961 lpfc_worker_wake_up(phba);
6962 return;
dea31012005-04-17 16:05:31 -05006963}
6964
James Smarte8d3c3b2013-10-10 12:21:30 -04006965/**
6966 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6967 * are pending
6968 * @phba: Pointer to HBA context object.
6969 *
6970 * This function checks if any mailbox completions are present on the mailbox
6971 * completion queue.
6972 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04006973static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04006974lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6975{
6976
6977 uint32_t idx;
6978 struct lpfc_queue *mcq;
6979 struct lpfc_mcqe *mcqe;
6980 bool pending_completions = false;
6981
6982 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6983 return false;
6984
6985 /* Check for completions on mailbox completion queue */
6986
6987 mcq = phba->sli4_hba.mbx_cq;
6988 idx = mcq->hba_index;
6989 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6990 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6991 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6992 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6993 pending_completions = true;
6994 break;
6995 }
6996 idx = (idx + 1) % mcq->entry_count;
6997 if (mcq->hba_index == idx)
6998 break;
6999 }
7000 return pending_completions;
7001
7002}
7003
7004/**
7005 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
7006 * that were missed.
7007 * @phba: Pointer to HBA context object.
7008 *
7009 * For sli4, it is possible to miss an interrupt. As such mbox completions
7010 * maybe missed causing erroneous mailbox timeouts to occur. This function
7011 * checks to see if mbox completions are on the mailbox completion queue
7012 * and will process all the completions associated with the eq for the
7013 * mailbox completion queue.
7014 **/
7015bool
7016lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
7017{
7018
7019 uint32_t eqidx;
7020 struct lpfc_queue *fpeq = NULL;
7021 struct lpfc_eqe *eqe;
7022 bool mbox_pending;
7023
7024 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
7025 return false;
7026
7027 /* Find the eq associated with the mcq */
7028
7029 if (phba->sli4_hba.hba_eq)
James Smart895427b2017-02-12 13:52:30 -08007030 for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
James Smarte8d3c3b2013-10-10 12:21:30 -04007031 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
7032 phba->sli4_hba.mbx_cq->assoc_qid) {
7033 fpeq = phba->sli4_hba.hba_eq[eqidx];
7034 break;
7035 }
7036 if (!fpeq)
7037 return false;
7038
7039 /* Turn off interrupts from this EQ */
7040
7041 lpfc_sli4_eq_clr_intr(fpeq);
7042
7043 /* Check to see if a mbox completion is pending */
7044
7045 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
7046
7047 /*
7048 * If a mbox completion is pending, process all the events on EQ
7049 * associated with the mbox completion queue (this could include
7050 * mailbox commands, async events, els commands, receive queue data
7051 * and fcp commands)
7052 */
7053
7054 if (mbox_pending)
7055 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
7056 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
7057 fpeq->EQ_processed++;
7058 }
7059
7060 /* Always clear and re-arm the EQ */
7061
7062 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
7063
7064 return mbox_pending;
7065
7066}
James Smarte59058c2008-08-24 21:49:00 -04007067
7068/**
James Smart3621a712009-04-06 18:47:14 -04007069 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04007070 * @phba: Pointer to HBA context object.
7071 *
7072 * This function is called from worker thread when a mailbox command times out.
7073 * The caller is not required to hold any locks. This function will reset the
7074 * HBA and recover all the pending commands.
7075 **/
dea31012005-04-17 16:05:31 -05007076void
7077lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
7078{
James Smart2e0fef82007-06-17 19:56:36 -05007079 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04007080 MAILBOX_t *mb = NULL;
7081
James Smart1dcb58e2007-04-25 09:51:30 -04007082 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007083
James Smarte8d3c3b2013-10-10 12:21:30 -04007084 /* If the mailbox completed, process the completion and return */
7085 if (lpfc_sli4_process_missed_mbox_completions(phba))
7086 return;
7087
James Smarteb016562014-09-03 12:58:06 -04007088 if (pmbox != NULL)
7089 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04007090 /* Check the pmbox pointer first. There is a race condition
7091 * between the mbox timeout handler getting executed in the
7092 * worklist and the mailbox actually completing. When this
7093 * race condition occurs, the mbox_active will be NULL.
7094 */
7095 spin_lock_irq(&phba->hbalock);
7096 if (pmbox == NULL) {
7097 lpfc_printf_log(phba, KERN_WARNING,
7098 LOG_MBOX | LOG_SLI,
7099 "0353 Active Mailbox cleared - mailbox timeout "
7100 "exiting\n");
7101 spin_unlock_irq(&phba->hbalock);
7102 return;
7103 }
7104
dea31012005-04-17 16:05:31 -05007105 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05007106 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007107 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007108 mb->mbxCommand,
7109 phba->pport->port_state,
7110 phba->sli.sli_flag,
7111 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04007112 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007113
James Smart1dcb58e2007-04-25 09:51:30 -04007114 /* Setting state unknown so lpfc_sli_abort_iocb_ring
7115 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007116 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04007117 */
James Smart2e0fef82007-06-17 19:56:36 -05007118 spin_lock_irq(&phba->pport->work_port_lock);
7119 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
7120 spin_unlock_irq(&phba->pport->work_port_lock);
7121 spin_lock_irq(&phba->hbalock);
7122 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04007123 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007124 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04007125
James Smartdb55fba2014-04-04 13:52:02 -04007126 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04007127
7128 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04007129 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04007130
7131 /* Reset the HBA device */
7132 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05007133}
7134
James Smarte59058c2008-08-24 21:49:00 -04007135/**
James Smart3772a992009-05-22 14:50:54 -04007136 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04007137 * @phba: Pointer to HBA context object.
7138 * @pmbox: Pointer to mailbox object.
7139 * @flag: Flag indicating how the mailbox need to be processed.
7140 *
7141 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04007142 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7143 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007144 * The mailbox command can be submitted in polling mode, in which case
7145 * this function will wait in a polling loop for the completion of the
7146 * mailbox.
7147 * If the mailbox is submitted in no_wait mode (not polling) the
7148 * function will submit the command and returns immediately without waiting
7149 * for the mailbox completion. The no_wait is supported only when HBA
7150 * is in SLI2/SLI3 mode - interrupts are enabled.
7151 * The SLI interface allows only one mailbox pending at a time. If the
7152 * mailbox is issued in polling mode and there is already a mailbox
7153 * pending, then the function will return an error. If the mailbox is issued
7154 * in NO_WAIT mode and there is a mailbox pending already, the function
7155 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7156 * The sli layer owns the mailbox object until the completion of mailbox
7157 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7158 * return codes the caller owns the mailbox command after the return of
7159 * the function.
7160 **/
James Smart3772a992009-05-22 14:50:54 -04007161static int
7162lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7163 uint32_t flag)
dea31012005-04-17 16:05:31 -05007164{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007165 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007166 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007167 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007168 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007169 int i;
James Smart09372822008-01-11 01:52:54 -05007170 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007171 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007172 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007173 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007174 int processing_queue = 0;
7175
7176 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7177 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007178 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007179 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007180 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7181 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7182 return MBX_SUCCESS;
7183 }
James Smart58da1ff2008-04-07 10:15:56 -04007184 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007185 pmbox = lpfc_mbox_get(phba);
7186 if (!pmbox) {
7187 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7188 return MBX_SUCCESS;
7189 }
7190 }
dea31012005-04-17 16:05:31 -05007191
James Smarted957682007-06-17 19:56:37 -05007192 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007193 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007194 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007195 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007196 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007197 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007198 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007199 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007200 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007201 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007202 }
7203 }
7204
Linas Vepstas8d63f372007-02-14 14:28:36 -06007205 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007206 if (unlikely(pci_channel_offline(phba->pcidev))) {
7207 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7208 goto out_not_finished;
7209 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007210
James Smarta257bf92009-04-06 18:48:10 -04007211 /* If HBA has a deferred error attention, fail the iocb. */
7212 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7213 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7214 goto out_not_finished;
7215 }
7216
dea31012005-04-17 16:05:31 -05007217 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007218
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007219 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007220 status = MBX_SUCCESS;
7221
James Smart2e0fef82007-06-17 19:56:36 -05007222 if (phba->link_state == LPFC_HBA_ERROR) {
7223 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007224
7225 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007226 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7227 "(%d):0311 Mailbox command x%x cannot "
7228 "issue Data: x%x x%x\n",
7229 pmbox->vport ? pmbox->vport->vpi : 0,
7230 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007231 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007232 }
7233
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007234 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007235 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7236 !(hc_copy & HC_MBINT_ENA)) {
7237 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7238 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007239 "(%d):2528 Mailbox command x%x cannot "
7240 "issue Data: x%x x%x\n",
7241 pmbox->vport ? pmbox->vport->vpi : 0,
7242 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007243 goto out_not_finished;
7244 }
James Smart92908312006-03-07 15:04:13 -05007245 }
7246
dea31012005-04-17 16:05:31 -05007247 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7248 /* Polling for a mbox command when another one is already active
7249 * is not allowed in SLI. Also, the driver must have established
7250 * SLI2 mode to queue and process multiple mbox commands.
7251 */
7252
7253 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007254 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007255
7256 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007257 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7258 "(%d):2529 Mailbox command x%x "
7259 "cannot issue Data: x%x x%x\n",
7260 pmbox->vport ? pmbox->vport->vpi : 0,
7261 pmbox->u.mb.mbxCommand,
7262 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007263 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007264 }
7265
James Smart3772a992009-05-22 14:50:54 -04007266 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007267 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007268 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007269 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7270 "(%d):2530 Mailbox command x%x "
7271 "cannot issue Data: x%x x%x\n",
7272 pmbox->vport ? pmbox->vport->vpi : 0,
7273 pmbox->u.mb.mbxCommand,
7274 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007275 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007276 }
7277
dea31012005-04-17 16:05:31 -05007278 /* Another mailbox command is still being processed, queue this
7279 * command to be processed later.
7280 */
7281 lpfc_mbox_put(phba, pmbox);
7282
7283 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007284 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007285 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007286 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007287 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007288 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007289 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007290
7291 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007292 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007293
James Smart858c9f62007-06-17 19:56:39 -05007294 if (pmbox->vport) {
7295 lpfc_debugfs_disc_trc(pmbox->vport,
7296 LPFC_DISC_TRC_MBOX_VPORT,
7297 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007298 (uint32_t)mbx->mbxCommand,
7299 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007300 }
7301 else {
7302 lpfc_debugfs_disc_trc(phba->pport,
7303 LPFC_DISC_TRC_MBOX,
7304 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007305 (uint32_t)mbx->mbxCommand,
7306 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007307 }
7308
James Smart2e0fef82007-06-17 19:56:36 -05007309 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007310 }
7311
dea31012005-04-17 16:05:31 -05007312 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7313
7314 /* If we are not polling, we MUST be in SLI2 mode */
7315 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007316 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007317 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007318 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007319 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007320 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007321 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7322 "(%d):2531 Mailbox command x%x "
7323 "cannot issue Data: x%x x%x\n",
7324 pmbox->vport ? pmbox->vport->vpi : 0,
7325 pmbox->u.mb.mbxCommand,
7326 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007327 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007328 }
7329 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007330 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7331 1000);
7332 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007333 }
7334
7335 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007336 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007337 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007338 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007339 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007340 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007341 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007342
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007343 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007344 if (pmbox->vport) {
7345 lpfc_debugfs_disc_trc(pmbox->vport,
7346 LPFC_DISC_TRC_MBOX_VPORT,
7347 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007348 (uint32_t)mbx->mbxCommand,
7349 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007350 }
7351 else {
7352 lpfc_debugfs_disc_trc(phba->pport,
7353 LPFC_DISC_TRC_MBOX,
7354 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007355 (uint32_t)mbx->mbxCommand,
7356 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007357 }
7358 }
7359
dea31012005-04-17 16:05:31 -05007360 psli->slistat.mbox_cmd++;
7361 evtctr = psli->slistat.mbox_event;
7362
7363 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007364 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007365
James Smart3772a992009-05-22 14:50:54 -04007366 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
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 = (uint8_t *)phba->mbox_ext
7371 - (uint8_t *)phba->mbox;
7372 }
7373
7374 /* Copy the mailbox extension data */
7375 if (pmbox->in_ext_byte_len && pmbox->context2) {
7376 lpfc_sli_pcimem_bcopy(pmbox->context2,
7377 (uint8_t *)phba->mbox_ext,
7378 pmbox->in_ext_byte_len);
7379 }
7380 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007381 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007382 } else {
James Smart7a470272010-03-15 11:25:20 -04007383 /* Populate mbox extension offset word. */
7384 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007385 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007386 = MAILBOX_HBA_EXT_OFFSET;
7387
7388 /* Copy the mailbox extension data */
James Smart895427b2017-02-12 13:52:30 -08007389 if (pmbox->in_ext_byte_len && pmbox->context2)
James Smart7a470272010-03-15 11:25:20 -04007390 lpfc_memcpy_to_slim(phba->MBslimaddr +
7391 MAILBOX_HBA_EXT_OFFSET,
7392 pmbox->context2, pmbox->in_ext_byte_len);
7393
James Smart895427b2017-02-12 13:52:30 -08007394 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007395 /* copy command data into host mbox for cmpl */
James Smart895427b2017-02-12 13:52:30 -08007396 lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
7397 MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007398
7399 /* First copy mbox command data to HBA SLIM, skip past first
7400 word */
7401 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007402 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007403 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7404
7405 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007406 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007407 to_slim = phba->MBslimaddr;
7408 writel(ldata, to_slim);
7409 readl(to_slim); /* flush */
7410
James Smart895427b2017-02-12 13:52:30 -08007411 if (mbx->mbxCommand == MBX_CONFIG_PORT)
dea31012005-04-17 16:05:31 -05007412 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007413 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007414 }
7415
7416 wmb();
dea31012005-04-17 16:05:31 -05007417
7418 switch (flag) {
7419 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007420 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007421 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007422 /* Interrupt board to do it */
7423 writel(CA_MBATT, phba->CAregaddr);
7424 readl(phba->CAregaddr); /* flush */
7425 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007426 break;
7427
7428 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007429 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007430 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007431 /* Interrupt board to do it */
7432 writel(CA_MBATT, phba->CAregaddr);
7433 readl(phba->CAregaddr); /* flush */
7434
James Smart3772a992009-05-22 14:50:54 -04007435 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007436 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007437 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007438 word0 = le32_to_cpu(word0);
7439 } else {
7440 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007441 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7442 spin_unlock_irqrestore(&phba->hbalock,
7443 drvr_flag);
7444 goto out_not_finished;
7445 }
dea31012005-04-17 16:05:31 -05007446 }
7447
7448 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007449 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7450 spin_unlock_irqrestore(&phba->hbalock,
7451 drvr_flag);
7452 goto out_not_finished;
7453 }
James Smarta183a152011-10-10 21:32:43 -04007454 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7455 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007456 i = 0;
dea31012005-04-17 16:05:31 -05007457 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007458 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7459 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007460 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007461 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007462 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007463 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007464 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007465 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007466 }
7467
7468 /* Check if we took a mbox interrupt while we were
7469 polling */
7470 if (((word0 & OWN_CHIP) != OWN_CHIP)
7471 && (evtctr != psli->slistat.mbox_event))
7472 break;
7473
James Smart09372822008-01-11 01:52:54 -05007474 if (i++ > 10) {
7475 spin_unlock_irqrestore(&phba->hbalock,
7476 drvr_flag);
7477 msleep(1);
7478 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7479 }
dea31012005-04-17 16:05:31 -05007480
James Smart3772a992009-05-22 14:50:54 -04007481 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007482 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007483 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007484 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007485 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007486 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007487 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007488 /* Check real SLIM for any errors */
7489 slimword0 = readl(phba->MBslimaddr);
7490 slimmb = (MAILBOX_t *) & slimword0;
7491 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7492 && slimmb->mbxStatus) {
7493 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007494 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007495 word0 = slimword0;
7496 }
7497 }
7498 } else {
7499 /* First copy command data */
7500 word0 = readl(phba->MBslimaddr);
7501 }
7502 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007503 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7504 spin_unlock_irqrestore(&phba->hbalock,
7505 drvr_flag);
7506 goto out_not_finished;
7507 }
dea31012005-04-17 16:05:31 -05007508 }
7509
James Smart3772a992009-05-22 14:50:54 -04007510 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007511 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007512 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7513 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007514 /* Copy the mailbox extension data */
7515 if (pmbox->out_ext_byte_len && pmbox->context2) {
7516 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7517 pmbox->context2,
7518 pmbox->out_ext_byte_len);
7519 }
dea31012005-04-17 16:05:31 -05007520 } else {
7521 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007522 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007523 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007524 /* Copy the mailbox extension data */
7525 if (pmbox->out_ext_byte_len && pmbox->context2) {
7526 lpfc_memcpy_from_slim(pmbox->context2,
7527 phba->MBslimaddr +
7528 MAILBOX_HBA_EXT_OFFSET,
7529 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007530 }
7531 }
7532
7533 writel(HA_MBATT, phba->HAregaddr);
7534 readl(phba->HAregaddr); /* flush */
7535
7536 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007537 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007538 }
7539
James Smart2e0fef82007-06-17 19:56:36 -05007540 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7541 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007542
7543out_not_finished:
7544 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007545 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007546 lpfc_mbox_cmpl_put(phba, pmbox);
7547 }
7548 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007549}
7550
James Smarte59058c2008-08-24 21:49:00 -04007551/**
James Smartf1126682009-06-10 17:22:44 -04007552 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7553 * @phba: Pointer to HBA context object.
7554 *
7555 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7556 * the driver internal pending mailbox queue. It will then try to wait out the
7557 * possible outstanding mailbox command before return.
7558 *
7559 * Returns:
7560 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7561 * the outstanding mailbox command timed out.
7562 **/
7563static int
7564lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7565{
7566 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007567 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007568 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007569
7570 /* Mark the asynchronous mailbox command posting as blocked */
7571 spin_lock_irq(&phba->hbalock);
7572 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007573 /* Determine how long we might wait for the active mailbox
7574 * command to be gracefully completed by firmware.
7575 */
James Smarta183a152011-10-10 21:32:43 -04007576 if (phba->sli.mbox_active)
7577 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7578 phba->sli.mbox_active) *
7579 1000) + jiffies;
7580 spin_unlock_irq(&phba->hbalock);
7581
James Smarte8d3c3b2013-10-10 12:21:30 -04007582 /* Make sure the mailbox is really active */
7583 if (timeout)
7584 lpfc_sli4_process_missed_mbox_completions(phba);
7585
James Smartf1126682009-06-10 17:22:44 -04007586 /* Wait for the outstnading mailbox command to complete */
7587 while (phba->sli.mbox_active) {
7588 /* Check active mailbox complete status every 2ms */
7589 msleep(2);
7590 if (time_after(jiffies, timeout)) {
7591 /* Timeout, marked the outstanding cmd not complete */
7592 rc = 1;
7593 break;
7594 }
7595 }
7596
7597 /* Can not cleanly block async mailbox command, fails it */
7598 if (rc) {
7599 spin_lock_irq(&phba->hbalock);
7600 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7601 spin_unlock_irq(&phba->hbalock);
7602 }
7603 return rc;
7604}
7605
7606/**
7607 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7608 * @phba: Pointer to HBA context object.
7609 *
7610 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7611 * commands from the driver internal pending mailbox queue. It makes sure
7612 * that there is no outstanding mailbox command before resuming posting
7613 * asynchronous mailbox commands. If, for any reason, there is outstanding
7614 * mailbox command, it will try to wait it out before resuming asynchronous
7615 * mailbox command posting.
7616 **/
7617static void
7618lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7619{
7620 struct lpfc_sli *psli = &phba->sli;
7621
7622 spin_lock_irq(&phba->hbalock);
7623 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7624 /* Asynchronous mailbox posting is not blocked, do nothing */
7625 spin_unlock_irq(&phba->hbalock);
7626 return;
7627 }
7628
7629 /* Outstanding synchronous mailbox command is guaranteed to be done,
7630 * successful or timeout, after timing-out the outstanding mailbox
7631 * command shall always be removed, so just unblock posting async
7632 * mailbox command and resume
7633 */
7634 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7635 spin_unlock_irq(&phba->hbalock);
7636
7637 /* wake up worker thread to post asynchronlous mailbox command */
7638 lpfc_worker_wake_up(phba);
7639}
7640
7641/**
James Smart2d843ed2012-09-29 11:29:06 -04007642 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7643 * @phba: Pointer to HBA context object.
7644 * @mboxq: Pointer to mailbox object.
7645 *
7646 * The function waits for the bootstrap mailbox register ready bit from
7647 * port for twice the regular mailbox command timeout value.
7648 *
7649 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7650 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7651 **/
7652static int
7653lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7654{
7655 uint32_t db_ready;
7656 unsigned long timeout;
7657 struct lpfc_register bmbx_reg;
7658
7659 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7660 * 1000) + jiffies;
7661
7662 do {
7663 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7664 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7665 if (!db_ready)
7666 msleep(2);
7667
7668 if (time_after(jiffies, timeout))
7669 return MBXERR_ERROR;
7670 } while (!db_ready);
7671
7672 return 0;
7673}
7674
7675/**
James Smartda0436e2009-05-22 14:51:39 -04007676 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7677 * @phba: Pointer to HBA context object.
7678 * @mboxq: Pointer to mailbox object.
7679 *
7680 * The function posts a mailbox to the port. The mailbox is expected
7681 * to be comletely filled in and ready for the port to operate on it.
7682 * This routine executes a synchronous completion operation on the
7683 * mailbox by polling for its completion.
7684 *
7685 * The caller must not be holding any locks when calling this routine.
7686 *
7687 * Returns:
7688 * MBX_SUCCESS - mailbox posted successfully
7689 * Any of the MBX error values.
7690 **/
7691static int
7692lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7693{
7694 int rc = MBX_SUCCESS;
7695 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007696 uint32_t mcqe_status;
7697 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007698 struct lpfc_sli *psli = &phba->sli;
7699 struct lpfc_mqe *mb = &mboxq->u.mqe;
7700 struct lpfc_bmbx_create *mbox_rgn;
7701 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007702
7703 /*
7704 * Only one mailbox can be active to the bootstrap mailbox region
7705 * at a time and there is no queueing provided.
7706 */
7707 spin_lock_irqsave(&phba->hbalock, iflag);
7708 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7709 spin_unlock_irqrestore(&phba->hbalock, iflag);
7710 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007711 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007712 "cannot issue Data: x%x x%x\n",
7713 mboxq->vport ? mboxq->vport->vpi : 0,
7714 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007715 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7716 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007717 psli->sli_flag, MBX_POLL);
7718 return MBXERR_ERROR;
7719 }
7720 /* The server grabs the token and owns it until release */
7721 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7722 phba->sli.mbox_active = mboxq;
7723 spin_unlock_irqrestore(&phba->hbalock, iflag);
7724
James Smart2d843ed2012-09-29 11:29:06 -04007725 /* wait for bootstrap mbox register for readyness */
7726 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7727 if (rc)
7728 goto exit;
7729
James Smartda0436e2009-05-22 14:51:39 -04007730 /*
7731 * Initialize the bootstrap memory region to avoid stale data areas
7732 * in the mailbox post. Then copy the caller's mailbox contents to
7733 * the bmbx mailbox region.
7734 */
7735 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7736 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7737 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7738 sizeof(struct lpfc_mqe));
7739
7740 /* Post the high mailbox dma address to the port and wait for ready. */
7741 dma_address = &phba->sli4_hba.bmbx.dma_address;
7742 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7743
James Smart2d843ed2012-09-29 11:29:06 -04007744 /* wait for bootstrap mbox register for hi-address write done */
7745 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7746 if (rc)
7747 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007748
7749 /* Post the low mailbox dma address to the port. */
7750 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007751
James Smart2d843ed2012-09-29 11:29:06 -04007752 /* wait for bootstrap mbox register for low address write done */
7753 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7754 if (rc)
7755 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007756
7757 /*
7758 * Read the CQ to ensure the mailbox has completed.
7759 * If so, update the mailbox status so that the upper layers
7760 * can complete the request normally.
7761 */
7762 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7763 sizeof(struct lpfc_mqe));
7764 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7765 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7766 sizeof(struct lpfc_mcqe));
7767 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007768 /*
7769 * When the CQE status indicates a failure and the mailbox status
7770 * indicates success then copy the CQE status into the mailbox status
7771 * (and prefix it with x4000).
7772 */
James Smartda0436e2009-05-22 14:51:39 -04007773 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007774 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7775 bf_set(lpfc_mqe_status, mb,
7776 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007777 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007778 } else
7779 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007780
7781 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007782 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007783 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7784 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007785 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7786 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7787 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007788 bf_get(lpfc_mqe_status, mb),
7789 mb->un.mb_words[0], mb->un.mb_words[1],
7790 mb->un.mb_words[2], mb->un.mb_words[3],
7791 mb->un.mb_words[4], mb->un.mb_words[5],
7792 mb->un.mb_words[6], mb->un.mb_words[7],
7793 mb->un.mb_words[8], mb->un.mb_words[9],
7794 mb->un.mb_words[10], mb->un.mb_words[11],
7795 mb->un.mb_words[12], mboxq->mcqe.word0,
7796 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7797 mboxq->mcqe.trailer);
7798exit:
7799 /* We are holding the token, no needed for lock when release */
7800 spin_lock_irqsave(&phba->hbalock, iflag);
7801 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7802 phba->sli.mbox_active = NULL;
7803 spin_unlock_irqrestore(&phba->hbalock, iflag);
7804 return rc;
7805}
7806
7807/**
7808 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7809 * @phba: Pointer to HBA context object.
7810 * @pmbox: Pointer to mailbox object.
7811 * @flag: Flag indicating how the mailbox need to be processed.
7812 *
7813 * This function is called by discovery code and HBA management code to submit
7814 * a mailbox command to firmware with SLI-4 interface spec.
7815 *
7816 * Return codes the caller owns the mailbox command after the return of the
7817 * function.
7818 **/
7819static int
7820lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7821 uint32_t flag)
7822{
7823 struct lpfc_sli *psli = &phba->sli;
7824 unsigned long iflags;
7825 int rc;
7826
James Smartb76f2dc2011-07-22 18:37:42 -04007827 /* dump from issue mailbox command if setup */
7828 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7829
James Smart8fa38512009-07-19 10:01:03 -04007830 rc = lpfc_mbox_dev_check(phba);
7831 if (unlikely(rc)) {
7832 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007833 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007834 "cannot issue Data: x%x x%x\n",
7835 mboxq->vport ? mboxq->vport->vpi : 0,
7836 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007837 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7838 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007839 psli->sli_flag, flag);
7840 goto out_not_finished;
7841 }
7842
James Smartda0436e2009-05-22 14:51:39 -04007843 /* Detect polling mode and jump to a handler */
7844 if (!phba->sli4_hba.intr_enable) {
7845 if (flag == MBX_POLL)
7846 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7847 else
7848 rc = -EIO;
7849 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007850 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007851 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007852 "(x%x/x%x) failure: "
7853 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7854 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007855 mboxq->vport ? mboxq->vport->vpi : 0,
7856 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007857 lpfc_sli_config_mbox_subsys_get(phba,
7858 mboxq),
7859 lpfc_sli_config_mbox_opcode_get(phba,
7860 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007861 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7862 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7863 bf_get(lpfc_mcqe_ext_status,
7864 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007865 psli->sli_flag, flag);
7866 return rc;
7867 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007868 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7869 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007870 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007871 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007872 mboxq->vport ? mboxq->vport->vpi : 0,
7873 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007874 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7875 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007876 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007877 /* Try to block the asynchronous mailbox posting */
7878 rc = lpfc_sli4_async_mbox_block(phba);
7879 if (!rc) {
7880 /* Successfully blocked, now issue sync mbox cmd */
7881 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7882 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007883 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007884 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007885 "(%d):2597 Sync Mailbox command "
7886 "x%x (x%x/x%x) failure: "
7887 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7888 "Data: x%x x%x\n,",
7889 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007890 mboxq->u.mb.mbxCommand,
7891 lpfc_sli_config_mbox_subsys_get(phba,
7892 mboxq),
7893 lpfc_sli_config_mbox_opcode_get(phba,
7894 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007895 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7896 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7897 bf_get(lpfc_mcqe_ext_status,
7898 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007899 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007900 /* Unblock the async mailbox posting afterward */
7901 lpfc_sli4_async_mbox_unblock(phba);
7902 }
7903 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007904 }
7905
7906 /* Now, interrupt mode asynchrous mailbox command */
7907 rc = lpfc_mbox_cmd_check(phba, mboxq);
7908 if (rc) {
7909 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007910 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007911 "cannot issue Data: x%x x%x\n",
7912 mboxq->vport ? mboxq->vport->vpi : 0,
7913 mboxq->u.mb.mbxCommand,
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 psli->sli_flag, flag);
7917 goto out_not_finished;
7918 }
James Smartda0436e2009-05-22 14:51:39 -04007919
7920 /* Put the mailbox command to the driver internal FIFO */
7921 psli->slistat.mbox_busy++;
7922 spin_lock_irqsave(&phba->hbalock, iflags);
7923 lpfc_mbox_put(phba, mboxq);
7924 spin_unlock_irqrestore(&phba->hbalock, iflags);
7925 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7926 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007927 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007928 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7929 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007930 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7931 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007932 phba->pport->port_state,
7933 psli->sli_flag, MBX_NOWAIT);
7934 /* Wake up worker thread to transport mailbox command from head */
7935 lpfc_worker_wake_up(phba);
7936
7937 return MBX_BUSY;
7938
7939out_not_finished:
7940 return MBX_NOT_FINISHED;
7941}
7942
7943/**
7944 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7945 * @phba: Pointer to HBA context object.
7946 *
7947 * This function is called by worker thread to send a mailbox command to
7948 * SLI4 HBA firmware.
7949 *
7950 **/
7951int
7952lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7953{
7954 struct lpfc_sli *psli = &phba->sli;
7955 LPFC_MBOXQ_t *mboxq;
7956 int rc = MBX_SUCCESS;
7957 unsigned long iflags;
7958 struct lpfc_mqe *mqe;
7959 uint32_t mbx_cmnd;
7960
7961 /* Check interrupt mode before post async mailbox command */
7962 if (unlikely(!phba->sli4_hba.intr_enable))
7963 return MBX_NOT_FINISHED;
7964
7965 /* Check for mailbox command service token */
7966 spin_lock_irqsave(&phba->hbalock, iflags);
7967 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7968 spin_unlock_irqrestore(&phba->hbalock, iflags);
7969 return MBX_NOT_FINISHED;
7970 }
7971 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7972 spin_unlock_irqrestore(&phba->hbalock, iflags);
7973 return MBX_NOT_FINISHED;
7974 }
7975 if (unlikely(phba->sli.mbox_active)) {
7976 spin_unlock_irqrestore(&phba->hbalock, iflags);
7977 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7978 "0384 There is pending active mailbox cmd\n");
7979 return MBX_NOT_FINISHED;
7980 }
7981 /* Take the mailbox command service token */
7982 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7983
7984 /* Get the next mailbox command from head of queue */
7985 mboxq = lpfc_mbox_get(phba);
7986
7987 /* If no more mailbox command waiting for post, we're done */
7988 if (!mboxq) {
7989 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7990 spin_unlock_irqrestore(&phba->hbalock, iflags);
7991 return MBX_SUCCESS;
7992 }
7993 phba->sli.mbox_active = mboxq;
7994 spin_unlock_irqrestore(&phba->hbalock, iflags);
7995
7996 /* Check device readiness for posting mailbox command */
7997 rc = lpfc_mbox_dev_check(phba);
7998 if (unlikely(rc))
7999 /* Driver clean routine will clean up pending mailbox */
8000 goto out_not_finished;
8001
8002 /* Prepare the mbox command to be posted */
8003 mqe = &mboxq->u.mqe;
8004 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
8005
8006 /* Start timer for the mbox_tmo and log some mailbox post messages */
8007 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04008008 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04008009
8010 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008011 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04008012 "x%x x%x\n",
8013 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04008014 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8015 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008016 phba->pport->port_state, psli->sli_flag);
8017
8018 if (mbx_cmnd != MBX_HEARTBEAT) {
8019 if (mboxq->vport) {
8020 lpfc_debugfs_disc_trc(mboxq->vport,
8021 LPFC_DISC_TRC_MBOX_VPORT,
8022 "MBOX Send vport: cmd:x%x mb:x%x x%x",
8023 mbx_cmnd, mqe->un.mb_words[0],
8024 mqe->un.mb_words[1]);
8025 } else {
8026 lpfc_debugfs_disc_trc(phba->pport,
8027 LPFC_DISC_TRC_MBOX,
8028 "MBOX Send: cmd:x%x mb:x%x x%x",
8029 mbx_cmnd, mqe->un.mb_words[0],
8030 mqe->un.mb_words[1]);
8031 }
8032 }
8033 psli->slistat.mbox_cmd++;
8034
8035 /* Post the mailbox command to the port */
8036 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
8037 if (rc != MBX_SUCCESS) {
8038 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04008039 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04008040 "cannot issue Data: x%x x%x\n",
8041 mboxq->vport ? mboxq->vport->vpi : 0,
8042 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04008043 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
8044 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04008045 psli->sli_flag, MBX_NOWAIT);
8046 goto out_not_finished;
8047 }
8048
8049 return rc;
8050
8051out_not_finished:
8052 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05008053 if (phba->sli.mbox_active) {
8054 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8055 __lpfc_mbox_cmpl_put(phba, mboxq);
8056 /* Release the token */
8057 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8058 phba->sli.mbox_active = NULL;
8059 }
James Smartda0436e2009-05-22 14:51:39 -04008060 spin_unlock_irqrestore(&phba->hbalock, iflags);
8061
8062 return MBX_NOT_FINISHED;
8063}
8064
8065/**
8066 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
8067 * @phba: Pointer to HBA context object.
8068 * @pmbox: Pointer to mailbox object.
8069 * @flag: Flag indicating how the mailbox need to be processed.
8070 *
8071 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
8072 * the API jump table function pointer from the lpfc_hba struct.
8073 *
8074 * Return codes the caller owns the mailbox command after the return of the
8075 * function.
8076 **/
8077int
8078lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
8079{
8080 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
8081}
8082
8083/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008084 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04008085 * @phba: The hba struct for which this call is being executed.
8086 * @dev_grp: The HBA PCI-Device group number.
8087 *
8088 * This routine sets up the mbox interface API function jump table in @phba
8089 * struct.
8090 * Returns: 0 - success, -ENODEV - failure.
8091 **/
8092int
8093lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8094{
8095
8096 switch (dev_grp) {
8097 case LPFC_PCI_DEV_LP:
8098 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
8099 phba->lpfc_sli_handle_slow_ring_event =
8100 lpfc_sli_handle_slow_ring_event_s3;
8101 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
8102 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
8103 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
8104 break;
8105 case LPFC_PCI_DEV_OC:
8106 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
8107 phba->lpfc_sli_handle_slow_ring_event =
8108 lpfc_sli_handle_slow_ring_event_s4;
8109 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
8110 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
8111 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
8112 break;
8113 default:
8114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8115 "1420 Invalid HBA PCI-device group: 0x%x\n",
8116 dev_grp);
8117 return -ENODEV;
8118 break;
8119 }
8120 return 0;
8121}
8122
8123/**
James Smart3621a712009-04-06 18:47:14 -04008124 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04008125 * @phba: Pointer to HBA context object.
8126 * @pring: Pointer to driver SLI ring object.
8127 * @piocb: Pointer to address of newly added command iocb.
8128 *
8129 * This function is called with hbalock held to add a command
8130 * iocb to the txq when SLI layer cannot submit the command iocb
8131 * to the ring.
8132 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008133void
James Smart92d7f7b2007-06-17 19:56:38 -05008134__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008135 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05008136{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008137 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05008138 /* Insert the caller's iocb in the txq tail for later processing. */
8139 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05008140}
8141
James Smarte59058c2008-08-24 21:49:00 -04008142/**
James Smart3621a712009-04-06 18:47:14 -04008143 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008144 * @phba: Pointer to HBA context object.
8145 * @pring: Pointer to driver SLI ring object.
8146 * @piocb: Pointer to address of newly added command iocb.
8147 *
8148 * This function is called with hbalock held before a new
8149 * iocb is submitted to the firmware. This function checks
8150 * txq to flush the iocbs in txq to Firmware before
8151 * submitting new iocbs to the Firmware.
8152 * If there are iocbs in the txq which need to be submitted
8153 * to firmware, lpfc_sli_next_iocb returns the first element
8154 * of the txq after dequeuing it from txq.
8155 * If there is no iocb in the txq then the function will return
8156 * *piocb and *piocb is set to NULL. Caller needs to check
8157 * *piocb to find if there are more commands in the txq.
8158 **/
dea31012005-04-17 16:05:31 -05008159static struct lpfc_iocbq *
8160lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008161 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008162{
8163 struct lpfc_iocbq * nextiocb;
8164
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008165 lockdep_assert_held(&phba->hbalock);
8166
dea31012005-04-17 16:05:31 -05008167 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8168 if (!nextiocb) {
8169 nextiocb = *piocb;
8170 *piocb = NULL;
8171 }
8172
8173 return nextiocb;
8174}
8175
James Smarte59058c2008-08-24 21:49:00 -04008176/**
James Smart3772a992009-05-22 14:50:54 -04008177 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008178 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008179 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008180 * @piocb: Pointer to command iocb.
8181 * @flag: Flag indicating if this command can be put into txq.
8182 *
James Smart3772a992009-05-22 14:50:54 -04008183 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8184 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8185 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8186 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8187 * this function allows only iocbs for posting buffers. This function finds
8188 * next available slot in the command ring and posts the command to the
8189 * available slot and writes the port attention register to request HBA start
8190 * processing new iocb. If there is no slot available in the ring and
8191 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8192 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008193 *
James Smart3772a992009-05-22 14:50:54 -04008194 * This function is called with hbalock held. The function will return success
8195 * after it successfully submit the iocb to firmware or after adding to the
8196 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008197 **/
James Smart98c9ea52007-10-27 13:37:33 -04008198static int
James Smart3772a992009-05-22 14:50:54 -04008199__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008200 struct lpfc_iocbq *piocb, uint32_t flag)
8201{
8202 struct lpfc_iocbq *nextiocb;
8203 IOCB_t *iocb;
James Smart895427b2017-02-12 13:52:30 -08008204 struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -05008205
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008206 lockdep_assert_held(&phba->hbalock);
8207
James Smart92d7f7b2007-06-17 19:56:38 -05008208 if (piocb->iocb_cmpl && (!piocb->vport) &&
8209 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8210 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8211 lpfc_printf_log(phba, KERN_ERR,
8212 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008213 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008214 piocb->iocb.ulpCommand);
8215 dump_stack();
8216 return IOCB_ERROR;
8217 }
8218
8219
Linas Vepstas8d63f372007-02-14 14:28:36 -06008220 /* If the PCI channel is in offline state, do not post iocbs. */
8221 if (unlikely(pci_channel_offline(phba->pcidev)))
8222 return IOCB_ERROR;
8223
James Smarta257bf92009-04-06 18:48:10 -04008224 /* If HBA has a deferred error attention, fail the iocb. */
8225 if (unlikely(phba->hba_flag & DEFER_ERATT))
8226 return IOCB_ERROR;
8227
dea31012005-04-17 16:05:31 -05008228 /*
8229 * We should never get an IOCB if we are in a < LINK_DOWN state
8230 */
James Smart2e0fef82007-06-17 19:56:36 -05008231 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008232 return IOCB_ERROR;
8233
8234 /*
8235 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008236 * outstanding event.
dea31012005-04-17 16:05:31 -05008237 */
James Smart0b727fe2007-10-27 13:37:25 -04008238 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008239 goto iocb_busy;
8240
James Smart2e0fef82007-06-17 19:56:36 -05008241 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008242 /*
James Smart2680eea2007-04-25 09:52:55 -04008243 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008244 * can be issued if the link is not up.
8245 */
8246 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008247 case CMD_GEN_REQUEST64_CR:
8248 case CMD_GEN_REQUEST64_CX:
8249 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8250 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008251 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008252 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8253 MENLO_TRANSPORT_TYPE))
8254
8255 goto iocb_busy;
8256 break;
dea31012005-04-17 16:05:31 -05008257 case CMD_QUE_RING_BUF_CN:
8258 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008259 /*
8260 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8261 * completion, iocb_cmpl MUST be 0.
8262 */
8263 if (piocb->iocb_cmpl)
8264 piocb->iocb_cmpl = NULL;
8265 /*FALLTHROUGH*/
8266 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008267 case CMD_CLOSE_XRI_CN:
8268 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008269 break;
8270 default:
8271 goto iocb_busy;
8272 }
8273
8274 /*
8275 * For FCP commands, we must be in a state where we can process link
8276 * attention events.
8277 */
James Smart895427b2017-02-12 13:52:30 -08008278 } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
James Smart92d7f7b2007-06-17 19:56:38 -05008279 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008280 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008281 }
dea31012005-04-17 16:05:31 -05008282
dea31012005-04-17 16:05:31 -05008283 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8284 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8285 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8286
8287 if (iocb)
8288 lpfc_sli_update_ring(phba, pring);
8289 else
8290 lpfc_sli_update_full_ring(phba, pring);
8291
8292 if (!piocb)
8293 return IOCB_SUCCESS;
8294
8295 goto out_busy;
8296
8297 iocb_busy:
8298 pring->stats.iocb_cmd_delay++;
8299
8300 out_busy:
8301
8302 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008303 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008304 return IOCB_SUCCESS;
8305 }
8306
8307 return IOCB_BUSY;
8308}
8309
James Smart3772a992009-05-22 14:50:54 -04008310/**
James Smart4f774512009-05-22 14:52:35 -04008311 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8312 * @phba: Pointer to HBA context object.
8313 * @piocb: Pointer to command iocb.
8314 * @sglq: Pointer to the scatter gather queue object.
8315 *
8316 * This routine converts the bpl or bde that is in the IOCB
8317 * to a sgl list for the sli4 hardware. The physical address
8318 * of the bpl/bde is converted back to a virtual address.
8319 * If the IOCB contains a BPL then the list of BDE's is
8320 * converted to sli4_sge's. If the IOCB contains a single
8321 * BDE then it is converted to a single sli_sge.
8322 * The IOCB is still in cpu endianess so the contents of
8323 * the bpl can be used without byte swapping.
8324 *
8325 * Returns valid XRI = Success, NO_XRI = Failure.
8326**/
8327static uint16_t
8328lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8329 struct lpfc_sglq *sglq)
8330{
8331 uint16_t xritag = NO_XRI;
8332 struct ulp_bde64 *bpl = NULL;
8333 struct ulp_bde64 bde;
8334 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008335 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008336 IOCB_t *icmd;
8337 int numBdes = 0;
8338 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008339 uint32_t offset = 0; /* accumulated offset in the sg request list */
8340 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008341
8342 if (!piocbq || !sglq)
8343 return xritag;
8344
8345 sgl = (struct sli4_sge *)sglq->sgl;
8346 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008347 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8348 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008349 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8350 numBdes = icmd->un.genreq64.bdl.bdeSize /
8351 sizeof(struct ulp_bde64);
8352 /* The addrHigh and addrLow fields within the IOCB
8353 * have not been byteswapped yet so there is no
8354 * need to swap them back.
8355 */
James Smart1b511972011-12-13 13:23:09 -05008356 if (piocbq->context3)
8357 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8358 else
8359 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008360
James Smart1b511972011-12-13 13:23:09 -05008361 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008362 if (!bpl)
8363 return xritag;
8364
8365 for (i = 0; i < numBdes; i++) {
8366 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008367 sgl->addr_hi = bpl->addrHigh;
8368 sgl->addr_lo = bpl->addrLow;
8369
James Smart05580562011-05-24 11:40:48 -04008370 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008371 if ((i+1) == numBdes)
8372 bf_set(lpfc_sli4_sge_last, sgl, 1);
8373 else
8374 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008375 /* swap the size field back to the cpu so we
8376 * can assign it to the sgl.
8377 */
8378 bde.tus.w = le32_to_cpu(bpl->tus.w);
8379 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008380 /* The offsets in the sgl need to be accumulated
8381 * separately for the request and reply lists.
8382 * The request is always first, the reply follows.
8383 */
8384 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8385 /* add up the reply sg entries */
8386 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8387 inbound++;
8388 /* first inbound? reset the offset */
8389 if (inbound == 1)
8390 offset = 0;
8391 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008392 bf_set(lpfc_sli4_sge_type, sgl,
8393 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008394 offset += bde.tus.f.bdeSize;
8395 }
James Smart546fc852011-03-11 16:06:29 -05008396 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008397 bpl++;
8398 sgl++;
8399 }
8400 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8401 /* The addrHigh and addrLow fields of the BDE have not
8402 * been byteswapped yet so they need to be swapped
8403 * before putting them in the sgl.
8404 */
8405 sgl->addr_hi =
8406 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8407 sgl->addr_lo =
8408 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008409 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008410 bf_set(lpfc_sli4_sge_last, sgl, 1);
8411 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008412 sgl->sge_len =
8413 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008414 }
8415 return sglq->sli4_xritag;
8416}
8417
8418/**
James Smart4f774512009-05-22 14:52:35 -04008419 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8420 * @phba: Pointer to HBA context object.
8421 * @piocb: Pointer to command iocb.
8422 * @wqe: Pointer to the work queue entry.
8423 *
8424 * This routine converts the iocb command to its Work Queue Entry
8425 * equivalent. The wqe pointer should not have any fields set when
8426 * this routine is called because it will memcpy over them.
8427 * This routine does not set the CQ_ID or the WQEC bits in the
8428 * wqe.
8429 *
8430 * Returns: 0 = Success, IOCB_ERROR = Failure.
8431 **/
8432static int
8433lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8434 union lpfc_wqe *wqe)
8435{
James Smart5ffc2662009-11-18 15:39:44 -05008436 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008437 uint8_t ct = 0;
8438 uint32_t fip;
8439 uint32_t abort_tag;
8440 uint8_t command_type = ELS_COMMAND_NON_FIP;
8441 uint8_t cmnd;
8442 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008443 uint16_t abrt_iotag;
8444 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008445 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008446 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008447 int numBdes, i;
8448 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008449 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008450 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008451 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008452
James Smart45ed1192009-10-02 15:17:02 -04008453 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008454 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008455 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008456 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008457 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008458 command_type = ELS_COMMAND_FIP;
8459 else
8460 command_type = ELS_COMMAND_NON_FIP;
8461
James Smartb5c53952016-03-31 14:12:30 -07008462 if (phba->fcp_embed_io)
8463 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008464 /* Some of the fields are in the right position already */
8465 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartf0d9bcc2010-10-22 11:07:09 -04008466 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart28d7f3d2014-05-21 08:05:28 -04008467 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07008468
8469 abort_tag = (uint32_t) iocbq->iotag;
8470 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008471 /* words0-2 bpl convert bde */
8472 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008473 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8474 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008475 bpl = (struct ulp_bde64 *)
8476 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8477 if (!bpl)
8478 return IOCB_ERROR;
8479
8480 /* Should already be byte swapped. */
8481 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8482 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8483 /* swap the size field back to the cpu so we
8484 * can assign it to the sgl.
8485 */
8486 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008487 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8488 total_len = 0;
8489 for (i = 0; i < numBdes; i++) {
8490 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8491 total_len += bde.tus.f.bdeSize;
8492 }
James Smart4f774512009-05-22 14:52:35 -04008493 } else
James Smart5ffc2662009-11-18 15:39:44 -05008494 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008495
8496 iocbq->iocb.ulpIoTag = iocbq->iotag;
8497 cmnd = iocbq->iocb.ulpCommand;
8498
8499 switch (iocbq->iocb.ulpCommand) {
8500 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008501 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8502 ndlp = iocbq->context_un.ndlp;
8503 else
8504 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008505 if (!iocbq->iocb.ulpLe) {
8506 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8507 "2007 Only Limited Edition cmd Format"
8508 " supported 0x%x\n",
8509 iocbq->iocb.ulpCommand);
8510 return IOCB_ERROR;
8511 }
James Smartff78d8f2011-12-13 13:21:35 -05008512
James Smart5ffc2662009-11-18 15:39:44 -05008513 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008514 /* Els_reguest64 has a TMO */
8515 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8516 iocbq->iocb.ulpTimeout);
8517 /* Need a VF for word 4 set the vf bit*/
8518 bf_set(els_req64_vf, &wqe->els_req, 0);
8519 /* And a VFID for word 12 */
8520 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008521 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008522 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8523 iocbq->iocb.ulpContext);
8524 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8525 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008526 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008527 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008528 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8529 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008530 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8531 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008532 if_type = bf_get(lpfc_sli_intf_if_type,
8533 &phba->sli4_hba.sli_intf);
8534 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008535 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008536 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008537 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008538 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008539 *pcmd == ELS_CMD_PLOGI)) {
8540 bf_set(els_req64_sp, &wqe->els_req, 1);
8541 bf_set(els_req64_sid, &wqe->els_req,
8542 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008543 if ((*pcmd == ELS_CMD_FLOGI) &&
8544 !(phba->fc_topology ==
8545 LPFC_TOPOLOGY_LOOP))
8546 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008547 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8548 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008549 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008550 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008551 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8552 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8553 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8554 }
James Smartc8685952009-11-18 15:39:16 -05008555 }
James Smart6d368e52011-05-24 11:44:12 -04008556 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8557 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008558 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8559 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8560 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8561 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8562 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8563 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008564 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008565 break;
James Smart5ffc2662009-11-18 15:39:44 -05008566 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008567 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8568 iocbq->iocb.un.ulpWord[3]);
8569 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008570 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008571 /* The entire sequence is transmitted for this IOCB */
8572 xmit_len = total_len;
8573 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008574 if (phba->link_flag & LS_LOOPBACK_MODE)
8575 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008576 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008577 /* word3 iocb=io_tag32 wqe=reserved */
8578 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008579 /* word4 relative_offset memcpy */
8580 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008581 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8582 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8583 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8584 LPFC_WQE_IOD_WRITE);
8585 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8586 LPFC_WQE_LENLOC_WORD12);
8587 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008588 wqe->xmit_sequence.xmit_len = xmit_len;
8589 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008590 break;
James Smart4f774512009-05-22 14:52:35 -04008591 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008592 /* word3 iocb=iotag32 wqe=seq_payload_len */
8593 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008594 /* word4 iocb=rsvd wqe=rsvd */
8595 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8596 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008597 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008598 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008599 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8600 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8601 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8602 LPFC_WQE_LENLOC_WORD3);
8603 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008604 break;
James Smart4f774512009-05-22 14:52:35 -04008605 case CMD_FCP_IWRITE64_CR:
8606 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008607 /* word3 iocb=iotag wqe=payload_offset_len */
8608 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008609 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8610 xmit_len + sizeof(struct fcp_rsp));
8611 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8612 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008613 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8614 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8615 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8616 iocbq->iocb.ulpFCP2Rcvy);
8617 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8618 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008619 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8620 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8621 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008622 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008623 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008624 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8625 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008626 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8627 if (iocbq->priority) {
8628 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8629 (iocbq->priority << 1));
8630 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008631 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8632 (phba->cfg_XLanePriority << 1));
8633 }
8634 }
James Smartb5c53952016-03-31 14:12:30 -07008635 /* Note, word 10 is already initialized to 0 */
8636
8637 if (phba->fcp_embed_io) {
8638 struct lpfc_scsi_buf *lpfc_cmd;
8639 struct sli4_sge *sgl;
8640 union lpfc_wqe128 *wqe128;
8641 struct fcp_cmnd *fcp_cmnd;
8642 uint32_t *ptr;
8643
8644 /* 128 byte wqe support here */
8645 wqe128 = (union lpfc_wqe128 *)wqe;
8646
8647 lpfc_cmd = iocbq->context1;
8648 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8649 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8650
8651 /* Word 0-2 - FCP_CMND */
8652 wqe128->generic.bde.tus.f.bdeFlags =
8653 BUFF_TYPE_BDE_IMMED;
8654 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8655 wqe128->generic.bde.addrHigh = 0;
8656 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8657
8658 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8659
8660 /* Word 22-29 FCP CMND Payload */
8661 ptr = &wqe128->words[22];
8662 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8663 }
James Smart7851fe22011-07-22 18:36:52 -04008664 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008665 case CMD_FCP_IREAD64_CR:
8666 /* word3 iocb=iotag wqe=payload_offset_len */
8667 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008668 bf_set(payload_offset_len, &wqe->fcp_iread,
8669 xmit_len + sizeof(struct fcp_rsp));
8670 bf_set(cmd_buff_len, &wqe->fcp_iread,
8671 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008672 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8673 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8674 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8675 iocbq->iocb.ulpFCP2Rcvy);
8676 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008677 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008678 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8679 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8680 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008681 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008682 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008683 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8684 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008685 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8686 if (iocbq->priority) {
8687 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8688 (iocbq->priority << 1));
8689 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008690 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8691 (phba->cfg_XLanePriority << 1));
8692 }
8693 }
James Smartb5c53952016-03-31 14:12:30 -07008694 /* Note, word 10 is already initialized to 0 */
8695
8696 if (phba->fcp_embed_io) {
8697 struct lpfc_scsi_buf *lpfc_cmd;
8698 struct sli4_sge *sgl;
8699 union lpfc_wqe128 *wqe128;
8700 struct fcp_cmnd *fcp_cmnd;
8701 uint32_t *ptr;
8702
8703 /* 128 byte wqe support here */
8704 wqe128 = (union lpfc_wqe128 *)wqe;
8705
8706 lpfc_cmd = iocbq->context1;
8707 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8708 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8709
8710 /* Word 0-2 - FCP_CMND */
8711 wqe128->generic.bde.tus.f.bdeFlags =
8712 BUFF_TYPE_BDE_IMMED;
8713 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8714 wqe128->generic.bde.addrHigh = 0;
8715 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8716
8717 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8718
8719 /* Word 22-29 FCP CMND Payload */
8720 ptr = &wqe128->words[22];
8721 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8722 }
James Smart7851fe22011-07-22 18:36:52 -04008723 break;
James Smartf1126682009-06-10 17:22:44 -04008724 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008725 /* word3 iocb=iotag wqe=payload_offset_len */
8726 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8727 bf_set(payload_offset_len, &wqe->fcp_icmd,
8728 xmit_len + sizeof(struct fcp_rsp));
8729 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8730 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008731 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008732 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008733 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008734 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8735 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8736 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8737 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8738 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008739 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8740 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008741 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8742 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008743 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8744 if (iocbq->priority) {
8745 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8746 (iocbq->priority << 1));
8747 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008748 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8749 (phba->cfg_XLanePriority << 1));
8750 }
8751 }
James Smartb5c53952016-03-31 14:12:30 -07008752 /* Note, word 10 is already initialized to 0 */
8753
8754 if (phba->fcp_embed_io) {
8755 struct lpfc_scsi_buf *lpfc_cmd;
8756 struct sli4_sge *sgl;
8757 union lpfc_wqe128 *wqe128;
8758 struct fcp_cmnd *fcp_cmnd;
8759 uint32_t *ptr;
8760
8761 /* 128 byte wqe support here */
8762 wqe128 = (union lpfc_wqe128 *)wqe;
8763
8764 lpfc_cmd = iocbq->context1;
8765 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8766 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8767
8768 /* Word 0-2 - FCP_CMND */
8769 wqe128->generic.bde.tus.f.bdeFlags =
8770 BUFF_TYPE_BDE_IMMED;
8771 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8772 wqe128->generic.bde.addrHigh = 0;
8773 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8774
8775 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8776
8777 /* Word 22-29 FCP CMND Payload */
8778 ptr = &wqe128->words[22];
8779 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8780 }
James Smart7851fe22011-07-22 18:36:52 -04008781 break;
James Smart4f774512009-05-22 14:52:35 -04008782 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008783 /* For this command calculate the xmit length of the
8784 * request bde.
8785 */
8786 xmit_len = 0;
8787 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8788 sizeof(struct ulp_bde64);
8789 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008790 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008791 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8792 break;
James Smart63e801c2010-11-20 23:14:19 -05008793 xmit_len += bde.tus.f.bdeSize;
8794 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008795 /* word3 iocb=IO_TAG wqe=request_payload_len */
8796 wqe->gen_req.request_payload_len = xmit_len;
8797 /* word4 iocb=parameter wqe=relative_offset memcpy */
8798 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008799 /* word6 context tag copied in memcpy */
8800 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8801 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8802 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8803 "2015 Invalid CT %x command 0x%x\n",
8804 ct, iocbq->iocb.ulpCommand);
8805 return IOCB_ERROR;
8806 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008807 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8808 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8809 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8810 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8811 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8812 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8813 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8814 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008815 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008816 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008817 break;
James Smart4f774512009-05-22 14:52:35 -04008818 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008819 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008820 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008821 /* word3 iocb=iotag32 wqe=response_payload_len */
8822 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008823 /* word4 */
8824 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008825 /* word5 iocb=rsvd wge=did */
8826 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008827 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8828
8829 if_type = bf_get(lpfc_sli_intf_if_type,
8830 &phba->sli4_hba.sli_intf);
8831 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8832 if (iocbq->vport->fc_flag & FC_PT2PT) {
8833 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8834 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8835 iocbq->vport->fc_myDID);
8836 if (iocbq->vport->fc_myDID == Fabric_DID) {
8837 bf_set(wqe_els_did,
8838 &wqe->xmit_els_rsp.wqe_dest, 0);
8839 }
8840 }
8841 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008842 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8843 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8844 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8845 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008846 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008847 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008848 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008849 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008850 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8851 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8852 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8853 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8854 LPFC_WQE_LENLOC_WORD3);
8855 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008856 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8857 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008858 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8859 iocbq->context2)->virt);
8860 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008861 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8862 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008863 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008864 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8865 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008866 phba->vpi_ids[phba->pport->vpi]);
8867 }
James Smart4f774512009-05-22 14:52:35 -04008868 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008869 break;
James Smart4f774512009-05-22 14:52:35 -04008870 case CMD_CLOSE_XRI_CN:
8871 case CMD_ABORT_XRI_CN:
8872 case CMD_ABORT_XRI_CX:
8873 /* words 0-2 memcpy should be 0 rserved */
8874 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008875 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8876 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8877 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8878 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8879 } else
8880 fip = 0;
8881
8882 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008883 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008884 * The link is down, or the command was ELS_FIP
8885 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008886 * on the wire.
8887 */
8888 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8889 else
8890 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8891 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008892 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8893 wqe->abort_cmd.rsrvd5 = 0;
8894 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008895 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8896 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008897 /*
8898 * The abort handler will send us CMD_ABORT_XRI_CN or
8899 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8900 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008901 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8902 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8903 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8904 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008905 cmnd = CMD_ABORT_XRI_CX;
8906 command_type = OTHER_COMMAND;
8907 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008908 break;
James Smart6669f9b2009-10-02 15:16:45 -04008909 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008910 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008911 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008912 * we re-construct this WQE here based on information in
8913 * iocbq from scratch.
8914 */
8915 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008916 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008917 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008918 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8919 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008920 LPFC_ABTS_UNSOL_INT) {
8921 /* ABTS sent by initiator to CT exchange, the
8922 * RX_ID field will be filled with the newly
8923 * allocated responder XRI.
8924 */
8925 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8926 iocbq->sli4_xritag);
8927 } else {
8928 /* ABTS sent by responder to CT exchange, the
8929 * RX_ID field will be filled with the responder
8930 * RX_ID from ABTS.
8931 */
8932 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008933 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008934 }
James Smart6669f9b2009-10-02 15:16:45 -04008935 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8936 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008937
8938 /* Use CT=VPI */
8939 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8940 ndlp->nlp_DID);
8941 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8942 iocbq->iocb.ulpContext);
8943 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008944 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008945 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008946 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8947 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8948 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008949 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8950 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008951 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8952 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8953 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8954 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8955 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8956 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8957 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8958 }
8959
James Smart7851fe22011-07-22 18:36:52 -04008960 break;
James Smart4f774512009-05-22 14:52:35 -04008961 case CMD_XRI_ABORTED_CX:
8962 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008963 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8964 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8965 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8966 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8967 default:
8968 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8969 "2014 Invalid command 0x%x\n",
8970 iocbq->iocb.ulpCommand);
8971 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008972 break;
James Smart4f774512009-05-22 14:52:35 -04008973 }
James Smart6d368e52011-05-24 11:44:12 -04008974
James Smart8012cc32012-10-31 14:44:49 -04008975 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8976 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8977 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8978 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8979 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8980 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8981 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8982 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008983 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8984 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8985 wqe->generic.wqe_com.abort_tag = abort_tag;
8986 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8987 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8988 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8989 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008990 return 0;
8991}
8992
8993/**
8994 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8995 * @phba: Pointer to HBA context object.
8996 * @ring_number: SLI ring number to issue iocb on.
8997 * @piocb: Pointer to command iocb.
8998 * @flag: Flag indicating if this command can be put into txq.
8999 *
9000 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
9001 * an iocb command to an HBA with SLI-4 interface spec.
9002 *
9003 * This function is called with hbalock held. The function will return success
9004 * after it successfully submit the iocb to firmware or after adding to the
9005 * txq.
9006 **/
9007static int
9008__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
9009 struct lpfc_iocbq *piocb, uint32_t flag)
9010{
9011 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07009012 union lpfc_wqe *wqe;
9013 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05009014 struct lpfc_queue *wq;
James Smart895427b2017-02-12 13:52:30 -08009015 struct lpfc_sli_ring *pring;
James Smart4f774512009-05-22 14:52:35 -04009016
James Smart895427b2017-02-12 13:52:30 -08009017 /* Get the WQ */
9018 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
9019 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
9020 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
9021 wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
9022 else
9023 wq = phba->sli4_hba.oas_wq;
9024 } else {
9025 wq = phba->sli4_hba.els_wq;
9026 }
9027
9028 /* Get corresponding ring */
9029 pring = wq->pring;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009030
James Smartb5c53952016-03-31 14:12:30 -07009031 /*
9032 * The WQE can be either 64 or 128 bytes,
9033 * so allocate space on the stack assuming the largest.
9034 */
9035 wqe = (union lpfc_wqe *)&wqe128;
9036
James Smart895427b2017-02-12 13:52:30 -08009037 lockdep_assert_held(&phba->hbalock);
9038
James Smart4f774512009-05-22 14:52:35 -04009039 if (piocb->sli4_xritag == NO_XRI) {
9040 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05009041 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04009042 sglq = NULL;
9043 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05009044 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04009045 if (!(flag & SLI_IOCB_RET_IOCB)) {
9046 __lpfc_sli_ringtx_put(phba,
9047 pring, piocb);
9048 return IOCB_SUCCESS;
9049 } else {
9050 return IOCB_BUSY;
9051 }
9052 } else {
James Smart895427b2017-02-12 13:52:30 -08009053 sglq = __lpfc_sli_get_els_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04009054 if (!sglq) {
9055 if (!(flag & SLI_IOCB_RET_IOCB)) {
9056 __lpfc_sli_ringtx_put(phba,
9057 pring,
9058 piocb);
9059 return IOCB_SUCCESS;
9060 } else
9061 return IOCB_BUSY;
9062 }
9063 }
James Smart4f774512009-05-22 14:52:35 -04009064 }
James Smart2ea259e2017-02-12 13:52:27 -08009065 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04009066 /* These IO's already have an XRI and a mapped sgl. */
9067 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08009068 else {
James Smart6d368e52011-05-24 11:44:12 -04009069 /*
9070 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04009071 * sglq is on the active list
9072 */
James Smartedccdc12013-01-03 15:43:45 -05009073 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04009074 if (!sglq)
9075 return IOCB_ERROR;
9076 }
9077
9078 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04009079 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009080 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04009081 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04009082 return IOCB_ERROR;
9083 }
9084
James Smartb5c53952016-03-31 14:12:30 -07009085 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04009086 return IOCB_ERROR;
9087
James Smart895427b2017-02-12 13:52:30 -08009088 if (lpfc_sli4_wq_put(wq, wqe))
9089 return IOCB_ERROR;
James Smart4f774512009-05-22 14:52:35 -04009090 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
9091
9092 return 0;
9093}
9094
9095/**
James Smart3772a992009-05-22 14:50:54 -04009096 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
9097 *
9098 * This routine wraps the actual lockless version for issusing IOCB function
9099 * pointer from the lpfc_hba struct.
9100 *
9101 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07009102 * IOCB_ERROR - Error
9103 * IOCB_SUCCESS - Success
9104 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04009105 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04009106int
James Smart3772a992009-05-22 14:50:54 -04009107__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9108 struct lpfc_iocbq *piocb, uint32_t flag)
9109{
9110 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9111}
9112
9113/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009114 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04009115 * @phba: The hba struct for which this call is being executed.
9116 * @dev_grp: The HBA PCI-Device group number.
9117 *
9118 * This routine sets up the SLI interface API function jump table in @phba
9119 * struct.
9120 * Returns: 0 - success, -ENODEV - failure.
9121 **/
9122int
9123lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
9124{
9125
9126 switch (dev_grp) {
9127 case LPFC_PCI_DEV_LP:
9128 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
9129 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
9130 break;
James Smart4f774512009-05-22 14:52:35 -04009131 case LPFC_PCI_DEV_OC:
9132 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
9133 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
9134 break;
James Smart3772a992009-05-22 14:50:54 -04009135 default:
9136 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9137 "1419 Invalid HBA PCI-device group: 0x%x\n",
9138 dev_grp);
9139 return -ENODEV;
9140 break;
9141 }
9142 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9143 return 0;
9144}
James Smart92d7f7b2007-06-17 19:56:38 -05009145
James Smarta1efe162015-05-21 13:55:20 -04009146/**
James Smart895427b2017-02-12 13:52:30 -08009147 * lpfc_sli4_calc_ring - Calculates which ring to use
James Smarta1efe162015-05-21 13:55:20 -04009148 * @phba: Pointer to HBA context object.
James Smarta1efe162015-05-21 13:55:20 -04009149 * @piocb: Pointer to command iocb.
9150 *
James Smart895427b2017-02-12 13:52:30 -08009151 * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
9152 * hba_wqidx, thus we need to calculate the corresponding ring.
James Smarta1efe162015-05-21 13:55:20 -04009153 * Since ABORTS must go on the same WQ of the command they are
James Smart895427b2017-02-12 13:52:30 -08009154 * aborting, we use command's hba_wqidx.
James Smarta1efe162015-05-21 13:55:20 -04009155 */
James Smart895427b2017-02-12 13:52:30 -08009156struct lpfc_sli_ring *
9157lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
James Smart9bd2bff52014-09-03 12:57:30 -04009158{
James Smart895427b2017-02-12 13:52:30 -08009159 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
James Smart8b0dff12015-05-22 10:42:38 -04009160 if (!(phba->cfg_fof) ||
James Smart895427b2017-02-12 13:52:30 -08009161 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
James Smart8b0dff12015-05-22 10:42:38 -04009162 if (unlikely(!phba->sli4_hba.fcp_wq))
James Smart895427b2017-02-12 13:52:30 -08009163 return NULL;
James Smart8b0dff12015-05-22 10:42:38 -04009164 /*
James Smart895427b2017-02-12 13:52:30 -08009165 * for abort iocb hba_wqidx should already
James Smart8b0dff12015-05-22 10:42:38 -04009166 * be setup based on what work queue we used.
9167 */
9168 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
James Smart895427b2017-02-12 13:52:30 -08009169 piocb->hba_wqidx =
James Smart8b0dff12015-05-22 10:42:38 -04009170 lpfc_sli4_scmd_to_wqidx_distr(phba,
9171 piocb->context1);
James Smart895427b2017-02-12 13:52:30 -08009172 return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
James Smart8b0dff12015-05-22 10:42:38 -04009173 } else {
9174 if (unlikely(!phba->sli4_hba.oas_wq))
James Smart895427b2017-02-12 13:52:30 -08009175 return NULL;
9176 piocb->hba_wqidx = 0;
9177 return phba->sli4_hba.oas_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009178 }
James Smart895427b2017-02-12 13:52:30 -08009179 } else {
9180 if (unlikely(!phba->sli4_hba.els_wq))
9181 return NULL;
9182 piocb->hba_wqidx = 0;
9183 return phba->sli4_hba.els_wq->pring;
James Smart9bd2bff52014-09-03 12:57:30 -04009184 }
James Smart9bd2bff52014-09-03 12:57:30 -04009185}
9186
James Smarte59058c2008-08-24 21:49:00 -04009187/**
James Smart3621a712009-04-06 18:47:14 -04009188 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009189 * @phba: Pointer to HBA context object.
9190 * @pring: Pointer to driver SLI ring object.
9191 * @piocb: Pointer to command iocb.
9192 * @flag: Flag indicating if this command can be put into txq.
9193 *
9194 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9195 * function. This function gets the hbalock and calls
9196 * __lpfc_sli_issue_iocb function and will return the error returned
9197 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9198 * functions which do not hold hbalock.
9199 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009200int
James Smart3772a992009-05-22 14:50:54 -04009201lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009202 struct lpfc_iocbq *piocb, uint32_t flag)
9203{
James Smart895427b2017-02-12 13:52:30 -08009204 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009205 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009206 struct lpfc_queue *fpeq;
9207 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009208 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009209 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009210
James Smart7e56aa22012-08-03 12:35:34 -04009211 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -08009212 pring = lpfc_sli4_calc_ring(phba, piocb);
9213 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -04009214 return IOCB_ERROR;
James Smartba20c852012-08-03 12:36:52 -04009215
James Smart9bd2bff52014-09-03 12:57:30 -04009216 spin_lock_irqsave(&pring->ring_lock, iflags);
9217 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9218 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009219
James Smart9bd2bff52014-09-03 12:57:30 -04009220 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
James Smart895427b2017-02-12 13:52:30 -08009221 idx = piocb->hba_wqidx;
9222 hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009223
James Smart895427b2017-02-12 13:52:30 -08009224 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009225
James Smart9bd2bff52014-09-03 12:57:30 -04009226 /* Get associated EQ with this index */
9227 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009228
James Smart9bd2bff52014-09-03 12:57:30 -04009229 /* Turn off interrupts from this EQ */
9230 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009231
James Smart9bd2bff52014-09-03 12:57:30 -04009232 /*
9233 * Process all the events on FCP EQ
9234 */
9235 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9236 lpfc_sli4_hba_handle_eqe(phba,
9237 eqe, idx);
9238 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009239 }
James Smartba20c852012-08-03 12:36:52 -04009240
James Smart9bd2bff52014-09-03 12:57:30 -04009241 /* Always clear and re-arm the EQ */
9242 lpfc_sli4_eq_release(fpeq,
9243 LPFC_QUEUE_REARM);
9244 }
James Smart895427b2017-02-12 13:52:30 -08009245 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009246 }
James Smart7e56aa22012-08-03 12:35:34 -04009247 } else {
9248 /* For now, SLI2/3 will still use hbalock */
9249 spin_lock_irqsave(&phba->hbalock, iflags);
9250 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9251 spin_unlock_irqrestore(&phba->hbalock, iflags);
9252 }
James Smart92d7f7b2007-06-17 19:56:38 -05009253 return rc;
9254}
9255
James Smarte59058c2008-08-24 21:49:00 -04009256/**
James Smart3621a712009-04-06 18:47:14 -04009257 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009258 * @phba: Pointer to HBA context object.
9259 *
9260 * This function is called while driver attaches with the
9261 * HBA to setup the extra ring. The extra ring is used
9262 * only when driver needs to support target mode functionality
9263 * or IP over FC functionalities.
9264 *
James Smart895427b2017-02-12 13:52:30 -08009265 * This function is called with no lock held. SLI3 only.
James Smarte59058c2008-08-24 21:49:00 -04009266 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009267static int
9268lpfc_extra_ring_setup( struct lpfc_hba *phba)
9269{
9270 struct lpfc_sli *psli;
9271 struct lpfc_sli_ring *pring;
9272
9273 psli = &phba->sli;
9274
9275 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009276
9277 /* Take some away from the FCP ring */
James Smart895427b2017-02-12 13:52:30 -08009278 pring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart7e56aa22012-08-03 12:35:34 -04009279 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9280 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9281 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9282 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009283
James Smarta4bc3372006-12-02 13:34:16 -05009284 /* and give them to the extra ring */
James Smart895427b2017-02-12 13:52:30 -08009285 pring = &psli->sli3_ring[LPFC_EXTRA_RING];
James Smarta4bc3372006-12-02 13:34:16 -05009286
James Smart7e56aa22012-08-03 12:35:34 -04009287 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9288 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9289 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9290 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009291
9292 /* Setup default profile for this ring */
9293 pring->iotag_max = 4096;
9294 pring->num_mask = 1;
9295 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009296 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9297 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009298 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9299 return 0;
9300}
9301
James Smartcb69f7d2011-12-13 13:21:57 -05009302/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9303 * @phba: Pointer to HBA context object.
9304 * @iocbq: Pointer to iocb object.
9305 *
9306 * The async_event handler calls this routine when it receives
9307 * an ASYNC_STATUS_CN event from the port. The port generates
9308 * this event when an Abort Sequence request to an rport fails
9309 * twice in succession. The abort could be originated by the
9310 * driver or by the port. The ABTS could have been for an ELS
9311 * or FCP IO. The port only generates this event when an ABTS
9312 * fails to complete after one retry.
9313 */
9314static void
9315lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9316 struct lpfc_iocbq *iocbq)
9317{
9318 struct lpfc_nodelist *ndlp = NULL;
9319 uint16_t rpi = 0, vpi = 0;
9320 struct lpfc_vport *vport = NULL;
9321
9322 /* The rpi in the ulpContext is vport-sensitive. */
9323 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9324 rpi = iocbq->iocb.ulpContext;
9325
9326 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9327 "3092 Port generated ABTS async event "
9328 "on vpi %d rpi %d status 0x%x\n",
9329 vpi, rpi, iocbq->iocb.ulpStatus);
9330
9331 vport = lpfc_find_vport_by_vpid(phba, vpi);
9332 if (!vport)
9333 goto err_exit;
9334 ndlp = lpfc_findnode_rpi(vport, rpi);
9335 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9336 goto err_exit;
9337
9338 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9339 lpfc_sli_abts_recover_port(vport, ndlp);
9340 return;
9341
9342 err_exit:
9343 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9344 "3095 Event Context not found, no "
9345 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9346 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9347 vpi, rpi);
9348}
9349
9350/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9351 * @phba: pointer to HBA context object.
9352 * @ndlp: nodelist pointer for the impacted rport.
9353 * @axri: pointer to the wcqe containing the failed exchange.
9354 *
9355 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9356 * port. The port generates this event when an abort exchange request to an
9357 * rport fails twice in succession with no reply. The abort could be originated
9358 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9359 */
9360void
9361lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9362 struct lpfc_nodelist *ndlp,
9363 struct sli4_wcqe_xri_aborted *axri)
9364{
9365 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009366 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009367
James Smart6b5151f2012-01-18 16:24:06 -05009368 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009369 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9370 "3115 Node Context not found, driver "
9371 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009372 return;
9373 }
9374
James Smartcb69f7d2011-12-13 13:21:57 -05009375 vport = ndlp->vport;
9376 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9377 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009378 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009379 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009380 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009381 bf_get(lpfc_wcqe_xa_status, axri),
9382 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009383
James Smart5c1db2a2012-03-01 22:34:36 -05009384 /*
9385 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9386 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9387 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9388 */
James Smarte3d2b802012-08-14 14:25:43 -04009389 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009390 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9391 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009392 lpfc_sli_abts_recover_port(vport, ndlp);
9393}
9394
James Smarte59058c2008-08-24 21:49:00 -04009395/**
James Smart3621a712009-04-06 18:47:14 -04009396 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009397 * @phba: Pointer to HBA context object.
9398 * @pring: Pointer to driver SLI ring object.
9399 * @iocbq: Pointer to iocb object.
9400 *
9401 * This function is called by the slow ring event handler
9402 * function when there is an ASYNC event iocb in the ring.
9403 * This function is called with no lock held.
9404 * Currently this function handles only temperature related
9405 * ASYNC events. The function decodes the temperature sensor
9406 * event message and posts events for the management applications.
9407 **/
James Smart98c9ea52007-10-27 13:37:33 -04009408static void
James Smart57127f12007-10-27 13:37:05 -04009409lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9410 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9411{
9412 IOCB_t *icmd;
9413 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009414 struct temp_event temp_event_data;
9415 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009416 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009417
9418 icmd = &iocbq->iocb;
9419 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009420
James Smartcb69f7d2011-12-13 13:21:57 -05009421 switch (evt_code) {
9422 case ASYNC_TEMP_WARN:
9423 case ASYNC_TEMP_SAFE:
9424 temp_event_data.data = (uint32_t) icmd->ulpContext;
9425 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9426 if (evt_code == ASYNC_TEMP_WARN) {
9427 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9428 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9429 "0347 Adapter is very hot, please take "
9430 "corrective action. temperature : %d Celsius\n",
9431 (uint32_t) icmd->ulpContext);
9432 } else {
9433 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9434 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9435 "0340 Adapter temperature is OK now. "
9436 "temperature : %d Celsius\n",
9437 (uint32_t) icmd->ulpContext);
9438 }
9439
9440 /* Send temperature change event to applications */
9441 shost = lpfc_shost_from_vport(phba->pport);
9442 fc_host_post_vendor_event(shost, fc_get_event_number(),
9443 sizeof(temp_event_data), (char *) &temp_event_data,
9444 LPFC_NL_VENDOR_ID);
9445 break;
9446 case ASYNC_STATUS_CN:
9447 lpfc_sli_abts_err_handler(phba, iocbq);
9448 break;
9449 default:
James Smarta257bf92009-04-06 18:48:10 -04009450 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009451 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009452 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009453 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009454 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9455 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9456 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9457 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009458 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009459 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9460 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9461 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9462 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9463
James Smartcb69f7d2011-12-13 13:21:57 -05009464 break;
James Smart57127f12007-10-27 13:37:05 -04009465 }
James Smart57127f12007-10-27 13:37:05 -04009466}
9467
9468
James Smarte59058c2008-08-24 21:49:00 -04009469/**
James Smart895427b2017-02-12 13:52:30 -08009470 * lpfc_sli4_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009471 * @phba: Pointer to HBA context object.
9472 *
9473 * lpfc_sli_setup sets up rings of the SLI interface with
9474 * number of iocbs per ring and iotags. This function is
9475 * called while driver attach to the HBA and before the
9476 * interrupts are enabled. So there is no need for locking.
9477 *
9478 * This function always returns 0.
9479 **/
dea31012005-04-17 16:05:31 -05009480int
James Smart895427b2017-02-12 13:52:30 -08009481lpfc_sli4_setup(struct lpfc_hba *phba)
9482{
9483 struct lpfc_sli_ring *pring;
9484
9485 pring = phba->sli4_hba.els_wq->pring;
9486 pring->num_mask = LPFC_MAX_RING_MASK;
9487 pring->prt[0].profile = 0; /* Mask 0 */
9488 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9489 pring->prt[0].type = FC_TYPE_ELS;
9490 pring->prt[0].lpfc_sli_rcv_unsol_event =
9491 lpfc_els_unsol_event;
9492 pring->prt[1].profile = 0; /* Mask 1 */
9493 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9494 pring->prt[1].type = FC_TYPE_ELS;
9495 pring->prt[1].lpfc_sli_rcv_unsol_event =
9496 lpfc_els_unsol_event;
9497 pring->prt[2].profile = 0; /* Mask 2 */
9498 /* NameServer Inquiry */
9499 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9500 /* NameServer */
9501 pring->prt[2].type = FC_TYPE_CT;
9502 pring->prt[2].lpfc_sli_rcv_unsol_event =
9503 lpfc_ct_unsol_event;
9504 pring->prt[3].profile = 0; /* Mask 3 */
9505 /* NameServer response */
9506 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9507 /* NameServer */
9508 pring->prt[3].type = FC_TYPE_CT;
9509 pring->prt[3].lpfc_sli_rcv_unsol_event =
9510 lpfc_ct_unsol_event;
9511 return 0;
9512}
9513
9514/**
9515 * lpfc_sli_setup - SLI ring setup function
9516 * @phba: Pointer to HBA context object.
9517 *
9518 * lpfc_sli_setup sets up rings of the SLI interface with
9519 * number of iocbs per ring and iotags. This function is
9520 * called while driver attach to the HBA and before the
9521 * interrupts are enabled. So there is no need for locking.
9522 *
9523 * This function always returns 0. SLI3 only.
9524 **/
9525int
dea31012005-04-17 16:05:31 -05009526lpfc_sli_setup(struct lpfc_hba *phba)
9527{
James Smarted957682007-06-17 19:56:37 -05009528 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009529 struct lpfc_sli *psli = &phba->sli;
9530 struct lpfc_sli_ring *pring;
9531
James Smart2a76a282012-08-03 12:35:54 -04009532 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
dea31012005-04-17 16:05:31 -05009533 psli->sli_flag = 0;
dea31012005-04-17 16:05:31 -05009534
James Bottomley604a3e32005-10-29 10:28:33 -05009535 psli->iocbq_lookup = NULL;
9536 psli->iocbq_lookup_len = 0;
9537 psli->last_iotag = 0;
9538
dea31012005-04-17 16:05:31 -05009539 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009540 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009541 switch (i) {
9542 case LPFC_FCP_RING: /* ring 0 - FCP */
9543 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009544 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9545 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9546 pring->sli.sli3.numCiocb +=
9547 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9548 pring->sli.sli3.numRiocb +=
9549 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9550 pring->sli.sli3.numCiocb +=
9551 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9552 pring->sli.sli3.numRiocb +=
9553 SLI2_IOCB_RSP_R3XTRA_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;
dea31012005-04-17 16:05:31 -05009560 pring->iotag_ctr = 0;
9561 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009562 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009563 pring->fast_iotag = pring->iotag_max;
9564 pring->num_mask = 0;
9565 break;
James Smarta4bc3372006-12-02 13:34:16 -05009566 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009567 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009568 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9569 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9570 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009571 SLI3_IOCB_CMD_SIZE :
9572 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009573 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009574 SLI3_IOCB_RSP_SIZE :
9575 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009576 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009577 pring->num_mask = 0;
9578 break;
9579 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9580 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009581 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9582 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9583 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009584 SLI3_IOCB_CMD_SIZE :
9585 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009586 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009587 SLI3_IOCB_RSP_SIZE :
9588 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009589 pring->fast_iotag = 0;
9590 pring->iotag_ctr = 0;
9591 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009592 pring->lpfc_sli_rcv_async_status =
9593 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009594 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009595 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009596 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9597 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009598 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009599 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009600 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009601 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9602 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009603 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009604 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009605 pring->prt[2].profile = 0; /* Mask 2 */
9606 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009607 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009608 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009609 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009610 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009611 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009612 pring->prt[3].profile = 0; /* Mask 3 */
9613 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009614 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009615 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009616 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009617 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009618 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009619 break;
9620 }
James Smart7e56aa22012-08-03 12:35:34 -04009621 totiocbsize += (pring->sli.sli3.numCiocb *
9622 pring->sli.sli3.sizeCiocb) +
9623 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009624 }
James Smarted957682007-06-17 19:56:37 -05009625 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009626 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009627 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9628 "SLI2 SLIM Data: x%x x%lx\n",
9629 phba->brd_no, totiocbsize,
9630 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009631 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009632 if (phba->cfg_multi_ring_support == 2)
9633 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009634
9635 return 0;
9636}
9637
James Smarte59058c2008-08-24 21:49:00 -04009638/**
James Smart895427b2017-02-12 13:52:30 -08009639 * lpfc_sli4_queue_init - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009640 * @phba: Pointer to HBA context object.
9641 *
James Smart895427b2017-02-12 13:52:30 -08009642 * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
James Smarte59058c2008-08-24 21:49:00 -04009643 * ring. This function also initializes ring indices of each ring.
9644 * This function is called during the initialization of the SLI
9645 * interface of an HBA.
9646 * This function is called with no lock held and always returns
9647 * 1.
9648 **/
James Smart895427b2017-02-12 13:52:30 -08009649void
9650lpfc_sli4_queue_init(struct lpfc_hba *phba)
9651{
9652 struct lpfc_sli *psli;
9653 struct lpfc_sli_ring *pring;
9654 int i;
9655
9656 psli = &phba->sli;
9657 spin_lock_irq(&phba->hbalock);
9658 INIT_LIST_HEAD(&psli->mboxq);
9659 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9660 /* Initialize list headers for txq and txcmplq as double linked lists */
9661 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
9662 pring = phba->sli4_hba.fcp_wq[i]->pring;
9663 pring->flag = 0;
9664 pring->ringno = LPFC_FCP_RING;
9665 INIT_LIST_HEAD(&pring->txq);
9666 INIT_LIST_HEAD(&pring->txcmplq);
9667 INIT_LIST_HEAD(&pring->iocb_continueq);
9668 spin_lock_init(&pring->ring_lock);
9669 }
9670 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
9671 pring = phba->sli4_hba.nvme_wq[i]->pring;
9672 pring->flag = 0;
9673 pring->ringno = LPFC_FCP_RING;
9674 INIT_LIST_HEAD(&pring->txq);
9675 INIT_LIST_HEAD(&pring->txcmplq);
9676 INIT_LIST_HEAD(&pring->iocb_continueq);
9677 spin_lock_init(&pring->ring_lock);
9678 }
9679 pring = phba->sli4_hba.els_wq->pring;
9680 pring->flag = 0;
9681 pring->ringno = LPFC_ELS_RING;
9682 INIT_LIST_HEAD(&pring->txq);
9683 INIT_LIST_HEAD(&pring->txcmplq);
9684 INIT_LIST_HEAD(&pring->iocb_continueq);
9685 spin_lock_init(&pring->ring_lock);
9686
9687 if (phba->cfg_nvme_io_channel) {
9688 pring = phba->sli4_hba.nvmels_wq->pring;
9689 pring->flag = 0;
9690 pring->ringno = LPFC_ELS_RING;
9691 INIT_LIST_HEAD(&pring->txq);
9692 INIT_LIST_HEAD(&pring->txcmplq);
9693 INIT_LIST_HEAD(&pring->iocb_continueq);
9694 spin_lock_init(&pring->ring_lock);
9695 }
9696
9697 if (phba->cfg_fof) {
9698 pring = phba->sli4_hba.oas_wq->pring;
9699 pring->flag = 0;
9700 pring->ringno = LPFC_FCP_RING;
9701 INIT_LIST_HEAD(&pring->txq);
9702 INIT_LIST_HEAD(&pring->txcmplq);
9703 INIT_LIST_HEAD(&pring->iocb_continueq);
9704 spin_lock_init(&pring->ring_lock);
9705 }
9706
9707 spin_unlock_irq(&phba->hbalock);
9708}
9709
9710/**
9711 * lpfc_sli_queue_init - Queue initialization function
9712 * @phba: Pointer to HBA context object.
9713 *
9714 * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
9715 * ring. This function also initializes ring indices of each ring.
9716 * This function is called during the initialization of the SLI
9717 * interface of an HBA.
9718 * This function is called with no lock held and always returns
9719 * 1.
9720 **/
9721void
9722lpfc_sli_queue_init(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009723{
9724 struct lpfc_sli *psli;
9725 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009726 int i;
dea31012005-04-17 16:05:31 -05009727
9728 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009729 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009730 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009731 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009732 /* Initialize list headers for txq and txcmplq as double linked lists */
9733 for (i = 0; i < psli->num_rings; i++) {
James Smart895427b2017-02-12 13:52:30 -08009734 pring = &psli->sli3_ring[i];
dea31012005-04-17 16:05:31 -05009735 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009736 pring->sli.sli3.next_cmdidx = 0;
9737 pring->sli.sli3.local_getidx = 0;
9738 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05009739 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009740 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009741 INIT_LIST_HEAD(&pring->postbufq);
James Smart895427b2017-02-12 13:52:30 -08009742 pring->flag = 0;
9743 INIT_LIST_HEAD(&pring->txq);
9744 INIT_LIST_HEAD(&pring->txcmplq);
James Smart7e56aa22012-08-03 12:35:34 -04009745 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009746 }
James Smart2e0fef82007-06-17 19:56:36 -05009747 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009748}
9749
James Smarte59058c2008-08-24 21:49:00 -04009750/**
James Smart04c68492009-05-22 14:52:52 -04009751 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9752 * @phba: Pointer to HBA context object.
9753 *
9754 * This routine flushes the mailbox command subsystem. It will unconditionally
9755 * flush all the mailbox commands in the three possible stages in the mailbox
9756 * command sub-system: pending mailbox command queue; the outstanding mailbox
9757 * command; and completed mailbox command queue. It is caller's responsibility
9758 * to make sure that the driver is in the proper state to flush the mailbox
9759 * command sub-system. Namely, the posting of mailbox commands into the
9760 * pending mailbox command queue from the various clients must be stopped;
9761 * either the HBA is in a state that it will never works on the outstanding
9762 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9763 * mailbox command has been completed.
9764 **/
9765static void
9766lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9767{
9768 LIST_HEAD(completions);
9769 struct lpfc_sli *psli = &phba->sli;
9770 LPFC_MBOXQ_t *pmb;
9771 unsigned long iflag;
9772
9773 /* Flush all the mailbox commands in the mbox system */
9774 spin_lock_irqsave(&phba->hbalock, iflag);
9775 /* The pending mailbox command queue */
9776 list_splice_init(&phba->sli.mboxq, &completions);
9777 /* The outstanding active mailbox command */
9778 if (psli->mbox_active) {
9779 list_add_tail(&psli->mbox_active->list, &completions);
9780 psli->mbox_active = NULL;
9781 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9782 }
9783 /* The completed mailbox command queue */
9784 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9785 spin_unlock_irqrestore(&phba->hbalock, iflag);
9786
9787 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9788 while (!list_empty(&completions)) {
9789 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9790 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9791 if (pmb->mbox_cmpl)
9792 pmb->mbox_cmpl(phba, pmb);
9793 }
9794}
9795
9796/**
James Smart3621a712009-04-06 18:47:14 -04009797 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009798 * @vport: Pointer to virtual port object.
9799 *
9800 * lpfc_sli_host_down is called to clean up the resources
9801 * associated with a vport before destroying virtual
9802 * port data structures.
9803 * This function does following operations:
9804 * - Free discovery resources associated with this virtual
9805 * port.
9806 * - Free iocbs associated with this virtual port in
9807 * the txq.
9808 * - Send abort for all iocb commands associated with this
9809 * vport in txcmplq.
9810 *
9811 * This function is called with no lock held and always returns 1.
9812 **/
dea31012005-04-17 16:05:31 -05009813int
James Smart92d7f7b2007-06-17 19:56:38 -05009814lpfc_sli_host_down(struct lpfc_vport *vport)
9815{
James Smart858c9f62007-06-17 19:56:39 -05009816 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009817 struct lpfc_hba *phba = vport->phba;
9818 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08009819 struct lpfc_queue *qp = NULL;
James Smart92d7f7b2007-06-17 19:56:38 -05009820 struct lpfc_sli_ring *pring;
9821 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009822 int i;
9823 unsigned long flags = 0;
9824 uint16_t prev_pring_flag;
9825
9826 lpfc_cleanup_discovery_resources(vport);
9827
9828 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009829
James Smart895427b2017-02-12 13:52:30 -08009830 /*
9831 * Error everything on the txq since these iocbs
9832 * have not been given to the FW yet.
9833 * Also issue ABTS for everything on the txcmplq
9834 */
9835 if (phba->sli_rev != LPFC_SLI_REV4) {
9836 for (i = 0; i < psli->num_rings; i++) {
9837 pring = &psli->sli3_ring[i];
9838 prev_pring_flag = pring->flag;
9839 /* Only slow rings */
9840 if (pring->ringno == LPFC_ELS_RING) {
9841 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9842 /* Set the lpfc data pending flag */
9843 set_bit(LPFC_DATA_READY, &phba->data_flags);
9844 }
9845 list_for_each_entry_safe(iocb, next_iocb,
9846 &pring->txq, list) {
9847 if (iocb->vport != vport)
9848 continue;
9849 list_move_tail(&iocb->list, &completions);
9850 }
9851 list_for_each_entry_safe(iocb, next_iocb,
9852 &pring->txcmplq, list) {
9853 if (iocb->vport != vport)
9854 continue;
9855 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9856 }
9857 pring->flag = prev_pring_flag;
James Smart92d7f7b2007-06-17 19:56:38 -05009858 }
James Smart895427b2017-02-12 13:52:30 -08009859 } else {
9860 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
9861 pring = qp->pring;
9862 if (!pring)
9863 continue;
9864 if (pring == phba->sli4_hba.els_wq->pring) {
9865 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9866 /* Set the lpfc data pending flag */
9867 set_bit(LPFC_DATA_READY, &phba->data_flags);
9868 }
9869 prev_pring_flag = pring->flag;
9870 spin_lock_irq(&pring->ring_lock);
9871 list_for_each_entry_safe(iocb, next_iocb,
9872 &pring->txq, list) {
9873 if (iocb->vport != vport)
9874 continue;
9875 list_move_tail(&iocb->list, &completions);
9876 }
9877 spin_unlock_irq(&pring->ring_lock);
9878 list_for_each_entry_safe(iocb, next_iocb,
9879 &pring->txcmplq, list) {
9880 if (iocb->vport != vport)
9881 continue;
9882 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9883 }
9884 pring->flag = prev_pring_flag;
9885 }
James Smart92d7f7b2007-06-17 19:56:38 -05009886 }
James Smart92d7f7b2007-06-17 19:56:38 -05009887 spin_unlock_irqrestore(&phba->hbalock, flags);
9888
James Smarta257bf92009-04-06 18:48:10 -04009889 /* Cancel all the IOCBs from the completions list */
9890 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9891 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009892 return 1;
9893}
9894
James Smarte59058c2008-08-24 21:49:00 -04009895/**
James Smart3621a712009-04-06 18:47:14 -04009896 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009897 * @phba: Pointer to HBA context object.
9898 *
9899 * This function cleans up all iocb, buffers, mailbox commands
9900 * while shutting down the HBA. This function is called with no
9901 * lock held and always returns 1.
9902 * This function does the following to cleanup driver resources:
9903 * - Free discovery resources for each virtual port
9904 * - Cleanup any pending fabric iocbs
9905 * - Iterate through the iocb txq and free each entry
9906 * in the list.
9907 * - Free up any buffer posted to the HBA
9908 * - Free mailbox commands in the mailbox queue.
9909 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009910int
James Smart2e0fef82007-06-17 19:56:36 -05009911lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009912{
James Smart2534ba72007-04-25 09:52:20 -04009913 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009914 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08009915 struct lpfc_queue *qp = NULL;
dea31012005-04-17 16:05:31 -05009916 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009917 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009918 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009919 int i;
9920
9921 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009922 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009923
dea31012005-04-17 16:05:31 -05009924 lpfc_hba_down_prep(phba);
9925
James Smart92d7f7b2007-06-17 19:56:38 -05009926 lpfc_fabric_abort_hba(phba);
9927
James Smart2e0fef82007-06-17 19:56:36 -05009928 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009929
James Smart895427b2017-02-12 13:52:30 -08009930 /*
9931 * Error everything on the txq since these iocbs
9932 * have not been given to the FW yet.
9933 */
9934 if (phba->sli_rev != LPFC_SLI_REV4) {
9935 for (i = 0; i < psli->num_rings; i++) {
9936 pring = &psli->sli3_ring[i];
9937 /* Only slow rings */
9938 if (pring->ringno == LPFC_ELS_RING) {
9939 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9940 /* Set the lpfc data pending flag */
9941 set_bit(LPFC_DATA_READY, &phba->data_flags);
9942 }
9943 list_splice_init(&pring->txq, &completions);
9944 }
9945 } else {
9946 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
9947 pring = qp->pring;
9948 if (!pring)
9949 continue;
9950 spin_lock_irq(&pring->ring_lock);
9951 list_splice_init(&pring->txq, &completions);
9952 spin_unlock_irq(&pring->ring_lock);
9953 if (pring == phba->sli4_hba.els_wq->pring) {
9954 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9955 /* Set the lpfc data pending flag */
9956 set_bit(LPFC_DATA_READY, &phba->data_flags);
9957 }
9958 }
dea31012005-04-17 16:05:31 -05009959 }
James Smart2e0fef82007-06-17 19:56:36 -05009960 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009961
James Smarta257bf92009-04-06 18:48:10 -04009962 /* Cancel all the IOCBs from the completions list */
9963 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9964 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009965
James Smart0ff10d42008-01-11 01:52:36 -05009966 spin_lock_irqsave(&phba->hbalock, flags);
9967 list_splice_init(&phba->elsbuf, &completions);
9968 phba->elsbuf_cnt = 0;
9969 phba->elsbuf_prev_cnt = 0;
9970 spin_unlock_irqrestore(&phba->hbalock, flags);
9971
9972 while (!list_empty(&completions)) {
9973 list_remove_head(&completions, buf_ptr,
9974 struct lpfc_dmabuf, list);
9975 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9976 kfree(buf_ptr);
9977 }
9978
dea31012005-04-17 16:05:31 -05009979 /* Return any active mbox cmds */
9980 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009981
James Smartda0436e2009-05-22 14:51:39 -04009982 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009983 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009984 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009985
James Smartda0436e2009-05-22 14:51:39 -04009986 return 1;
9987}
James Smart92d7f7b2007-06-17 19:56:38 -05009988
James Smartda0436e2009-05-22 14:51:39 -04009989/**
James Smart3621a712009-04-06 18:47:14 -04009990 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009991 * @srcp: Source memory pointer.
9992 * @destp: Destination memory pointer.
9993 * @cnt: Number of words required to be copied.
9994 *
9995 * This function is used for copying data between driver memory
9996 * and the SLI memory. This function also changes the endianness
9997 * of each word if native endianness is different from SLI
9998 * endianness. This function can be called with or without
9999 * lock.
10000 **/
dea31012005-04-17 16:05:31 -050010001void
10002lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
10003{
10004 uint32_t *src = srcp;
10005 uint32_t *dest = destp;
10006 uint32_t ldata;
10007 int i;
10008
10009 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
10010 ldata = *src;
10011 ldata = le32_to_cpu(ldata);
10012 *dest = ldata;
10013 src++;
10014 dest++;
10015 }
10016}
10017
James Smarte59058c2008-08-24 21:49:00 -040010018
10019/**
James Smarta0c87cb2009-07-19 10:01:10 -040010020 * lpfc_sli_bemem_bcopy - SLI memory copy function
10021 * @srcp: Source memory pointer.
10022 * @destp: Destination memory pointer.
10023 * @cnt: Number of words required to be copied.
10024 *
10025 * This function is used for copying data between a data structure
10026 * with big endian representation to local endianness.
10027 * This function can be called with or without lock.
10028 **/
10029void
10030lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
10031{
10032 uint32_t *src = srcp;
10033 uint32_t *dest = destp;
10034 uint32_t ldata;
10035 int i;
10036
10037 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
10038 ldata = *src;
10039 ldata = be32_to_cpu(ldata);
10040 *dest = ldata;
10041 src++;
10042 dest++;
10043 }
10044}
10045
10046/**
James Smart3621a712009-04-06 18:47:14 -040010047 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -040010048 * @phba: Pointer to HBA context object.
10049 * @pring: Pointer to driver SLI ring object.
10050 * @mp: Pointer to driver buffer object.
10051 *
10052 * This function is called with no lock held.
10053 * It always return zero after adding the buffer to the postbufq
10054 * buffer list.
10055 **/
dea31012005-04-17 16:05:31 -050010056int
James Smart2e0fef82007-06-17 19:56:36 -050010057lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10058 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -050010059{
10060 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
10061 later */
James Smart2e0fef82007-06-17 19:56:36 -050010062 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010063 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -050010064 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -050010065 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010066 return 0;
10067}
10068
James Smarte59058c2008-08-24 21:49:00 -040010069/**
James Smart3621a712009-04-06 18:47:14 -040010070 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -040010071 * @phba: Pointer to HBA context object.
10072 *
10073 * When HBQ is enabled, buffers are searched based on tags. This function
10074 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
10075 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
10076 * does not conflict with tags of buffer posted for unsolicited events.
10077 * The function returns the allocated tag. The function is called with
10078 * no locks held.
10079 **/
James Smart76bb24e2007-10-27 13:38:00 -040010080uint32_t
10081lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
10082{
10083 spin_lock_irq(&phba->hbalock);
10084 phba->buffer_tag_count++;
10085 /*
10086 * Always set the QUE_BUFTAG_BIT to distiguish between
10087 * a tag assigned by HBQ.
10088 */
10089 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
10090 spin_unlock_irq(&phba->hbalock);
10091 return phba->buffer_tag_count;
10092}
10093
James Smarte59058c2008-08-24 21:49:00 -040010094/**
James Smart3621a712009-04-06 18:47:14 -040010095 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -040010096 * @phba: Pointer to HBA context object.
10097 * @pring: Pointer to driver SLI ring object.
10098 * @tag: Buffer tag.
10099 *
10100 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
10101 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
10102 * iocb is posted to the response ring with the tag of the buffer.
10103 * This function searches the pring->postbufq list using the tag
10104 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
10105 * iocb. If the buffer is found then lpfc_dmabuf object of the
10106 * buffer is returned to the caller else NULL is returned.
10107 * This function is called with no lock held.
10108 **/
James Smart76bb24e2007-10-27 13:38:00 -040010109struct lpfc_dmabuf *
10110lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10111 uint32_t tag)
10112{
10113 struct lpfc_dmabuf *mp, *next_mp;
10114 struct list_head *slp = &pring->postbufq;
10115
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010116 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -040010117 spin_lock_irq(&phba->hbalock);
10118 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10119 if (mp->buffer_tag == tag) {
10120 list_del_init(&mp->list);
10121 pring->postbufq_cnt--;
10122 spin_unlock_irq(&phba->hbalock);
10123 return mp;
10124 }
10125 }
10126
10127 spin_unlock_irq(&phba->hbalock);
10128 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -040010129 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -040010130 "ring %d Data x%lx x%p x%p x%x\n",
10131 pring->ringno, (unsigned long) tag,
10132 slp->next, slp->prev, pring->postbufq_cnt);
10133
10134 return NULL;
10135}
dea31012005-04-17 16:05:31 -050010136
James Smarte59058c2008-08-24 21:49:00 -040010137/**
James Smart3621a712009-04-06 18:47:14 -040010138 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -040010139 * @phba: Pointer to HBA context object.
10140 * @pring: Pointer to driver SLI ring object.
10141 * @phys: DMA address of the buffer.
10142 *
10143 * This function searches the buffer list using the dma_address
10144 * of unsolicited event to find the driver's lpfc_dmabuf object
10145 * corresponding to the dma_address. The function returns the
10146 * lpfc_dmabuf object if a buffer is found else it returns NULL.
10147 * This function is called by the ct and els unsolicited event
10148 * handlers to get the buffer associated with the unsolicited
10149 * event.
10150 *
10151 * This function is called with no lock held.
10152 **/
dea31012005-04-17 16:05:31 -050010153struct lpfc_dmabuf *
10154lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10155 dma_addr_t phys)
10156{
10157 struct lpfc_dmabuf *mp, *next_mp;
10158 struct list_head *slp = &pring->postbufq;
10159
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010160 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -050010161 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010162 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
10163 if (mp->phys == phys) {
10164 list_del_init(&mp->list);
10165 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -050010166 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010167 return mp;
10168 }
10169 }
10170
James Smart2e0fef82007-06-17 19:56:36 -050010171 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -050010172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -040010173 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -050010174 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010175 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -050010176 slp->next, slp->prev, pring->postbufq_cnt);
10177 return NULL;
10178}
10179
James Smarte59058c2008-08-24 21:49:00 -040010180/**
James Smart3621a712009-04-06 18:47:14 -040010181 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -040010182 * @phba: Pointer to HBA context object.
10183 * @cmdiocb: Pointer to driver command iocb object.
10184 * @rspiocb: Pointer to driver response iocb object.
10185 *
10186 * This function is the completion handler for the abort iocbs for
10187 * ELS commands. This function is called from the ELS ring event
10188 * handler with no lock held. This function frees memory resources
10189 * associated with the abort iocb.
10190 **/
dea31012005-04-17 16:05:31 -050010191static void
James Smart2e0fef82007-06-17 19:56:36 -050010192lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10193 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -050010194{
James Smart2e0fef82007-06-17 19:56:36 -050010195 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -040010196 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -050010197 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -040010198
10199 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -050010200
10201 /*
10202 * Assume that the port already completed and returned, or
10203 * will return the iocb. Just Log the message.
10204 */
James Smart2680eea2007-04-25 09:52:55 -040010205 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
10206 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
10207
James Smart2e0fef82007-06-17 19:56:36 -050010208 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -040010209 if (phba->sli_rev < LPFC_SLI_REV4) {
10210 if (abort_iotag != 0 &&
10211 abort_iotag <= phba->sli.last_iotag)
10212 abort_iocb =
10213 phba->sli.iocbq_lookup[abort_iotag];
10214 } else
10215 /* For sli4 the abort_tag is the XRI,
10216 * so the abort routine puts the iotag of the iocb
10217 * being aborted in the context field of the abort
10218 * IOCB.
10219 */
10220 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -040010221
James Smart2a9bf3d2010-06-07 15:24:45 -040010222 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
10223 "0327 Cannot abort els iocb %p "
10224 "with tag %x context %x, abort status %x, "
10225 "abort code %x\n",
10226 abort_iocb, abort_iotag, abort_context,
10227 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -040010228
James Smartff78d8f2011-12-13 13:21:35 -050010229 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -040010230 }
James Bottomley604a3e32005-10-29 10:28:33 -050010231 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -050010232 return;
10233}
10234
James Smarte59058c2008-08-24 21:49:00 -040010235/**
James Smart3621a712009-04-06 18:47:14 -040010236 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -040010237 * @phba: Pointer to HBA context object.
10238 * @cmdiocb: Pointer to driver command iocb object.
10239 * @rspiocb: Pointer to driver response iocb object.
10240 *
10241 * The function is called from SLI ring event handler with no
10242 * lock held. This function is the completion handler for ELS commands
10243 * which are aborted. The function frees memory resources used for
10244 * the aborted ELS commands.
10245 **/
James Smart92d7f7b2007-06-17 19:56:38 -050010246static void
10247lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10248 struct lpfc_iocbq *rspiocb)
10249{
10250 IOCB_t *irsp = &rspiocb->iocb;
10251
10252 /* ELS cmd tag <ulpIoTag> completes */
10253 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -040010254 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -050010255 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010256 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -050010257 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -050010258 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
10259 lpfc_ct_free_iocb(phba, cmdiocb);
10260 else
10261 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -050010262 return;
10263}
10264
James Smarte59058c2008-08-24 21:49:00 -040010265/**
James Smart5af5eee2010-10-22 11:06:38 -040010266 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -040010267 * @phba: Pointer to HBA context object.
10268 * @pring: Pointer to driver SLI ring object.
10269 * @cmdiocb: Pointer to driver command iocb object.
10270 *
James Smart5af5eee2010-10-22 11:06:38 -040010271 * This function issues an abort iocb for the provided command iocb down to
10272 * the port. Other than the case the outstanding command iocb is an abort
10273 * request, this function issues abort out unconditionally. This function is
10274 * called with hbalock held. The function returns 0 when it fails due to
10275 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -040010276 **/
James Smart5af5eee2010-10-22 11:06:38 -040010277static int
10278lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -050010279 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -050010280{
James Smart2e0fef82007-06-17 19:56:36 -050010281 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010282 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -050010283 IOCB_t *icmd = NULL;
10284 IOCB_t *iabt = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010285 int retval;
James Smart7e56aa22012-08-03 12:35:34 -040010286 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -040010287
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010288 lockdep_assert_held(&phba->hbalock);
10289
James Smart92d7f7b2007-06-17 19:56:38 -050010290 /*
10291 * There are certain command types we don't want to abort. And we
10292 * don't want to abort commands that are already in the process of
10293 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010294 */
10295 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010296 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010297 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10298 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010299 return 0;
10300
dea31012005-04-17 16:05:31 -050010301 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010302 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010303 if (abtsiocbp == NULL)
10304 return 0;
dea31012005-04-17 16:05:31 -050010305
James Smart07951072007-04-25 09:51:38 -040010306 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010307 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010308 */
10309 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10310
dea31012005-04-17 16:05:31 -050010311 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010312 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10313 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010314 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010315 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010316 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10317 }
James Smartda0436e2009-05-22 14:51:39 -040010318 else
10319 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010320 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010321 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010322
James Smart5ffc2662009-11-18 15:39:44 -050010323 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010324 abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010325 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10326 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010327 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10328 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010329
James Smart2e0fef82007-06-17 19:56:36 -050010330 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010331 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10332 else
10333 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10334
10335 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010336 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010337
James Smarte8b62012007-08-02 11:10:09 -040010338 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10339 "0339 Abort xri x%x, original iotag x%x, "
10340 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010341 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010342 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010343 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010344
10345 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010346 pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
10347 if (unlikely(pring == NULL))
James Smart9bd2bff52014-09-03 12:57:30 -040010348 return 0;
James Smart7e56aa22012-08-03 12:35:34 -040010349 /* Note: both hbalock and ring_lock need to be set here */
10350 spin_lock_irqsave(&pring->ring_lock, iflags);
10351 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10352 abtsiocbp, 0);
10353 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10354 } else {
10355 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10356 abtsiocbp, 0);
10357 }
James Smart07951072007-04-25 09:51:38 -040010358
James Smartd7c255b2008-08-24 21:50:00 -040010359 if (retval)
10360 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010361
10362 /*
10363 * Caller to this routine should check for IOCB_ERROR
10364 * and handle it properly. This routine no longer removes
10365 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10366 */
10367 return retval;
10368}
10369
10370/**
10371 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10372 * @phba: Pointer to HBA context object.
10373 * @pring: Pointer to driver SLI ring object.
10374 * @cmdiocb: Pointer to driver command iocb object.
10375 *
10376 * This function issues an abort iocb for the provided command iocb. In case
10377 * of unloading, the abort iocb will not be issued to commands on the ELS
10378 * ring. Instead, the callback function shall be changed to those commands
10379 * so that nothing happens when them finishes. This function is called with
10380 * hbalock held. The function returns 0 when the command iocb is an abort
10381 * request.
10382 **/
10383int
10384lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10385 struct lpfc_iocbq *cmdiocb)
10386{
10387 struct lpfc_vport *vport = cmdiocb->vport;
10388 int retval = IOCB_ERROR;
10389 IOCB_t *icmd = NULL;
10390
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010391 lockdep_assert_held(&phba->hbalock);
10392
James Smart5af5eee2010-10-22 11:06:38 -040010393 /*
10394 * There are certain command types we don't want to abort. And we
10395 * don't want to abort commands that are already in the process of
10396 * being aborted.
10397 */
10398 icmd = &cmdiocb->iocb;
10399 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10400 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10401 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10402 return 0;
10403
10404 /*
10405 * If we're unloading, don't abort iocb on the ELS ring, but change
10406 * the callback so that nothing happens when it finishes.
10407 */
10408 if ((vport->load_flag & FC_UNLOADING) &&
10409 (pring->ringno == LPFC_ELS_RING)) {
10410 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10411 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10412 else
10413 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10414 goto abort_iotag_exit;
10415 }
10416
10417 /* Now, we try to issue the abort to the cmdiocb out */
10418 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10419
James Smart07951072007-04-25 09:51:38 -040010420abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010421 /*
10422 * Caller to this routine should check for IOCB_ERROR
10423 * and handle it properly. This routine no longer removes
10424 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010425 */
James Smart2e0fef82007-06-17 19:56:36 -050010426 return retval;
dea31012005-04-17 16:05:31 -050010427}
10428
James Smarte59058c2008-08-24 21:49:00 -040010429/**
James Smart895427b2017-02-12 13:52:30 -080010430 * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
10431 * @phba: Pointer to HBA context object.
10432 * @pring: Pointer to driver SLI ring object.
10433 * @cmdiocb: Pointer to driver command iocb object.
10434 *
10435 * This function issues an abort iocb for the provided command iocb down to
10436 * the port. Other than the case the outstanding command iocb is an abort
10437 * request, this function issues abort out unconditionally. This function is
10438 * called with hbalock held. The function returns 0 when it fails due to
10439 * memory allocation failure or when the command iocb is an abort request.
10440 **/
10441static int
10442lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10443 struct lpfc_iocbq *cmdiocb)
10444{
10445 struct lpfc_vport *vport = cmdiocb->vport;
10446 struct lpfc_iocbq *abtsiocbp;
10447 union lpfc_wqe *abts_wqe;
10448 int retval;
10449
10450 /*
10451 * There are certain command types we don't want to abort. And we
10452 * don't want to abort commands that are already in the process of
10453 * being aborted.
10454 */
10455 if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
10456 cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
10457 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10458 return 0;
10459
10460 /* issue ABTS for this io based on iotag */
10461 abtsiocbp = __lpfc_sli_get_iocbq(phba);
10462 if (abtsiocbp == NULL)
10463 return 0;
10464
10465 /* This signals the response to set the correct status
10466 * before calling the completion handler
10467 */
10468 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10469
10470 /* Complete prepping the abort wqe and issue to the FW. */
10471 abts_wqe = &abtsiocbp->wqe;
10472 bf_set(abort_cmd_ia, &abts_wqe->abort_cmd, 0);
10473 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
10474
10475 /* Explicitly set reserved fields to zero.*/
10476 abts_wqe->abort_cmd.rsrvd4 = 0;
10477 abts_wqe->abort_cmd.rsrvd5 = 0;
10478
10479 /* WQE Common - word 6. Context is XRI tag. Set 0. */
10480 bf_set(wqe_xri_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10481 bf_set(wqe_ctxt_tag, &abts_wqe->abort_cmd.wqe_com, 0);
10482
10483 /* word 7 */
10484 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
10485 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
10486 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
10487 cmdiocb->iocb.ulpClass);
10488
10489 /* word 8 - tell the FW to abort the IO associated with this
10490 * outstanding exchange ID.
10491 */
10492 abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
10493
10494 /* word 9 - this is the iotag for the abts_wqe completion. */
10495 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
10496 abtsiocbp->iotag);
10497
10498 /* word 10 */
10499 bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, cmdiocb->hba_wqidx);
10500 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
10501 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
10502
10503 /* word 11 */
10504 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
10505 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
10506 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
10507
10508 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10509 abtsiocbp->iocb_flag |= LPFC_IO_NVME;
10510 abtsiocbp->vport = vport;
James Smart01649562017-02-12 13:52:32 -080010511 abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
James Smart895427b2017-02-12 13:52:30 -080010512 retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
10513 if (retval == IOCB_ERROR) {
10514 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10515 "6147 Failed abts issue_wqe with status x%x "
10516 "for oxid x%x\n",
10517 retval, cmdiocb->sli4_xritag);
10518 lpfc_sli_release_iocbq(phba, abtsiocbp);
10519 return retval;
10520 }
10521
10522 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
10523 "6148 Drv Abort NVME Request Issued for "
10524 "ox_id x%x on reqtag x%x\n",
10525 cmdiocb->sli4_xritag,
10526 abtsiocbp->iotag);
10527
10528 return retval;
10529}
10530
10531/**
James Smart5af5eee2010-10-22 11:06:38 -040010532 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10533 * @phba: pointer to lpfc HBA data structure.
10534 *
10535 * This routine will abort all pending and outstanding iocbs to an HBA.
10536 **/
10537void
10538lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10539{
10540 struct lpfc_sli *psli = &phba->sli;
10541 struct lpfc_sli_ring *pring;
James Smart895427b2017-02-12 13:52:30 -080010542 struct lpfc_queue *qp = NULL;
James Smart5af5eee2010-10-22 11:06:38 -040010543 int i;
10544
James Smart895427b2017-02-12 13:52:30 -080010545 if (phba->sli_rev != LPFC_SLI_REV4) {
10546 for (i = 0; i < psli->num_rings; i++) {
10547 pring = &psli->sli3_ring[i];
10548 lpfc_sli_abort_iocb_ring(phba, pring);
10549 }
10550 return;
10551 }
10552 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
10553 pring = qp->pring;
10554 if (!pring)
10555 continue;
James Smartdb55fba2014-04-04 13:52:02 -040010556 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010557 }
10558}
10559
10560/**
James Smart3621a712009-04-06 18:47:14 -040010561 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010562 * @iocbq: Pointer to driver iocb object.
10563 * @vport: Pointer to driver virtual port object.
10564 * @tgt_id: SCSI ID of the target.
10565 * @lun_id: LUN ID of the scsi device.
10566 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10567 *
James Smart3621a712009-04-06 18:47:14 -040010568 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010569 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10570 * 0 if the filtering criteria is met for the given iocb and will return
10571 * 1 if the filtering criteria is not met.
10572 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10573 * given iocb is for the SCSI device specified by vport, tgt_id and
10574 * lun_id parameter.
10575 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10576 * given iocb is for the SCSI target specified by vport and tgt_id
10577 * parameters.
10578 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10579 * given iocb is for the SCSI host associated with the given vport.
10580 * This function is called with no locks held.
10581 **/
dea31012005-04-17 16:05:31 -050010582static int
James Smart51ef4c22007-08-02 11:10:31 -040010583lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10584 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010585 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010586{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010587 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010588 int rc = 1;
10589
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010590 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10591 return rc;
10592
James Smart51ef4c22007-08-02 11:10:31 -040010593 if (iocbq->vport != vport)
10594 return rc;
10595
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010596 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010597
James Smart495a7142008-06-14 22:52:59 -040010598 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010599 return rc;
10600
10601 switch (ctx_cmd) {
10602 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010603 if ((lpfc_cmd->rdata->pnode) &&
10604 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10605 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010606 rc = 0;
10607 break;
10608 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010609 if ((lpfc_cmd->rdata->pnode) &&
10610 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010611 rc = 0;
10612 break;
dea31012005-04-17 16:05:31 -050010613 case LPFC_CTX_HOST:
10614 rc = 0;
10615 break;
10616 default:
10617 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010618 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010619 break;
10620 }
10621
10622 return rc;
10623}
10624
James Smarte59058c2008-08-24 21:49:00 -040010625/**
James Smart3621a712009-04-06 18:47:14 -040010626 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010627 * @vport: Pointer to virtual port.
10628 * @tgt_id: SCSI ID of the target.
10629 * @lun_id: LUN ID of the scsi device.
10630 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10631 *
10632 * This function returns number of FCP commands pending for the vport.
10633 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10634 * commands pending on the vport associated with SCSI device specified
10635 * by tgt_id and lun_id parameters.
10636 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10637 * commands pending on the vport associated with SCSI target specified
10638 * by tgt_id parameter.
10639 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10640 * commands pending on the vport.
10641 * This function returns the number of iocbs which satisfy the filter.
10642 * This function is called without any lock held.
10643 **/
dea31012005-04-17 16:05:31 -050010644int
James Smart51ef4c22007-08-02 11:10:31 -040010645lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10646 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010647{
James Smart51ef4c22007-08-02 11:10:31 -040010648 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010649 struct lpfc_iocbq *iocbq;
10650 int sum, i;
dea31012005-04-17 16:05:31 -050010651
Johannes Thumshirn31979002016-07-18 16:06:03 +020010652 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010653 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10654 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010655
James Smart51ef4c22007-08-02 11:10:31 -040010656 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10657 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010658 sum++;
dea31012005-04-17 16:05:31 -050010659 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010660 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010661
dea31012005-04-17 16:05:31 -050010662 return sum;
10663}
10664
James Smarte59058c2008-08-24 21:49:00 -040010665/**
James Smart3621a712009-04-06 18:47:14 -040010666 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010667 * @phba: Pointer to HBA context object
10668 * @cmdiocb: Pointer to command iocb object.
10669 * @rspiocb: Pointer to response iocb object.
10670 *
10671 * This function is called when an aborted FCP iocb completes. This
10672 * function is called by the ring event handler with no lock held.
10673 * This function frees the iocb.
10674 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010675void
James Smart2e0fef82007-06-17 19:56:36 -050010676lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10677 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010678{
James Smartcb69f7d2011-12-13 13:21:57 -050010679 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010680 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010681 "original iotag x%x, abort cmd iotag x%x "
10682 "status 0x%x, reason 0x%x\n",
10683 cmdiocb->iocb.un.acxri.abortContextTag,
10684 cmdiocb->iocb.un.acxri.abortIoTag,
10685 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10686 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010687 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010688 return;
10689}
10690
James Smarte59058c2008-08-24 21:49:00 -040010691/**
James Smart3621a712009-04-06 18:47:14 -040010692 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010693 * @vport: Pointer to virtual port.
10694 * @pring: Pointer to driver SLI ring object.
10695 * @tgt_id: SCSI ID of the target.
10696 * @lun_id: LUN ID of the scsi device.
10697 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10698 *
10699 * This function sends an abort command for every SCSI command
10700 * associated with the given virtual port pending on the ring
10701 * filtered by lpfc_sli_validate_fcp_iocb function.
10702 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10703 * FCP iocbs associated with lun specified by tgt_id and lun_id
10704 * parameters
10705 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10706 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10707 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10708 * FCP iocbs associated with virtual port.
10709 * This function returns number of iocbs it failed to abort.
10710 * This function is called with no locks held.
10711 **/
dea31012005-04-17 16:05:31 -050010712int
James Smart51ef4c22007-08-02 11:10:31 -040010713lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10714 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010715{
James Smart51ef4c22007-08-02 11:10:31 -040010716 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010717 struct lpfc_iocbq *iocbq;
10718 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010719 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010720 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010721 int i;
dea31012005-04-17 16:05:31 -050010722
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010723 for (i = 1; i <= phba->sli.last_iotag; i++) {
10724 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010725
James Smart51ef4c22007-08-02 11:10:31 -040010726 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010727 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010728 continue;
10729
James Smartafbd8d82013-09-06 12:22:13 -040010730 /*
10731 * If the iocbq is already being aborted, don't take a second
10732 * action, but do count it.
10733 */
10734 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10735 continue;
10736
dea31012005-04-17 16:05:31 -050010737 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010738 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010739 if (abtsiocb == NULL) {
10740 errcnt++;
10741 continue;
10742 }
dea31012005-04-17 16:05:31 -050010743
James Smartafbd8d82013-09-06 12:22:13 -040010744 /* indicate the IO is being aborted by the driver. */
10745 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10746
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010747 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010748 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10749 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010750 if (phba->sli_rev == LPFC_SLI_REV4)
10751 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10752 else
10753 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010754 abtsiocb->iocb.ulpLe = 1;
10755 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010756 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010757
James Smart5ffc2662009-11-18 15:39:44 -050010758 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010759 abtsiocb->hba_wqidx = iocbq->hba_wqidx;
James Smart341af102010-01-26 23:07:37 -050010760 if (iocbq->iocb_flag & LPFC_IO_FCP)
10761 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010762 if (iocbq->iocb_flag & LPFC_IO_FOF)
10763 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010764
James Smart2e0fef82007-06-17 19:56:36 -050010765 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010766 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10767 else
10768 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10769
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010770 /* Setup callback routine and issue the command. */
10771 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010772 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10773 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010774 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010775 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010776 errcnt++;
10777 continue;
10778 }
10779 }
10780
10781 return errcnt;
10782}
10783
James Smarte59058c2008-08-24 21:49:00 -040010784/**
James Smart98912dda2014-04-04 13:52:31 -040010785 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10786 * @vport: Pointer to virtual port.
10787 * @pring: Pointer to driver SLI ring object.
10788 * @tgt_id: SCSI ID of the target.
10789 * @lun_id: LUN ID of the scsi device.
10790 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10791 *
10792 * This function sends an abort command for every SCSI command
10793 * associated with the given virtual port pending on the ring
10794 * filtered by lpfc_sli_validate_fcp_iocb function.
10795 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10796 * FCP iocbs associated with lun specified by tgt_id and lun_id
10797 * parameters
10798 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10799 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10800 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10801 * FCP iocbs associated with virtual port.
10802 * This function returns number of iocbs it aborted .
10803 * This function is called with no locks held right after a taskmgmt
10804 * command is sent.
10805 **/
10806int
10807lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10808 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10809{
10810 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040010811 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040010812 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040010813 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040010814 struct lpfc_iocbq *iocbq;
10815 IOCB_t *icmd;
10816 int sum, i, ret_val;
10817 unsigned long iflags;
10818 struct lpfc_sli_ring *pring_s4;
James Smart98912dda2014-04-04 13:52:31 -040010819
10820 spin_lock_irq(&phba->hbalock);
10821
10822 /* all I/Os are in process of being flushed */
10823 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10824 spin_unlock_irq(&phba->hbalock);
10825 return 0;
10826 }
10827 sum = 0;
10828
10829 for (i = 1; i <= phba->sli.last_iotag; i++) {
10830 iocbq = phba->sli.iocbq_lookup[i];
10831
10832 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10833 cmd) != 0)
10834 continue;
10835
10836 /*
10837 * If the iocbq is already being aborted, don't take a second
10838 * action, but do count it.
10839 */
10840 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10841 continue;
10842
10843 /* issue ABTS for this IOCB based on iotag */
10844 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10845 if (abtsiocbq == NULL)
10846 continue;
10847
10848 icmd = &iocbq->iocb;
10849 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10850 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10851 if (phba->sli_rev == LPFC_SLI_REV4)
10852 abtsiocbq->iocb.un.acxri.abortIoTag =
10853 iocbq->sli4_xritag;
10854 else
10855 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10856 abtsiocbq->iocb.ulpLe = 1;
10857 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10858 abtsiocbq->vport = vport;
10859
10860 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
James Smart895427b2017-02-12 13:52:30 -080010861 abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
James Smart98912dda2014-04-04 13:52:31 -040010862 if (iocbq->iocb_flag & LPFC_IO_FCP)
10863 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010864 if (iocbq->iocb_flag & LPFC_IO_FOF)
10865 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040010866
James Smart8c50d252014-09-03 12:58:16 -040010867 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10868 ndlp = lpfc_cmd->rdata->pnode;
10869
10870 if (lpfc_is_link_up(phba) &&
10871 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040010872 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10873 else
10874 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10875
10876 /* Setup callback routine and issue the command. */
10877 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10878
10879 /*
10880 * Indicate the IO is being aborted by the driver and set
10881 * the caller's flag into the aborted IO.
10882 */
10883 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10884
10885 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart895427b2017-02-12 13:52:30 -080010886 pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
10887 if (pring_s4 == NULL)
10888 continue;
James Smart98912dda2014-04-04 13:52:31 -040010889 /* Note: both hbalock and ring_lock must be set here */
10890 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10891 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10892 abtsiocbq, 0);
10893 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10894 } else {
10895 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10896 abtsiocbq, 0);
10897 }
10898
10899
10900 if (ret_val == IOCB_ERROR)
10901 __lpfc_sli_release_iocbq(phba, abtsiocbq);
10902 else
10903 sum++;
10904 }
10905 spin_unlock_irq(&phba->hbalock);
10906 return sum;
10907}
10908
10909/**
James Smart3621a712009-04-06 18:47:14 -040010910 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040010911 * @phba: Pointer to HBA context object.
10912 * @cmdiocbq: Pointer to command iocb.
10913 * @rspiocbq: Pointer to response iocb.
10914 *
10915 * This function is the completion handler for iocbs issued using
10916 * lpfc_sli_issue_iocb_wait function. This function is called by the
10917 * ring event handler function without any lock held. This function
10918 * can be called from both worker thread context and interrupt
10919 * context. This function also can be called from other thread which
10920 * cleans up the SLI layer objects.
10921 * This function copy the contents of the response iocb to the
10922 * response iocb memory object provided by the caller of
10923 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10924 * sleeps for the iocb completion.
10925 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010926static void
10927lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10928 struct lpfc_iocbq *cmdiocbq,
10929 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050010930{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010931 wait_queue_head_t *pdone_q;
10932 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050010933 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010934
James Smart2e0fef82007-06-17 19:56:36 -050010935 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040010936 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10937
10938 /*
10939 * A time out has occurred for the iocb. If a time out
10940 * completion handler has been supplied, call it. Otherwise,
10941 * just free the iocbq.
10942 */
10943
10944 spin_unlock_irqrestore(&phba->hbalock, iflags);
10945 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10946 cmdiocbq->wait_iocb_cmpl = NULL;
10947 if (cmdiocbq->iocb_cmpl)
10948 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10949 else
10950 lpfc_sli_release_iocbq(phba, cmdiocbq);
10951 return;
10952 }
10953
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010954 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10955 if (cmdiocbq->context2 && rspiocbq)
10956 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10957 &rspiocbq->iocb, sizeof(IOCB_t));
10958
James Smart0f65ff62010-02-26 14:14:23 -050010959 /* Set the exchange busy flag for task management commands */
10960 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10961 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10962 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10963 cur_iocbq);
10964 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10965 }
10966
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010967 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010968 if (pdone_q)
10969 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050010970 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050010971 return;
10972}
10973
James Smarte59058c2008-08-24 21:49:00 -040010974/**
James Smartd11e31d2009-06-10 17:23:06 -040010975 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10976 * @phba: Pointer to HBA context object..
10977 * @piocbq: Pointer to command iocb.
10978 * @flag: Flag to test.
10979 *
10980 * This routine grabs the hbalock and then test the iocb_flag to
10981 * see if the passed in flag is set.
10982 * Returns:
10983 * 1 if flag is set.
10984 * 0 if flag is not set.
10985 **/
10986static int
10987lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10988 struct lpfc_iocbq *piocbq, uint32_t flag)
10989{
10990 unsigned long iflags;
10991 int ret;
10992
10993 spin_lock_irqsave(&phba->hbalock, iflags);
10994 ret = piocbq->iocb_flag & flag;
10995 spin_unlock_irqrestore(&phba->hbalock, iflags);
10996 return ret;
10997
10998}
10999
11000/**
James Smart3621a712009-04-06 18:47:14 -040011001 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040011002 * @phba: Pointer to HBA context object..
11003 * @pring: Pointer to sli ring.
11004 * @piocb: Pointer to command iocb.
11005 * @prspiocbq: Pointer to response iocb.
11006 * @timeout: Timeout in number of seconds.
11007 *
11008 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040011009 * iocb to complete. The iocb_cmpl field of the shall be used
11010 * to handle iocbs which time out. If the field is NULL, the
11011 * function shall free the iocbq structure. If more clean up is
11012 * needed, the caller is expected to provide a completion function
11013 * that will provide the needed clean up. If the iocb command is
11014 * not completed within timeout seconds, the function will either
11015 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
11016 * completion function set in the iocb_cmpl field and then return
11017 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
11018 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040011019 * The function waits for the iocb completion using an
11020 * non-interruptible wait.
11021 * This function will sleep while waiting for iocb completion.
11022 * So, this function should not be called from any context which
11023 * does not allow sleeping. Due to the same reason, this function
11024 * cannot be called with interrupt disabled.
11025 * This function assumes that the iocb completions occur while
11026 * this function sleep. So, this function cannot be called from
11027 * the thread which process iocb completion for this ring.
11028 * This function clears the iocb_flag of the iocb object before
11029 * issuing the iocb and the iocb completion handler sets this
11030 * flag and wakes this thread when the iocb completes.
11031 * The contents of the response iocb will be copied to prspiocbq
11032 * by the completion handler when the command completes.
11033 * This function returns IOCB_SUCCESS when success.
11034 * This function is called with no lock held.
11035 **/
dea31012005-04-17 16:05:31 -050011036int
James Smart2e0fef82007-06-17 19:56:36 -050011037lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040011038 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050011039 struct lpfc_iocbq *piocb,
11040 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011041 uint32_t timeout)
dea31012005-04-17 16:05:31 -050011042{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011043 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011044 long timeleft, timeout_req = 0;
11045 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011046 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050011047 struct lpfc_iocbq *iocb;
11048 int txq_cnt = 0;
11049 int txcmplq_cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080011050 struct lpfc_sli_ring *pring;
James Smart5a0916b2013-07-15 18:31:42 -040011051 unsigned long iflags;
11052 bool iocb_completed = true;
11053
James Smart895427b2017-02-12 13:52:30 -080011054 if (phba->sli_rev >= LPFC_SLI_REV4)
11055 pring = lpfc_sli4_calc_ring(phba, piocb);
11056 else
11057 pring = &phba->sli.sli3_ring[ring_number];
dea31012005-04-17 16:05:31 -050011058 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011059 * If the caller has provided a response iocbq buffer, then context2
11060 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050011061 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011062 if (prspiocbq) {
11063 if (piocb->context2)
11064 return IOCB_ERROR;
11065 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050011066 }
11067
James Smart5a0916b2013-07-15 18:31:42 -040011068 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011069 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
11070 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040011071 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050011072
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011073 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011074 if (lpfc_readl(phba->HCregaddr, &creg_val))
11075 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011076 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
11077 writel(creg_val, phba->HCregaddr);
11078 readl(phba->HCregaddr); /* flush */
11079 }
11080
James Smart2a9bf3d2010-06-07 15:24:45 -040011081 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
11082 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011083 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040011084 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011085 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040011086 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011087 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040011088 spin_lock_irqsave(&phba->hbalock, iflags);
11089 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050011090
James Smart5a0916b2013-07-15 18:31:42 -040011091 /*
11092 * IOCB timed out. Inform the wake iocb wait
11093 * completion function and set local status
11094 */
11095
11096 iocb_completed = false;
11097 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
11098 }
11099 spin_unlock_irqrestore(&phba->hbalock, iflags);
11100 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040011101 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011102 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040011103 /* Note: we are not indicating if the IOCB has a success
11104 * status or not - that's for the caller to check.
11105 * IOCB_SUCCESS means just that the command was sent and
11106 * completed. Not that it completed successfully.
11107 * */
James Smart7054a602007-04-25 09:52:34 -040011108 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011109 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011110 "0338 IOCB wait timeout error - no "
11111 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011112 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040011113 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011114 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011115 "0330 IOCB wake NOT set, "
11116 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011117 timeout, (timeleft / jiffies));
11118 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050011119 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011120 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050011121 if (phba->cfg_log_verbose & LOG_SLI) {
11122 list_for_each_entry(iocb, &pring->txq, list) {
11123 txq_cnt++;
11124 }
11125 list_for_each_entry(iocb, &pring->txcmplq, list) {
11126 txcmplq_cnt++;
11127 }
11128 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11129 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
11130 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
11131 }
James Smart2a9bf3d2010-06-07 15:24:45 -040011132 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011133 } else {
11134 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011135 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011136 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011137 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050011138 }
11139
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011140 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050011141 if (lpfc_readl(phba->HCregaddr, &creg_val))
11142 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050011143 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
11144 writel(creg_val, phba->HCregaddr);
11145 readl(phba->HCregaddr); /* flush */
11146 }
11147
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011148 if (prspiocbq)
11149 piocb->context2 = NULL;
11150
11151 piocb->context_un.wait_queue = NULL;
11152 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050011153 return retval;
11154}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040011155
James Smarte59058c2008-08-24 21:49:00 -040011156/**
James Smart3621a712009-04-06 18:47:14 -040011157 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040011158 * @phba: Pointer to HBA context object.
11159 * @pmboxq: Pointer to driver mailbox object.
11160 * @timeout: Timeout in number of seconds.
11161 *
11162 * This function issues the mailbox to firmware and waits for the
11163 * mailbox command to complete. If the mailbox command is not
11164 * completed within timeout seconds, it returns MBX_TIMEOUT.
11165 * The function waits for the mailbox completion using an
11166 * interruptible wait. If the thread is woken up due to a
11167 * signal, MBX_TIMEOUT error is returned to the caller. Caller
11168 * should not free the mailbox resources, if this function returns
11169 * MBX_TIMEOUT.
11170 * This function will sleep while waiting for mailbox completion.
11171 * So, this function should not be called from any context which
11172 * does not allow sleeping. Due to the same reason, this function
11173 * cannot be called with interrupt disabled.
11174 * This function assumes that the mailbox completion occurs while
11175 * this function sleep. So, this function cannot be called from
11176 * the worker thread which processes mailbox completion.
11177 * This function is called in the context of HBA management
11178 * applications.
11179 * This function returns MBX_SUCCESS when successful.
11180 * This function is called with no lock held.
11181 **/
dea31012005-04-17 16:05:31 -050011182int
James Smart2e0fef82007-06-17 19:56:36 -050011183lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050011184 uint32_t timeout)
11185{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080011186 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040011187 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050011188 int retval;
James Smart858c9f62007-06-17 19:56:39 -050011189 unsigned long flag;
dea31012005-04-17 16:05:31 -050011190
James Smartb230b8a2013-05-31 17:05:27 -040011191 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040011192 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040011193 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050011194
James Smart495a7142008-06-14 22:52:59 -040011195 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050011196 /* setup wake call as IOCB callback */
11197 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
11198 /* setup context field to pass wait_queue pointer to wake function */
11199 pmboxq->context1 = &done_q;
11200
dea31012005-04-17 16:05:31 -050011201 /* now issue the command */
11202 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050011203 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040011204 wait_event_interruptible_timeout(done_q,
11205 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040011206 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040011207
James Smart858c9f62007-06-17 19:56:39 -050011208 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011209 /* restore the possible extended buffer for free resource */
11210 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040011211 /*
11212 * if LPFC_MBX_WAKE flag is set the mailbox is completed
11213 * else do not free the resources.
11214 */
James Smartd7c47992010-06-08 18:31:54 -040011215 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050011216 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040011217 } else {
James Smart7054a602007-04-25 09:52:34 -040011218 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050011219 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11220 }
11221 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040011222 } else {
11223 /* restore the possible extended buffer for free resource */
11224 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050011225 }
11226
dea31012005-04-17 16:05:31 -050011227 return retval;
11228}
11229
James Smarte59058c2008-08-24 21:49:00 -040011230/**
James Smart3772a992009-05-22 14:50:54 -040011231 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040011232 * @phba: Pointer to HBA context.
11233 *
James Smart3772a992009-05-22 14:50:54 -040011234 * This function is called to shutdown the driver's mailbox sub-system.
11235 * It first marks the mailbox sub-system is in a block state to prevent
11236 * the asynchronous mailbox command from issued off the pending mailbox
11237 * command queue. If the mailbox command sub-system shutdown is due to
11238 * HBA error conditions such as EEH or ERATT, this routine shall invoke
11239 * the mailbox sub-system flush routine to forcefully bring down the
11240 * mailbox sub-system. Otherwise, if it is due to normal condition (such
11241 * as with offline or HBA function reset), this routine will wait for the
11242 * outstanding mailbox command to complete before invoking the mailbox
11243 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040011244 **/
James Smart3772a992009-05-22 14:50:54 -040011245void
James Smart618a5232012-06-12 13:54:36 -040011246lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040011247{
James Smart3772a992009-05-22 14:50:54 -040011248 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040011249 unsigned long timeout;
11250
James Smart618a5232012-06-12 13:54:36 -040011251 if (mbx_action == LPFC_MBX_NO_WAIT) {
11252 /* delay 100ms for port state */
11253 msleep(100);
11254 lpfc_sli_mbox_sys_flush(phba);
11255 return;
11256 }
James Smarta183a152011-10-10 21:32:43 -040011257 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050011258
James Smart3772a992009-05-22 14:50:54 -040011259 spin_lock_irq(&phba->hbalock);
11260 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040011261
11262 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040011263 /* Determine how long we might wait for the active mailbox
11264 * command to be gracefully completed by firmware.
11265 */
James Smarta183a152011-10-10 21:32:43 -040011266 if (phba->sli.mbox_active)
11267 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
11268 phba->sli.mbox_active) *
11269 1000) + jiffies;
11270 spin_unlock_irq(&phba->hbalock);
11271
James Smart3772a992009-05-22 14:50:54 -040011272 while (phba->sli.mbox_active) {
11273 /* Check active mailbox complete status every 2ms */
11274 msleep(2);
11275 if (time_after(jiffies, timeout))
11276 /* Timeout, let the mailbox flush routine to
11277 * forcefully release active mailbox command
11278 */
11279 break;
11280 }
James Smartd7069f02012-03-01 22:36:29 -050011281 } else
11282 spin_unlock_irq(&phba->hbalock);
11283
James Smart3772a992009-05-22 14:50:54 -040011284 lpfc_sli_mbox_sys_flush(phba);
11285}
11286
11287/**
11288 * lpfc_sli_eratt_read - read sli-3 error attention events
11289 * @phba: Pointer to HBA context.
11290 *
11291 * This function is called to read the SLI3 device error attention registers
11292 * for possible error attention events. The caller must hold the hostlock
11293 * with spin_lock_irq().
11294 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011295 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040011296 * Register and returns 0 otherwise.
11297 **/
11298static int
11299lpfc_sli_eratt_read(struct lpfc_hba *phba)
11300{
James Smarted957682007-06-17 19:56:37 -050011301 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040011302
James Smart3772a992009-05-22 14:50:54 -040011303 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050011304 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11305 goto unplug_err;
11306
James Smart3772a992009-05-22 14:50:54 -040011307 if (ha_copy & HA_ERATT) {
11308 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050011309 if (lpfc_sli_read_hs(phba))
11310 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040011311
James Smart3772a992009-05-22 14:50:54 -040011312 /* Check if there is a deferred error condition is active */
11313 if ((HS_FFER1 & phba->work_hs) &&
11314 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011315 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040011316 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040011317 /* Clear all interrupt enable conditions */
11318 writel(0, phba->HCregaddr);
11319 readl(phba->HCregaddr);
11320 }
11321
11322 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040011323 phba->work_ha |= HA_ERATT;
11324 /* Indicate polling handles this ERATT */
11325 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011326 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011327 }
James Smart3772a992009-05-22 14:50:54 -040011328 return 0;
James Smart9940b972011-03-11 16:06:12 -050011329
11330unplug_err:
11331 /* Set the driver HS work bitmap */
11332 phba->work_hs |= UNPLUG_ERR;
11333 /* Set the driver HA work bitmap */
11334 phba->work_ha |= HA_ERATT;
11335 /* Indicate polling handles this ERATT */
11336 phba->hba_flag |= HBA_ERATT_HANDLED;
11337 return 1;
James Smartb4c02652006-07-06 15:50:43 -040011338}
11339
James Smarte59058c2008-08-24 21:49:00 -040011340/**
James Smartda0436e2009-05-22 14:51:39 -040011341 * lpfc_sli4_eratt_read - read sli-4 error attention events
11342 * @phba: Pointer to HBA context.
11343 *
11344 * This function is called to read the SLI4 device error attention registers
11345 * for possible error attention events. The caller must hold the hostlock
11346 * with spin_lock_irq().
11347 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011348 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040011349 * Register and returns 0 otherwise.
11350 **/
11351static int
11352lpfc_sli4_eratt_read(struct lpfc_hba *phba)
11353{
11354 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050011355 uint32_t if_type, portsmphr;
11356 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040011357
James Smart2fcee4b2010-12-15 17:57:46 -050011358 /*
11359 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040011360 * registers for error attention. This can be changed later.
11361 */
James Smart2fcee4b2010-12-15 17:57:46 -050011362 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
11363 switch (if_type) {
11364 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050011365 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
11366 &uerr_sta_lo) ||
11367 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
11368 &uerr_sta_hi)) {
11369 phba->work_hs |= UNPLUG_ERR;
11370 phba->work_ha |= HA_ERATT;
11371 phba->hba_flag |= HBA_ERATT_HANDLED;
11372 return 1;
11373 }
James Smart2fcee4b2010-12-15 17:57:46 -050011374 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
11375 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
11376 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11377 "1423 HBA Unrecoverable error: "
11378 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
11379 "ue_mask_lo_reg=0x%x, "
11380 "ue_mask_hi_reg=0x%x\n",
11381 uerr_sta_lo, uerr_sta_hi,
11382 phba->sli4_hba.ue_mask_lo,
11383 phba->sli4_hba.ue_mask_hi);
11384 phba->work_status[0] = uerr_sta_lo;
11385 phba->work_status[1] = uerr_sta_hi;
11386 phba->work_ha |= HA_ERATT;
11387 phba->hba_flag |= HBA_ERATT_HANDLED;
11388 return 1;
11389 }
11390 break;
11391 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050011392 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
11393 &portstat_reg.word0) ||
11394 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
11395 &portsmphr)){
11396 phba->work_hs |= UNPLUG_ERR;
11397 phba->work_ha |= HA_ERATT;
11398 phba->hba_flag |= HBA_ERATT_HANDLED;
11399 return 1;
11400 }
James Smart2fcee4b2010-12-15 17:57:46 -050011401 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
11402 phba->work_status[0] =
11403 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
11404 phba->work_status[1] =
11405 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
11406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011407 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011408 "port status reg 0x%x, "
11409 "port smphr reg 0x%x, "
11410 "error 1=0x%x, error 2=0x%x\n",
11411 portstat_reg.word0,
11412 portsmphr,
11413 phba->work_status[0],
11414 phba->work_status[1]);
11415 phba->work_ha |= HA_ERATT;
11416 phba->hba_flag |= HBA_ERATT_HANDLED;
11417 return 1;
11418 }
11419 break;
11420 case LPFC_SLI_INTF_IF_TYPE_1:
11421 default:
James Smarta747c9c2009-11-18 15:41:10 -050011422 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011423 "2886 HBA Error Attention on unsupported "
11424 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011425 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011426 }
James Smart2fcee4b2010-12-15 17:57:46 -050011427
James Smartda0436e2009-05-22 14:51:39 -040011428 return 0;
11429}
11430
11431/**
James Smart3621a712009-04-06 18:47:14 -040011432 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011433 * @phba: Pointer to HBA context.
11434 *
James Smart3772a992009-05-22 14:50:54 -040011435 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011436 * error attention register bit for error attention events.
11437 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011438 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011439 * Register and returns 0 otherwise.
11440 **/
11441int
11442lpfc_sli_check_eratt(struct lpfc_hba *phba)
11443{
11444 uint32_t ha_copy;
11445
11446 /* If somebody is waiting to handle an eratt, don't process it
11447 * here. The brdkill function will do this.
11448 */
11449 if (phba->link_flag & LS_IGNORE_ERATT)
11450 return 0;
11451
11452 /* Check if interrupt handler handles this ERATT */
11453 spin_lock_irq(&phba->hbalock);
11454 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11455 /* Interrupt handler has handled ERATT */
11456 spin_unlock_irq(&phba->hbalock);
11457 return 0;
11458 }
11459
James Smarta257bf92009-04-06 18:48:10 -040011460 /*
11461 * If there is deferred error attention, do not check for error
11462 * attention
11463 */
11464 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11465 spin_unlock_irq(&phba->hbalock);
11466 return 0;
11467 }
11468
James Smart3772a992009-05-22 14:50:54 -040011469 /* If PCI channel is offline, don't process it */
11470 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011471 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011472 return 0;
11473 }
11474
11475 switch (phba->sli_rev) {
11476 case LPFC_SLI_REV2:
11477 case LPFC_SLI_REV3:
11478 /* Read chip Host Attention (HA) register */
11479 ha_copy = lpfc_sli_eratt_read(phba);
11480 break;
James Smartda0436e2009-05-22 14:51:39 -040011481 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011482 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011483 ha_copy = lpfc_sli4_eratt_read(phba);
11484 break;
James Smart3772a992009-05-22 14:50:54 -040011485 default:
11486 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11487 "0299 Invalid SLI revision (%d)\n",
11488 phba->sli_rev);
11489 ha_copy = 0;
11490 break;
James Smart93996272008-08-24 21:50:30 -040011491 }
11492 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011493
11494 return ha_copy;
11495}
11496
11497/**
11498 * lpfc_intr_state_check - Check device state for interrupt handling
11499 * @phba: Pointer to HBA context.
11500 *
11501 * This inline routine checks whether a device or its PCI slot is in a state
11502 * that the interrupt should be handled.
11503 *
11504 * This function returns 0 if the device or the PCI slot is in a state that
11505 * interrupt should be handled, otherwise -EIO.
11506 */
11507static inline int
11508lpfc_intr_state_check(struct lpfc_hba *phba)
11509{
11510 /* If the pci channel is offline, ignore all the interrupts */
11511 if (unlikely(pci_channel_offline(phba->pcidev)))
11512 return -EIO;
11513
11514 /* Update device level interrupt statistics */
11515 phba->sli.slistat.sli_intr++;
11516
11517 /* Ignore all interrupts during initialization. */
11518 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11519 return -EIO;
11520
James Smart93996272008-08-24 21:50:30 -040011521 return 0;
11522}
11523
11524/**
James Smart3772a992009-05-22 14:50:54 -040011525 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011526 * @irq: Interrupt number.
11527 * @dev_id: The device context pointer.
11528 *
James Smart93996272008-08-24 21:50:30 -040011529 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011530 * service routine when device with SLI-3 interface spec is enabled with
11531 * MSI-X multi-message interrupt mode and there are slow-path events in
11532 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11533 * interrupt mode, this function is called as part of the device-level
11534 * interrupt handler. When the PCI slot is in error recovery or the HBA
11535 * is undergoing initialization, the interrupt handler will not process
11536 * the interrupt. The link attention and ELS ring attention events are
11537 * handled by the worker thread. The interrupt handler signals the worker
11538 * thread and returns for these events. This function is called without
11539 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011540 * structures.
11541 *
11542 * This function returns IRQ_HANDLED when interrupt is handled else it
11543 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011544 **/
dea31012005-04-17 16:05:31 -050011545irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011546lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011547{
James Smart2e0fef82007-06-17 19:56:36 -050011548 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011549 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011550 uint32_t work_ha_copy;
11551 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011552 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011553 uint32_t control;
11554
James Smart92d7f7b2007-06-17 19:56:38 -050011555 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011556 struct lpfc_vport *vport;
11557 struct lpfc_nodelist *ndlp;
11558 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011559 LPFC_MBOXQ_t *pmb;
11560 int rc;
11561
dea31012005-04-17 16:05:31 -050011562 /*
11563 * Get the driver's phba structure from the dev_id and
11564 * assume the HBA is not interrupting.
11565 */
James Smart93996272008-08-24 21:50:30 -040011566 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011567
11568 if (unlikely(!phba))
11569 return IRQ_NONE;
11570
dea31012005-04-17 16:05:31 -050011571 /*
James Smart93996272008-08-24 21:50:30 -040011572 * Stuff needs to be attented to when this function is invoked as an
11573 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011574 */
James Smart93996272008-08-24 21:50:30 -040011575 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011576 /* Check device state for handling interrupt */
11577 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011578 return IRQ_NONE;
11579 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011580 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011581 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11582 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011583 /* If somebody is waiting to handle an eratt don't process it
11584 * here. The brdkill function will do this.
11585 */
11586 if (phba->link_flag & LS_IGNORE_ERATT)
11587 ha_copy &= ~HA_ERATT;
11588 /* Check the need for handling ERATT in interrupt handler */
11589 if (ha_copy & HA_ERATT) {
11590 if (phba->hba_flag & HBA_ERATT_HANDLED)
11591 /* ERATT polling has handled ERATT */
11592 ha_copy &= ~HA_ERATT;
11593 else
11594 /* Indicate interrupt handler handles ERATT */
11595 phba->hba_flag |= HBA_ERATT_HANDLED;
11596 }
James Smarta257bf92009-04-06 18:48:10 -040011597
11598 /*
11599 * If there is deferred error attention, do not check for any
11600 * interrupt.
11601 */
11602 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011603 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011604 return IRQ_NONE;
11605 }
11606
James Smart93996272008-08-24 21:50:30 -040011607 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011608 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11609 goto unplug_error;
11610
James Smarta747c9c2009-11-18 15:41:10 -050011611 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11612 HC_LAINT_ENA | HC_ERINT_ENA),
11613 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011614 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11615 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011616 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011617 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011618 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011619 } else
11620 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011621
dea31012005-04-17 16:05:31 -050011622 work_ha_copy = ha_copy & phba->work_ha_mask;
11623
James Smart93996272008-08-24 21:50:30 -040011624 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011625 if (work_ha_copy & HA_LATT) {
11626 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11627 /*
11628 * Turn off Link Attention interrupts
11629 * until CLEAR_LA done
11630 */
James Smart5b75da22008-12-04 22:39:35 -050011631 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011632 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011633 if (lpfc_readl(phba->HCregaddr, &control))
11634 goto unplug_error;
dea31012005-04-17 16:05:31 -050011635 control &= ~HC_LAINT_ENA;
11636 writel(control, phba->HCregaddr);
11637 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011638 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011639 }
11640 else
11641 work_ha_copy &= ~HA_LATT;
11642 }
11643
James Smart93996272008-08-24 21:50:30 -040011644 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011645 /*
11646 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11647 * the only slow ring.
11648 */
11649 status = (work_ha_copy &
11650 (HA_RXMASK << (4*LPFC_ELS_RING)));
11651 status >>= (4*LPFC_ELS_RING);
11652 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011653 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011654 if (lpfc_readl(phba->HCregaddr, &control))
11655 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011656
11657 lpfc_debugfs_slow_ring_trc(phba,
11658 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11659 control, status,
11660 (uint32_t)phba->sli.slistat.sli_intr);
11661
James Smart858c9f62007-06-17 19:56:39 -050011662 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011663 lpfc_debugfs_slow_ring_trc(phba,
11664 "ISR Disable ring:"
11665 "pwork: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 Smart858c9f62007-06-17 19:56:39 -050011670 control &=
11671 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011672 writel(control, phba->HCregaddr);
11673 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011674 }
James Smarta58cbd52007-08-02 11:09:43 -040011675 else {
11676 lpfc_debugfs_slow_ring_trc(phba,
11677 "ISR slow ring: pwork:"
11678 "x%x hawork:x%x wait:x%x",
11679 phba->work_ha, work_ha_copy,
11680 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011681 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011682 }
James Smart5b75da22008-12-04 22:39:35 -050011683 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011684 }
11685 }
James Smart5b75da22008-12-04 22:39:35 -050011686 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011687 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011688 if (lpfc_sli_read_hs(phba))
11689 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011690 /*
11691 * Check if there is a deferred error condition
11692 * is active
11693 */
11694 if ((HS_FFER1 & phba->work_hs) &&
11695 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011696 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11697 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011698 phba->hba_flag |= DEFER_ERATT;
11699 /* Clear all interrupt enable conditions */
11700 writel(0, phba->HCregaddr);
11701 readl(phba->HCregaddr);
11702 }
11703 }
11704
James Smart93996272008-08-24 21:50:30 -040011705 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011706 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011707 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011708 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011709 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011710
11711 /* First check out the status word */
11712 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11713 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011714 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011715 /*
11716 * Stray Mailbox Interrupt, mbxCommand <cmd>
11717 * mbxStatus <status>
11718 */
James Smart09372822008-01-11 01:52:54 -050011719 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011720 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011721 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011722 "Interrupt mbxCommand x%x "
11723 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011724 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011725 pmbox->mbxCommand,
11726 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011727 /* clear mailbox attention bit */
11728 work_ha_copy &= ~HA_MBATT;
11729 } else {
James Smart97eab632008-04-07 10:16:05 -040011730 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011731 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011732 phba->last_completion_time = jiffies;
11733 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011734 if (pmb->mbox_cmpl) {
11735 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11736 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011737 if (pmb->out_ext_byte_len &&
11738 pmb->context2)
11739 lpfc_sli_pcimem_bcopy(
11740 phba->mbox_ext,
11741 pmb->context2,
11742 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011743 }
James Smart09372822008-01-11 01:52:54 -050011744 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11745 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11746
11747 lpfc_debugfs_disc_trc(vport,
11748 LPFC_DISC_TRC_MBOX_VPORT,
11749 "MBOX dflt rpi: : "
11750 "status:x%x rpi:x%x",
11751 (uint32_t)pmbox->mbxStatus,
11752 pmbox->un.varWords[0], 0);
11753
11754 if (!pmbox->mbxStatus) {
11755 mp = (struct lpfc_dmabuf *)
11756 (pmb->context1);
11757 ndlp = (struct lpfc_nodelist *)
11758 pmb->context2;
11759
11760 /* Reg_LOGIN of dflt RPI was
11761 * successful. new lets get
11762 * rid of the RPI using the
11763 * same mbox buffer.
11764 */
11765 lpfc_unreg_login(phba,
11766 vport->vpi,
11767 pmbox->un.varWords[0],
11768 pmb);
11769 pmb->mbox_cmpl =
11770 lpfc_mbx_cmpl_dflt_rpi;
11771 pmb->context1 = mp;
11772 pmb->context2 = ndlp;
11773 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040011774 rc = lpfc_sli_issue_mbox(phba,
11775 pmb,
11776 MBX_NOWAIT);
11777 if (rc != MBX_BUSY)
11778 lpfc_printf_log(phba,
11779 KERN_ERR,
11780 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011781 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040011782 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040011783 if (rc != MBX_NOT_FINISHED)
11784 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050011785 }
11786 }
James Smart5b75da22008-12-04 22:39:35 -050011787 spin_lock_irqsave(
11788 &phba->pport->work_port_lock,
11789 iflag);
James Smart09372822008-01-11 01:52:54 -050011790 phba->pport->work_port_events &=
11791 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050011792 spin_unlock_irqrestore(
11793 &phba->pport->work_port_lock,
11794 iflag);
James Smart09372822008-01-11 01:52:54 -050011795 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050011796 }
James Smart97eab632008-04-07 10:16:05 -040011797 } else
James Smart5b75da22008-12-04 22:39:35 -050011798 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011799
James Smart92d7f7b2007-06-17 19:56:38 -050011800 if ((work_ha_copy & HA_MBATT) &&
11801 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050011802send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050011803 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040011804 do {
11805 rc = lpfc_sli_issue_mbox(phba, NULL,
11806 MBX_NOWAIT);
11807 } while (rc == MBX_NOT_FINISHED);
11808 if (rc != MBX_SUCCESS)
11809 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11810 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040011811 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050011812 }
11813
James Smart5b75da22008-12-04 22:39:35 -050011814 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011815 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050011816 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040011817 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050011818 }
James Smart93996272008-08-24 21:50:30 -040011819 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050011820unplug_error:
11821 spin_unlock_irqrestore(&phba->hbalock, iflag);
11822 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050011823
James Smart3772a992009-05-22 14:50:54 -040011824} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040011825
11826/**
James Smart3772a992009-05-22 14:50:54 -040011827 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040011828 * @irq: Interrupt number.
11829 * @dev_id: The device context pointer.
11830 *
11831 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011832 * service routine when device with SLI-3 interface spec is enabled with
11833 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11834 * ring event in the HBA. However, when the device is enabled with either
11835 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11836 * device-level interrupt handler. When the PCI slot is in error recovery
11837 * or the HBA is undergoing initialization, the interrupt handler will not
11838 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11839 * the intrrupt context. This function is called without any lock held.
11840 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011841 *
11842 * This function returns IRQ_HANDLED when interrupt is handled else it
11843 * returns IRQ_NONE.
11844 **/
11845irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011846lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011847{
11848 struct lpfc_hba *phba;
11849 uint32_t ha_copy;
11850 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011851 unsigned long iflag;
James Smart895427b2017-02-12 13:52:30 -080011852 struct lpfc_sli_ring *pring;
James Smart93996272008-08-24 21:50:30 -040011853
11854 /* Get the driver's phba structure from the dev_id and
11855 * assume the HBA is not interrupting.
11856 */
11857 phba = (struct lpfc_hba *) dev_id;
11858
11859 if (unlikely(!phba))
11860 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050011861
11862 /*
James Smart93996272008-08-24 21:50:30 -040011863 * Stuff needs to be attented to when this function is invoked as an
11864 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011865 */
James Smart93996272008-08-24 21:50:30 -040011866 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011867 /* Check device state for handling interrupt */
11868 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011869 return IRQ_NONE;
11870 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050011871 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11872 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040011873 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050011874 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011875 /*
11876 * If there is deferred error attention, do not check for
11877 * any interrupt.
11878 */
11879 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011880 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011881 return IRQ_NONE;
11882 }
James Smart93996272008-08-24 21:50:30 -040011883 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11884 phba->HAregaddr);
11885 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011886 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011887 } else
11888 ha_copy = phba->ha_copy;
11889
11890 /*
11891 * Process all events on FCP ring. Take the optimized path for FCP IO.
11892 */
11893 ha_copy &= ~(phba->work_ha_mask);
11894
11895 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050011896 status >>= (4*LPFC_FCP_RING);
James Smart895427b2017-02-12 13:52:30 -080011897 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
James Smart858c9f62007-06-17 19:56:39 -050011898 if (status & HA_RXMASK)
James Smart895427b2017-02-12 13:52:30 -080011899 lpfc_sli_handle_fast_ring_event(phba, pring, status);
James Smarta4bc3372006-12-02 13:34:16 -050011900
11901 if (phba->cfg_multi_ring_support == 2) {
11902 /*
James Smart93996272008-08-24 21:50:30 -040011903 * Process all events on extra ring. Take the optimized path
11904 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050011905 */
James Smart93996272008-08-24 21:50:30 -040011906 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050011907 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050011908 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050011909 lpfc_sli_handle_fast_ring_event(phba,
James Smart895427b2017-02-12 13:52:30 -080011910 &phba->sli.sli3_ring[LPFC_EXTRA_RING],
James Smarta4bc3372006-12-02 13:34:16 -050011911 status);
11912 }
11913 }
dea31012005-04-17 16:05:31 -050011914 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011915} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050011916
James Smart93996272008-08-24 21:50:30 -040011917/**
James Smart3772a992009-05-22 14:50:54 -040011918 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040011919 * @irq: Interrupt number.
11920 * @dev_id: The device context pointer.
11921 *
James Smart3772a992009-05-22 14:50:54 -040011922 * This function is the HBA device-level interrupt handler to device with
11923 * SLI-3 interface spec, called from the PCI layer when either MSI or
11924 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11925 * requires driver attention. This function invokes the slow-path interrupt
11926 * attention handling function and fast-path interrupt attention handling
11927 * function in turn to process the relevant HBA attention events. This
11928 * function is called without any lock held. It gets the hbalock to access
11929 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011930 *
11931 * This function returns IRQ_HANDLED when interrupt is handled, else it
11932 * returns IRQ_NONE.
11933 **/
11934irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011935lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011936{
11937 struct lpfc_hba *phba;
11938 irqreturn_t sp_irq_rc, fp_irq_rc;
11939 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050011940 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040011941
11942 /*
11943 * Get the driver's phba structure from the dev_id and
11944 * assume the HBA is not interrupting.
11945 */
11946 phba = (struct lpfc_hba *) dev_id;
11947
11948 if (unlikely(!phba))
11949 return IRQ_NONE;
11950
James Smart3772a992009-05-22 14:50:54 -040011951 /* Check device state for handling interrupt */
11952 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011953 return IRQ_NONE;
11954
11955 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050011956 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11957 spin_unlock(&phba->hbalock);
11958 return IRQ_HANDLED;
11959 }
11960
James Smart93996272008-08-24 21:50:30 -040011961 if (unlikely(!phba->ha_copy)) {
11962 spin_unlock(&phba->hbalock);
11963 return IRQ_NONE;
11964 } else if (phba->ha_copy & HA_ERATT) {
11965 if (phba->hba_flag & HBA_ERATT_HANDLED)
11966 /* ERATT polling has handled ERATT */
11967 phba->ha_copy &= ~HA_ERATT;
11968 else
11969 /* Indicate interrupt handler handles ERATT */
11970 phba->hba_flag |= HBA_ERATT_HANDLED;
11971 }
11972
James Smarta257bf92009-04-06 18:48:10 -040011973 /*
11974 * If there is deferred error attention, do not check for any interrupt.
11975 */
11976 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020011977 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040011978 return IRQ_NONE;
11979 }
11980
James Smart93996272008-08-24 21:50:30 -040011981 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050011982 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11983 spin_unlock(&phba->hbalock);
11984 return IRQ_HANDLED;
11985 }
James Smarta747c9c2009-11-18 15:41:10 -050011986 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11987 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11988 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011989 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011990 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011991 readl(phba->HAregaddr); /* flush */
11992 spin_unlock(&phba->hbalock);
11993
11994 /*
11995 * Invokes slow-path host attention interrupt handling as appropriate.
11996 */
11997
11998 /* status of events with mailbox and link attention */
11999 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
12000
12001 /* status of events with ELS ring */
12002 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
12003 status2 >>= (4*LPFC_ELS_RING);
12004
12005 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012006 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012007 else
12008 sp_irq_rc = IRQ_NONE;
12009
12010 /*
12011 * Invoke fast-path host attention interrupt handling as appropriate.
12012 */
12013
12014 /* status of events with FCP ring */
12015 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
12016 status1 >>= (4*LPFC_FCP_RING);
12017
12018 /* status of events with extra ring */
12019 if (phba->cfg_multi_ring_support == 2) {
12020 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
12021 status2 >>= (4*LPFC_EXTRA_RING);
12022 } else
12023 status2 = 0;
12024
12025 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040012026 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040012027 else
12028 fp_irq_rc = IRQ_NONE;
12029
12030 /* Return device-level interrupt handling status */
12031 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040012032} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040012033
12034/**
12035 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
12036 * @phba: pointer to lpfc hba data structure.
12037 *
12038 * This routine is invoked by the worker thread to process all the pending
12039 * SLI4 FCP abort XRI events.
12040 **/
12041void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
12042{
12043 struct lpfc_cq_event *cq_event;
12044
12045 /* First, declare the fcp xri abort event has been handled */
12046 spin_lock_irq(&phba->hbalock);
12047 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
12048 spin_unlock_irq(&phba->hbalock);
12049 /* Now, handle all the fcp xri abort events */
12050 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
12051 /* Get the first event from the head of the event queue */
12052 spin_lock_irq(&phba->hbalock);
12053 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
12054 cq_event, struct lpfc_cq_event, list);
12055 spin_unlock_irq(&phba->hbalock);
12056 /* Notify aborted XRI for FCP work queue */
12057 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12058 /* Free the event processed back to the free pool */
12059 lpfc_sli4_cq_event_release(phba, cq_event);
12060 }
12061}
12062
12063/**
12064 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
12065 * @phba: pointer to lpfc hba data structure.
12066 *
12067 * This routine is invoked by the worker thread to process all the pending
12068 * SLI4 els abort xri events.
12069 **/
12070void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
12071{
12072 struct lpfc_cq_event *cq_event;
12073
12074 /* First, declare the els xri abort event has been handled */
12075 spin_lock_irq(&phba->hbalock);
12076 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
12077 spin_unlock_irq(&phba->hbalock);
12078 /* Now, handle all the els xri abort events */
12079 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
12080 /* Get the first event from the head of the event queue */
12081 spin_lock_irq(&phba->hbalock);
12082 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
12083 cq_event, struct lpfc_cq_event, list);
12084 spin_unlock_irq(&phba->hbalock);
12085 /* Notify aborted XRI for ELS work queue */
12086 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
12087 /* Free the event processed back to the free pool */
12088 lpfc_sli4_cq_event_release(phba, cq_event);
12089 }
12090}
12091
James Smart341af102010-01-26 23:07:37 -050012092/**
12093 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
12094 * @phba: pointer to lpfc hba data structure
12095 * @pIocbIn: pointer to the rspiocbq
12096 * @pIocbOut: pointer to the cmdiocbq
12097 * @wcqe: pointer to the complete wcqe
12098 *
12099 * This routine transfers the fields of a command iocbq to a response iocbq
12100 * by copying all the IOCB fields from command iocbq and transferring the
12101 * completion status information from the complete wcqe.
12102 **/
James Smart4f774512009-05-22 14:52:35 -040012103static void
James Smart341af102010-01-26 23:07:37 -050012104lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
12105 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040012106 struct lpfc_iocbq *pIocbOut,
12107 struct lpfc_wcqe_complete *wcqe)
12108{
James Smartaf227412013-10-10 12:23:10 -040012109 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050012110 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040012111 uint32_t status, max_response;
12112 struct lpfc_dmabuf *dmabuf;
12113 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040012114 size_t offset = offsetof(struct lpfc_iocbq, iocb);
12115
12116 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
12117 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040012118 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050012119 status = bf_get(lpfc_wcqe_c_status, wcqe);
12120 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040012121 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
12122 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
12123 pIocbIn->iocb.un.fcpi.fcpi_parm =
12124 pIocbOut->iocb.un.fcpi.fcpi_parm -
12125 wcqe->total_data_placed;
12126 else
12127 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050012128 else {
James Smart4f774512009-05-22 14:52:35 -040012129 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040012130 switch (pIocbOut->iocb.ulpCommand) {
12131 case CMD_ELS_REQUEST64_CR:
12132 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12133 bpl = (struct ulp_bde64 *)dmabuf->virt;
12134 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
12135 max_response = bde.tus.f.bdeSize;
12136 break;
12137 case CMD_GEN_REQUEST64_CR:
12138 max_response = 0;
12139 if (!pIocbOut->context3)
12140 break;
12141 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
12142 sizeof(struct ulp_bde64);
12143 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
12144 bpl = (struct ulp_bde64 *)dmabuf->virt;
12145 for (i = 0; i < numBdes; i++) {
12146 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
12147 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
12148 max_response += bde.tus.f.bdeSize;
12149 }
12150 break;
12151 default:
12152 max_response = wcqe->total_data_placed;
12153 break;
12154 }
12155 if (max_response < wcqe->total_data_placed)
12156 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
12157 else
12158 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
12159 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050012160 }
James Smart341af102010-01-26 23:07:37 -050012161
James Smartacd68592012-01-18 16:25:09 -050012162 /* Convert BG errors for completion status */
12163 if (status == CQE_STATUS_DI_ERROR) {
12164 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
12165
12166 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
12167 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
12168 else
12169 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
12170
12171 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
12172 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
12173 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12174 BGS_GUARD_ERR_MASK;
12175 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
12176 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12177 BGS_APPTAG_ERR_MASK;
12178 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
12179 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12180 BGS_REFTAG_ERR_MASK;
12181
12182 /* Check to see if there was any good data before the error */
12183 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
12184 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12185 BGS_HI_WATER_MARK_PRESENT_MASK;
12186 pIocbIn->iocb.unsli3.sli3_bg.bghm =
12187 wcqe->total_data_placed;
12188 }
12189
12190 /*
12191 * Set ALL the error bits to indicate we don't know what
12192 * type of error it is.
12193 */
12194 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
12195 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
12196 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
12197 BGS_GUARD_ERR_MASK);
12198 }
12199
James Smart341af102010-01-26 23:07:37 -050012200 /* Pick up HBA exchange busy condition */
12201 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
12202 spin_lock_irqsave(&phba->hbalock, iflags);
12203 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
12204 spin_unlock_irqrestore(&phba->hbalock, iflags);
12205 }
James Smart4f774512009-05-22 14:52:35 -040012206}
12207
12208/**
James Smart45ed1192009-10-02 15:17:02 -040012209 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
12210 * @phba: Pointer to HBA context object.
12211 * @wcqe: Pointer to work-queue completion queue entry.
12212 *
12213 * This routine handles an ELS work-queue completion event and construct
12214 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
12215 * discovery engine to handle.
12216 *
12217 * Return: Pointer to the receive IOCBQ, NULL otherwise.
12218 **/
12219static struct lpfc_iocbq *
12220lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
12221 struct lpfc_iocbq *irspiocbq)
12222{
James Smart895427b2017-02-12 13:52:30 -080012223 struct lpfc_sli_ring *pring;
James Smart45ed1192009-10-02 15:17:02 -040012224 struct lpfc_iocbq *cmdiocbq;
12225 struct lpfc_wcqe_complete *wcqe;
12226 unsigned long iflags;
12227
James Smart895427b2017-02-12 13:52:30 -080012228 pring = lpfc_phba_elsring(phba);
12229
James Smart45ed1192009-10-02 15:17:02 -040012230 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040012231 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012232 pring->stats.iocb_event++;
12233 /* Look up the ELS command IOCB and create pseudo response IOCB */
12234 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12235 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart89533e92016-10-13 15:06:15 -070012236 /* Put the iocb back on the txcmplq */
12237 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
James Smart7e56aa22012-08-03 12:35:34 -040012238 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040012239
12240 if (unlikely(!cmdiocbq)) {
12241 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12242 "0386 ELS complete with no corresponding "
12243 "cmdiocb: iotag (%d)\n",
12244 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12245 lpfc_sli_release_iocbq(phba, irspiocbq);
12246 return NULL;
12247 }
12248
12249 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012250 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040012251
12252 return irspiocbq;
12253}
12254
12255/**
James Smart04c68492009-05-22 14:52:52 -040012256 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
12257 * @phba: Pointer to HBA context object.
12258 * @cqe: Pointer to mailbox completion queue entry.
12259 *
12260 * This routine process a mailbox completion queue entry with asynchrous
12261 * event.
12262 *
12263 * Return: true if work posted to worker thread, otherwise false.
12264 **/
12265static bool
12266lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12267{
12268 struct lpfc_cq_event *cq_event;
12269 unsigned long iflags;
12270
12271 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
12272 "0392 Async Event: word0:x%x, word1:x%x, "
12273 "word2:x%x, word3:x%x\n", mcqe->word0,
12274 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
12275
12276 /* Allocate a new internal CQ_EVENT entry */
12277 cq_event = lpfc_sli4_cq_event_alloc(phba);
12278 if (!cq_event) {
12279 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12280 "0394 Failed to allocate CQ_EVENT entry\n");
12281 return false;
12282 }
12283
12284 /* Move the CQE into an asynchronous event entry */
12285 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
12286 spin_lock_irqsave(&phba->hbalock, iflags);
12287 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
12288 /* Set the async event flag */
12289 phba->hba_flag |= ASYNC_EVENT;
12290 spin_unlock_irqrestore(&phba->hbalock, iflags);
12291
12292 return true;
12293}
12294
12295/**
12296 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
12297 * @phba: Pointer to HBA context object.
12298 * @cqe: Pointer to mailbox completion queue entry.
12299 *
12300 * This routine process a mailbox completion queue entry with mailbox
12301 * completion event.
12302 *
12303 * Return: true if work posted to worker thread, otherwise false.
12304 **/
12305static bool
12306lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
12307{
12308 uint32_t mcqe_status;
12309 MAILBOX_t *mbox, *pmbox;
12310 struct lpfc_mqe *mqe;
12311 struct lpfc_vport *vport;
12312 struct lpfc_nodelist *ndlp;
12313 struct lpfc_dmabuf *mp;
12314 unsigned long iflags;
12315 LPFC_MBOXQ_t *pmb;
12316 bool workposted = false;
12317 int rc;
12318
12319 /* If not a mailbox complete MCQE, out by checking mailbox consume */
12320 if (!bf_get(lpfc_trailer_completed, mcqe))
12321 goto out_no_mqe_complete;
12322
12323 /* Get the reference to the active mbox command */
12324 spin_lock_irqsave(&phba->hbalock, iflags);
12325 pmb = phba->sli.mbox_active;
12326 if (unlikely(!pmb)) {
12327 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
12328 "1832 No pending MBOX command to handle\n");
12329 spin_unlock_irqrestore(&phba->hbalock, iflags);
12330 goto out_no_mqe_complete;
12331 }
12332 spin_unlock_irqrestore(&phba->hbalock, iflags);
12333 mqe = &pmb->u.mqe;
12334 pmbox = (MAILBOX_t *)&pmb->u.mqe;
12335 mbox = phba->mbox;
12336 vport = pmb->vport;
12337
12338 /* Reset heartbeat timer */
12339 phba->last_completion_time = jiffies;
12340 del_timer(&phba->sli.mbox_tmo);
12341
12342 /* Move mbox data to caller's mailbox region, do endian swapping */
12343 if (pmb->mbox_cmpl && mbox)
12344 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040012345
James Smart73d91e52011-10-10 21:32:10 -040012346 /*
12347 * For mcqe errors, conditionally move a modified error code to
12348 * the mbox so that the error will not be missed.
12349 */
12350 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
12351 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
12352 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
12353 bf_set(lpfc_mqe_status, mqe,
12354 (LPFC_MBX_ERROR_RANGE | mcqe_status));
12355 }
James Smart04c68492009-05-22 14:52:52 -040012356 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
12357 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
12358 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
12359 "MBOX dflt rpi: status:x%x rpi:x%x",
12360 mcqe_status,
12361 pmbox->un.varWords[0], 0);
12362 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
12363 mp = (struct lpfc_dmabuf *)(pmb->context1);
12364 ndlp = (struct lpfc_nodelist *)pmb->context2;
12365 /* Reg_LOGIN of dflt RPI was successful. Now lets get
12366 * RID of the PPI using the same mbox buffer.
12367 */
12368 lpfc_unreg_login(phba, vport->vpi,
12369 pmbox->un.varWords[0], pmb);
12370 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
12371 pmb->context1 = mp;
12372 pmb->context2 = ndlp;
12373 pmb->vport = vport;
12374 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
12375 if (rc != MBX_BUSY)
12376 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
12377 LOG_SLI, "0385 rc should "
12378 "have been MBX_BUSY\n");
12379 if (rc != MBX_NOT_FINISHED)
12380 goto send_current_mbox;
12381 }
12382 }
12383 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
12384 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
12385 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
12386
12387 /* There is mailbox completion work to do */
12388 spin_lock_irqsave(&phba->hbalock, iflags);
12389 __lpfc_mbox_cmpl_put(phba, pmb);
12390 phba->work_ha |= HA_MBATT;
12391 spin_unlock_irqrestore(&phba->hbalock, iflags);
12392 workposted = true;
12393
12394send_current_mbox:
12395 spin_lock_irqsave(&phba->hbalock, iflags);
12396 /* Release the mailbox command posting token */
12397 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
12398 /* Setting active mailbox pointer need to be in sync to flag clear */
12399 phba->sli.mbox_active = NULL;
12400 spin_unlock_irqrestore(&phba->hbalock, iflags);
12401 /* Wake up worker thread to post the next pending mailbox command */
12402 lpfc_worker_wake_up(phba);
12403out_no_mqe_complete:
12404 if (bf_get(lpfc_trailer_consumed, mcqe))
12405 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
12406 return workposted;
12407}
12408
12409/**
12410 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12411 * @phba: Pointer to HBA context object.
12412 * @cqe: Pointer to mailbox completion queue entry.
12413 *
12414 * This routine process a mailbox completion queue entry, it invokes the
12415 * proper mailbox complete handling or asynchrous event handling routine
12416 * according to the MCQE's async bit.
12417 *
12418 * Return: true if work posted to worker thread, otherwise false.
12419 **/
12420static bool
12421lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12422{
12423 struct lpfc_mcqe mcqe;
12424 bool workposted;
12425
12426 /* Copy the mailbox MCQE and convert endian order as needed */
12427 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12428
12429 /* Invoke the proper event handling routine */
12430 if (!bf_get(lpfc_trailer_async, &mcqe))
12431 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12432 else
12433 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12434 return workposted;
12435}
12436
12437/**
James Smart4f774512009-05-22 14:52:35 -040012438 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12439 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012440 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012441 * @wcqe: Pointer to work-queue completion queue entry.
12442 *
12443 * This routine handles an ELS work-queue completion event.
12444 *
12445 * Return: true if work posted to worker thread, otherwise false.
12446 **/
12447static bool
James Smart2a76a282012-08-03 12:35:54 -040012448lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012449 struct lpfc_wcqe_complete *wcqe)
12450{
James Smart4f774512009-05-22 14:52:35 -040012451 struct lpfc_iocbq *irspiocbq;
12452 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012453 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012454 int txq_cnt = 0;
12455 int txcmplq_cnt = 0;
12456 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012457
James Smart45ed1192009-10-02 15:17:02 -040012458 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012459 irspiocbq = lpfc_sli_get_iocbq(phba);
12460 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012461 if (!list_empty(&pring->txq))
12462 txq_cnt++;
12463 if (!list_empty(&pring->txcmplq))
12464 txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012465 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012466 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12467 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012468 txq_cnt, phba->iocb_cnt,
12469 fcp_txcmplq_cnt,
12470 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012471 return false;
James Smart4f774512009-05-22 14:52:35 -040012472 }
James Smart4f774512009-05-22 14:52:35 -040012473
James Smart45ed1192009-10-02 15:17:02 -040012474 /* Save off the slow-path queue event for work thread to process */
12475 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012476 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012477 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012478 &phba->sli4_hba.sp_queue_event);
12479 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012480 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012481
James Smart45ed1192009-10-02 15:17:02 -040012482 return true;
James Smart4f774512009-05-22 14:52:35 -040012483}
12484
12485/**
12486 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12487 * @phba: Pointer to HBA context object.
12488 * @wcqe: Pointer to work-queue completion queue entry.
12489 *
12490 * This routine handles slow-path WQ entry comsumed event by invoking the
12491 * proper WQ release routine to the slow-path WQ.
12492 **/
12493static void
12494lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12495 struct lpfc_wcqe_release *wcqe)
12496{
James Smart2e90f4b2011-12-13 13:22:37 -050012497 /* sanity check on queue memory */
12498 if (unlikely(!phba->sli4_hba.els_wq))
12499 return;
James Smart4f774512009-05-22 14:52:35 -040012500 /* Check for the slow-path ELS work queue */
12501 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12502 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12503 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12504 else
12505 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12506 "2579 Slow-path wqe consume event carries "
12507 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12508 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12509 phba->sli4_hba.els_wq->queue_id);
12510}
12511
12512/**
12513 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12514 * @phba: Pointer to HBA context object.
12515 * @cq: Pointer to a WQ completion queue.
12516 * @wcqe: Pointer to work-queue completion queue entry.
12517 *
12518 * This routine handles an XRI abort event.
12519 *
12520 * Return: true if work posted to worker thread, otherwise false.
12521 **/
12522static bool
12523lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12524 struct lpfc_queue *cq,
12525 struct sli4_wcqe_xri_aborted *wcqe)
12526{
12527 bool workposted = false;
12528 struct lpfc_cq_event *cq_event;
12529 unsigned long iflags;
12530
12531 /* Allocate a new internal CQ_EVENT entry */
12532 cq_event = lpfc_sli4_cq_event_alloc(phba);
12533 if (!cq_event) {
12534 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12535 "0602 Failed to allocate CQ_EVENT entry\n");
12536 return false;
12537 }
12538
12539 /* Move the CQE into the proper xri abort event list */
12540 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12541 switch (cq->subtype) {
12542 case LPFC_FCP:
12543 spin_lock_irqsave(&phba->hbalock, iflags);
12544 list_add_tail(&cq_event->list,
12545 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12546 /* Set the fcp xri abort event flag */
12547 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12548 spin_unlock_irqrestore(&phba->hbalock, iflags);
12549 workposted = true;
12550 break;
12551 case LPFC_ELS:
12552 spin_lock_irqsave(&phba->hbalock, iflags);
12553 list_add_tail(&cq_event->list,
12554 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12555 /* Set the els xri abort event flag */
12556 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12557 spin_unlock_irqrestore(&phba->hbalock, iflags);
12558 workposted = true;
12559 break;
12560 default:
12561 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12562 "0603 Invalid work queue CQE subtype (x%x)\n",
12563 cq->subtype);
12564 workposted = false;
12565 break;
12566 }
12567 return workposted;
12568}
12569
12570/**
James Smart4d9ab992009-10-02 15:16:39 -040012571 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012572 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012573 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012574 *
James Smart4d9ab992009-10-02 15:16:39 -040012575 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012576 *
12577 * Return: true if work posted to worker thread, otherwise false.
12578 **/
12579static bool
James Smart4d9ab992009-10-02 15:16:39 -040012580lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12581{
12582 bool workposted = false;
James Smart895427b2017-02-12 13:52:30 -080012583 struct fc_frame_header *fc_hdr;
James Smart4d9ab992009-10-02 15:16:39 -040012584 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12585 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12586 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012587 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012588 unsigned long iflags;
12589
James Smart2e90f4b2011-12-13 13:22:37 -050012590 /* sanity check on queue memory */
12591 if (unlikely(!hrq) || unlikely(!drq))
12592 return workposted;
12593
James Smart7851fe22011-07-22 18:36:52 -040012594 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12595 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12596 else
12597 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12598 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012599 goto out;
12600
12601 status = bf_get(lpfc_rcqe_status, rcqe);
12602 switch (status) {
12603 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12604 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12605 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040012606 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040012607 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012608 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012609 spin_lock_irqsave(&phba->hbalock, iflags);
12610 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12611 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012612 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012613 spin_unlock_irqrestore(&phba->hbalock, iflags);
12614 goto out;
12615 }
James Smartb84daac2012-08-03 12:35:13 -040012616 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012617 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
James Smart895427b2017-02-12 13:52:30 -080012618
12619 /* If a NVME LS event (type 0x28), treat it as Fast path */
12620 fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
12621
James Smart4d9ab992009-10-02 15:16:39 -040012622 /* save off the frame for the word thread to process */
12623 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012624 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012625 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012626 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012627 spin_unlock_irqrestore(&phba->hbalock, iflags);
12628 workposted = true;
12629 break;
12630 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12631 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040012632 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012633 /* Post more buffers if possible */
12634 spin_lock_irqsave(&phba->hbalock, iflags);
12635 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12636 spin_unlock_irqrestore(&phba->hbalock, iflags);
12637 workposted = true;
12638 break;
12639 }
12640out:
12641 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012642}
12643
12644/**
12645 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12646 * @phba: Pointer to HBA context object.
12647 * @cq: Pointer to the completion queue.
12648 * @wcqe: Pointer to a completion queue entry.
12649 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012650 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012651 * entry.
12652 *
12653 * Return: true if work posted to worker thread, otherwise false.
12654 **/
12655static bool
12656lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012657 struct lpfc_cqe *cqe)
12658{
James Smart45ed1192009-10-02 15:17:02 -040012659 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012660 bool workposted = false;
12661
12662 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012663 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012664
12665 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012666 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012667 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012668 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012669 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012670 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012671 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012672 break;
12673 case CQE_CODE_RELEASE_WQE:
12674 /* Process the WQ release event */
12675 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012676 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012677 break;
12678 case CQE_CODE_XRI_ABORTED:
12679 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012680 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012681 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012682 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012683 break;
James Smart4d9ab992009-10-02 15:16:39 -040012684 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012685 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012686 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012687 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012688 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012689 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012690 break;
James Smart4f774512009-05-22 14:52:35 -040012691 default:
12692 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12693 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012694 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012695 break;
12696 }
12697 return workposted;
12698}
12699
12700/**
James Smart4f774512009-05-22 14:52:35 -040012701 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12702 * @phba: Pointer to HBA context object.
12703 * @eqe: Pointer to fast-path event queue entry.
12704 *
12705 * This routine process a event queue entry from the slow-path event queue.
12706 * It will check the MajorCode and MinorCode to determine this is for a
12707 * completion event on a completion queue, if not, an error shall be logged
12708 * and just return. Otherwise, it will get to the corresponding completion
12709 * queue and process all the entries on that completion queue, rearm the
12710 * completion queue, and then return.
12711 *
12712 **/
12713static void
James Smart67d12732012-08-03 12:36:13 -040012714lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12715 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040012716{
James Smart67d12732012-08-03 12:36:13 -040012717 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040012718 struct lpfc_cqe *cqe;
12719 bool workposted = false;
12720 int ecount = 0;
12721 uint16_t cqid;
12722
James Smart4f774512009-05-22 14:52:35 -040012723 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040012724 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040012725
James Smart4f774512009-05-22 14:52:35 -040012726 list_for_each_entry(childq, &speq->child_list, list) {
12727 if (childq->queue_id == cqid) {
12728 cq = childq;
12729 break;
12730 }
12731 }
12732 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012733 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12734 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12735 "0365 Slow-path CQ identifier "
12736 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040012737 return;
12738 }
12739
James Smart895427b2017-02-12 13:52:30 -080012740 /* Save EQ associated with this CQ */
12741 cq->assoc_qp = speq;
12742
James Smart4f774512009-05-22 14:52:35 -040012743 /* Process all the entries to the CQ */
12744 switch (cq->type) {
12745 case LPFC_MCQ:
12746 while ((cqe = lpfc_sli4_cq_get(cq))) {
12747 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012748 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012749 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012750 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040012751 }
12752 break;
12753 case LPFC_WCQ:
12754 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080012755 if ((cq->subtype == LPFC_FCP) ||
12756 (cq->subtype == LPFC_NVME))
12757 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
James Smart05580562011-05-24 11:40:48 -040012758 cqe);
12759 else
12760 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12761 cqe);
James Smart73d91e52011-10-10 21:32:10 -040012762 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012763 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12764 }
James Smartb84daac2012-08-03 12:35:13 -040012765
12766 /* Track the max number of CQEs processed in 1 EQ */
12767 if (ecount > cq->CQ_max_cqe)
12768 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040012769 break;
12770 default:
12771 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12772 "0370 Invalid completion queue type (%d)\n",
12773 cq->type);
12774 return;
12775 }
12776
12777 /* Catch the no cq entry condition, log an error */
12778 if (unlikely(ecount == 0))
12779 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12780 "0371 No entry from the CQ: identifier "
12781 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12782
12783 /* In any case, flash and re-arm the RCQ */
12784 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12785
12786 /* wake up worker thread if there are works to be done */
12787 if (workposted)
12788 lpfc_worker_wake_up(phba);
12789}
12790
12791/**
12792 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040012793 * @phba: Pointer to HBA context object.
12794 * @cq: Pointer to associated CQ
12795 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012796 *
12797 * This routine process a fast-path work queue completion entry from fast-path
12798 * event queue for FCP command response completion.
12799 **/
12800static void
James Smart2a76a282012-08-03 12:35:54 -040012801lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012802 struct lpfc_wcqe_complete *wcqe)
12803{
James Smart2a76a282012-08-03 12:35:54 -040012804 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040012805 struct lpfc_iocbq *cmdiocbq;
12806 struct lpfc_iocbq irspiocbq;
12807 unsigned long iflags;
12808
James Smart4f774512009-05-22 14:52:35 -040012809 /* Check for response status */
12810 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12811 /* If resource errors reported from HBA, reduce queue
12812 * depth of the SCSI device.
12813 */
James Smarte3d2b802012-08-14 14:25:43 -040012814 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12815 IOSTAT_LOCAL_REJECT)) &&
12816 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12817 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040012818 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040012819
James Smart4f774512009-05-22 14:52:35 -040012820 /* Log the error status */
12821 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12822 "0373 FCP complete error: status=x%x, "
12823 "hw_status=x%x, total_data_specified=%d, "
12824 "parameter=x%x, word3=x%x\n",
12825 bf_get(lpfc_wcqe_c_status, wcqe),
12826 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12827 wcqe->total_data_placed, wcqe->parameter,
12828 wcqe->word3);
12829 }
12830
12831 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040012832 spin_lock_irqsave(&pring->ring_lock, iflags);
12833 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040012834 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12835 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040012836 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012837 if (unlikely(!cmdiocbq)) {
12838 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12839 "0374 FCP complete with no corresponding "
12840 "cmdiocb: iotag (%d)\n",
12841 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12842 return;
12843 }
James Smart895427b2017-02-12 13:52:30 -080012844
12845 if (cq->assoc_qp)
12846 cmdiocbq->isr_timestamp =
12847 cq->assoc_qp->isr_timestamp;
12848
12849 if (cmdiocbq->iocb_cmpl == NULL) {
12850 if (cmdiocbq->wqe_cmpl) {
12851 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12852 spin_lock_irqsave(&phba->hbalock, iflags);
12853 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12854 spin_unlock_irqrestore(&phba->hbalock, iflags);
12855 }
12856
12857 /* Pass the cmd_iocb and the wcqe to the upper layer */
12858 (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
12859 return;
12860 }
James Smart4f774512009-05-22 14:52:35 -040012861 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12862 "0375 FCP cmdiocb not callback function "
12863 "iotag: (%d)\n",
12864 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12865 return;
12866 }
12867
12868 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012869 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040012870
James Smart0f65ff62010-02-26 14:14:23 -050012871 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12872 spin_lock_irqsave(&phba->hbalock, iflags);
12873 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12874 spin_unlock_irqrestore(&phba->hbalock, iflags);
12875 }
12876
James Smart4f774512009-05-22 14:52:35 -040012877 /* Pass the cmd_iocb and the rsp state to the upper layer */
12878 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12879}
12880
12881/**
12882 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12883 * @phba: Pointer to HBA context object.
12884 * @cq: Pointer to completion queue.
12885 * @wcqe: Pointer to work-queue completion queue entry.
12886 *
12887 * This routine handles an fast-path WQ entry comsumed event by invoking the
12888 * proper WQ release routine to the slow-path WQ.
12889 **/
12890static void
12891lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12892 struct lpfc_wcqe_release *wcqe)
12893{
12894 struct lpfc_queue *childwq;
12895 bool wqid_matched = false;
James Smart895427b2017-02-12 13:52:30 -080012896 uint16_t hba_wqid;
James Smart4f774512009-05-22 14:52:35 -040012897
12898 /* Check for fast-path FCP work queue release */
James Smart895427b2017-02-12 13:52:30 -080012899 hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
James Smart4f774512009-05-22 14:52:35 -040012900 list_for_each_entry(childwq, &cq->child_list, list) {
James Smart895427b2017-02-12 13:52:30 -080012901 if (childwq->queue_id == hba_wqid) {
James Smart4f774512009-05-22 14:52:35 -040012902 lpfc_sli4_wq_release(childwq,
12903 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12904 wqid_matched = true;
12905 break;
12906 }
12907 }
12908 /* Report warning log message if no match found */
12909 if (wqid_matched != true)
12910 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12911 "2580 Fast-path wqe consume event carries "
James Smart895427b2017-02-12 13:52:30 -080012912 "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
James Smart4f774512009-05-22 14:52:35 -040012913}
12914
12915/**
James Smart895427b2017-02-12 13:52:30 -080012916 * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
James Smart4f774512009-05-22 14:52:35 -040012917 * @cq: Pointer to the completion queue.
12918 * @eqe: Pointer to fast-path completion queue entry.
12919 *
12920 * This routine process a fast-path work queue completion entry from fast-path
12921 * event queue for FCP command response completion.
12922 **/
12923static int
James Smart895427b2017-02-12 13:52:30 -080012924lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012925 struct lpfc_cqe *cqe)
12926{
12927 struct lpfc_wcqe_release wcqe;
12928 bool workposted = false;
12929
12930 /* Copy the work queue CQE and convert endian order if needed */
12931 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12932
12933 /* Check and process for different type of WCQE and dispatch */
12934 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12935 case CQE_CODE_COMPL_WQE:
James Smart895427b2017-02-12 13:52:30 -080012936 case CQE_CODE_NVME_ERSP:
James Smartb84daac2012-08-03 12:35:13 -040012937 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040012938 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040012939 phba->last_completion_time = jiffies;
James Smart895427b2017-02-12 13:52:30 -080012940 if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
12941 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
12942 (struct lpfc_wcqe_complete *)&wcqe);
12943 if (cq->subtype == LPFC_NVME_LS)
12944 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040012945 (struct lpfc_wcqe_complete *)&wcqe);
12946 break;
12947 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012948 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040012949 /* Process the WQ release event */
12950 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12951 (struct lpfc_wcqe_release *)&wcqe);
12952 break;
12953 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040012954 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040012955 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012956 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012957 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12958 (struct sli4_wcqe_xri_aborted *)&wcqe);
12959 break;
James Smart895427b2017-02-12 13:52:30 -080012960 case CQE_CODE_RECEIVE_V1:
12961 case CQE_CODE_RECEIVE:
12962 phba->last_completion_time = jiffies;
12963 break;
James Smart4f774512009-05-22 14:52:35 -040012964 default:
12965 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart895427b2017-02-12 13:52:30 -080012966 "0144 Not a valid CQE code: x%x\n",
James Smart4f774512009-05-22 14:52:35 -040012967 bf_get(lpfc_wcqe_c_code, &wcqe));
12968 break;
12969 }
12970 return workposted;
12971}
12972
12973/**
James Smart67d12732012-08-03 12:36:13 -040012974 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040012975 * @phba: Pointer to HBA context object.
12976 * @eqe: Pointer to fast-path event queue entry.
12977 *
12978 * This routine process a event queue entry from the fast-path event queue.
12979 * It will check the MajorCode and MinorCode to determine this is for a
12980 * completion event on a completion queue, if not, an error shall be logged
12981 * and just return. Otherwise, it will get to the corresponding completion
12982 * queue and process all the entries on the completion queue, rearm the
12983 * completion queue, and then return.
12984 **/
12985static void
James Smart67d12732012-08-03 12:36:13 -040012986lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12987 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040012988{
James Smart895427b2017-02-12 13:52:30 -080012989 struct lpfc_queue *cq = NULL;
James Smart4f774512009-05-22 14:52:35 -040012990 struct lpfc_cqe *cqe;
12991 bool workposted = false;
12992 uint16_t cqid;
12993 int ecount = 0;
12994
James Smartcb5172e2010-03-15 11:25:07 -040012995 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040012996 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040012997 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040012998 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040012999 bf_get_le32(lpfc_eqe_major_code, eqe),
13000 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040013001 return;
13002 }
13003
James Smart67d12732012-08-03 12:36:13 -040013004 /* Get the reference to the corresponding CQ */
13005 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13006
James Smart895427b2017-02-12 13:52:30 -080013007 if (phba->sli4_hba.nvme_cq_map &&
13008 (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
13009 /* Process NVME command completion */
13010 cq = phba->sli4_hba.nvme_cq[qidx];
13011 goto process_cq;
13012 }
13013
13014 if (phba->sli4_hba.fcp_cq_map &&
13015 (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
13016 /* Process FCP command completion */
13017 cq = phba->sli4_hba.fcp_cq[qidx];
13018 goto process_cq;
13019 }
13020
13021 if (phba->sli4_hba.nvmels_cq &&
13022 (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
13023 /* Process NVME unsol rcv */
13024 cq = phba->sli4_hba.nvmels_cq;
13025 }
13026
13027 /* Otherwise this is a Slow path event */
13028 if (cq == NULL) {
13029 lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013030 return;
13031 }
13032
James Smart895427b2017-02-12 13:52:30 -080013033process_cq:
James Smart4f774512009-05-22 14:52:35 -040013034 if (unlikely(cqid != cq->queue_id)) {
13035 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13036 "0368 Miss-matched fast-path completion "
13037 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
13038 cqid, cq->queue_id);
13039 return;
13040 }
13041
James Smart895427b2017-02-12 13:52:30 -080013042 /* Save EQ associated with this CQ */
13043 cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
13044
James Smart4f774512009-05-22 14:52:35 -040013045 /* Process all the entries to the CQ */
13046 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013047 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040013048 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040013049 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13050 }
13051
James Smartb84daac2012-08-03 12:35:13 -040013052 /* Track the max number of CQEs processed in 1 EQ */
13053 if (ecount > cq->CQ_max_cqe)
13054 cq->CQ_max_cqe = ecount;
13055
James Smart4f774512009-05-22 14:52:35 -040013056 /* Catch the no cq entry condition */
13057 if (unlikely(ecount == 0))
13058 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13059 "0369 No entry from fast-path completion "
13060 "queue fcpcqid=%d\n", cq->queue_id);
13061
13062 /* In any case, flash and re-arm the CQ */
13063 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13064
13065 /* wake up worker thread if there are works to be done */
13066 if (workposted)
13067 lpfc_worker_wake_up(phba);
13068}
13069
13070static void
13071lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
13072{
13073 struct lpfc_eqe *eqe;
13074
13075 /* walk all the EQ entries and drop on the floor */
13076 while ((eqe = lpfc_sli4_eq_get(eq)))
13077 ;
13078
13079 /* Clear and re-arm the EQ */
13080 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13081}
13082
James Smart1ba981f2014-02-20 09:56:45 -050013083
13084/**
13085 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
13086 * entry
13087 * @phba: Pointer to HBA context object.
13088 * @eqe: Pointer to fast-path event queue entry.
13089 *
13090 * This routine process a event queue entry from the Flash Optimized Fabric
13091 * event queue. It will check the MajorCode and MinorCode to determine this
13092 * is for a completion event on a completion queue, if not, an error shall be
13093 * logged and just return. Otherwise, it will get to the corresponding
13094 * completion queue and process all the entries on the completion queue, rearm
13095 * the completion queue, and then return.
13096 **/
13097static void
13098lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
13099{
13100 struct lpfc_queue *cq;
13101 struct lpfc_cqe *cqe;
13102 bool workposted = false;
13103 uint16_t cqid;
13104 int ecount = 0;
13105
13106 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
13107 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13108 "9147 Not a valid completion "
13109 "event: majorcode=x%x, minorcode=x%x\n",
13110 bf_get_le32(lpfc_eqe_major_code, eqe),
13111 bf_get_le32(lpfc_eqe_minor_code, eqe));
13112 return;
13113 }
13114
13115 /* Get the reference to the corresponding CQ */
13116 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
13117
13118 /* Next check for OAS */
13119 cq = phba->sli4_hba.oas_cq;
13120 if (unlikely(!cq)) {
13121 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
13122 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13123 "9148 OAS completion queue "
13124 "does not exist\n");
13125 return;
13126 }
13127
13128 if (unlikely(cqid != cq->queue_id)) {
13129 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13130 "9149 Miss-matched fast-path compl "
13131 "queue id: eqcqid=%d, fcpcqid=%d\n",
13132 cqid, cq->queue_id);
13133 return;
13134 }
13135
13136 /* Process all the entries to the OAS CQ */
13137 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart895427b2017-02-12 13:52:30 -080013138 workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
James Smart1ba981f2014-02-20 09:56:45 -050013139 if (!(++ecount % cq->entry_repost))
13140 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
13141 }
13142
13143 /* Track the max number of CQEs processed in 1 EQ */
13144 if (ecount > cq->CQ_max_cqe)
13145 cq->CQ_max_cqe = ecount;
13146
13147 /* Catch the no cq entry condition */
13148 if (unlikely(ecount == 0))
13149 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13150 "9153 No entry from fast-path completion "
13151 "queue fcpcqid=%d\n", cq->queue_id);
13152
13153 /* In any case, flash and re-arm the CQ */
13154 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
13155
13156 /* wake up worker thread if there are works to be done */
13157 if (workposted)
13158 lpfc_worker_wake_up(phba);
13159}
13160
13161/**
13162 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
13163 * @irq: Interrupt number.
13164 * @dev_id: The device context pointer.
13165 *
13166 * This function is directly called from the PCI layer as an interrupt
13167 * service routine when device with SLI-4 interface spec is enabled with
13168 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
13169 * IOCB ring event in the HBA. However, when the device is enabled with either
13170 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13171 * device-level interrupt handler. When the PCI slot is in error recovery
13172 * or the HBA is undergoing initialization, the interrupt handler will not
13173 * process the interrupt. The Flash Optimized Fabric ring event are handled in
13174 * the intrrupt context. This function is called without any lock held.
13175 * It gets the hbalock to access and update SLI data structures. Note that,
13176 * the EQ to CQ are one-to-one map such that the EQ index is
13177 * equal to that of CQ index.
13178 *
13179 * This function returns IRQ_HANDLED when interrupt is handled else it
13180 * returns IRQ_NONE.
13181 **/
13182irqreturn_t
13183lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
13184{
13185 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013186 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart1ba981f2014-02-20 09:56:45 -050013187 struct lpfc_queue *eq;
13188 struct lpfc_eqe *eqe;
13189 unsigned long iflag;
13190 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050013191
13192 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013193 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13194 phba = hba_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050013195
13196 if (unlikely(!phba))
13197 return IRQ_NONE;
13198
13199 /* Get to the EQ struct associated with this vector */
13200 eq = phba->sli4_hba.fof_eq;
13201 if (unlikely(!eq))
13202 return IRQ_NONE;
13203
13204 /* Check device state for handling interrupt */
13205 if (unlikely(lpfc_intr_state_check(phba))) {
13206 eq->EQ_badstate++;
13207 /* Check again for link_state with lock held */
13208 spin_lock_irqsave(&phba->hbalock, iflag);
13209 if (phba->link_state < LPFC_LINK_DOWN)
13210 /* Flush, clear interrupt, and rearm the EQ */
13211 lpfc_sli4_eq_flush(phba, eq);
13212 spin_unlock_irqrestore(&phba->hbalock, iflag);
13213 return IRQ_NONE;
13214 }
13215
13216 /*
13217 * Process all the event on FCP fast-path EQ
13218 */
13219 while ((eqe = lpfc_sli4_eq_get(eq))) {
13220 lpfc_sli4_fof_handle_eqe(phba, eqe);
13221 if (!(++ecount % eq->entry_repost))
13222 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
13223 eq->EQ_processed++;
13224 }
13225
13226 /* Track the max number of EQEs processed in 1 intr */
13227 if (ecount > eq->EQ_max_eqe)
13228 eq->EQ_max_eqe = ecount;
13229
13230
13231 if (unlikely(ecount == 0)) {
13232 eq->EQ_no_entry++;
13233
13234 if (phba->intr_type == MSIX)
13235 /* MSI-X treated interrupt served as no EQ share INT */
13236 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13237 "9145 MSI-X interrupt with no EQE\n");
13238 else {
13239 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13240 "9146 ISR interrupt with no EQE\n");
13241 /* Non MSI-X treated on interrupt as EQ share INT */
13242 return IRQ_NONE;
13243 }
13244 }
13245 /* Always clear and re-arm the fast-path EQ */
13246 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
13247 return IRQ_HANDLED;
13248}
13249
James Smart4f774512009-05-22 14:52:35 -040013250/**
James Smart67d12732012-08-03 12:36:13 -040013251 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040013252 * @irq: Interrupt number.
13253 * @dev_id: The device context pointer.
13254 *
13255 * This function is directly called from the PCI layer as an interrupt
13256 * service routine when device with SLI-4 interface spec is enabled with
13257 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
13258 * ring event in the HBA. However, when the device is enabled with either
13259 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
13260 * device-level interrupt handler. When the PCI slot is in error recovery
13261 * or the HBA is undergoing initialization, the interrupt handler will not
13262 * process the interrupt. The SCSI FCP fast-path ring event are handled in
13263 * the intrrupt context. This function is called without any lock held.
13264 * It gets the hbalock to access and update SLI data structures. Note that,
13265 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
13266 * equal to that of FCP CQ index.
13267 *
James Smart67d12732012-08-03 12:36:13 -040013268 * The link attention and ELS ring attention events are handled
13269 * by the worker thread. The interrupt handler signals the worker thread
13270 * and returns for these events. This function is called without any lock
13271 * held. It gets the hbalock to access and update SLI data structures.
13272 *
James Smart4f774512009-05-22 14:52:35 -040013273 * This function returns IRQ_HANDLED when interrupt is handled else it
13274 * returns IRQ_NONE.
13275 **/
13276irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040013277lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040013278{
13279 struct lpfc_hba *phba;
James Smart895427b2017-02-12 13:52:30 -080013280 struct lpfc_hba_eq_hdl *hba_eq_hdl;
James Smart4f774512009-05-22 14:52:35 -040013281 struct lpfc_queue *fpeq;
13282 struct lpfc_eqe *eqe;
13283 unsigned long iflag;
13284 int ecount = 0;
James Smart895427b2017-02-12 13:52:30 -080013285 int hba_eqidx;
James Smart4f774512009-05-22 14:52:35 -040013286
13287 /* Get the driver's phba structure from the dev_id */
James Smart895427b2017-02-12 13:52:30 -080013288 hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
13289 phba = hba_eq_hdl->phba;
13290 hba_eqidx = hba_eq_hdl->idx;
James Smart4f774512009-05-22 14:52:35 -040013291
13292 if (unlikely(!phba))
13293 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040013294 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040013295 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013296
13297 /* Get to the EQ struct associated with this vector */
James Smart895427b2017-02-12 13:52:30 -080013298 fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050013299 if (unlikely(!fpeq))
13300 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013301
James Smartbd2cdd52017-02-12 13:52:33 -080013302#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
13303 if (phba->ktime_on)
13304 fpeq->isr_timestamp = ktime_get_ns();
13305#endif
13306
James Smartba20c852012-08-03 12:36:52 -040013307 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013308 if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
James Smartba20c852012-08-03 12:36:52 -040013309 lpfc_sli4_eq_clr_intr(fpeq);
13310 else {
James Smart895427b2017-02-12 13:52:30 -080013311 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013312 return IRQ_NONE;
13313 }
13314 }
13315
James Smart4f774512009-05-22 14:52:35 -040013316 /* Check device state for handling interrupt */
13317 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040013318 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040013319 /* Check again for link_state with lock held */
13320 spin_lock_irqsave(&phba->hbalock, iflag);
13321 if (phba->link_state < LPFC_LINK_DOWN)
13322 /* Flush, clear interrupt, and rearm the EQ */
13323 lpfc_sli4_eq_flush(phba, fpeq);
13324 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040013325 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013326 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040013327 return IRQ_NONE;
13328 }
13329
13330 /*
13331 * Process all the event on FCP fast-path EQ
13332 */
13333 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040013334 if (eqe == NULL)
13335 break;
13336
James Smart895427b2017-02-12 13:52:30 -080013337 lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040013338 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040013339 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040013340 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040013341 }
13342
James Smartb84daac2012-08-03 12:35:13 -040013343 /* Track the max number of EQEs processed in 1 intr */
13344 if (ecount > fpeq->EQ_max_eqe)
13345 fpeq->EQ_max_eqe = ecount;
13346
James Smart4f774512009-05-22 14:52:35 -040013347 /* Always clear and re-arm the fast-path EQ */
13348 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
13349
13350 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040013351 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040013352
13353 if (lpfc_fcp_look_ahead) {
James Smart895427b2017-02-12 13:52:30 -080013354 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
James Smartba20c852012-08-03 12:36:52 -040013355 return IRQ_NONE;
13356 }
13357
James Smart4f774512009-05-22 14:52:35 -040013358 if (phba->intr_type == MSIX)
13359 /* MSI-X treated interrupt served as no EQ share INT */
13360 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13361 "0358 MSI-X interrupt with no EQE\n");
13362 else
13363 /* Non MSI-X treated on interrupt as EQ share INT */
13364 return IRQ_NONE;
13365 }
13366
James Smartba20c852012-08-03 12:36:52 -040013367 if (lpfc_fcp_look_ahead)
James Smart895427b2017-02-12 13:52:30 -080013368 atomic_inc(&hba_eq_hdl->hba_eq_in_use);
13369
James Smart4f774512009-05-22 14:52:35 -040013370 return IRQ_HANDLED;
13371} /* lpfc_sli4_fp_intr_handler */
13372
13373/**
13374 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
13375 * @irq: Interrupt number.
13376 * @dev_id: The device context pointer.
13377 *
13378 * This function is the device-level interrupt handler to device with SLI-4
13379 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
13380 * interrupt mode is enabled and there is an event in the HBA which requires
13381 * driver attention. This function invokes the slow-path interrupt attention
13382 * handling function and fast-path interrupt attention handling function in
13383 * turn to process the relevant HBA attention events. This function is called
13384 * without any lock held. It gets the hbalock to access and update SLI data
13385 * structures.
13386 *
13387 * This function returns IRQ_HANDLED when interrupt is handled, else it
13388 * returns IRQ_NONE.
13389 **/
13390irqreturn_t
13391lpfc_sli4_intr_handler(int irq, void *dev_id)
13392{
13393 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040013394 irqreturn_t hba_irq_rc;
13395 bool hba_handled = false;
James Smart895427b2017-02-12 13:52:30 -080013396 int qidx;
James Smart4f774512009-05-22 14:52:35 -040013397
13398 /* Get the driver's phba structure from the dev_id */
13399 phba = (struct lpfc_hba *)dev_id;
13400
13401 if (unlikely(!phba))
13402 return IRQ_NONE;
13403
13404 /*
James Smart4f774512009-05-22 14:52:35 -040013405 * Invoke fast-path host attention interrupt handling as appropriate.
13406 */
James Smart895427b2017-02-12 13:52:30 -080013407 for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
James Smart67d12732012-08-03 12:36:13 -040013408 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013409 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart67d12732012-08-03 12:36:13 -040013410 if (hba_irq_rc == IRQ_HANDLED)
13411 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040013412 }
13413
James Smart1ba981f2014-02-20 09:56:45 -050013414 if (phba->cfg_fof) {
13415 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
James Smart895427b2017-02-12 13:52:30 -080013416 &phba->sli4_hba.hba_eq_hdl[qidx]);
James Smart1ba981f2014-02-20 09:56:45 -050013417 if (hba_irq_rc == IRQ_HANDLED)
13418 hba_handled |= true;
13419 }
13420
James Smart67d12732012-08-03 12:36:13 -040013421 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040013422} /* lpfc_sli4_intr_handler */
13423
13424/**
13425 * lpfc_sli4_queue_free - free a queue structure and associated memory
13426 * @queue: The queue structure to free.
13427 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040013428 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040013429 * the host resident queue. This function must be called after destroying the
13430 * queue on the HBA.
13431 **/
13432void
13433lpfc_sli4_queue_free(struct lpfc_queue *queue)
13434{
13435 struct lpfc_dmabuf *dmabuf;
13436
13437 if (!queue)
13438 return;
13439
13440 while (!list_empty(&queue->page_list)) {
13441 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
13442 list);
James Smart49198b32010-04-06 15:04:33 -040013443 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040013444 dmabuf->virt, dmabuf->phys);
13445 kfree(dmabuf);
13446 }
James Smart895427b2017-02-12 13:52:30 -080013447 if (queue->rqbp) {
13448 lpfc_free_rq_buffer(queue->phba, queue);
13449 kfree(queue->rqbp);
13450 }
13451 kfree(queue->pring);
James Smart4f774512009-05-22 14:52:35 -040013452 kfree(queue);
13453 return;
13454}
13455
13456/**
13457 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
13458 * @phba: The HBA that this queue is being created on.
13459 * @entry_size: The size of each queue entry for this queue.
13460 * @entry count: The number of entries that this queue will handle.
13461 *
13462 * This function allocates a queue structure and the DMAable memory used for
13463 * the host resident queue. This function must be called before creating the
13464 * queue on the HBA.
13465 **/
13466struct lpfc_queue *
13467lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
13468 uint32_t entry_count)
13469{
13470 struct lpfc_queue *queue;
13471 struct lpfc_dmabuf *dmabuf;
13472 int x, total_qe_count;
13473 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040013474 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040013475
James Smartcb5172e2010-03-15 11:25:07 -040013476 if (!phba->sli4_hba.pc_sli4_params.supported)
13477 hw_page_size = SLI4_PAGE_SIZE;
13478
James Smart4f774512009-05-22 14:52:35 -040013479 queue = kzalloc(sizeof(struct lpfc_queue) +
13480 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13481 if (!queue)
13482 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040013483 queue->page_count = (ALIGN(entry_size * entry_count,
13484 hw_page_size))/hw_page_size;
James Smart895427b2017-02-12 13:52:30 -080013485
13486 /* If needed, Adjust page count to match the max the adapter supports */
13487 if (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt)
13488 queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
13489
James Smart4f774512009-05-22 14:52:35 -040013490 INIT_LIST_HEAD(&queue->list);
James Smart895427b2017-02-12 13:52:30 -080013491 INIT_LIST_HEAD(&queue->wq_list);
James Smart4f774512009-05-22 14:52:35 -040013492 INIT_LIST_HEAD(&queue->page_list);
13493 INIT_LIST_HEAD(&queue->child_list);
13494 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13495 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13496 if (!dmabuf)
13497 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040013498 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13499 hw_page_size, &dmabuf->phys,
13500 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013501 if (!dmabuf->virt) {
13502 kfree(dmabuf);
13503 goto out_fail;
13504 }
13505 dmabuf->buffer_tag = x;
13506 list_add_tail(&dmabuf->list, &queue->page_list);
13507 /* initialize queue's entry array */
13508 dma_pointer = dmabuf->virt;
13509 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040013510 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040013511 total_qe_count++, dma_pointer += entry_size) {
13512 queue->qe[total_qe_count].address = dma_pointer;
13513 }
13514 }
13515 queue->entry_size = entry_size;
13516 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040013517
13518 /*
13519 * entry_repost is calculated based on the number of entries in the
13520 * queue. This works out except for RQs. If buffers are NOT initially
13521 * posted for every RQE, entry_repost should be adjusted accordingly.
13522 */
13523 queue->entry_repost = (entry_count >> 3);
13524 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13525 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040013526 queue->phba = phba;
13527
13528 return queue;
13529out_fail:
13530 lpfc_sli4_queue_free(queue);
13531 return NULL;
13532}
13533
13534/**
James Smart962bc512013-01-03 15:44:00 -050013535 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13536 * @phba: HBA structure that indicates port to create a queue on.
13537 * @pci_barset: PCI BAR set flag.
13538 *
13539 * This function shall perform iomap of the specified PCI BAR address to host
13540 * memory address if not already done so and return it. The returned host
13541 * memory address can be NULL.
13542 */
13543static void __iomem *
13544lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13545{
James Smart962bc512013-01-03 15:44:00 -050013546 if (!phba->pcidev)
13547 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013548
13549 switch (pci_barset) {
13550 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013551 return phba->pci_bar0_memmap_p;
13552 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013553 return phba->pci_bar2_memmap_p;
13554 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013555 return phba->pci_bar4_memmap_p;
13556 default:
13557 break;
13558 }
13559 return NULL;
13560}
13561
13562/**
James Smart895427b2017-02-12 13:52:30 -080013563 * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
James Smart173edbb2012-06-12 13:54:50 -040013564 * @phba: HBA structure that indicates port to create a queue on.
13565 * @startq: The starting FCP EQ to modify
13566 *
13567 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13568 *
13569 * The @phba struct is used to send mailbox command to HBA. The @startq
13570 * is used to get the starting FCP EQ to change.
13571 * This function is asynchronous and will wait for the mailbox
13572 * command to finish before continuing.
13573 *
13574 * On success this function will return a zero. If unable to allocate enough
13575 * memory this function will return -ENOMEM. If the queue create mailbox command
13576 * fails this function will return -ENXIO.
13577 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013578int
James Smart895427b2017-02-12 13:52:30 -080013579lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013580{
13581 struct lpfc_mbx_modify_eq_delay *eq_delay;
13582 LPFC_MBOXQ_t *mbox;
13583 struct lpfc_queue *eq;
13584 int cnt, rc, length, status = 0;
13585 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013586 uint32_t result;
James Smart895427b2017-02-12 13:52:30 -080013587 int qidx;
James Smart173edbb2012-06-12 13:54:50 -040013588 union lpfc_sli4_cfg_shdr *shdr;
13589 uint16_t dmult;
13590
James Smart895427b2017-02-12 13:52:30 -080013591 if (startq >= phba->io_channel_irqs)
James Smart173edbb2012-06-12 13:54:50 -040013592 return 0;
13593
13594 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13595 if (!mbox)
13596 return -ENOMEM;
13597 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13598 sizeof(struct lpfc_sli4_cfg_mhdr));
13599 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13600 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13601 length, LPFC_SLI4_MBX_EMBED);
13602 eq_delay = &mbox->u.mqe.un.eq_delay;
13603
13604 /* Calculate delay multiper from maximum interrupt per second */
James Smart895427b2017-02-12 13:52:30 -080013605 result = phba->cfg_fcp_imax / phba->io_channel_irqs;
13606 if (result > LPFC_DMULT_CONST || result == 0)
James Smartee020062012-09-29 11:28:52 -040013607 dmult = 0;
13608 else
13609 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040013610
13611 cnt = 0;
James Smart895427b2017-02-12 13:52:30 -080013612 for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
13613 eq = phba->sli4_hba.hba_eq[qidx];
James Smart173edbb2012-06-12 13:54:50 -040013614 if (!eq)
13615 continue;
13616 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13617 eq_delay->u.request.eq[cnt].phase = 0;
13618 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13619 cnt++;
13620 if (cnt >= LPFC_MAX_EQ_DELAY)
13621 break;
13622 }
13623 eq_delay->u.request.num_eq = cnt;
13624
13625 mbox->vport = phba->pport;
13626 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13627 mbox->context1 = NULL;
13628 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13629 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13630 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13631 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13632 if (shdr_status || shdr_add_status || rc) {
13633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13634 "2512 MODIFY_EQ_DELAY mailbox failed with "
13635 "status x%x add_status x%x, mbx status x%x\n",
13636 shdr_status, shdr_add_status, rc);
13637 status = -ENXIO;
13638 }
13639 mempool_free(mbox, phba->mbox_mem_pool);
13640 return status;
13641}
13642
13643/**
James Smart4f774512009-05-22 14:52:35 -040013644 * lpfc_eq_create - Create an Event Queue on the HBA
13645 * @phba: HBA structure that indicates port to create a queue on.
13646 * @eq: The queue structure to use to create the event queue.
13647 * @imax: The maximum interrupt per second limit.
13648 *
13649 * This function creates an event queue, as detailed in @eq, on a port,
13650 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13651 *
13652 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13653 * is used to get the entry count and entry size that are necessary to
13654 * determine the number of pages to allocate and use for this queue. This
13655 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13656 * event queue. This function is asynchronous and will wait for the mailbox
13657 * command to finish before continuing.
13658 *
13659 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013660 * memory this function will return -ENOMEM. If the queue create mailbox command
13661 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013662 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013663int
James Smartee020062012-09-29 11:28:52 -040013664lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040013665{
13666 struct lpfc_mbx_eq_create *eq_create;
13667 LPFC_MBOXQ_t *mbox;
13668 int rc, length, status = 0;
13669 struct lpfc_dmabuf *dmabuf;
13670 uint32_t shdr_status, shdr_add_status;
13671 union lpfc_sli4_cfg_shdr *shdr;
13672 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040013673 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13674
James Smart2e90f4b2011-12-13 13:22:37 -050013675 /* sanity check on queue memory */
13676 if (!eq)
13677 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013678 if (!phba->sli4_hba.pc_sli4_params.supported)
13679 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013680
13681 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13682 if (!mbox)
13683 return -ENOMEM;
13684 length = (sizeof(struct lpfc_mbx_eq_create) -
13685 sizeof(struct lpfc_sli4_cfg_mhdr));
13686 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13687 LPFC_MBOX_OPCODE_EQ_CREATE,
13688 length, LPFC_SLI4_MBX_EMBED);
13689 eq_create = &mbox->u.mqe.un.eq_create;
13690 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13691 eq->page_count);
13692 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13693 LPFC_EQE_SIZE);
13694 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040013695 /* don't setup delay multiplier using EQ_CREATE */
13696 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040013697 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13698 dmult);
13699 switch (eq->entry_count) {
13700 default:
13701 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13702 "0360 Unsupported EQ count. (%d)\n",
13703 eq->entry_count);
13704 if (eq->entry_count < 256)
13705 return -EINVAL;
13706 /* otherwise default to smallest count (drop through) */
13707 case 256:
13708 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13709 LPFC_EQ_CNT_256);
13710 break;
13711 case 512:
13712 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13713 LPFC_EQ_CNT_512);
13714 break;
13715 case 1024:
13716 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13717 LPFC_EQ_CNT_1024);
13718 break;
13719 case 2048:
13720 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13721 LPFC_EQ_CNT_2048);
13722 break;
13723 case 4096:
13724 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13725 LPFC_EQ_CNT_4096);
13726 break;
13727 }
13728 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013729 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013730 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13731 putPaddrLow(dmabuf->phys);
13732 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13733 putPaddrHigh(dmabuf->phys);
13734 }
13735 mbox->vport = phba->pport;
13736 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13737 mbox->context1 = NULL;
13738 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13739 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13740 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13741 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13742 if (shdr_status || shdr_add_status || rc) {
13743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13744 "2500 EQ_CREATE mailbox failed with "
13745 "status x%x add_status x%x, mbx status x%x\n",
13746 shdr_status, shdr_add_status, rc);
13747 status = -ENXIO;
13748 }
13749 eq->type = LPFC_EQ;
13750 eq->subtype = LPFC_NONE;
13751 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13752 if (eq->queue_id == 0xFFFF)
13753 status = -ENXIO;
13754 eq->host_index = 0;
13755 eq->hba_index = 0;
13756
James Smart8fa38512009-07-19 10:01:03 -040013757 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013758 return status;
13759}
13760
13761/**
13762 * lpfc_cq_create - Create a Completion Queue on the HBA
13763 * @phba: HBA structure that indicates port to create a queue on.
13764 * @cq: The queue structure to use to create the completion queue.
13765 * @eq: The event queue to bind this completion queue to.
13766 *
13767 * This function creates a completion queue, as detailed in @wq, on a port,
13768 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13769 *
13770 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13771 * is used to get the entry count and entry size that are necessary to
13772 * determine the number of pages to allocate and use for this queue. The @eq
13773 * is used to indicate which event queue to bind this completion queue to. This
13774 * function will send the CQ_CREATE mailbox command to the HBA to setup the
13775 * completion queue. This function is asynchronous and will wait for the mailbox
13776 * command to finish before continuing.
13777 *
13778 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013779 * memory this function will return -ENOMEM. If the queue create mailbox command
13780 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013781 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013782int
James Smart4f774512009-05-22 14:52:35 -040013783lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13784 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13785{
13786 struct lpfc_mbx_cq_create *cq_create;
13787 struct lpfc_dmabuf *dmabuf;
13788 LPFC_MBOXQ_t *mbox;
13789 int rc, length, status = 0;
13790 uint32_t shdr_status, shdr_add_status;
13791 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013792 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13793
James Smart2e90f4b2011-12-13 13:22:37 -050013794 /* sanity check on queue memory */
13795 if (!cq || !eq)
13796 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013797 if (!phba->sli4_hba.pc_sli4_params.supported)
13798 hw_page_size = SLI4_PAGE_SIZE;
13799
James Smart4f774512009-05-22 14:52:35 -040013800 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13801 if (!mbox)
13802 return -ENOMEM;
13803 length = (sizeof(struct lpfc_mbx_cq_create) -
13804 sizeof(struct lpfc_sli4_cfg_mhdr));
13805 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13806 LPFC_MBOX_OPCODE_CQ_CREATE,
13807 length, LPFC_SLI4_MBX_EMBED);
13808 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013809 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013810 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13811 cq->page_count);
13812 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13813 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013814 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13815 phba->sli4_hba.pc_sli4_params.cqv);
13816 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040013817 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13818 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013819 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13820 eq->queue_id);
13821 } else {
13822 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13823 eq->queue_id);
13824 }
James Smart4f774512009-05-22 14:52:35 -040013825 switch (cq->entry_count) {
13826 default:
13827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080013828 "0361 Unsupported CQ count: "
13829 "entry cnt %d sz %d pg cnt %d repost %d\n",
13830 cq->entry_count, cq->entry_size,
13831 cq->page_count, cq->entry_repost);
James Smart4f4c1862012-06-12 13:54:02 -040013832 if (cq->entry_count < 256) {
13833 status = -EINVAL;
13834 goto out;
13835 }
James Smart4f774512009-05-22 14:52:35 -040013836 /* otherwise default to smallest count (drop through) */
13837 case 256:
13838 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13839 LPFC_CQ_CNT_256);
13840 break;
13841 case 512:
13842 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13843 LPFC_CQ_CNT_512);
13844 break;
13845 case 1024:
13846 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13847 LPFC_CQ_CNT_1024);
13848 break;
13849 }
13850 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013851 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013852 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13853 putPaddrLow(dmabuf->phys);
13854 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13855 putPaddrHigh(dmabuf->phys);
13856 }
13857 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13858
13859 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013860 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13861 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13862 if (shdr_status || shdr_add_status || rc) {
13863 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13864 "2501 CQ_CREATE mailbox failed with "
13865 "status x%x add_status x%x, mbx status x%x\n",
13866 shdr_status, shdr_add_status, rc);
13867 status = -ENXIO;
13868 goto out;
13869 }
13870 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13871 if (cq->queue_id == 0xFFFF) {
13872 status = -ENXIO;
13873 goto out;
13874 }
13875 /* link the cq onto the parent eq child list */
13876 list_add_tail(&cq->list, &eq->child_list);
13877 /* Set up completion queue's type and subtype */
13878 cq->type = type;
13879 cq->subtype = subtype;
13880 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050013881 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013882 cq->host_index = 0;
13883 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040013884
James Smart8fa38512009-07-19 10:01:03 -040013885out:
13886 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013887 return status;
13888}
13889
13890/**
James Smartb19a0612010-04-06 14:48:51 -040013891 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040013892 * @phba: HBA structure that indicates port to create a queue on.
13893 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040013894 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13895 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040013896 *
James Smartb19a0612010-04-06 14:48:51 -040013897 * This function provides failback (fb) functionality when the
13898 * mq_create_ext fails on older FW generations. It's purpose is identical
13899 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040013900 *
James Smartb19a0612010-04-06 14:48:51 -040013901 * This routine cannot fail as all attributes were previously accessed and
13902 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040013903 **/
James Smartb19a0612010-04-06 14:48:51 -040013904static void
13905lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13906 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040013907{
13908 struct lpfc_mbx_mq_create *mq_create;
13909 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040013910 int length;
James Smart04c68492009-05-22 14:52:52 -040013911
James Smart04c68492009-05-22 14:52:52 -040013912 length = (sizeof(struct lpfc_mbx_mq_create) -
13913 sizeof(struct lpfc_sli4_cfg_mhdr));
13914 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13915 LPFC_MBOX_OPCODE_MQ_CREATE,
13916 length, LPFC_SLI4_MBX_EMBED);
13917 mq_create = &mbox->u.mqe.un.mq_create;
13918 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040013919 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040013920 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040013921 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040013922 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13923 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040013924 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013925 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13926 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040013927 break;
13928 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013929 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13930 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040013931 break;
13932 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013933 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13934 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040013935 break;
13936 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013937 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13938 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040013939 break;
13940 }
13941 list_for_each_entry(dmabuf, &mq->page_list, list) {
13942 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040013943 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040013944 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040013945 putPaddrHigh(dmabuf->phys);
13946 }
13947}
13948
13949/**
13950 * lpfc_mq_create - Create a mailbox Queue on the HBA
13951 * @phba: HBA structure that indicates port to create a queue on.
13952 * @mq: The queue structure to use to create the mailbox queue.
13953 * @cq: The completion queue to associate with this cq.
13954 * @subtype: The queue's subtype.
13955 *
13956 * This function creates a mailbox queue, as detailed in @mq, on a port,
13957 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13958 *
13959 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13960 * is used to get the entry count and entry size that are necessary to
13961 * determine the number of pages to allocate and use for this queue. This
13962 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13963 * mailbox queue. This function is asynchronous and will wait for the mailbox
13964 * command to finish before continuing.
13965 *
13966 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013967 * memory this function will return -ENOMEM. If the queue create mailbox command
13968 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040013969 **/
13970int32_t
13971lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13972 struct lpfc_queue *cq, uint32_t subtype)
13973{
13974 struct lpfc_mbx_mq_create *mq_create;
13975 struct lpfc_mbx_mq_create_ext *mq_create_ext;
13976 struct lpfc_dmabuf *dmabuf;
13977 LPFC_MBOXQ_t *mbox;
13978 int rc, length, status = 0;
13979 uint32_t shdr_status, shdr_add_status;
13980 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013981 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040013982
James Smart2e90f4b2011-12-13 13:22:37 -050013983 /* sanity check on queue memory */
13984 if (!mq || !cq)
13985 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013986 if (!phba->sli4_hba.pc_sli4_params.supported)
13987 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040013988
13989 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13990 if (!mbox)
13991 return -ENOMEM;
13992 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13993 sizeof(struct lpfc_sli4_cfg_mhdr));
13994 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13995 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13996 length, LPFC_SLI4_MBX_EMBED);
13997
13998 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050013999 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050014000 bf_set(lpfc_mbx_mq_create_ext_num_pages,
14001 &mq_create_ext->u.request, mq->page_count);
14002 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
14003 &mq_create_ext->u.request, 1);
14004 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040014005 &mq_create_ext->u.request, 1);
14006 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
14007 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050014008 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
14009 &mq_create_ext->u.request, 1);
14010 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
14011 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040014012 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050014013 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14014 phba->sli4_hba.pc_sli4_params.mqv);
14015 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
14016 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
14017 cq->queue_id);
14018 else
14019 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
14020 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040014021 switch (mq->entry_count) {
14022 default:
14023 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14024 "0362 Unsupported MQ count. (%d)\n",
14025 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014026 if (mq->entry_count < 16) {
14027 status = -EINVAL;
14028 goto out;
14029 }
James Smartb19a0612010-04-06 14:48:51 -040014030 /* otherwise default to smallest count (drop through) */
14031 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050014032 bf_set(lpfc_mq_context_ring_size,
14033 &mq_create_ext->u.request.context,
14034 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040014035 break;
14036 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050014037 bf_set(lpfc_mq_context_ring_size,
14038 &mq_create_ext->u.request.context,
14039 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040014040 break;
14041 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050014042 bf_set(lpfc_mq_context_ring_size,
14043 &mq_create_ext->u.request.context,
14044 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040014045 break;
14046 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050014047 bf_set(lpfc_mq_context_ring_size,
14048 &mq_create_ext->u.request.context,
14049 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040014050 break;
14051 }
14052 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014053 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040014054 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
14055 putPaddrLow(dmabuf->phys);
14056 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040014057 putPaddrHigh(dmabuf->phys);
14058 }
14059 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040014060 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14061 &mq_create_ext->u.response);
14062 if (rc != MBX_SUCCESS) {
14063 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
14064 "2795 MQ_CREATE_EXT failed with "
14065 "status x%x. Failback to MQ_CREATE.\n",
14066 rc);
14067 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
14068 mq_create = &mbox->u.mqe.un.mq_create;
14069 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14070 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
14071 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
14072 &mq_create->u.response);
14073 }
14074
James Smart04c68492009-05-22 14:52:52 -040014075 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040014076 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14077 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14078 if (shdr_status || shdr_add_status || rc) {
14079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14080 "2502 MQ_CREATE mailbox failed with "
14081 "status x%x add_status x%x, mbx status x%x\n",
14082 shdr_status, shdr_add_status, rc);
14083 status = -ENXIO;
14084 goto out;
14085 }
James Smart04c68492009-05-22 14:52:52 -040014086 if (mq->queue_id == 0xFFFF) {
14087 status = -ENXIO;
14088 goto out;
14089 }
14090 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050014091 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040014092 mq->subtype = subtype;
14093 mq->host_index = 0;
14094 mq->hba_index = 0;
14095
14096 /* link the mq onto the parent cq child list */
14097 list_add_tail(&mq->list, &cq->child_list);
14098out:
James Smart8fa38512009-07-19 10:01:03 -040014099 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014100 return status;
14101}
14102
14103/**
James Smart4f774512009-05-22 14:52:35 -040014104 * lpfc_wq_create - Create a Work Queue on the HBA
14105 * @phba: HBA structure that indicates port to create a queue on.
14106 * @wq: The queue structure to use to create the work queue.
14107 * @cq: The completion queue to bind this work queue to.
14108 * @subtype: The subtype of the work queue indicating its functionality.
14109 *
14110 * This function creates a work queue, as detailed in @wq, on a port, described
14111 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
14112 *
14113 * The @phba struct is used to send mailbox command to HBA. The @wq struct
14114 * is used to get the entry count and entry size that are necessary to
14115 * determine the number of pages to allocate and use for this queue. The @cq
14116 * is used to indicate which completion queue to bind this work queue to. This
14117 * function will send the WQ_CREATE mailbox command to the HBA to setup the
14118 * work queue. This function is asynchronous and will wait for the mailbox
14119 * command to finish before continuing.
14120 *
14121 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014122 * memory this function will return -ENOMEM. If the queue create mailbox command
14123 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014124 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014125int
James Smart4f774512009-05-22 14:52:35 -040014126lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
14127 struct lpfc_queue *cq, uint32_t subtype)
14128{
14129 struct lpfc_mbx_wq_create *wq_create;
14130 struct lpfc_dmabuf *dmabuf;
14131 LPFC_MBOXQ_t *mbox;
14132 int rc, length, status = 0;
14133 uint32_t shdr_status, shdr_add_status;
14134 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014135 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050014136 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050014137 void __iomem *bar_memmap_p;
14138 uint32_t db_offset;
14139 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040014140
James Smart2e90f4b2011-12-13 13:22:37 -050014141 /* sanity check on queue memory */
14142 if (!wq || !cq)
14143 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014144 if (!phba->sli4_hba.pc_sli4_params.supported)
14145 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014146
14147 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14148 if (!mbox)
14149 return -ENOMEM;
14150 length = (sizeof(struct lpfc_mbx_wq_create) -
14151 sizeof(struct lpfc_sli4_cfg_mhdr));
14152 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14153 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
14154 length, LPFC_SLI4_MBX_EMBED);
14155 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014156 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040014157 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
14158 wq->page_count);
14159 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
14160 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040014161
14162 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050014163 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14164 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050014165
James Smart0c651872013-07-15 18:33:23 -040014166 switch (phba->sli4_hba.pc_sli4_params.wqv) {
14167 case LPFC_Q_CREATE_VERSION_0:
14168 switch (wq->entry_size) {
14169 default:
14170 case 64:
14171 /* Nothing to do, version 0 ONLY supports 64 byte */
14172 page = wq_create->u.request.page;
14173 break;
14174 case 128:
14175 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14176 LPFC_WQ_SZ128_SUPPORT)) {
14177 status = -ERANGE;
14178 goto out;
14179 }
14180 /* If we get here the HBA MUST also support V1 and
14181 * we MUST use it
14182 */
14183 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14184 LPFC_Q_CREATE_VERSION_1);
14185
14186 bf_set(lpfc_mbx_wq_create_wqe_count,
14187 &wq_create->u.request_1, wq->entry_count);
14188 bf_set(lpfc_mbx_wq_create_wqe_size,
14189 &wq_create->u.request_1,
14190 LPFC_WQ_WQE_SIZE_128);
14191 bf_set(lpfc_mbx_wq_create_page_size,
14192 &wq_create->u.request_1,
James Smart8ea73db2017-02-12 13:52:25 -080014193 LPFC_WQ_PAGE_SIZE_4096);
James Smart0c651872013-07-15 18:33:23 -040014194 page = wq_create->u.request_1.page;
14195 break;
14196 }
14197 break;
14198 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050014199 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
14200 wq->entry_count);
14201 switch (wq->entry_size) {
14202 default:
14203 case 64:
14204 bf_set(lpfc_mbx_wq_create_wqe_size,
14205 &wq_create->u.request_1,
14206 LPFC_WQ_WQE_SIZE_64);
14207 break;
14208 case 128:
James Smart0c651872013-07-15 18:33:23 -040014209 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
14210 LPFC_WQ_SZ128_SUPPORT)) {
14211 status = -ERANGE;
14212 goto out;
14213 }
James Smart5a6f1332011-03-11 16:05:35 -050014214 bf_set(lpfc_mbx_wq_create_wqe_size,
14215 &wq_create->u.request_1,
14216 LPFC_WQ_WQE_SIZE_128);
14217 break;
14218 }
James Smart8ea73db2017-02-12 13:52:25 -080014219 bf_set(lpfc_mbx_wq_create_page_size,
14220 &wq_create->u.request_1,
14221 LPFC_WQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050014222 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040014223 break;
14224 default:
14225 status = -ERANGE;
14226 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050014227 }
James Smart0c651872013-07-15 18:33:23 -040014228
James Smart4f774512009-05-22 14:52:35 -040014229 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014230 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050014231 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
14232 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040014233 }
James Smart962bc512013-01-03 15:44:00 -050014234
14235 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14236 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
14237
James Smart4f774512009-05-22 14:52:35 -040014238 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14239 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014240 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14241 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14242 if (shdr_status || shdr_add_status || rc) {
14243 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14244 "2503 WQ_CREATE mailbox failed with "
14245 "status x%x add_status x%x, mbx status x%x\n",
14246 shdr_status, shdr_add_status, rc);
14247 status = -ENXIO;
14248 goto out;
14249 }
14250 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
14251 if (wq->queue_id == 0xFFFF) {
14252 status = -ENXIO;
14253 goto out;
14254 }
James Smart962bc512013-01-03 15:44:00 -050014255 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14256 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
14257 &wq_create->u.response);
14258 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
14259 (wq->db_format != LPFC_DB_RING_FORMAT)) {
14260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14261 "3265 WQ[%d] doorbell format not "
14262 "supported: x%x\n", wq->queue_id,
14263 wq->db_format);
14264 status = -EINVAL;
14265 goto out;
14266 }
14267 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
14268 &wq_create->u.response);
14269 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14270 if (!bar_memmap_p) {
14271 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14272 "3263 WQ[%d] failed to memmap pci "
14273 "barset:x%x\n", wq->queue_id,
14274 pci_barset);
14275 status = -ENOMEM;
14276 goto out;
14277 }
14278 db_offset = wq_create->u.response.doorbell_offset;
14279 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
14280 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
14281 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14282 "3252 WQ[%d] doorbell offset not "
14283 "supported: x%x\n", wq->queue_id,
14284 db_offset);
14285 status = -EINVAL;
14286 goto out;
14287 }
14288 wq->db_regaddr = bar_memmap_p + db_offset;
14289 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040014290 "3264 WQ[%d]: barset:x%x, offset:x%x, "
14291 "format:x%x\n", wq->queue_id, pci_barset,
14292 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014293 } else {
14294 wq->db_format = LPFC_DB_LIST_FORMAT;
14295 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
14296 }
James Smart895427b2017-02-12 13:52:30 -080014297 wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
14298 if (wq->pring == NULL) {
14299 status = -ENOMEM;
14300 goto out;
14301 }
James Smart4f774512009-05-22 14:52:35 -040014302 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050014303 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014304 wq->subtype = subtype;
14305 wq->host_index = 0;
14306 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050014307 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040014308
14309 /* link the wq onto the parent cq child list */
14310 list_add_tail(&wq->list, &cq->child_list);
14311out:
James Smart8fa38512009-07-19 10:01:03 -040014312 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014313 return status;
14314}
14315
14316/**
James Smart73d91e52011-10-10 21:32:10 -040014317 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
14318 * @phba: HBA structure that indicates port to create a queue on.
14319 * @rq: The queue structure to use for the receive queue.
14320 * @qno: The associated HBQ number
14321 *
14322 *
14323 * For SLI4 we need to adjust the RQ repost value based on
14324 * the number of buffers that are initially posted to the RQ.
14325 */
14326void
14327lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
14328{
14329 uint32_t cnt;
14330
James Smart2e90f4b2011-12-13 13:22:37 -050014331 /* sanity check on queue memory */
14332 if (!rq)
14333 return;
James Smart73d91e52011-10-10 21:32:10 -040014334 cnt = lpfc_hbq_defs[qno]->entry_count;
14335
14336 /* Recalc repost for RQs based on buffers initially posted */
14337 cnt = (cnt >> 3);
14338 if (cnt < LPFC_QUEUE_MIN_REPOST)
14339 cnt = LPFC_QUEUE_MIN_REPOST;
14340
14341 rq->entry_repost = cnt;
14342}
14343
14344/**
James Smart4f774512009-05-22 14:52:35 -040014345 * lpfc_rq_create - Create a Receive Queue on the HBA
14346 * @phba: HBA structure that indicates port to create a queue on.
14347 * @hrq: The queue structure to use to create the header receive queue.
14348 * @drq: The queue structure to use to create the data receive queue.
14349 * @cq: The completion queue to bind this work queue to.
14350 *
14351 * This function creates a receive buffer queue pair , as detailed in @hrq and
14352 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
14353 * to the HBA.
14354 *
14355 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
14356 * struct is used to get the entry count that is necessary to determine the
14357 * number of pages to use for this queue. The @cq is used to indicate which
14358 * completion queue to bind received buffers that are posted to these queues to.
14359 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
14360 * receive queue pair. This function is asynchronous and will wait for the
14361 * mailbox command to finish before continuing.
14362 *
14363 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040014364 * memory this function will return -ENOMEM. If the queue create mailbox command
14365 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014366 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014367int
James Smart4f774512009-05-22 14:52:35 -040014368lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14369 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
14370{
14371 struct lpfc_mbx_rq_create *rq_create;
14372 struct lpfc_dmabuf *dmabuf;
14373 LPFC_MBOXQ_t *mbox;
14374 int rc, length, status = 0;
14375 uint32_t shdr_status, shdr_add_status;
14376 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040014377 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050014378 void __iomem *bar_memmap_p;
14379 uint32_t db_offset;
14380 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040014381
James Smart2e90f4b2011-12-13 13:22:37 -050014382 /* sanity check on queue memory */
14383 if (!hrq || !drq || !cq)
14384 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040014385 if (!phba->sli4_hba.pc_sli4_params.supported)
14386 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040014387
14388 if (hrq->entry_count != drq->entry_count)
14389 return -EINVAL;
14390 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14391 if (!mbox)
14392 return -ENOMEM;
14393 length = (sizeof(struct lpfc_mbx_rq_create) -
14394 sizeof(struct lpfc_sli4_cfg_mhdr));
14395 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14396 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14397 length, LPFC_SLI4_MBX_EMBED);
14398 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050014399 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14400 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14401 phba->sli4_hba.pc_sli4_params.rqv);
14402 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14403 bf_set(lpfc_rq_context_rqe_count_1,
14404 &rq_create->u.request.context,
14405 hrq->entry_count);
14406 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040014407 bf_set(lpfc_rq_context_rqe_size,
14408 &rq_create->u.request.context,
14409 LPFC_RQE_SIZE_8);
14410 bf_set(lpfc_rq_context_page_size,
14411 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080014412 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050014413 } else {
14414 switch (hrq->entry_count) {
14415 default:
14416 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14417 "2535 Unsupported RQ count. (%d)\n",
14418 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014419 if (hrq->entry_count < 512) {
14420 status = -EINVAL;
14421 goto out;
14422 }
James Smart5a6f1332011-03-11 16:05:35 -050014423 /* otherwise default to smallest count (drop through) */
14424 case 512:
14425 bf_set(lpfc_rq_context_rqe_count,
14426 &rq_create->u.request.context,
14427 LPFC_RQ_RING_SIZE_512);
14428 break;
14429 case 1024:
14430 bf_set(lpfc_rq_context_rqe_count,
14431 &rq_create->u.request.context,
14432 LPFC_RQ_RING_SIZE_1024);
14433 break;
14434 case 2048:
14435 bf_set(lpfc_rq_context_rqe_count,
14436 &rq_create->u.request.context,
14437 LPFC_RQ_RING_SIZE_2048);
14438 break;
14439 case 4096:
14440 bf_set(lpfc_rq_context_rqe_count,
14441 &rq_create->u.request.context,
14442 LPFC_RQ_RING_SIZE_4096);
14443 break;
14444 }
14445 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14446 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040014447 }
14448 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14449 cq->queue_id);
14450 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14451 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040014452 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040014453 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040014454 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14455 putPaddrLow(dmabuf->phys);
14456 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14457 putPaddrHigh(dmabuf->phys);
14458 }
James Smart962bc512013-01-03 15:44:00 -050014459 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14460 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
14461
James Smart4f774512009-05-22 14:52:35 -040014462 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14463 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040014464 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14465 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14466 if (shdr_status || shdr_add_status || rc) {
14467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14468 "2504 RQ_CREATE mailbox failed with "
14469 "status x%x add_status x%x, mbx status x%x\n",
14470 shdr_status, shdr_add_status, rc);
14471 status = -ENXIO;
14472 goto out;
14473 }
14474 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14475 if (hrq->queue_id == 0xFFFF) {
14476 status = -ENXIO;
14477 goto out;
14478 }
James Smart962bc512013-01-03 15:44:00 -050014479
14480 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14481 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
14482 &rq_create->u.response);
14483 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
14484 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
14485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14486 "3262 RQ [%d] doorbell format not "
14487 "supported: x%x\n", hrq->queue_id,
14488 hrq->db_format);
14489 status = -EINVAL;
14490 goto out;
14491 }
14492
14493 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
14494 &rq_create->u.response);
14495 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14496 if (!bar_memmap_p) {
14497 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14498 "3269 RQ[%d] failed to memmap pci "
14499 "barset:x%x\n", hrq->queue_id,
14500 pci_barset);
14501 status = -ENOMEM;
14502 goto out;
14503 }
14504
14505 db_offset = rq_create->u.response.doorbell_offset;
14506 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
14507 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
14508 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14509 "3270 RQ[%d] doorbell offset not "
14510 "supported: x%x\n", hrq->queue_id,
14511 db_offset);
14512 status = -EINVAL;
14513 goto out;
14514 }
14515 hrq->db_regaddr = bar_memmap_p + db_offset;
14516 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040014517 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
14518 "format:x%x\n", hrq->queue_id, pci_barset,
14519 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014520 } else {
14521 hrq->db_format = LPFC_DB_RING_FORMAT;
14522 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
14523 }
James Smart4f774512009-05-22 14:52:35 -040014524 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014525 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014526 hrq->subtype = subtype;
14527 hrq->host_index = 0;
14528 hrq->hba_index = 0;
14529
14530 /* now create the data queue */
14531 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14532 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14533 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050014534 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14535 phba->sli4_hba.pc_sli4_params.rqv);
14536 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14537 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040014538 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050014539 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040014540 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
14541 LPFC_RQE_SIZE_8);
14542 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
14543 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014544 } else {
14545 switch (drq->entry_count) {
14546 default:
14547 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14548 "2536 Unsupported RQ count. (%d)\n",
14549 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014550 if (drq->entry_count < 512) {
14551 status = -EINVAL;
14552 goto out;
14553 }
James Smart5a6f1332011-03-11 16:05:35 -050014554 /* otherwise default to smallest count (drop through) */
14555 case 512:
14556 bf_set(lpfc_rq_context_rqe_count,
14557 &rq_create->u.request.context,
14558 LPFC_RQ_RING_SIZE_512);
14559 break;
14560 case 1024:
14561 bf_set(lpfc_rq_context_rqe_count,
14562 &rq_create->u.request.context,
14563 LPFC_RQ_RING_SIZE_1024);
14564 break;
14565 case 2048:
14566 bf_set(lpfc_rq_context_rqe_count,
14567 &rq_create->u.request.context,
14568 LPFC_RQ_RING_SIZE_2048);
14569 break;
14570 case 4096:
14571 bf_set(lpfc_rq_context_rqe_count,
14572 &rq_create->u.request.context,
14573 LPFC_RQ_RING_SIZE_4096);
14574 break;
14575 }
14576 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14577 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040014578 }
14579 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14580 cq->queue_id);
14581 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14582 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040014583 list_for_each_entry(dmabuf, &drq->page_list, list) {
14584 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14585 putPaddrLow(dmabuf->phys);
14586 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14587 putPaddrHigh(dmabuf->phys);
14588 }
James Smart962bc512013-01-03 15:44:00 -050014589 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14590 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040014591 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14592 /* The IOCTL status is embedded in the mailbox subheader. */
14593 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14594 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14595 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14596 if (shdr_status || shdr_add_status || rc) {
14597 status = -ENXIO;
14598 goto out;
14599 }
14600 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14601 if (drq->queue_id == 0xFFFF) {
14602 status = -ENXIO;
14603 goto out;
14604 }
14605 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014606 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014607 drq->subtype = subtype;
14608 drq->host_index = 0;
14609 drq->hba_index = 0;
14610
14611 /* link the header and data RQs onto the parent cq child list */
14612 list_add_tail(&hrq->list, &cq->child_list);
14613 list_add_tail(&drq->list, &cq->child_list);
14614
14615out:
James Smart8fa38512009-07-19 10:01:03 -040014616 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014617 return status;
14618}
14619
14620/**
14621 * lpfc_eq_destroy - Destroy an event Queue on the HBA
14622 * @eq: The queue structure associated with the queue to destroy.
14623 *
14624 * This function destroys a queue, as detailed in @eq by sending an mailbox
14625 * command, specific to the type of queue, to the HBA.
14626 *
14627 * The @eq struct is used to get the queue ID of the queue to destroy.
14628 *
14629 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014630 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014631 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014632int
James Smart4f774512009-05-22 14:52:35 -040014633lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
14634{
14635 LPFC_MBOXQ_t *mbox;
14636 int rc, length, status = 0;
14637 uint32_t shdr_status, shdr_add_status;
14638 union lpfc_sli4_cfg_shdr *shdr;
14639
James Smart2e90f4b2011-12-13 13:22:37 -050014640 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014641 if (!eq)
14642 return -ENODEV;
14643 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
14644 if (!mbox)
14645 return -ENOMEM;
14646 length = (sizeof(struct lpfc_mbx_eq_destroy) -
14647 sizeof(struct lpfc_sli4_cfg_mhdr));
14648 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14649 LPFC_MBOX_OPCODE_EQ_DESTROY,
14650 length, LPFC_SLI4_MBX_EMBED);
14651 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
14652 eq->queue_id);
14653 mbox->vport = eq->phba->pport;
14654 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14655
14656 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
14657 /* The IOCTL status is embedded in the mailbox subheader. */
14658 shdr = (union lpfc_sli4_cfg_shdr *)
14659 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
14660 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14661 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14662 if (shdr_status || shdr_add_status || rc) {
14663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14664 "2505 EQ_DESTROY mailbox failed with "
14665 "status x%x add_status x%x, mbx status x%x\n",
14666 shdr_status, shdr_add_status, rc);
14667 status = -ENXIO;
14668 }
14669
14670 /* Remove eq from any list */
14671 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040014672 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014673 return status;
14674}
14675
14676/**
14677 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
14678 * @cq: The queue structure associated with the queue to destroy.
14679 *
14680 * This function destroys a queue, as detailed in @cq by sending an mailbox
14681 * command, specific to the type of queue, to the HBA.
14682 *
14683 * The @cq struct is used to get the queue ID of the queue to destroy.
14684 *
14685 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014686 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014687 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014688int
James Smart4f774512009-05-22 14:52:35 -040014689lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14690{
14691 LPFC_MBOXQ_t *mbox;
14692 int rc, length, status = 0;
14693 uint32_t shdr_status, shdr_add_status;
14694 union lpfc_sli4_cfg_shdr *shdr;
14695
James Smart2e90f4b2011-12-13 13:22:37 -050014696 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014697 if (!cq)
14698 return -ENODEV;
14699 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14700 if (!mbox)
14701 return -ENOMEM;
14702 length = (sizeof(struct lpfc_mbx_cq_destroy) -
14703 sizeof(struct lpfc_sli4_cfg_mhdr));
14704 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14705 LPFC_MBOX_OPCODE_CQ_DESTROY,
14706 length, LPFC_SLI4_MBX_EMBED);
14707 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14708 cq->queue_id);
14709 mbox->vport = cq->phba->pport;
14710 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14711 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14712 /* The IOCTL status is embedded in the mailbox subheader. */
14713 shdr = (union lpfc_sli4_cfg_shdr *)
14714 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14715 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14716 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14717 if (shdr_status || shdr_add_status || rc) {
14718 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14719 "2506 CQ_DESTROY mailbox failed with "
14720 "status x%x add_status x%x, mbx status x%x\n",
14721 shdr_status, shdr_add_status, rc);
14722 status = -ENXIO;
14723 }
14724 /* Remove cq from any list */
14725 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040014726 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014727 return status;
14728}
14729
14730/**
James Smart04c68492009-05-22 14:52:52 -040014731 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14732 * @qm: The queue structure associated with the queue to destroy.
14733 *
14734 * This function destroys a queue, as detailed in @mq by sending an mailbox
14735 * command, specific to the type of queue, to the HBA.
14736 *
14737 * The @mq struct is used to get the queue ID of the queue to destroy.
14738 *
14739 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014740 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040014741 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014742int
James Smart04c68492009-05-22 14:52:52 -040014743lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14744{
14745 LPFC_MBOXQ_t *mbox;
14746 int rc, length, status = 0;
14747 uint32_t shdr_status, shdr_add_status;
14748 union lpfc_sli4_cfg_shdr *shdr;
14749
James Smart2e90f4b2011-12-13 13:22:37 -050014750 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040014751 if (!mq)
14752 return -ENODEV;
14753 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14754 if (!mbox)
14755 return -ENOMEM;
14756 length = (sizeof(struct lpfc_mbx_mq_destroy) -
14757 sizeof(struct lpfc_sli4_cfg_mhdr));
14758 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14759 LPFC_MBOX_OPCODE_MQ_DESTROY,
14760 length, LPFC_SLI4_MBX_EMBED);
14761 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14762 mq->queue_id);
14763 mbox->vport = mq->phba->pport;
14764 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14765 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14766 /* The IOCTL status is embedded in the mailbox subheader. */
14767 shdr = (union lpfc_sli4_cfg_shdr *)
14768 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14769 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14770 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14771 if (shdr_status || shdr_add_status || rc) {
14772 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14773 "2507 MQ_DESTROY mailbox failed with "
14774 "status x%x add_status x%x, mbx status x%x\n",
14775 shdr_status, shdr_add_status, rc);
14776 status = -ENXIO;
14777 }
14778 /* Remove mq from any list */
14779 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040014780 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014781 return status;
14782}
14783
14784/**
James Smart4f774512009-05-22 14:52:35 -040014785 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14786 * @wq: The queue structure associated with the queue to destroy.
14787 *
14788 * This function destroys a queue, as detailed in @wq by sending an mailbox
14789 * command, specific to the type of queue, to the HBA.
14790 *
14791 * The @wq struct is used to get the queue ID of the queue to destroy.
14792 *
14793 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014794 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014795 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014796int
James Smart4f774512009-05-22 14:52:35 -040014797lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14798{
14799 LPFC_MBOXQ_t *mbox;
14800 int rc, length, status = 0;
14801 uint32_t shdr_status, shdr_add_status;
14802 union lpfc_sli4_cfg_shdr *shdr;
14803
James Smart2e90f4b2011-12-13 13:22:37 -050014804 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014805 if (!wq)
14806 return -ENODEV;
14807 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14808 if (!mbox)
14809 return -ENOMEM;
14810 length = (sizeof(struct lpfc_mbx_wq_destroy) -
14811 sizeof(struct lpfc_sli4_cfg_mhdr));
14812 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14813 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14814 length, LPFC_SLI4_MBX_EMBED);
14815 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14816 wq->queue_id);
14817 mbox->vport = wq->phba->pport;
14818 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14819 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14820 shdr = (union lpfc_sli4_cfg_shdr *)
14821 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14822 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14823 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14824 if (shdr_status || shdr_add_status || rc) {
14825 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14826 "2508 WQ_DESTROY mailbox failed with "
14827 "status x%x add_status x%x, mbx status x%x\n",
14828 shdr_status, shdr_add_status, rc);
14829 status = -ENXIO;
14830 }
14831 /* Remove wq from any list */
14832 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040014833 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014834 return status;
14835}
14836
14837/**
14838 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14839 * @rq: The queue structure associated with the queue to destroy.
14840 *
14841 * This function destroys a queue, as detailed in @rq by sending an mailbox
14842 * command, specific to the type of queue, to the HBA.
14843 *
14844 * The @rq struct is used to get the queue ID of the queue to destroy.
14845 *
14846 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014847 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014848 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014849int
James Smart4f774512009-05-22 14:52:35 -040014850lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14851 struct lpfc_queue *drq)
14852{
14853 LPFC_MBOXQ_t *mbox;
14854 int rc, length, status = 0;
14855 uint32_t shdr_status, shdr_add_status;
14856 union lpfc_sli4_cfg_shdr *shdr;
14857
James Smart2e90f4b2011-12-13 13:22:37 -050014858 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014859 if (!hrq || !drq)
14860 return -ENODEV;
14861 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14862 if (!mbox)
14863 return -ENOMEM;
14864 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050014865 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040014866 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14867 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14868 length, LPFC_SLI4_MBX_EMBED);
14869 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14870 hrq->queue_id);
14871 mbox->vport = hrq->phba->pport;
14872 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14873 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14874 /* The IOCTL status is embedded in the mailbox subheader. */
14875 shdr = (union lpfc_sli4_cfg_shdr *)
14876 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14877 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14878 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14879 if (shdr_status || shdr_add_status || rc) {
14880 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14881 "2509 RQ_DESTROY mailbox failed with "
14882 "status x%x add_status x%x, mbx status x%x\n",
14883 shdr_status, shdr_add_status, rc);
14884 if (rc != MBX_TIMEOUT)
14885 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14886 return -ENXIO;
14887 }
14888 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14889 drq->queue_id);
14890 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14891 shdr = (union lpfc_sli4_cfg_shdr *)
14892 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14893 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14894 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14895 if (shdr_status || shdr_add_status || rc) {
14896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14897 "2510 RQ_DESTROY mailbox failed with "
14898 "status x%x add_status x%x, mbx status x%x\n",
14899 shdr_status, shdr_add_status, rc);
14900 status = -ENXIO;
14901 }
14902 list_del_init(&hrq->list);
14903 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040014904 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014905 return status;
14906}
14907
14908/**
14909 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14910 * @phba: The virtual port for which this call being executed.
14911 * @pdma_phys_addr0: Physical address of the 1st SGL page.
14912 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14913 * @xritag: the xritag that ties this io to the SGL pages.
14914 *
14915 * This routine will post the sgl pages for the IO that has the xritag
14916 * that is in the iocbq structure. The xritag is assigned during iocbq
14917 * creation and persists for as long as the driver is loaded.
14918 * if the caller has fewer than 256 scatter gather segments to map then
14919 * pdma_phys_addr1 should be 0.
14920 * If the caller needs to map more than 256 scatter gather segment then
14921 * pdma_phys_addr1 should be a valid physical address.
14922 * physical address for SGLs must be 64 byte aligned.
14923 * If you are going to map 2 SGL's then the first one must have 256 entries
14924 * the second sgl can have between 1 and 256 entries.
14925 *
14926 * Return codes:
14927 * 0 - Success
14928 * -ENXIO, -ENOMEM - Failure
14929 **/
14930int
14931lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14932 dma_addr_t pdma_phys_addr0,
14933 dma_addr_t pdma_phys_addr1,
14934 uint16_t xritag)
14935{
14936 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14937 LPFC_MBOXQ_t *mbox;
14938 int rc;
14939 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040014940 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040014941 union lpfc_sli4_cfg_shdr *shdr;
14942
14943 if (xritag == NO_XRI) {
14944 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14945 "0364 Invalid param:\n");
14946 return -EINVAL;
14947 }
14948
14949 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14950 if (!mbox)
14951 return -ENOMEM;
14952
14953 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14954 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14955 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050014956 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040014957
14958 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14959 &mbox->u.mqe.un.post_sgl_pages;
14960 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14961 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14962
14963 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14964 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14965 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14966 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14967
14968 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14969 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14970 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14971 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14972 if (!phba->sli4_hba.intr_enable)
14973 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040014974 else {
James Smarta183a152011-10-10 21:32:43 -040014975 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040014976 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14977 }
James Smart4f774512009-05-22 14:52:35 -040014978 /* The IOCTL status is embedded in the mailbox subheader. */
14979 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14980 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14981 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14982 if (rc != MBX_TIMEOUT)
14983 mempool_free(mbox, phba->mbox_mem_pool);
14984 if (shdr_status || shdr_add_status || rc) {
14985 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14986 "2511 POST_SGL mailbox failed with "
14987 "status x%x add_status x%x, mbx status x%x\n",
14988 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040014989 }
14990 return 0;
14991}
James Smart4f774512009-05-22 14:52:35 -040014992
14993/**
James Smart88a2cfb2011-07-22 18:36:33 -040014994 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040014995 * @phba: pointer to lpfc hba data structure.
14996 *
14997 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040014998 * HBA consistent with the SLI-4 interface spec. This routine
14999 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15000 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040015001 *
James Smart88a2cfb2011-07-22 18:36:33 -040015002 * Returns
15003 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15004 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15005 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015006static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015007lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
15008{
15009 unsigned long xri;
15010
15011 /*
15012 * Fetch the next logical xri. Because this index is logical,
15013 * the driver starts at 0 each time.
15014 */
15015 spin_lock_irq(&phba->hbalock);
15016 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
15017 phba->sli4_hba.max_cfg_param.max_xri, 0);
15018 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
15019 spin_unlock_irq(&phba->hbalock);
15020 return NO_XRI;
15021 } else {
15022 set_bit(xri, phba->sli4_hba.xri_bmask);
15023 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040015024 }
James Smart6d368e52011-05-24 11:44:12 -040015025 spin_unlock_irq(&phba->hbalock);
15026 return xri;
15027}
15028
15029/**
15030 * lpfc_sli4_free_xri - Release an xri for reuse.
15031 * @phba: pointer to lpfc hba data structure.
15032 *
15033 * This routine is invoked to release an xri to the pool of
15034 * available rpis maintained by the driver.
15035 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015036static void
James Smart6d368e52011-05-24 11:44:12 -040015037__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15038{
15039 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040015040 phba->sli4_hba.max_cfg_param.xri_used--;
15041 }
15042}
15043
15044/**
15045 * lpfc_sli4_free_xri - Release an xri for reuse.
15046 * @phba: pointer to lpfc hba data structure.
15047 *
15048 * This routine is invoked to release an xri to the pool of
15049 * available rpis maintained by the driver.
15050 **/
15051void
15052lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
15053{
15054 spin_lock_irq(&phba->hbalock);
15055 __lpfc_sli4_free_xri(phba, xri);
15056 spin_unlock_irq(&phba->hbalock);
15057}
15058
15059/**
James Smart4f774512009-05-22 14:52:35 -040015060 * lpfc_sli4_next_xritag - Get an xritag for the io
15061 * @phba: Pointer to HBA context object.
15062 *
15063 * This function gets an xritag for the iocb. If there is no unused xritag
15064 * it will return 0xffff.
15065 * The function returns the allocated xritag if successful, else returns zero.
15066 * Zero is not a valid xritag.
15067 * The caller is not required to hold any lock.
15068 **/
15069uint16_t
15070lpfc_sli4_next_xritag(struct lpfc_hba *phba)
15071{
James Smart6d368e52011-05-24 11:44:12 -040015072 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040015073
James Smart6d368e52011-05-24 11:44:12 -040015074 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040015075 if (xri_index == NO_XRI)
15076 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
15077 "2004 Failed to allocate XRI.last XRITAG is %d"
15078 " Max XRI is %d, Used XRI is %d\n",
15079 xri_index,
15080 phba->sli4_hba.max_cfg_param.max_xri,
15081 phba->sli4_hba.max_cfg_param.xri_used);
15082 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040015083}
15084
15085/**
James Smart895427b2017-02-12 13:52:30 -080015086 * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040015087 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040015088 * @post_sgl_list: pointer to els sgl entry list.
15089 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040015090 *
15091 * This routine is invoked to post a block of driver's sgl pages to the
15092 * HBA using non-embedded mailbox command. No Lock is held. This routine
15093 * is only called when the driver is loading and after all IO has been
15094 * stopped.
15095 **/
James Smart8a9d2e82012-05-09 21:16:12 -040015096static int
James Smart895427b2017-02-12 13:52:30 -080015097lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
James Smart8a9d2e82012-05-09 21:16:12 -040015098 struct list_head *post_sgl_list,
15099 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040015100{
James Smart8a9d2e82012-05-09 21:16:12 -040015101 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040015102 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15103 struct sgl_page_pairs *sgl_pg_pairs;
15104 void *viraddr;
15105 LPFC_MBOXQ_t *mbox;
15106 uint32_t reqlen, alloclen, pg_pairs;
15107 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040015108 uint16_t xritag_start = 0;
15109 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040015110 uint32_t shdr_status, shdr_add_status;
15111 union lpfc_sli4_cfg_shdr *shdr;
15112
James Smart895427b2017-02-12 13:52:30 -080015113 reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040015114 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040015115 if (reqlen > SLI4_PAGE_SIZE) {
James Smart895427b2017-02-12 13:52:30 -080015116 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart4f774512009-05-22 14:52:35 -040015117 "2559 Block sgl registration required DMA "
15118 "size (%d) great than a page\n", reqlen);
15119 return -ENOMEM;
15120 }
James Smart895427b2017-02-12 13:52:30 -080015121
James Smart4f774512009-05-22 14:52:35 -040015122 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040015123 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040015124 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040015125
15126 /* Allocate DMA memory and set up the non-embedded mailbox command */
15127 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15128 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15129 LPFC_SLI4_MBX_NEMBED);
15130
15131 if (alloclen < reqlen) {
15132 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15133 "0285 Allocated DMA memory size (%d) is "
15134 "less than the requested DMA memory "
15135 "size (%d)\n", alloclen, reqlen);
15136 lpfc_sli4_mbox_cmd_free(phba, mbox);
15137 return -ENOMEM;
15138 }
James Smart4f774512009-05-22 14:52:35 -040015139 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040015140 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040015141 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15142 sgl_pg_pairs = &sgl->sgl_pg_pairs;
15143
James Smart8a9d2e82012-05-09 21:16:12 -040015144 pg_pairs = 0;
15145 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040015146 /* Set up the sge entry */
15147 sgl_pg_pairs->sgl_pg0_addr_lo =
15148 cpu_to_le32(putPaddrLow(sglq_entry->phys));
15149 sgl_pg_pairs->sgl_pg0_addr_hi =
15150 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
15151 sgl_pg_pairs->sgl_pg1_addr_lo =
15152 cpu_to_le32(putPaddrLow(0));
15153 sgl_pg_pairs->sgl_pg1_addr_hi =
15154 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040015155
James Smart4f774512009-05-22 14:52:35 -040015156 /* Keep the first xritag on the list */
15157 if (pg_pairs == 0)
15158 xritag_start = sglq_entry->sli4_xritag;
15159 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040015160 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040015161 }
James Smart6d368e52011-05-24 11:44:12 -040015162
15163 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040015164 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart895427b2017-02-12 13:52:30 -080015165 bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
James Smart4f774512009-05-22 14:52:35 -040015166 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart895427b2017-02-12 13:52:30 -080015167
James Smart4f774512009-05-22 14:52:35 -040015168 if (!phba->sli4_hba.intr_enable)
15169 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15170 else {
James Smarta183a152011-10-10 21:32:43 -040015171 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040015172 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15173 }
15174 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
15175 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15176 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15177 if (rc != MBX_TIMEOUT)
15178 lpfc_sli4_mbox_cmd_free(phba, mbox);
15179 if (shdr_status || shdr_add_status || rc) {
15180 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15181 "2513 POST_SGL_BLOCK mailbox command failed "
15182 "status x%x add_status x%x mbx status x%x\n",
15183 shdr_status, shdr_add_status, rc);
15184 rc = -ENXIO;
15185 }
15186 return rc;
15187}
15188
15189/**
15190 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
15191 * @phba: pointer to lpfc hba data structure.
15192 * @sblist: pointer to scsi buffer list.
15193 * @count: number of scsi buffers on the list.
15194 *
15195 * This routine is invoked to post a block of @count scsi sgl pages from a
15196 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
15197 * No Lock is held.
15198 *
15199 **/
15200int
James Smart8a9d2e82012-05-09 21:16:12 -040015201lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
15202 struct list_head *sblist,
15203 int count)
James Smart4f774512009-05-22 14:52:35 -040015204{
15205 struct lpfc_scsi_buf *psb;
15206 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
15207 struct sgl_page_pairs *sgl_pg_pairs;
15208 void *viraddr;
15209 LPFC_MBOXQ_t *mbox;
15210 uint32_t reqlen, alloclen, pg_pairs;
15211 uint32_t mbox_tmo;
15212 uint16_t xritag_start = 0;
15213 int rc = 0;
15214 uint32_t shdr_status, shdr_add_status;
15215 dma_addr_t pdma_phys_bpl1;
15216 union lpfc_sli4_cfg_shdr *shdr;
15217
15218 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040015219 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040015220 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040015221 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040015222 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
15223 "0217 Block sgl registration required DMA "
15224 "size (%d) great than a page\n", reqlen);
15225 return -ENOMEM;
15226 }
15227 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15228 if (!mbox) {
15229 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15230 "0283 Failed to allocate mbox cmd memory\n");
15231 return -ENOMEM;
15232 }
15233
15234 /* Allocate DMA memory and set up the non-embedded mailbox command */
15235 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15236 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
15237 LPFC_SLI4_MBX_NEMBED);
15238
15239 if (alloclen < reqlen) {
15240 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15241 "2561 Allocated DMA memory size (%d) is "
15242 "less than the requested DMA memory "
15243 "size (%d)\n", alloclen, reqlen);
15244 lpfc_sli4_mbox_cmd_free(phba, mbox);
15245 return -ENOMEM;
15246 }
James Smart6d368e52011-05-24 11:44:12 -040015247
James Smart4f774512009-05-22 14:52:35 -040015248 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040015249 viraddr = mbox->sge_array->addr[0];
15250
15251 /* Set up the SGL pages in the non-embedded DMA pages */
15252 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
15253 sgl_pg_pairs = &sgl->sgl_pg_pairs;
15254
15255 pg_pairs = 0;
15256 list_for_each_entry(psb, sblist, list) {
15257 /* Set up the sge entry */
15258 sgl_pg_pairs->sgl_pg0_addr_lo =
15259 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
15260 sgl_pg_pairs->sgl_pg0_addr_hi =
15261 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
15262 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
15263 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
15264 else
15265 pdma_phys_bpl1 = 0;
15266 sgl_pg_pairs->sgl_pg1_addr_lo =
15267 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
15268 sgl_pg_pairs->sgl_pg1_addr_hi =
15269 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
15270 /* Keep the first xritag on the list */
15271 if (pg_pairs == 0)
15272 xritag_start = psb->cur_iocbq.sli4_xritag;
15273 sgl_pg_pairs++;
15274 pg_pairs++;
15275 }
15276 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
15277 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
15278 /* Perform endian conversion if necessary */
15279 sgl->word0 = cpu_to_le32(sgl->word0);
15280
15281 if (!phba->sli4_hba.intr_enable)
15282 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15283 else {
James Smarta183a152011-10-10 21:32:43 -040015284 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040015285 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15286 }
15287 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
15288 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15289 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15290 if (rc != MBX_TIMEOUT)
15291 lpfc_sli4_mbox_cmd_free(phba, mbox);
15292 if (shdr_status || shdr_add_status || rc) {
15293 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15294 "2564 POST_SGL_BLOCK mailbox command failed "
15295 "status x%x add_status x%x mbx status x%x\n",
15296 shdr_status, shdr_add_status, rc);
15297 rc = -ENXIO;
15298 }
15299 return rc;
15300}
15301
James Smart2ea259e2017-02-12 13:52:27 -080015302static char *lpfc_rctl_names[] = FC_RCTL_NAMES_INIT;
15303static char *lpfc_type_names[] = FC_TYPE_NAMES_INIT;
15304
James Smart4f774512009-05-22 14:52:35 -040015305/**
15306 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
15307 * @phba: pointer to lpfc_hba struct that the frame was received on
15308 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
15309 *
15310 * This function checks the fields in the @fc_hdr to see if the FC frame is a
15311 * valid type of frame that the LPFC driver will handle. This function will
15312 * return a zero if the frame is a valid frame or a non zero value when the
15313 * frame does not pass the check.
15314 **/
15315static int
15316lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
15317{
Tomas Henzl474ffb72010-12-22 16:52:40 +010015318 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040015319 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050015320 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040015321
15322 switch (fc_hdr->fh_r_ctl) {
15323 case FC_RCTL_DD_UNCAT: /* uncategorized information */
15324 case FC_RCTL_DD_SOL_DATA: /* solicited data */
15325 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
15326 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
15327 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
15328 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
15329 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
15330 case FC_RCTL_DD_CMD_STATUS: /* command status */
15331 case FC_RCTL_ELS_REQ: /* extended link services request */
15332 case FC_RCTL_ELS_REP: /* extended link services reply */
15333 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
15334 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
15335 case FC_RCTL_BA_NOP: /* basic link service NOP */
15336 case FC_RCTL_BA_ABTS: /* basic link service abort */
15337 case FC_RCTL_BA_RMC: /* remove connection */
15338 case FC_RCTL_BA_ACC: /* basic accept */
15339 case FC_RCTL_BA_RJT: /* basic reject */
15340 case FC_RCTL_BA_PRMT:
15341 case FC_RCTL_ACK_1: /* acknowledge_1 */
15342 case FC_RCTL_ACK_0: /* acknowledge_0 */
15343 case FC_RCTL_P_RJT: /* port reject */
15344 case FC_RCTL_F_RJT: /* fabric reject */
15345 case FC_RCTL_P_BSY: /* port busy */
15346 case FC_RCTL_F_BSY: /* fabric busy to data frame */
15347 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
15348 case FC_RCTL_LCR: /* link credit reset */
15349 case FC_RCTL_END: /* end */
15350 break;
15351 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
15352 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
15353 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
15354 return lpfc_fc_frame_check(phba, fc_hdr);
15355 default:
15356 goto drop;
15357 }
15358 switch (fc_hdr->fh_type) {
15359 case FC_TYPE_BLS:
15360 case FC_TYPE_ELS:
15361 case FC_TYPE_FCP:
15362 case FC_TYPE_CT:
James Smart895427b2017-02-12 13:52:30 -080015363 case FC_TYPE_NVME:
James Smart4f774512009-05-22 14:52:35 -040015364 break;
15365 case FC_TYPE_IP:
15366 case FC_TYPE_ILS:
15367 default:
15368 goto drop;
15369 }
James Smart546fc852011-03-11 16:06:29 -050015370
James Smart4f774512009-05-22 14:52:35 -040015371 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040015372 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
15373 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart2ea259e2017-02-12 13:52:27 -080015374 lpfc_rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
15375 lpfc_type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050015376 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
15377 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040015378 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
15379 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040015380 return 0;
15381drop:
15382 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
15383 "2539 Dropped frame rctl:%s type:%s\n",
James Smart2ea259e2017-02-12 13:52:27 -080015384 lpfc_rctl_names[fc_hdr->fh_r_ctl],
15385 lpfc_type_names[fc_hdr->fh_type]);
James Smart4f774512009-05-22 14:52:35 -040015386 return 1;
15387}
15388
15389/**
15390 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
15391 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
15392 *
15393 * This function processes the FC header to retrieve the VFI from the VF
15394 * header, if one exists. This function will return the VFI if one exists
15395 * or 0 if no VSAN Header exists.
15396 **/
15397static uint32_t
15398lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
15399{
15400 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
15401
15402 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
15403 return 0;
15404 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
15405}
15406
15407/**
15408 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
15409 * @phba: Pointer to the HBA structure to search for the vport on
15410 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
15411 * @fcfi: The FC Fabric ID that the frame came from
15412 *
15413 * This function searches the @phba for a vport that matches the content of the
15414 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
15415 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
15416 * returns the matching vport pointer or NULL if unable to match frame to a
15417 * vport.
15418 **/
15419static struct lpfc_vport *
15420lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
James Smart895427b2017-02-12 13:52:30 -080015421 uint16_t fcfi, uint32_t did)
James Smart4f774512009-05-22 14:52:35 -040015422{
15423 struct lpfc_vport **vports;
15424 struct lpfc_vport *vport = NULL;
15425 int i;
James Smart939723a2012-05-09 21:19:03 -040015426
James Smartbf086112011-08-21 21:48:13 -040015427 if (did == Fabric_DID)
15428 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040015429 if ((phba->pport->fc_flag & FC_PT2PT) &&
15430 !(phba->link_state == LPFC_HBA_READY))
15431 return phba->pport;
15432
James Smart4f774512009-05-22 14:52:35 -040015433 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -080015434 if (vports != NULL) {
James Smart4f774512009-05-22 14:52:35 -040015435 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
15436 if (phba->fcf.fcfi == fcfi &&
15437 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
15438 vports[i]->fc_myDID == did) {
15439 vport = vports[i];
15440 break;
15441 }
15442 }
James Smart895427b2017-02-12 13:52:30 -080015443 }
James Smart4f774512009-05-22 14:52:35 -040015444 lpfc_destroy_vport_work_array(phba, vports);
15445 return vport;
15446}
15447
15448/**
James Smart45ed1192009-10-02 15:17:02 -040015449 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
15450 * @vport: The vport to work on.
15451 *
15452 * This function updates the receive sequence time stamp for this vport. The
15453 * receive sequence time stamp indicates the time that the last frame of the
15454 * the sequence that has been idle for the longest amount of time was received.
15455 * the driver uses this time stamp to indicate if any received sequences have
15456 * timed out.
15457 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015458static void
James Smart45ed1192009-10-02 15:17:02 -040015459lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
15460{
15461 struct lpfc_dmabuf *h_buf;
15462 struct hbq_dmabuf *dmabuf = NULL;
15463
15464 /* get the oldest sequence on the rcv list */
15465 h_buf = list_get_first(&vport->rcv_buffer_list,
15466 struct lpfc_dmabuf, list);
15467 if (!h_buf)
15468 return;
15469 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15470 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
15471}
15472
15473/**
15474 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
15475 * @vport: The vport that the received sequences were sent to.
15476 *
15477 * This function cleans up all outstanding received sequences. This is called
15478 * by the driver when a link event or user action invalidates all the received
15479 * sequences.
15480 **/
15481void
15482lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
15483{
15484 struct lpfc_dmabuf *h_buf, *hnext;
15485 struct lpfc_dmabuf *d_buf, *dnext;
15486 struct hbq_dmabuf *dmabuf = NULL;
15487
15488 /* start with the oldest sequence on the rcv list */
15489 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15490 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15491 list_del_init(&dmabuf->hbuf.list);
15492 list_for_each_entry_safe(d_buf, dnext,
15493 &dmabuf->dbuf.list, list) {
15494 list_del_init(&d_buf->list);
15495 lpfc_in_buf_free(vport->phba, d_buf);
15496 }
15497 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15498 }
15499}
15500
15501/**
15502 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
15503 * @vport: The vport that the received sequences were sent to.
15504 *
15505 * This function determines whether any received sequences have timed out by
15506 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
15507 * indicates that there is at least one timed out sequence this routine will
15508 * go through the received sequences one at a time from most inactive to most
15509 * active to determine which ones need to be cleaned up. Once it has determined
15510 * that a sequence needs to be cleaned up it will simply free up the resources
15511 * without sending an abort.
15512 **/
15513void
15514lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
15515{
15516 struct lpfc_dmabuf *h_buf, *hnext;
15517 struct lpfc_dmabuf *d_buf, *dnext;
15518 struct hbq_dmabuf *dmabuf = NULL;
15519 unsigned long timeout;
15520 int abort_count = 0;
15521
15522 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15523 vport->rcv_buffer_time_stamp);
15524 if (list_empty(&vport->rcv_buffer_list) ||
15525 time_before(jiffies, timeout))
15526 return;
15527 /* start with the oldest sequence on the rcv list */
15528 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15529 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15530 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15531 dmabuf->time_stamp);
15532 if (time_before(jiffies, timeout))
15533 break;
15534 abort_count++;
15535 list_del_init(&dmabuf->hbuf.list);
15536 list_for_each_entry_safe(d_buf, dnext,
15537 &dmabuf->dbuf.list, list) {
15538 list_del_init(&d_buf->list);
15539 lpfc_in_buf_free(vport->phba, d_buf);
15540 }
15541 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15542 }
15543 if (abort_count)
15544 lpfc_update_rcv_time_stamp(vport);
15545}
15546
15547/**
James Smart4f774512009-05-22 14:52:35 -040015548 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
15549 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
15550 *
15551 * This function searches through the existing incomplete sequences that have
15552 * been sent to this @vport. If the frame matches one of the incomplete
15553 * sequences then the dbuf in the @dmabuf is added to the list of frames that
15554 * make up that sequence. If no sequence is found that matches this frame then
15555 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
15556 * This function returns a pointer to the first dmabuf in the sequence list that
15557 * the frame was linked to.
15558 **/
15559static struct hbq_dmabuf *
15560lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15561{
15562 struct fc_frame_header *new_hdr;
15563 struct fc_frame_header *temp_hdr;
15564 struct lpfc_dmabuf *d_buf;
15565 struct lpfc_dmabuf *h_buf;
15566 struct hbq_dmabuf *seq_dmabuf = NULL;
15567 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050015568 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040015569
James Smart4d9ab992009-10-02 15:16:39 -040015570 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015571 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040015572 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050015573
James Smart4f774512009-05-22 14:52:35 -040015574 /* Use the hdr_buf to find the sequence that this frame belongs to */
15575 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15576 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15577 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15578 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15579 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15580 continue;
15581 /* found a pending sequence that matches this frame */
15582 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15583 break;
15584 }
15585 if (!seq_dmabuf) {
15586 /*
15587 * This indicates first frame received for this sequence.
15588 * Queue the buffer on the vport's rcv_buffer_list.
15589 */
15590 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040015591 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015592 return dmabuf;
15593 }
15594 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050015595 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
15596 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040015597 list_del_init(&seq_dmabuf->hbuf.list);
15598 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15599 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015600 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015601 return dmabuf;
15602 }
James Smart45ed1192009-10-02 15:17:02 -040015603 /* move this sequence to the tail to indicate a young sequence */
15604 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
15605 seq_dmabuf->time_stamp = jiffies;
15606 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050015607 if (list_empty(&seq_dmabuf->dbuf.list)) {
15608 temp_hdr = dmabuf->hbuf.virt;
15609 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15610 return seq_dmabuf;
15611 }
James Smart4f774512009-05-22 14:52:35 -040015612 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050015613 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
15614 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040015615 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15616 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
15617 /*
15618 * If the frame's sequence count is greater than the frame on
15619 * the list then insert the frame right after this frame
15620 */
James Smarteeead812009-12-21 17:01:23 -050015621 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
15622 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040015623 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050015624 found = 1;
15625 break;
James Smart4f774512009-05-22 14:52:35 -040015626 }
James Smart4360ca92015-12-16 18:12:04 -050015627
15628 if (&d_buf->list == &seq_dmabuf->dbuf.list)
15629 break;
15630 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040015631 }
James Smart4360ca92015-12-16 18:12:04 -050015632
15633 if (found)
15634 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015635 return NULL;
15636}
15637
15638/**
James Smart6669f9b2009-10-02 15:16:45 -040015639 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
15640 * @vport: pointer to a vitural port
15641 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15642 *
15643 * This function tries to abort from the partially assembed sequence, described
15644 * by the information from basic abbort @dmabuf. It checks to see whether such
15645 * partially assembled sequence held by the driver. If so, it shall free up all
15646 * the frames from the partially assembled sequence.
15647 *
15648 * Return
15649 * true -- if there is matching partially assembled sequence present and all
15650 * the frames freed with the sequence;
15651 * false -- if there is no matching partially assembled sequence present so
15652 * nothing got aborted in the lower layer driver
15653 **/
15654static bool
15655lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
15656 struct hbq_dmabuf *dmabuf)
15657{
15658 struct fc_frame_header *new_hdr;
15659 struct fc_frame_header *temp_hdr;
15660 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
15661 struct hbq_dmabuf *seq_dmabuf = NULL;
15662
15663 /* Use the hdr_buf to find the sequence that matches this frame */
15664 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15665 INIT_LIST_HEAD(&dmabuf->hbuf.list);
15666 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15667 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15668 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15669 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15670 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15671 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15672 continue;
15673 /* found a pending sequence that matches this frame */
15674 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15675 break;
15676 }
15677
15678 /* Free up all the frames from the partially assembled sequence */
15679 if (seq_dmabuf) {
15680 list_for_each_entry_safe(d_buf, n_buf,
15681 &seq_dmabuf->dbuf.list, list) {
15682 list_del_init(&d_buf->list);
15683 lpfc_in_buf_free(vport->phba, d_buf);
15684 }
15685 return true;
15686 }
15687 return false;
15688}
15689
15690/**
James Smart6dd9e312013-01-03 15:43:37 -050015691 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15692 * @vport: pointer to a vitural port
15693 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15694 *
15695 * This function tries to abort from the assembed sequence from upper level
15696 * protocol, described by the information from basic abbort @dmabuf. It
15697 * checks to see whether such pending context exists at upper level protocol.
15698 * If so, it shall clean up the pending context.
15699 *
15700 * Return
15701 * true -- if there is matching pending context of the sequence cleaned
15702 * at ulp;
15703 * false -- if there is no matching pending context of the sequence present
15704 * at ulp.
15705 **/
15706static bool
15707lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15708{
15709 struct lpfc_hba *phba = vport->phba;
15710 int handled;
15711
15712 /* Accepting abort at ulp with SLI4 only */
15713 if (phba->sli_rev < LPFC_SLI_REV4)
15714 return false;
15715
15716 /* Register all caring upper level protocols to attend abort */
15717 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15718 if (handled)
15719 return true;
15720
15721 return false;
15722}
15723
15724/**
James Smart546fc852011-03-11 16:06:29 -050015725 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040015726 * @phba: Pointer to HBA context object.
15727 * @cmd_iocbq: pointer to the command iocbq structure.
15728 * @rsp_iocbq: pointer to the response iocbq structure.
15729 *
James Smart546fc852011-03-11 16:06:29 -050015730 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040015731 * event. It properly releases the memory allocated to the sequence abort
15732 * accept iocb.
15733 **/
15734static void
James Smart546fc852011-03-11 16:06:29 -050015735lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040015736 struct lpfc_iocbq *cmd_iocbq,
15737 struct lpfc_iocbq *rsp_iocbq)
15738{
James Smart6dd9e312013-01-03 15:43:37 -050015739 struct lpfc_nodelist *ndlp;
15740
15741 if (cmd_iocbq) {
15742 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15743 lpfc_nlp_put(ndlp);
15744 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015745 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050015746 }
James Smart6b5151f2012-01-18 16:24:06 -050015747
15748 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15749 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15750 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15751 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
15752 rsp_iocbq->iocb.ulpStatus,
15753 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040015754}
15755
15756/**
James Smart6d368e52011-05-24 11:44:12 -040015757 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15758 * @phba: Pointer to HBA context object.
15759 * @xri: xri id in transaction.
15760 *
15761 * This function validates the xri maps to the known range of XRIs allocated an
15762 * used by the driver.
15763 **/
James Smart7851fe22011-07-22 18:36:52 -040015764uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015765lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15766 uint16_t xri)
15767{
James Smarta2fc4aef2014-09-03 12:57:55 -040015768 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040015769
15770 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15771 if (xri == phba->sli4_hba.xri_ids[i])
15772 return i;
15773 }
15774 return NO_XRI;
15775}
15776
James Smart6d368e52011-05-24 11:44:12 -040015777/**
James Smart546fc852011-03-11 16:06:29 -050015778 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015779 * @phba: Pointer to HBA context object.
15780 * @fc_hdr: pointer to a FC frame header.
15781 *
James Smart546fc852011-03-11 16:06:29 -050015782 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015783 * event after aborting the sequence handling.
15784 **/
15785static void
James Smart6dd9e312013-01-03 15:43:37 -050015786lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15787 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040015788{
James Smart6dd9e312013-01-03 15:43:37 -050015789 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040015790 struct lpfc_iocbq *ctiocb = NULL;
15791 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040015792 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050015793 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040015794 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050015795 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040015796
15797 if (!lpfc_is_link_up(phba))
15798 return;
15799
15800 sid = sli4_sid_from_fc_hdr(fc_hdr);
15801 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050015802 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040015803
James Smart6dd9e312013-01-03 15:43:37 -050015804 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040015805 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050015806 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15807 if (!ndlp) {
15808 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15809 "1268 Failed to allocate ndlp for "
15810 "oxid:x%x SID:x%x\n", oxid, sid);
15811 return;
15812 }
15813 lpfc_nlp_init(vport, ndlp, sid);
15814 /* Put ndlp onto pport node list */
15815 lpfc_enqueue_node(vport, ndlp);
15816 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15817 /* re-setup ndlp without removing from node list */
15818 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15819 if (!ndlp) {
15820 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15821 "3275 Failed to active ndlp found "
15822 "for oxid:x%x SID:x%x\n", oxid, sid);
15823 return;
15824 }
James Smart6669f9b2009-10-02 15:16:45 -040015825 }
15826
James Smart546fc852011-03-11 16:06:29 -050015827 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040015828 ctiocb = lpfc_sli_get_iocbq(phba);
15829 if (!ctiocb)
15830 return;
15831
James Smart5ffc2662009-11-18 15:39:44 -050015832 /* Extract the F_CTL field from FC_HDR */
15833 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15834
James Smart6669f9b2009-10-02 15:16:45 -040015835 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040015836 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050015837 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040015838 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15839 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15840 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15841
15842 /* Fill in the rest of iocb fields */
15843 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15844 icmd->ulpBdeCount = 0;
15845 icmd->ulpLe = 1;
15846 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040015847 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050015848 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015849
James Smart6669f9b2009-10-02 15:16:45 -040015850 ctiocb->iocb_cmpl = NULL;
15851 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050015852 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040015853 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050015854 ctiocb->sli4_xritag = NO_XRI;
15855
James Smartee0f4fe2012-05-09 21:19:14 -040015856 if (fctl & FC_FC_EX_CTX)
15857 /* Exchange responder sent the abort so we
15858 * own the oxid.
15859 */
15860 xri = oxid;
15861 else
15862 xri = rxid;
15863 lxri = lpfc_sli4_xri_inrange(phba, xri);
15864 if (lxri != NO_XRI)
15865 lpfc_set_rrq_active(phba, ndlp, lxri,
15866 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050015867 /* For BA_ABTS from exchange responder, if the logical xri with
15868 * the oxid maps to the FCP XRI range, the port no longer has
15869 * that exchange context, send a BLS_RJT. Override the IOCB for
15870 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050015871 */
James Smart6dd9e312013-01-03 15:43:37 -050015872 if ((fctl & FC_FC_EX_CTX) &&
James Smart895427b2017-02-12 13:52:30 -080015873 (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
James Smart6dd9e312013-01-03 15:43:37 -050015874 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15875 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15876 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15877 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15878 }
15879
15880 /* If BA_ABTS failed to abort a partially assembled receive sequence,
15881 * the driver no longer has that exchange, send a BLS_RJT. Override
15882 * the IOCB for a BA_RJT.
15883 */
15884 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050015885 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15886 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15887 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15888 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15889 }
James Smart6669f9b2009-10-02 15:16:45 -040015890
James Smart5ffc2662009-11-18 15:39:44 -050015891 if (fctl & FC_FC_EX_CTX) {
15892 /* ABTS sent by responder to CT exchange, construction
15893 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15894 * field and RX_ID from ABTS for RX_ID field.
15895 */
James Smart546fc852011-03-11 16:06:29 -050015896 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050015897 } else {
15898 /* ABTS sent by initiator to CT exchange, construction
15899 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050015900 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050015901 */
James Smart546fc852011-03-11 16:06:29 -050015902 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050015903 }
James Smartf09c3ac2012-03-01 22:33:29 -050015904 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050015905 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050015906
James Smart546fc852011-03-11 16:06:29 -050015907 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050015908 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15909 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15910 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050015911
15912 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15913 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050015914 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15915 "2925 Failed to issue CT ABTS RSP x%x on "
15916 "xri x%x, Data x%x\n",
15917 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15918 phba->link_state);
15919 lpfc_nlp_put(ndlp);
15920 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050015921 lpfc_sli_release_iocbq(phba, ctiocb);
15922 }
James Smart6669f9b2009-10-02 15:16:45 -040015923}
15924
15925/**
15926 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15927 * @vport: Pointer to the vport on which this sequence was received
15928 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15929 *
15930 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15931 * receive sequence is only partially assembed by the driver, it shall abort
15932 * the partially assembled frames for the sequence. Otherwise, if the
15933 * unsolicited receive sequence has been completely assembled and passed to
15934 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15935 * unsolicited sequence has been aborted. After that, it will issue a basic
15936 * accept to accept the abort.
15937 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015938static void
James Smart6669f9b2009-10-02 15:16:45 -040015939lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15940 struct hbq_dmabuf *dmabuf)
15941{
15942 struct lpfc_hba *phba = vport->phba;
15943 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050015944 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050015945 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040015946
James Smart6669f9b2009-10-02 15:16:45 -040015947 /* Make a copy of fc_hdr before the dmabuf being released */
15948 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050015949 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040015950
James Smart5ffc2662009-11-18 15:39:44 -050015951 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050015952 /* ABTS by responder to exchange, no cleanup needed */
15953 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050015954 } else {
James Smart6dd9e312013-01-03 15:43:37 -050015955 /* ABTS by initiator to exchange, need to do cleanup */
15956 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15957 if (aborted == false)
15958 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050015959 }
James Smart6dd9e312013-01-03 15:43:37 -050015960 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15961
15962 /* Respond with BA_ACC or BA_RJT accordingly */
15963 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040015964}
15965
15966/**
James Smart4f774512009-05-22 14:52:35 -040015967 * lpfc_seq_complete - Indicates if a sequence is complete
15968 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15969 *
15970 * This function checks the sequence, starting with the frame described by
15971 * @dmabuf, to see if all the frames associated with this sequence are present.
15972 * the frames associated with this sequence are linked to the @dmabuf using the
15973 * dbuf list. This function looks for two major things. 1) That the first frame
15974 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15975 * set. 3) That there are no holes in the sequence count. The function will
15976 * return 1 when the sequence is complete, otherwise it will return 0.
15977 **/
15978static int
15979lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15980{
15981 struct fc_frame_header *hdr;
15982 struct lpfc_dmabuf *d_buf;
15983 struct hbq_dmabuf *seq_dmabuf;
15984 uint32_t fctl;
15985 int seq_count = 0;
15986
15987 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15988 /* make sure first fame of sequence has a sequence count of zero */
15989 if (hdr->fh_seq_cnt != seq_count)
15990 return 0;
15991 fctl = (hdr->fh_f_ctl[0] << 16 |
15992 hdr->fh_f_ctl[1] << 8 |
15993 hdr->fh_f_ctl[2]);
15994 /* If last frame of sequence we can return success. */
15995 if (fctl & FC_FC_END_SEQ)
15996 return 1;
15997 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15998 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15999 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16000 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050016001 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040016002 return 0;
16003 fctl = (hdr->fh_f_ctl[0] << 16 |
16004 hdr->fh_f_ctl[1] << 8 |
16005 hdr->fh_f_ctl[2]);
16006 /* If last frame of sequence we can return success. */
16007 if (fctl & FC_FC_END_SEQ)
16008 return 1;
16009 }
16010 return 0;
16011}
16012
16013/**
16014 * lpfc_prep_seq - Prep sequence for ULP processing
16015 * @vport: Pointer to the vport on which this sequence was received
16016 * @dmabuf: pointer to a dmabuf that describes the FC sequence
16017 *
16018 * This function takes a sequence, described by a list of frames, and creates
16019 * a list of iocbq structures to describe the sequence. This iocbq list will be
16020 * used to issue to the generic unsolicited sequence handler. This routine
16021 * returns a pointer to the first iocbq in the list. If the function is unable
16022 * to allocate an iocbq then it throw out the received frames that were not
16023 * able to be described and return a pointer to the first iocbq. If unable to
16024 * allocate any iocbqs (including the first) this function will return NULL.
16025 **/
16026static struct lpfc_iocbq *
16027lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
16028{
James Smart7851fe22011-07-22 18:36:52 -040016029 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040016030 struct lpfc_dmabuf *d_buf, *n_buf;
16031 struct lpfc_iocbq *first_iocbq, *iocbq;
16032 struct fc_frame_header *fc_hdr;
16033 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040016034 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050016035 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040016036
16037 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16038 /* remove from receive buffer list */
16039 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040016040 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040016041 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040016042 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040016043 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016044 /* Get an iocbq struct to fill in. */
16045 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
16046 if (first_iocbq) {
16047 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040016048 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040016049 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart895427b2017-02-12 13:52:30 -080016050 first_iocbq->vport = vport;
James Smart939723a2012-05-09 21:19:03 -040016051
16052 /* Check FC Header to see what TYPE of frame we are rcv'ing */
16053 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
16054 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
16055 first_iocbq->iocb.un.rcvels.parmRo =
16056 sli4_did_from_fc_hdr(fc_hdr);
16057 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
16058 } else
16059 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040016060 first_iocbq->iocb.ulpContext = NO_XRI;
16061 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
16062 be16_to_cpu(fc_hdr->fh_ox_id);
16063 /* iocbq is prepped for internal consumption. Physical vpi. */
16064 first_iocbq->iocb.unsli3.rcvsli3.vpi =
16065 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040016066 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040016067 tot_len = bf_get(lpfc_rcqe_length,
16068 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
16069
James Smart4f774512009-05-22 14:52:35 -040016070 first_iocbq->context2 = &seq_dmabuf->dbuf;
16071 first_iocbq->context3 = NULL;
16072 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040016073 if (tot_len > LPFC_DATA_BUF_SIZE)
16074 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040016075 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040016076 else
16077 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
16078
James Smart4f774512009-05-22 14:52:35 -040016079 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040016080
James Smart7851fe22011-07-22 18:36:52 -040016081 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040016082 }
16083 iocbq = first_iocbq;
16084 /*
16085 * Each IOCBq can have two Buffers assigned, so go through the list
16086 * of buffers for this sequence and save two buffers in each IOCBq
16087 */
16088 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
16089 if (!iocbq) {
16090 lpfc_in_buf_free(vport->phba, d_buf);
16091 continue;
16092 }
16093 if (!iocbq->context3) {
16094 iocbq->context3 = d_buf;
16095 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040016096 /* We need to get the size out of the right CQE */
16097 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16098 len = bf_get(lpfc_rcqe_length,
16099 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016100 pbde = (struct ulp_bde64 *)
16101 &iocbq->iocb.unsli3.sli3Words[4];
16102 if (len > LPFC_DATA_BUF_SIZE)
16103 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
16104 else
16105 pbde->tus.f.bdeSize = len;
16106
James Smart7851fe22011-07-22 18:36:52 -040016107 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
16108 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040016109 } else {
16110 iocbq = lpfc_sli_get_iocbq(vport->phba);
16111 if (!iocbq) {
16112 if (first_iocbq) {
16113 first_iocbq->iocb.ulpStatus =
16114 IOSTAT_FCP_RSP_ERROR;
16115 first_iocbq->iocb.un.ulpWord[4] =
16116 IOERR_NO_RESOURCES;
16117 }
16118 lpfc_in_buf_free(vport->phba, d_buf);
16119 continue;
16120 }
James Smart7851fe22011-07-22 18:36:52 -040016121 /* We need to get the size out of the right CQE */
16122 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
16123 len = bf_get(lpfc_rcqe_length,
16124 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040016125 iocbq->context2 = d_buf;
16126 iocbq->context3 = NULL;
16127 iocbq->iocb.ulpBdeCount = 1;
16128 if (len > LPFC_DATA_BUF_SIZE)
16129 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
16130 LPFC_DATA_BUF_SIZE;
16131 else
16132 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
16133
James Smart7851fe22011-07-22 18:36:52 -040016134 tot_len += len;
16135 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
16136
James Smart4f774512009-05-22 14:52:35 -040016137 iocbq->iocb.un.rcvels.remoteID = sid;
16138 list_add_tail(&iocbq->list, &first_iocbq->list);
16139 }
16140 }
16141 return first_iocbq;
16142}
16143
James Smart6669f9b2009-10-02 15:16:45 -040016144static void
16145lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
16146 struct hbq_dmabuf *seq_dmabuf)
16147{
16148 struct fc_frame_header *fc_hdr;
16149 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
16150 struct lpfc_hba *phba = vport->phba;
16151
16152 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
16153 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
16154 if (!iocbq) {
16155 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16156 "2707 Ring %d handler: Failed to allocate "
16157 "iocb Rctl x%x Type x%x received\n",
16158 LPFC_ELS_RING,
16159 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16160 return;
16161 }
16162 if (!lpfc_complete_unsol_iocb(phba,
James Smart895427b2017-02-12 13:52:30 -080016163 phba->sli4_hba.els_wq->pring,
James Smart6669f9b2009-10-02 15:16:45 -040016164 iocbq, fc_hdr->fh_r_ctl,
16165 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040016166 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040016167 "2540 Ring %d handler: unexpected Rctl "
16168 "x%x Type x%x received\n",
16169 LPFC_ELS_RING,
16170 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
16171
16172 /* Free iocb created in lpfc_prep_seq */
16173 list_for_each_entry_safe(curr_iocb, next_iocb,
16174 &iocbq->list, list) {
16175 list_del_init(&curr_iocb->list);
16176 lpfc_sli_release_iocbq(phba, curr_iocb);
16177 }
16178 lpfc_sli_release_iocbq(phba, iocbq);
16179}
16180
James Smart4f774512009-05-22 14:52:35 -040016181/**
16182 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
16183 * @phba: Pointer to HBA context object.
16184 *
16185 * This function is called with no lock held. This function processes all
16186 * the received buffers and gives it to upper layers when a received buffer
16187 * indicates that it is the final frame in the sequence. The interrupt
James Smart895427b2017-02-12 13:52:30 -080016188 * service routine processes received buffers at interrupt contexts.
James Smart4f774512009-05-22 14:52:35 -040016189 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
16190 * appropriate receive function when the final frame in a sequence is received.
16191 **/
James Smart4d9ab992009-10-02 15:16:39 -040016192void
16193lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
16194 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040016195{
James Smart4d9ab992009-10-02 15:16:39 -040016196 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040016197 struct fc_frame_header *fc_hdr;
16198 struct lpfc_vport *vport;
16199 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040016200 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040016201
James Smart4f774512009-05-22 14:52:35 -040016202 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040016203 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080016204
James Smart4d9ab992009-10-02 15:16:39 -040016205 /* check to see if this a valid type of frame */
16206 if (lpfc_fc_frame_check(phba, fc_hdr)) {
16207 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16208 return;
16209 }
James Smart2ea259e2017-02-12 13:52:27 -080016210
James Smart7851fe22011-07-22 18:36:52 -040016211 if ((bf_get(lpfc_cqe_code,
16212 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
16213 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
16214 &dmabuf->cq_event.cqe.rcqe_cmpl);
16215 else
16216 fcfi = bf_get(lpfc_rcqe_fcf_id,
16217 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040016218
James Smart895427b2017-02-12 13:52:30 -080016219 /* d_id this frame is directed to */
16220 did = sli4_did_from_fc_hdr(fc_hdr);
16221
16222 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
James Smart939723a2012-05-09 21:19:03 -040016223 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040016224 /* throw out the frame */
16225 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16226 return;
16227 }
James Smart939723a2012-05-09 21:19:03 -040016228
James Smart939723a2012-05-09 21:19:03 -040016229 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
16230 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
16231 (did != Fabric_DID)) {
16232 /*
16233 * Throw out the frame if we are not pt2pt.
16234 * The pt2pt protocol allows for discovery frames
16235 * to be received without a registered VPI.
16236 */
16237 if (!(vport->fc_flag & FC_PT2PT) ||
16238 (phba->link_state == LPFC_HBA_READY)) {
16239 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16240 return;
16241 }
16242 }
16243
James Smart6669f9b2009-10-02 15:16:45 -040016244 /* Handle the basic abort sequence (BA_ABTS) event */
16245 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
16246 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
16247 return;
16248 }
16249
James Smart4d9ab992009-10-02 15:16:39 -040016250 /* Link this frame */
16251 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
16252 if (!seq_dmabuf) {
16253 /* unable to add frame to vport - throw it out */
16254 lpfc_in_buf_free(phba, &dmabuf->dbuf);
16255 return;
16256 }
16257 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050016258 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040016259 return;
James Smartdef9c7a2009-12-21 17:02:28 -050016260
James Smart6669f9b2009-10-02 15:16:45 -040016261 /* Send the complete sequence to the upper layer protocol */
16262 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040016263}
James Smart6fb120a2009-05-22 14:52:59 -040016264
16265/**
16266 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
16267 * @phba: pointer to lpfc hba data structure.
16268 *
16269 * This routine is invoked to post rpi header templates to the
16270 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040016271 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16272 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040016273 *
16274 * This routine does not require any locks. It's usage is expected
16275 * to be driver load or reset recovery when the driver is
16276 * sequential.
16277 *
16278 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020016279 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040016280 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040016281 * When this error occurs, the driver is not guaranteed
16282 * to have any rpi regions posted to the device and
16283 * must either attempt to repost the regions or take a
16284 * fatal error.
16285 **/
16286int
16287lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
16288{
16289 struct lpfc_rpi_hdr *rpi_page;
16290 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040016291 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016292
James Smart6d368e52011-05-24 11:44:12 -040016293 /* SLI4 ports that support extents do not require RPI headers. */
16294 if (!phba->sli4_hba.rpi_hdrs_in_use)
16295 goto exit;
16296 if (phba->sli4_hba.extents_in_use)
16297 return -EIO;
16298
James Smart6fb120a2009-05-22 14:52:59 -040016299 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040016300 /*
16301 * Assign the rpi headers a physical rpi only if the driver
16302 * has not initialized those resources. A port reset only
16303 * needs the headers posted.
16304 */
16305 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
16306 LPFC_RPI_RSRC_RDY)
16307 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
16308
James Smart6fb120a2009-05-22 14:52:59 -040016309 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
16310 if (rc != MBX_SUCCESS) {
16311 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16312 "2008 Error %d posting all rpi "
16313 "headers\n", rc);
16314 rc = -EIO;
16315 break;
16316 }
16317 }
16318
James Smart6d368e52011-05-24 11:44:12 -040016319 exit:
16320 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
16321 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040016322 return rc;
16323}
16324
16325/**
16326 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
16327 * @phba: pointer to lpfc hba data structure.
16328 * @rpi_page: pointer to the rpi memory region.
16329 *
16330 * This routine is invoked to post a single rpi header to the
16331 * HBA consistent with the SLI-4 interface spec. This memory region
16332 * maps up to 64 rpi context regions.
16333 *
16334 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020016335 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040016336 * -ENOMEM - No available memory
16337 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040016338 **/
16339int
16340lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
16341{
16342 LPFC_MBOXQ_t *mboxq;
16343 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
16344 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016345 uint32_t shdr_status, shdr_add_status;
16346 union lpfc_sli4_cfg_shdr *shdr;
16347
James Smart6d368e52011-05-24 11:44:12 -040016348 /* SLI4 ports that support extents do not require RPI headers. */
16349 if (!phba->sli4_hba.rpi_hdrs_in_use)
16350 return rc;
16351 if (phba->sli4_hba.extents_in_use)
16352 return -EIO;
16353
James Smart6fb120a2009-05-22 14:52:59 -040016354 /* The port is notified of the header region via a mailbox command. */
16355 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16356 if (!mboxq) {
16357 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16358 "2001 Unable to allocate memory for issuing "
16359 "SLI_CONFIG_SPECIAL mailbox command\n");
16360 return -ENOMEM;
16361 }
16362
16363 /* Post all rpi memory regions to the port. */
16364 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040016365 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16366 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
16367 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050016368 sizeof(struct lpfc_sli4_cfg_mhdr),
16369 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040016370
16371
16372 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040016373 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
16374 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040016375 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
16376 hdr_tmpl, rpi_page->page_count);
16377
James Smart6fb120a2009-05-22 14:52:59 -040016378 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
16379 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040016380 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040016381 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
16382 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16383 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16384 if (rc != MBX_TIMEOUT)
16385 mempool_free(mboxq, phba->mbox_mem_pool);
16386 if (shdr_status || shdr_add_status || rc) {
16387 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16388 "2514 POST_RPI_HDR mailbox failed with "
16389 "status x%x add_status x%x, mbx status x%x\n",
16390 shdr_status, shdr_add_status, rc);
16391 rc = -ENXIO;
16392 }
16393 return rc;
16394}
16395
16396/**
16397 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
16398 * @phba: pointer to lpfc hba data structure.
16399 *
16400 * This routine is invoked to post rpi header templates to the
16401 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040016402 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
16403 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040016404 *
16405 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020016406 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040016407 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
16408 **/
16409int
16410lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
16411{
James Smart6d368e52011-05-24 11:44:12 -040016412 unsigned long rpi;
16413 uint16_t max_rpi, rpi_limit;
16414 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016415 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040016416 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040016417
James Smart6fb120a2009-05-22 14:52:59 -040016418 /*
James Smart6d368e52011-05-24 11:44:12 -040016419 * Fetch the next logical rpi. Because this index is logical,
16420 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040016421 */
James Smart4902b382013-10-10 12:20:35 -040016422 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040016423 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
16424 rpi_limit = phba->sli4_hba.next_rpi;
16425
James Smart6d368e52011-05-24 11:44:12 -040016426 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
16427 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040016428 rpi = LPFC_RPI_ALLOC_ERROR;
16429 else {
16430 set_bit(rpi, phba->sli4_hba.rpi_bmask);
16431 phba->sli4_hba.max_cfg_param.rpi_used++;
16432 phba->sli4_hba.rpi_count++;
16433 }
James Smartbe6bb942015-04-07 15:07:22 -040016434 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
16435 "0001 rpi:%x max:%x lim:%x\n",
16436 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040016437
16438 /*
16439 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040016440 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040016441 */
16442 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
16443 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040016444 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040016445 return rpi;
16446 }
16447
16448 /*
James Smart6d368e52011-05-24 11:44:12 -040016449 * RPI header postings are not required for SLI4 ports capable of
16450 * extents.
16451 */
16452 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040016453 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040016454 return rpi;
16455 }
16456
16457 /*
James Smart6fb120a2009-05-22 14:52:59 -040016458 * If the driver is running low on rpi resources, allocate another
16459 * page now. Note that the next_rpi value is used because
16460 * it represents how many are actually in use whereas max_rpi notes
16461 * how many are supported max by the device.
16462 */
James Smart6d368e52011-05-24 11:44:12 -040016463 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040016464 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040016465 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
16466 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
16467 if (!rpi_hdr) {
16468 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16469 "2002 Error Could not grow rpi "
16470 "count\n");
16471 } else {
James Smart6d368e52011-05-24 11:44:12 -040016472 lrpi = rpi_hdr->start_rpi;
16473 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040016474 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
16475 }
16476 }
16477
16478 return rpi;
16479}
16480
16481/**
16482 * lpfc_sli4_free_rpi - Release an rpi for reuse.
16483 * @phba: pointer to lpfc hba data structure.
16484 *
16485 * This routine is invoked to release an rpi to the pool of
16486 * available rpis maintained by the driver.
16487 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016488static void
James Smartd7c47992010-06-08 18:31:54 -040016489__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16490{
16491 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
16492 phba->sli4_hba.rpi_count--;
16493 phba->sli4_hba.max_cfg_param.rpi_used--;
16494 }
16495}
16496
16497/**
16498 * lpfc_sli4_free_rpi - Release an rpi for reuse.
16499 * @phba: pointer to lpfc hba data structure.
16500 *
16501 * This routine is invoked to release an rpi to the pool of
16502 * available rpis maintained by the driver.
16503 **/
16504void
James Smart6fb120a2009-05-22 14:52:59 -040016505lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16506{
16507 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040016508 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040016509 spin_unlock_irq(&phba->hbalock);
16510}
16511
16512/**
16513 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
16514 * @phba: pointer to lpfc hba data structure.
16515 *
16516 * This routine is invoked to remove the memory region that
16517 * provided rpi via a bitmask.
16518 **/
16519void
16520lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
16521{
16522 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040016523 kfree(phba->sli4_hba.rpi_ids);
16524 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040016525}
16526
16527/**
16528 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
16529 * @phba: pointer to lpfc hba data structure.
16530 *
16531 * This routine is invoked to remove the memory region that
16532 * provided rpi via a bitmask.
16533 **/
16534int
James Smart6b5151f2012-01-18 16:24:06 -050016535lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
16536 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040016537{
16538 LPFC_MBOXQ_t *mboxq;
16539 struct lpfc_hba *phba = ndlp->phba;
16540 int rc;
16541
16542 /* The port is notified of the header region via a mailbox command. */
16543 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16544 if (!mboxq)
16545 return -ENOMEM;
16546
16547 /* Post all rpi memory regions to the port. */
16548 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050016549 if (cmpl) {
16550 mboxq->mbox_cmpl = cmpl;
16551 mboxq->context1 = arg;
16552 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050016553 } else
16554 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050016555 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040016556 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16557 if (rc == MBX_NOT_FINISHED) {
16558 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16559 "2010 Resume RPI Mailbox failed "
16560 "status %d, mbxStatus x%x\n", rc,
16561 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16562 mempool_free(mboxq, phba->mbox_mem_pool);
16563 return -EIO;
16564 }
16565 return 0;
16566}
16567
16568/**
16569 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050016570 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040016571 *
James Smart76a95d72010-11-20 23:11:48 -050016572 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040016573 *
16574 * Returns:
16575 * 0 success
16576 * -Evalue otherwise
16577 **/
16578int
James Smart76a95d72010-11-20 23:11:48 -050016579lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040016580{
16581 LPFC_MBOXQ_t *mboxq;
16582 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040016583 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040016584 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050016585 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040016586 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16587 if (!mboxq)
16588 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050016589 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040016590 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040016591 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040016592 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050016593 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040016594 "2022 INIT VPI Mailbox failed "
16595 "status %d, mbxStatus x%x\n", rc,
16596 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040016597 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040016598 }
James Smart6a9c52c2009-10-02 15:16:51 -040016599 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050016600 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040016601
16602 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040016603}
16604
16605/**
16606 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
16607 * @phba: pointer to lpfc hba data structure.
16608 * @mboxq: Pointer to mailbox object.
16609 *
16610 * This routine is invoked to manually add a single FCF record. The caller
16611 * must pass a completely initialized FCF_Record. This routine takes
16612 * care of the nonembedded mailbox operations.
16613 **/
16614static void
16615lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
16616{
16617 void *virt_addr;
16618 union lpfc_sli4_cfg_shdr *shdr;
16619 uint32_t shdr_status, shdr_add_status;
16620
16621 virt_addr = mboxq->sge_array->addr[0];
16622 /* The IOCTL status is embedded in the mailbox subheader. */
16623 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
16624 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16625 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16626
16627 if ((shdr_status || shdr_add_status) &&
16628 (shdr_status != STATUS_FCF_IN_USE))
16629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16630 "2558 ADD_FCF_RECORD mailbox failed with "
16631 "status x%x add_status x%x\n",
16632 shdr_status, shdr_add_status);
16633
16634 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16635}
16636
16637/**
16638 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
16639 * @phba: pointer to lpfc hba data structure.
16640 * @fcf_record: pointer to the initialized fcf record to add.
16641 *
16642 * This routine is invoked to manually add a single FCF record. The caller
16643 * must pass a completely initialized FCF_Record. This routine takes
16644 * care of the nonembedded mailbox operations.
16645 **/
16646int
16647lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
16648{
16649 int rc = 0;
16650 LPFC_MBOXQ_t *mboxq;
16651 uint8_t *bytep;
16652 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040016653 struct lpfc_mbx_sge sge;
16654 uint32_t alloc_len, req_len;
16655 uint32_t fcfindex;
16656
16657 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16658 if (!mboxq) {
16659 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16660 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
16661 return -ENOMEM;
16662 }
16663
16664 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
16665 sizeof(uint32_t);
16666
16667 /* Allocate DMA memory and set up the non-embedded mailbox command */
16668 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16669 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
16670 req_len, LPFC_SLI4_MBX_NEMBED);
16671 if (alloc_len < req_len) {
16672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16673 "2523 Allocated DMA memory size (x%x) is "
16674 "less than the requested DMA memory "
16675 "size (x%x)\n", alloc_len, req_len);
16676 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16677 return -ENOMEM;
16678 }
16679
16680 /*
16681 * Get the first SGE entry from the non-embedded DMA memory. This
16682 * routine only uses a single SGE.
16683 */
16684 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040016685 virt_addr = mboxq->sge_array->addr[0];
16686 /*
16687 * Configure the FCF record for FCFI 0. This is the driver's
16688 * hardcoded default and gets used in nonFIP mode.
16689 */
16690 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16691 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16692 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16693
16694 /*
16695 * Copy the fcf_index and the FCF Record Data. The data starts after
16696 * the FCoE header plus word10. The data copy needs to be endian
16697 * correct.
16698 */
16699 bytep += sizeof(uint32_t);
16700 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16701 mboxq->vport = phba->pport;
16702 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16703 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16704 if (rc == MBX_NOT_FINISHED) {
16705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16706 "2515 ADD_FCF_RECORD mailbox failed with "
16707 "status 0x%x\n", rc);
16708 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16709 rc = -EIO;
16710 } else
16711 rc = 0;
16712
16713 return rc;
16714}
16715
16716/**
16717 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16718 * @phba: pointer to lpfc hba data structure.
16719 * @fcf_record: pointer to the fcf record to write the default data.
16720 * @fcf_index: FCF table entry index.
16721 *
16722 * This routine is invoked to build the driver's default FCF record. The
16723 * values used are hardcoded. This routine handles memory initialization.
16724 *
16725 **/
16726void
16727lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16728 struct fcf_record *fcf_record,
16729 uint16_t fcf_index)
16730{
16731 memset(fcf_record, 0, sizeof(struct fcf_record));
16732 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16733 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16734 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16735 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16736 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16737 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16738 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16739 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16740 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16741 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16742 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16743 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16744 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040016745 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040016746 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16747 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16748 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16749 /* Set the VLAN bit map */
16750 if (phba->valid_vlan) {
16751 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16752 = 1 << (phba->vlan_id % 8);
16753 }
16754}
16755
16756/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016757 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040016758 * @phba: pointer to lpfc hba data structure.
16759 * @fcf_index: FCF table entry offset.
16760 *
James Smart0c9ab6f2010-02-26 14:15:57 -050016761 * This routine is invoked to scan the entire FCF table by reading FCF
16762 * record and processing it one at a time starting from the @fcf_index
16763 * for initial FCF discovery or fast FCF failover rediscovery.
16764 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016765 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016766 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040016767 **/
16768int
James Smart0c9ab6f2010-02-26 14:15:57 -050016769lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040016770{
16771 int rc = 0, error;
16772 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040016773
James Smart32b97932009-07-19 10:01:21 -040016774 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050016775 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040016776 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16777 if (!mboxq) {
16778 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16779 "2000 Failed to allocate mbox for "
16780 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040016781 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050016782 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016783 }
James Smartecfd03c2010-02-12 14:41:27 -050016784 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050016785 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050016786 if (rc) {
16787 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050016788 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016789 }
James Smartecfd03c2010-02-12 14:41:27 -050016790 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040016791 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050016792 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040016793
16794 spin_lock_irq(&phba->hbalock);
16795 phba->hba_flag |= FCF_TS_INPROG;
16796 spin_unlock_irq(&phba->hbalock);
16797
James Smart6fb120a2009-05-22 14:52:59 -040016798 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050016799 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040016800 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050016801 else {
James Smart38b92ef2010-08-04 16:11:39 -040016802 /* Reset eligible FCF count for new scan */
16803 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040016804 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016805 error = 0;
James Smart32b97932009-07-19 10:01:21 -040016806 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016807fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040016808 if (error) {
16809 if (mboxq)
16810 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040016811 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040016812 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040016813 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040016814 spin_unlock_irq(&phba->hbalock);
16815 }
James Smart6fb120a2009-05-22 14:52:59 -040016816 return error;
16817}
James Smarta0c87cb2009-07-19 10:01:10 -040016818
16819/**
James Smarta93ff372010-10-22 11:06:08 -040016820 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050016821 * @phba: pointer to lpfc hba data structure.
16822 * @fcf_index: FCF table entry offset.
16823 *
16824 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040016825 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050016826 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016827 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016828 * otherwise.
16829 **/
16830int
16831lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16832{
16833 int rc = 0, error;
16834 LPFC_MBOXQ_t *mboxq;
16835
16836 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16837 if (!mboxq) {
16838 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16839 "2763 Failed to allocate mbox for "
16840 "READ_FCF cmd\n");
16841 error = -ENOMEM;
16842 goto fail_fcf_read;
16843 }
16844 /* Construct the read FCF record mailbox command */
16845 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16846 if (rc) {
16847 error = -EINVAL;
16848 goto fail_fcf_read;
16849 }
16850 /* Issue the mailbox command asynchronously */
16851 mboxq->vport = phba->pport;
16852 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16853 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16854 if (rc == MBX_NOT_FINISHED)
16855 error = -EIO;
16856 else
16857 error = 0;
16858
16859fail_fcf_read:
16860 if (error && mboxq)
16861 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16862 return error;
16863}
16864
16865/**
16866 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16867 * @phba: pointer to lpfc hba data structure.
16868 * @fcf_index: FCF table entry offset.
16869 *
16870 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040016871 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050016872 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016873 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016874 * otherwise.
16875 **/
16876int
16877lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16878{
16879 int rc = 0, error;
16880 LPFC_MBOXQ_t *mboxq;
16881
16882 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16883 if (!mboxq) {
16884 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16885 "2758 Failed to allocate mbox for "
16886 "READ_FCF cmd\n");
16887 error = -ENOMEM;
16888 goto fail_fcf_read;
16889 }
16890 /* Construct the read FCF record mailbox command */
16891 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16892 if (rc) {
16893 error = -EINVAL;
16894 goto fail_fcf_read;
16895 }
16896 /* Issue the mailbox command asynchronously */
16897 mboxq->vport = phba->pport;
16898 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16899 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16900 if (rc == MBX_NOT_FINISHED)
16901 error = -EIO;
16902 else
16903 error = 0;
16904
16905fail_fcf_read:
16906 if (error && mboxq)
16907 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16908 return error;
16909}
16910
16911/**
James Smartf5cb5302015-12-16 18:11:52 -050016912 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040016913 * phba pointer to the lpfc_hba struct for this port.
16914 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16915 * routine when the rr_bmask is empty. The FCF indecies are put into the
16916 * rr_bmask based on their priority level. Starting from the highest priority
16917 * to the lowest. The most likely FCF candidate will be in the highest
16918 * priority group. When this routine is called it searches the fcf_pri list for
16919 * next lowest priority group and repopulates the rr_bmask with only those
16920 * fcf_indexes.
16921 * returns:
16922 * 1=success 0=failure
16923 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016924static int
James Smart7d791df2011-07-22 18:37:52 -040016925lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16926{
16927 uint16_t next_fcf_pri;
16928 uint16_t last_index;
16929 struct lpfc_fcf_pri *fcf_pri;
16930 int rc;
16931 int ret = 0;
16932
16933 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16934 LPFC_SLI4_FCF_TBL_INDX_MAX);
16935 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16936 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050016937
16938 /* Verify the priority list has 2 or more entries */
16939 spin_lock_irq(&phba->hbalock);
16940 if (list_empty(&phba->fcf.fcf_pri_list) ||
16941 list_is_singular(&phba->fcf.fcf_pri_list)) {
16942 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040016943 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16944 "3061 Last IDX %d\n", last_index);
16945 return 0; /* Empty rr list */
16946 }
James Smart25626692013-03-01 16:36:54 -050016947 spin_unlock_irq(&phba->hbalock);
16948
James Smart7d791df2011-07-22 18:37:52 -040016949 next_fcf_pri = 0;
16950 /*
16951 * Clear the rr_bmask and set all of the bits that are at this
16952 * priority.
16953 */
16954 memset(phba->fcf.fcf_rr_bmask, 0,
16955 sizeof(*phba->fcf.fcf_rr_bmask));
16956 spin_lock_irq(&phba->hbalock);
16957 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16958 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16959 continue;
16960 /*
16961 * the 1st priority that has not FLOGI failed
16962 * will be the highest.
16963 */
16964 if (!next_fcf_pri)
16965 next_fcf_pri = fcf_pri->fcf_rec.priority;
16966 spin_unlock_irq(&phba->hbalock);
16967 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16968 rc = lpfc_sli4_fcf_rr_index_set(phba,
16969 fcf_pri->fcf_rec.fcf_index);
16970 if (rc)
16971 return 0;
16972 }
16973 spin_lock_irq(&phba->hbalock);
16974 }
16975 /*
16976 * if next_fcf_pri was not set above and the list is not empty then
16977 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070016978 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040016979 */
16980 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16981 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16982 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16983 /*
16984 * the 1st priority that has not FLOGI failed
16985 * will be the highest.
16986 */
16987 if (!next_fcf_pri)
16988 next_fcf_pri = fcf_pri->fcf_rec.priority;
16989 spin_unlock_irq(&phba->hbalock);
16990 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16991 rc = lpfc_sli4_fcf_rr_index_set(phba,
16992 fcf_pri->fcf_rec.fcf_index);
16993 if (rc)
16994 return 0;
16995 }
16996 spin_lock_irq(&phba->hbalock);
16997 }
16998 } else
16999 ret = 1;
17000 spin_unlock_irq(&phba->hbalock);
17001
17002 return ret;
17003}
17004/**
James Smart0c9ab6f2010-02-26 14:15:57 -050017005 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
17006 * @phba: pointer to lpfc hba data structure.
17007 *
17008 * This routine is to get the next eligible FCF record index in a round
17009 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040017010 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050017011 * shall be returned, otherwise, the next eligible FCF record's index
17012 * shall be returned.
17013 **/
17014uint16_t
17015lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
17016{
17017 uint16_t next_fcf_index;
17018
James Smart421c6622013-01-03 15:44:16 -050017019initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040017020 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050017021 next_fcf_index = phba->fcf.current_rec.fcf_indx;
17022
James Smart7d791df2011-07-22 18:37:52 -040017023next_priority:
James Smart421c6622013-01-03 15:44:16 -050017024 /* Determine the next fcf index to check */
17025 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050017026 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17027 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040017028 next_fcf_index);
17029
James Smart0c9ab6f2010-02-26 14:15:57 -050017030 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040017031 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17032 /*
17033 * If we have wrapped then we need to clear the bits that
17034 * have been tested so that we can detect when we should
17035 * change the priority level.
17036 */
James Smart0c9ab6f2010-02-26 14:15:57 -050017037 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
17038 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040017039 }
17040
James Smart0c9ab6f2010-02-26 14:15:57 -050017041
James Smart3804dc82010-07-14 15:31:37 -040017042 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040017043 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
17044 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
17045 /*
17046 * If next fcf index is not found check if there are lower
17047 * Priority level fcf's in the fcf_priority list.
17048 * Set up the rr_bmask with all of the avaiable fcf bits
17049 * at that level and continue the selection process.
17050 */
17051 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050017052 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040017053 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17054 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040017055 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
17056 return LPFC_FCOE_FCF_NEXT_NONE;
17057 else {
17058 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
17059 "3063 Only FCF available idx %d, flag %x\n",
17060 next_fcf_index,
17061 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
17062 return next_fcf_index;
17063 }
James Smart3804dc82010-07-14 15:31:37 -040017064 }
17065
James Smart7d791df2011-07-22 18:37:52 -040017066 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
17067 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050017068 LPFC_FCF_FLOGI_FAILED) {
17069 if (list_is_singular(&phba->fcf.fcf_pri_list))
17070 return LPFC_FCOE_FCF_NEXT_NONE;
17071
James Smart7d791df2011-07-22 18:37:52 -040017072 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050017073 }
James Smart7d791df2011-07-22 18:37:52 -040017074
James Smart3804dc82010-07-14 15:31:37 -040017075 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017076 "2845 Get next roundrobin failover FCF (x%x)\n",
17077 next_fcf_index);
17078
James Smart0c9ab6f2010-02-26 14:15:57 -050017079 return next_fcf_index;
17080}
17081
17082/**
17083 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
17084 * @phba: pointer to lpfc hba data structure.
17085 *
17086 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040017087 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050017088 * does not go beyond the range of the driver allocated bmask dimension
17089 * before setting the bit.
17090 *
17091 * Returns 0 if the index bit successfully set, otherwise, it returns
17092 * -EINVAL.
17093 **/
17094int
17095lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
17096{
17097 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17098 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017099 "2610 FCF (x%x) reached driver's book "
17100 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050017101 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17102 return -EINVAL;
17103 }
17104 /* Set the eligible FCF record index bmask */
17105 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
17106
James Smart3804dc82010-07-14 15:31:37 -040017107 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017108 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040017109 "bmask\n", fcf_index);
17110
James Smart0c9ab6f2010-02-26 14:15:57 -050017111 return 0;
17112}
17113
17114/**
James Smart3804dc82010-07-14 15:31:37 -040017115 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050017116 * @phba: pointer to lpfc hba data structure.
17117 *
17118 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040017119 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050017120 * does not go beyond the range of the driver allocated bmask dimension
17121 * before clearing the bit.
17122 **/
17123void
17124lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
17125{
James Smart9a803a72013-09-06 12:17:56 -040017126 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050017127 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
17128 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017129 "2762 FCF (x%x) reached driver's book "
17130 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050017131 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
17132 return;
17133 }
17134 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040017135 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040017136 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
17137 list) {
James Smart7d791df2011-07-22 18:37:52 -040017138 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
17139 list_del_init(&fcf_pri->list);
17140 break;
17141 }
17142 }
17143 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050017144 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040017145
17146 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017147 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040017148 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050017149}
17150
17151/**
James Smartecfd03c2010-02-12 14:41:27 -050017152 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
17153 * @phba: pointer to lpfc hba data structure.
17154 *
17155 * This routine is the completion routine for the rediscover FCF table mailbox
17156 * command. If the mailbox command returned failure, it will try to stop the
17157 * FCF rediscover wait timer.
17158 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040017159static void
James Smartecfd03c2010-02-12 14:41:27 -050017160lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
17161{
17162 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17163 uint32_t shdr_status, shdr_add_status;
17164
17165 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17166
17167 shdr_status = bf_get(lpfc_mbox_hdr_status,
17168 &redisc_fcf->header.cfg_shdr.response);
17169 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
17170 &redisc_fcf->header.cfg_shdr.response);
17171 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050017172 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050017173 "2746 Requesting for FCF rediscovery failed "
17174 "status x%x add_status x%x\n",
17175 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050017176 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050017177 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050017178 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050017179 spin_unlock_irq(&phba->hbalock);
17180 /*
17181 * CVL event triggered FCF rediscover request failed,
17182 * last resort to re-try current registered FCF entry.
17183 */
17184 lpfc_retry_pport_discovery(phba);
17185 } else {
17186 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050017187 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050017188 spin_unlock_irq(&phba->hbalock);
17189 /*
17190 * DEAD FCF event triggered FCF rediscover request
17191 * failed, last resort to fail over as a link down
17192 * to FCF registration.
17193 */
17194 lpfc_sli4_fcf_dead_failthrough(phba);
17195 }
James Smart0c9ab6f2010-02-26 14:15:57 -050017196 } else {
17197 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040017198 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050017199 /*
17200 * Start FCF rediscovery wait timer for pending FCF
17201 * before rescan FCF record table.
17202 */
17203 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050017204 }
James Smartecfd03c2010-02-12 14:41:27 -050017205
17206 mempool_free(mbox, phba->mbox_mem_pool);
17207}
17208
17209/**
James Smart3804dc82010-07-14 15:31:37 -040017210 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050017211 * @phba: pointer to lpfc hba data structure.
17212 *
17213 * This routine is invoked to request for rediscovery of the entire FCF table
17214 * by the port.
17215 **/
17216int
17217lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
17218{
17219 LPFC_MBOXQ_t *mbox;
17220 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
17221 int rc, length;
17222
James Smart0c9ab6f2010-02-26 14:15:57 -050017223 /* Cancel retry delay timers to all vports before FCF rediscover */
17224 lpfc_cancel_all_vport_retry_delay_timer(phba);
17225
James Smartecfd03c2010-02-12 14:41:27 -050017226 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17227 if (!mbox) {
17228 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17229 "2745 Failed to allocate mbox for "
17230 "requesting FCF rediscover.\n");
17231 return -ENOMEM;
17232 }
17233
17234 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
17235 sizeof(struct lpfc_sli4_cfg_mhdr));
17236 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
17237 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
17238 length, LPFC_SLI4_MBX_EMBED);
17239
17240 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
17241 /* Set count to 0 for invalidating the entire FCF database */
17242 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
17243
17244 /* Issue the mailbox command asynchronously */
17245 mbox->vport = phba->pport;
17246 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
17247 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
17248
17249 if (rc == MBX_NOT_FINISHED) {
17250 mempool_free(mbox, phba->mbox_mem_pool);
17251 return -EIO;
17252 }
17253 return 0;
17254}
17255
17256/**
James Smartfc2b9892010-02-26 14:15:29 -050017257 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
17258 * @phba: pointer to lpfc hba data structure.
17259 *
17260 * This function is the failover routine as a last resort to the FCF DEAD
17261 * event when driver failed to perform fast FCF failover.
17262 **/
17263void
17264lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
17265{
17266 uint32_t link_state;
17267
17268 /*
17269 * Last resort as FCF DEAD event failover will treat this as
17270 * a link down, but save the link state because we don't want
17271 * it to be changed to Link Down unless it is already down.
17272 */
17273 link_state = phba->link_state;
17274 lpfc_linkdown(phba);
17275 phba->link_state = link_state;
17276
17277 /* Unregister FCF if no devices connected to it */
17278 lpfc_unregister_unused_fcf(phba);
17279}
17280
17281/**
James Smart026abb82011-12-13 13:20:45 -050017282 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040017283 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050017284 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040017285 *
James Smart026abb82011-12-13 13:20:45 -050017286 * This function gets SLI3 port configure region 23 data through memory dump
17287 * mailbox command. When it successfully retrieves data, the size of the data
17288 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040017289 **/
James Smart026abb82011-12-13 13:20:45 -050017290static uint32_t
17291lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040017292{
17293 LPFC_MBOXQ_t *pmb = NULL;
17294 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050017295 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040017296 int rc;
17297
James Smart026abb82011-12-13 13:20:45 -050017298 if (!rgn23_data)
17299 return 0;
17300
James Smarta0c87cb2009-07-19 10:01:10 -040017301 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17302 if (!pmb) {
17303 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050017304 "2600 failed to allocate mailbox memory\n");
17305 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040017306 }
17307 mb = &pmb->u.mb;
17308
James Smarta0c87cb2009-07-19 10:01:10 -040017309 do {
17310 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
17311 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
17312
17313 if (rc != MBX_SUCCESS) {
17314 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050017315 "2601 failed to read config "
17316 "region 23, rc 0x%x Status 0x%x\n",
17317 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040017318 mb->un.varDmp.word_cnt = 0;
17319 }
17320 /*
17321 * dump mem may return a zero when finished or we got a
17322 * mailbox error, either way we are done.
17323 */
17324 if (mb->un.varDmp.word_cnt == 0)
17325 break;
17326 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
17327 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
17328
17329 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050017330 rgn23_data + offset,
17331 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040017332 offset += mb->un.varDmp.word_cnt;
17333 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
17334
James Smart026abb82011-12-13 13:20:45 -050017335 mempool_free(pmb, phba->mbox_mem_pool);
17336 return offset;
17337}
17338
17339/**
17340 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
17341 * @phba: pointer to lpfc hba data structure.
17342 * @rgn23_data: pointer to configure region 23 data.
17343 *
17344 * This function gets SLI4 port configure region 23 data through memory dump
17345 * mailbox command. When it successfully retrieves data, the size of the data
17346 * will be returned, otherwise, 0 will be returned.
17347 **/
17348static uint32_t
17349lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
17350{
17351 LPFC_MBOXQ_t *mboxq = NULL;
17352 struct lpfc_dmabuf *mp = NULL;
17353 struct lpfc_mqe *mqe;
17354 uint32_t data_length = 0;
17355 int rc;
17356
17357 if (!rgn23_data)
17358 return 0;
17359
17360 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17361 if (!mboxq) {
17362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17363 "3105 failed to allocate mailbox memory\n");
17364 return 0;
17365 }
17366
17367 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
17368 goto out;
17369 mqe = &mboxq->u.mqe;
17370 mp = (struct lpfc_dmabuf *) mboxq->context1;
17371 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
17372 if (rc)
17373 goto out;
17374 data_length = mqe->un.mb_words[5];
17375 if (data_length == 0)
17376 goto out;
17377 if (data_length > DMP_RGN23_SIZE) {
17378 data_length = 0;
17379 goto out;
17380 }
17381 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
17382out:
17383 mempool_free(mboxq, phba->mbox_mem_pool);
17384 if (mp) {
17385 lpfc_mbuf_free(phba, mp->virt, mp->phys);
17386 kfree(mp);
17387 }
17388 return data_length;
17389}
17390
17391/**
17392 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
17393 * @phba: pointer to lpfc hba data structure.
17394 *
17395 * This function read region 23 and parse TLV for port status to
17396 * decide if the user disaled the port. If the TLV indicates the
17397 * port is disabled, the hba_flag is set accordingly.
17398 **/
17399void
17400lpfc_sli_read_link_ste(struct lpfc_hba *phba)
17401{
17402 uint8_t *rgn23_data = NULL;
17403 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
17404 uint32_t offset = 0;
17405
17406 /* Get adapter Region 23 data */
17407 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
17408 if (!rgn23_data)
17409 goto out;
17410
17411 if (phba->sli_rev < LPFC_SLI_REV4)
17412 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
17413 else {
17414 if_type = bf_get(lpfc_sli_intf_if_type,
17415 &phba->sli4_hba.sli_intf);
17416 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
17417 goto out;
17418 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
17419 }
James Smarta0c87cb2009-07-19 10:01:10 -040017420
17421 if (!data_size)
17422 goto out;
17423
17424 /* Check the region signature first */
17425 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
17426 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17427 "2619 Config region 23 has bad signature\n");
17428 goto out;
17429 }
17430 offset += 4;
17431
17432 /* Check the data structure version */
17433 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
17434 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17435 "2620 Config region 23 has bad version\n");
17436 goto out;
17437 }
17438 offset += 4;
17439
17440 /* Parse TLV entries in the region */
17441 while (offset < data_size) {
17442 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
17443 break;
17444 /*
17445 * If the TLV is not driver specific TLV or driver id is
17446 * not linux driver id, skip the record.
17447 */
17448 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
17449 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
17450 (rgn23_data[offset + 3] != 0)) {
17451 offset += rgn23_data[offset + 1] * 4 + 4;
17452 continue;
17453 }
17454
17455 /* Driver found a driver specific TLV in the config region */
17456 sub_tlv_len = rgn23_data[offset + 1] * 4;
17457 offset += 4;
17458 tlv_offset = 0;
17459
17460 /*
17461 * Search for configured port state sub-TLV.
17462 */
17463 while ((offset < data_size) &&
17464 (tlv_offset < sub_tlv_len)) {
17465 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
17466 offset += 4;
17467 tlv_offset += 4;
17468 break;
17469 }
17470 if (rgn23_data[offset] != PORT_STE_TYPE) {
17471 offset += rgn23_data[offset + 1] * 4 + 4;
17472 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
17473 continue;
17474 }
17475
17476 /* This HBA contains PORT_STE configured */
17477 if (!rgn23_data[offset + 2])
17478 phba->hba_flag |= LINK_DISABLED;
17479
17480 goto out;
17481 }
17482 }
James Smart026abb82011-12-13 13:20:45 -050017483
James Smarta0c87cb2009-07-19 10:01:10 -040017484out:
James Smarta0c87cb2009-07-19 10:01:10 -040017485 kfree(rgn23_data);
17486 return;
17487}
James Smart695a8142010-01-26 23:08:03 -050017488
17489/**
James Smart52d52442011-05-24 11:42:45 -040017490 * lpfc_wr_object - write an object to the firmware
17491 * @phba: HBA structure that indicates port to create a queue on.
17492 * @dmabuf_list: list of dmabufs to write to the port.
17493 * @size: the total byte value of the objects to write to the port.
17494 * @offset: the current offset to be used to start the transfer.
17495 *
17496 * This routine will create a wr_object mailbox command to send to the port.
17497 * the mailbox command will be constructed using the dma buffers described in
17498 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
17499 * BDEs that the imbedded mailbox can support. The @offset variable will be
17500 * used to indicate the starting offset of the transfer and will also return
17501 * the offset after the write object mailbox has completed. @size is used to
17502 * determine the end of the object and whether the eof bit should be set.
17503 *
17504 * Return 0 is successful and offset will contain the the new offset to use
17505 * for the next write.
17506 * Return negative value for error cases.
17507 **/
17508int
17509lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
17510 uint32_t size, uint32_t *offset)
17511{
17512 struct lpfc_mbx_wr_object *wr_object;
17513 LPFC_MBOXQ_t *mbox;
17514 int rc = 0, i = 0;
17515 uint32_t shdr_status, shdr_add_status;
17516 uint32_t mbox_tmo;
17517 union lpfc_sli4_cfg_shdr *shdr;
17518 struct lpfc_dmabuf *dmabuf;
17519 uint32_t written = 0;
17520
17521 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17522 if (!mbox)
17523 return -ENOMEM;
17524
17525 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17526 LPFC_MBOX_OPCODE_WRITE_OBJECT,
17527 sizeof(struct lpfc_mbx_wr_object) -
17528 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17529
17530 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
17531 wr_object->u.request.write_offset = *offset;
17532 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
17533 wr_object->u.request.object_name[0] =
17534 cpu_to_le32(wr_object->u.request.object_name[0]);
17535 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
17536 list_for_each_entry(dmabuf, dmabuf_list, list) {
17537 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
17538 break;
17539 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
17540 wr_object->u.request.bde[i].addrHigh =
17541 putPaddrHigh(dmabuf->phys);
17542 if (written + SLI4_PAGE_SIZE >= size) {
17543 wr_object->u.request.bde[i].tus.f.bdeSize =
17544 (size - written);
17545 written += (size - written);
17546 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
17547 } else {
17548 wr_object->u.request.bde[i].tus.f.bdeSize =
17549 SLI4_PAGE_SIZE;
17550 written += SLI4_PAGE_SIZE;
17551 }
17552 i++;
17553 }
17554 wr_object->u.request.bde_count = i;
17555 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
17556 if (!phba->sli4_hba.intr_enable)
17557 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17558 else {
James Smarta183a152011-10-10 21:32:43 -040017559 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040017560 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17561 }
17562 /* The IOCTL status is embedded in the mailbox subheader. */
17563 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
17564 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17565 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17566 if (rc != MBX_TIMEOUT)
17567 mempool_free(mbox, phba->mbox_mem_pool);
17568 if (shdr_status || shdr_add_status || rc) {
17569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17570 "3025 Write Object mailbox failed with "
17571 "status x%x add_status x%x, mbx status x%x\n",
17572 shdr_status, shdr_add_status, rc);
17573 rc = -ENXIO;
17574 } else
17575 *offset += wr_object->u.response.actual_write_length;
17576 return rc;
17577}
17578
17579/**
James Smart695a8142010-01-26 23:08:03 -050017580 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
17581 * @vport: pointer to vport data structure.
17582 *
17583 * This function iterate through the mailboxq and clean up all REG_LOGIN
17584 * and REG_VPI mailbox commands associated with the vport. This function
17585 * is called when driver want to restart discovery of the vport due to
17586 * a Clear Virtual Link event.
17587 **/
17588void
17589lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
17590{
17591 struct lpfc_hba *phba = vport->phba;
17592 LPFC_MBOXQ_t *mb, *nextmb;
17593 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040017594 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040017595 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040017596 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040017597 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050017598 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050017599
James Smartd439d282010-09-29 11:18:45 -040017600 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050017601 spin_lock_irq(&phba->hbalock);
17602 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
17603 if (mb->vport != vport)
17604 continue;
17605
17606 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17607 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17608 continue;
17609
James Smartd439d282010-09-29 11:18:45 -040017610 list_del(&mb->list);
17611 list_add_tail(&mb->list, &mbox_cmd_list);
17612 }
17613 /* Clean up active mailbox command with the vport */
17614 mb = phba->sli.mbox_active;
17615 if (mb && (mb->vport == vport)) {
17616 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
17617 (mb->u.mb.mbxCommand == MBX_REG_VPI))
17618 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17619 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17620 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
17621 /* Put reference count for delayed processing */
17622 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
17623 /* Unregister the RPI when mailbox complete */
17624 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17625 }
17626 }
James Smart63e801c2010-11-20 23:14:19 -050017627 /* Cleanup any mailbox completions which are not yet processed */
17628 do {
17629 restart_loop = 0;
17630 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
17631 /*
17632 * If this mailox is already processed or it is
17633 * for another vport ignore it.
17634 */
17635 if ((mb->vport != vport) ||
17636 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
17637 continue;
17638
17639 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17640 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17641 continue;
17642
17643 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17644 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17645 ndlp = (struct lpfc_nodelist *)mb->context2;
17646 /* Unregister the RPI when mailbox complete */
17647 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17648 restart_loop = 1;
17649 spin_unlock_irq(&phba->hbalock);
17650 spin_lock(shost->host_lock);
17651 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17652 spin_unlock(shost->host_lock);
17653 spin_lock_irq(&phba->hbalock);
17654 break;
17655 }
17656 }
17657 } while (restart_loop);
17658
James Smartd439d282010-09-29 11:18:45 -040017659 spin_unlock_irq(&phba->hbalock);
17660
17661 /* Release the cleaned-up mailbox commands */
17662 while (!list_empty(&mbox_cmd_list)) {
17663 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050017664 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17665 mp = (struct lpfc_dmabuf *) (mb->context1);
17666 if (mp) {
17667 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
17668 kfree(mp);
17669 }
James Smart78730cf2010-04-06 15:06:30 -040017670 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040017671 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040017672 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017673 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040017674 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017675 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040017676 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040017677 }
James Smart695a8142010-01-26 23:08:03 -050017678 }
James Smart695a8142010-01-26 23:08:03 -050017679 mempool_free(mb, phba->mbox_mem_pool);
17680 }
James Smartd439d282010-09-29 11:18:45 -040017681
17682 /* Release the ndlp with the cleaned-up active mailbox command */
17683 if (act_mbx_ndlp) {
17684 spin_lock(shost->host_lock);
17685 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17686 spin_unlock(shost->host_lock);
17687 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050017688 }
James Smart695a8142010-01-26 23:08:03 -050017689}
17690
James Smart2a9bf3d2010-06-07 15:24:45 -040017691/**
17692 * lpfc_drain_txq - Drain the txq
17693 * @phba: Pointer to HBA context object.
17694 *
17695 * This function attempt to submit IOCBs on the txq
17696 * to the adapter. For SLI4 adapters, the txq contains
17697 * ELS IOCBs that have been deferred because the there
17698 * are no SGLs. This congestion can occur with large
17699 * vport counts during node discovery.
17700 **/
17701
17702uint32_t
17703lpfc_drain_txq(struct lpfc_hba *phba)
17704{
17705 LIST_HEAD(completions);
James Smart895427b2017-02-12 13:52:30 -080017706 struct lpfc_sli_ring *pring;
Daeseok Youn2e706372014-02-21 09:03:32 +090017707 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040017708 unsigned long iflags = 0;
17709 char *fail_msg = NULL;
17710 struct lpfc_sglq *sglq;
James Smart2f077842016-12-19 15:07:29 -080017711 union lpfc_wqe128 wqe128;
17712 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
James Smarta2fc4aef2014-09-03 12:57:55 -040017713 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040017714
James Smart895427b2017-02-12 13:52:30 -080017715 pring = lpfc_phba_elsring(phba);
17716
James Smart398d81c2013-05-31 17:04:19 -040017717 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050017718 list_for_each_entry(piocbq, &pring->txq, list) {
17719 txq_cnt++;
17720 }
17721
17722 if (txq_cnt > pring->txq_max)
17723 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017724
James Smart398d81c2013-05-31 17:04:19 -040017725 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017726
James Smart0e9bb8d2013-03-01 16:35:12 -050017727 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040017728 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017729
James Smart19ca7602010-11-20 23:11:55 -050017730 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040017731 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040017732 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040017733 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17734 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050017735 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040017736 break;
17737 }
James Smart895427b2017-02-12 13:52:30 -080017738 sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040017739 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050017740 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040017741 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017742 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040017743 }
James Smart0e9bb8d2013-03-01 16:35:12 -050017744 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040017745
17746 /* The xri and iocb resources secured,
17747 * attempt to issue request
17748 */
James Smart6d368e52011-05-24 11:44:12 -040017749 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040017750 piocbq->sli4_xritag = sglq->sli4_xritag;
17751 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17752 fail_msg = "to convert bpl to sgl";
James Smart2f077842016-12-19 15:07:29 -080017753 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040017754 fail_msg = "to convert iocb to wqe";
James Smart2f077842016-12-19 15:07:29 -080017755 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040017756 fail_msg = " - Wq is full";
17757 else
17758 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17759
17760 if (fail_msg) {
17761 /* Failed means we can't issue and need to cancel */
17762 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17763 "2822 IOCB failed %s iotag 0x%x "
17764 "xri 0x%x\n",
17765 fail_msg,
17766 piocbq->iotag, piocbq->sli4_xritag);
17767 list_add_tail(&piocbq->list, &completions);
17768 }
James Smart398d81c2013-05-31 17:04:19 -040017769 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017770 }
17771
James Smart2a9bf3d2010-06-07 15:24:45 -040017772 /* Cancel all the IOCBs that cannot be issued */
17773 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17774 IOERR_SLI_ABORTED);
17775
James Smart0e9bb8d2013-03-01 16:35:12 -050017776 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017777}
James Smart895427b2017-02-12 13:52:30 -080017778
17779/**
17780 * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
17781 * @phba: Pointer to HBA context object.
17782 * @pwqe: Pointer to command WQE.
17783 * @sglq: Pointer to the scatter gather queue object.
17784 *
17785 * This routine converts the bpl or bde that is in the WQE
17786 * to a sgl list for the sli4 hardware. The physical address
17787 * of the bpl/bde is converted back to a virtual address.
17788 * If the WQE contains a BPL then the list of BDE's is
17789 * converted to sli4_sge's. If the WQE contains a single
17790 * BDE then it is converted to a single sli_sge.
17791 * The WQE is still in cpu endianness so the contents of
17792 * the bpl can be used without byte swapping.
17793 *
17794 * Returns valid XRI = Success, NO_XRI = Failure.
17795 */
17796static uint16_t
17797lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
17798 struct lpfc_sglq *sglq)
17799{
17800 uint16_t xritag = NO_XRI;
17801 struct ulp_bde64 *bpl = NULL;
17802 struct ulp_bde64 bde;
17803 struct sli4_sge *sgl = NULL;
17804 struct lpfc_dmabuf *dmabuf;
17805 union lpfc_wqe *wqe;
17806 int numBdes = 0;
17807 int i = 0;
17808 uint32_t offset = 0; /* accumulated offset in the sg request list */
17809 int inbound = 0; /* number of sg reply entries inbound from firmware */
17810 uint32_t cmd;
17811
17812 if (!pwqeq || !sglq)
17813 return xritag;
17814
17815 sgl = (struct sli4_sge *)sglq->sgl;
17816 wqe = &pwqeq->wqe;
17817 pwqeq->iocb.ulpIoTag = pwqeq->iotag;
17818
17819 cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
17820 if (cmd == CMD_XMIT_BLS_RSP64_WQE)
17821 return sglq->sli4_xritag;
17822 numBdes = pwqeq->rsvd2;
17823 if (numBdes) {
17824 /* The addrHigh and addrLow fields within the WQE
17825 * have not been byteswapped yet so there is no
17826 * need to swap them back.
17827 */
17828 if (pwqeq->context3)
17829 dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
17830 else
17831 return xritag;
17832
17833 bpl = (struct ulp_bde64 *)dmabuf->virt;
17834 if (!bpl)
17835 return xritag;
17836
17837 for (i = 0; i < numBdes; i++) {
17838 /* Should already be byte swapped. */
17839 sgl->addr_hi = bpl->addrHigh;
17840 sgl->addr_lo = bpl->addrLow;
17841
17842 sgl->word2 = le32_to_cpu(sgl->word2);
17843 if ((i+1) == numBdes)
17844 bf_set(lpfc_sli4_sge_last, sgl, 1);
17845 else
17846 bf_set(lpfc_sli4_sge_last, sgl, 0);
17847 /* swap the size field back to the cpu so we
17848 * can assign it to the sgl.
17849 */
17850 bde.tus.w = le32_to_cpu(bpl->tus.w);
17851 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
17852 /* The offsets in the sgl need to be accumulated
17853 * separately for the request and reply lists.
17854 * The request is always first, the reply follows.
17855 */
17856 switch (cmd) {
17857 case CMD_GEN_REQUEST64_WQE:
17858 /* add up the reply sg entries */
17859 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
17860 inbound++;
17861 /* first inbound? reset the offset */
17862 if (inbound == 1)
17863 offset = 0;
17864 bf_set(lpfc_sli4_sge_offset, sgl, offset);
17865 bf_set(lpfc_sli4_sge_type, sgl,
17866 LPFC_SGE_TYPE_DATA);
17867 offset += bde.tus.f.bdeSize;
17868 break;
17869 case CMD_FCP_TRSP64_WQE:
17870 bf_set(lpfc_sli4_sge_offset, sgl, 0);
17871 bf_set(lpfc_sli4_sge_type, sgl,
17872 LPFC_SGE_TYPE_DATA);
17873 break;
17874 case CMD_FCP_TSEND64_WQE:
17875 case CMD_FCP_TRECEIVE64_WQE:
17876 bf_set(lpfc_sli4_sge_type, sgl,
17877 bpl->tus.f.bdeFlags);
17878 if (i < 3)
17879 offset = 0;
17880 else
17881 offset += bde.tus.f.bdeSize;
17882 bf_set(lpfc_sli4_sge_offset, sgl, offset);
17883 break;
17884 }
17885 sgl->word2 = cpu_to_le32(sgl->word2);
17886 bpl++;
17887 sgl++;
17888 }
17889 } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
17890 /* The addrHigh and addrLow fields of the BDE have not
17891 * been byteswapped yet so they need to be swapped
17892 * before putting them in the sgl.
17893 */
17894 sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
17895 sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
17896 sgl->word2 = le32_to_cpu(sgl->word2);
17897 bf_set(lpfc_sli4_sge_last, sgl, 1);
17898 sgl->word2 = cpu_to_le32(sgl->word2);
17899 sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
17900 }
17901 return sglq->sli4_xritag;
17902}
17903
17904/**
17905 * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
17906 * @phba: Pointer to HBA context object.
17907 * @ring_number: Base sli ring number
17908 * @pwqe: Pointer to command WQE.
17909 **/
17910int
17911lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
17912 struct lpfc_iocbq *pwqe)
17913{
17914 union lpfc_wqe *wqe = &pwqe->wqe;
17915 struct lpfc_queue *wq;
17916 struct lpfc_sglq *sglq;
17917 struct lpfc_sli_ring *pring;
17918 unsigned long iflags;
17919
17920 /* NVME_LS and NVME_LS ABTS requests. */
17921 if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
17922 pring = phba->sli4_hba.nvmels_wq->pring;
17923 spin_lock_irqsave(&pring->ring_lock, iflags);
17924 sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
17925 if (!sglq) {
17926 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17927 return WQE_BUSY;
17928 }
17929 pwqe->sli4_lxritag = sglq->sli4_lxritag;
17930 pwqe->sli4_xritag = sglq->sli4_xritag;
17931 if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
17932 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17933 return WQE_ERROR;
17934 }
17935 bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
17936 pwqe->sli4_xritag);
17937 if (lpfc_sli4_wq_put(phba->sli4_hba.nvmels_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 /* NVME_FCREQ and NVME_ABTS requests */
17947 if (pwqe->iocb_flag & LPFC_IO_NVME) {
17948 /* Get the IO distribution (hba_wqidx) for WQ assignment. */
17949 pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
17950
17951 spin_lock_irqsave(&pring->ring_lock, iflags);
17952 wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
17953 bf_set(wqe_cqid, &wqe->generic.wqe_com,
17954 phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
17955 if (lpfc_sli4_wq_put(wq, wqe)) {
17956 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17957 return WQE_ERROR;
17958 }
17959 lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
17960 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17961 return 0;
17962 }
17963
17964 return WQE_ERROR;
17965}