blob: 19543142c94ca94f19c1b3c34d3f02ff88c16297 [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 Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050038#include "lpfc_hw.h"
39#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040040#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040041#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc_disc.h"
43#include "lpfc_scsi.h"
44#include "lpfc.h"
45#include "lpfc_crtn.h"
46#include "lpfc_logmsg.h"
47#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050048#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040049#include "lpfc_vport.h"
James Smart61bda8f2016-10-13 15:06:05 -070050#include "lpfc_version.h"
dea31012005-04-17 16:05:31 -050051
52/* There are only four IOCB completion types. */
53typedef enum _lpfc_iocb_type {
54 LPFC_UNKNOWN_IOCB,
55 LPFC_UNSOL_IOCB,
56 LPFC_SOL_IOCB,
57 LPFC_ABORT_IOCB
58} lpfc_iocb_type;
59
James Smart4f774512009-05-22 14:52:35 -040060
61/* Provide function prototypes local to this module. */
62static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
63 uint32_t);
64static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040065 uint8_t *, uint32_t *);
66static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
67 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040068static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
69 struct hbq_dmabuf *);
James Smart05580562011-05-24 11:40:48 -040070static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
71 struct lpfc_cqe *);
James Smart8a9d2e82012-05-09 21:16:12 -040072static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
73 int);
James Smartba20c852012-08-03 12:36:52 -040074static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
75 uint32_t);
James Smarte8d3c3b2013-10-10 12:21:30 -040076static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
77static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart05580562011-05-24 11:40:48 -040078
James Smart4f774512009-05-22 14:52:35 -040079static IOCB_t *
80lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
81{
82 return &iocbq->iocb;
83}
84
85/**
86 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
87 * @q: The Work Queue to operate on.
88 * @wqe: The work Queue Entry to put on the Work queue.
89 *
90 * This routine will copy the contents of @wqe to the next available entry on
91 * the @q. This function will then ring the Work Queue Doorbell to signal the
92 * HBA to start processing the Work Queue Entry. This function returns 0 if
93 * successful. If no entries are available on @q then this function will return
94 * -ENOMEM.
95 * The caller is expected to hold the hbalock when calling this routine.
96 **/
97static uint32_t
98lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
99{
James Smart2e90f4b2011-12-13 13:22:37 -0500100 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400101 struct lpfc_register doorbell;
102 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400103 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400104
James Smart2e90f4b2011-12-13 13:22:37 -0500105 /* sanity check on queue memory */
106 if (unlikely(!q))
107 return -ENOMEM;
108 temp_wqe = q->qe[q->host_index].wqe;
109
James Smart4f774512009-05-22 14:52:35 -0400110 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400111 idx = ((q->host_index + 1) % q->entry_count);
112 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400113 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400114 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400115 }
116 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400117 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500118 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400119 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500120 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
121 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400122 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart6b3b3bd2016-12-19 15:07:30 -0800123 /* ensure WQE bcopy flushed before doorbell write */
124 wmb();
James Smart4f774512009-05-22 14:52:35 -0400125
126 /* Update the host index before invoking device */
127 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400128
129 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400130
131 /* Ring Doorbell */
132 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500133 if (q->db_format == LPFC_DB_LIST_FORMAT) {
134 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
135 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
136 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
137 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
138 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
139 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
140 } else {
141 return -EINVAL;
142 }
143 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400144
145 return 0;
146}
147
148/**
149 * lpfc_sli4_wq_release - Updates internal hba index for WQ
150 * @q: The Work Queue to operate on.
151 * @index: The index to advance the hba index to.
152 *
153 * This routine will update the HBA index of a queue to reflect consumption of
154 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
155 * an entry the host calls this function to update the queue's internal
156 * pointers. This routine returns the number of entries that were consumed by
157 * the HBA.
158 **/
159static uint32_t
160lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
161{
162 uint32_t released = 0;
163
James Smart2e90f4b2011-12-13 13:22:37 -0500164 /* sanity check on queue memory */
165 if (unlikely(!q))
166 return 0;
167
James Smart4f774512009-05-22 14:52:35 -0400168 if (q->hba_index == index)
169 return 0;
170 do {
171 q->hba_index = ((q->hba_index + 1) % q->entry_count);
172 released++;
173 } while (q->hba_index != index);
174 return released;
175}
176
177/**
178 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
179 * @q: The Mailbox Queue to operate on.
180 * @wqe: The Mailbox Queue Entry to put on the Work queue.
181 *
182 * This routine will copy the contents of @mqe to the next available entry on
183 * the @q. This function will then ring the Work Queue Doorbell to signal the
184 * HBA to start processing the Work Queue Entry. This function returns 0 if
185 * successful. If no entries are available on @q then this function will return
186 * -ENOMEM.
187 * The caller is expected to hold the hbalock when calling this routine.
188 **/
189static uint32_t
190lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
191{
James Smart2e90f4b2011-12-13 13:22:37 -0500192 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400193 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400194
James Smart2e90f4b2011-12-13 13:22:37 -0500195 /* sanity check on queue memory */
196 if (unlikely(!q))
197 return -ENOMEM;
198 temp_mqe = q->qe[q->host_index].mqe;
199
James Smart4f774512009-05-22 14:52:35 -0400200 /* If the host has not yet processed the next entry then we are done */
201 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
202 return -ENOMEM;
203 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
204 /* Save off the mailbox pointer for completion */
205 q->phba->mbox = (MAILBOX_t *)temp_mqe;
206
207 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400208 q->host_index = ((q->host_index + 1) % q->entry_count);
209
210 /* Ring Doorbell */
211 doorbell.word0 = 0;
212 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
213 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
214 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400215 return 0;
216}
217
218/**
219 * lpfc_sli4_mq_release - Updates internal hba index for MQ
220 * @q: The Mailbox Queue to operate on.
221 *
222 * This routine will update the HBA index of a queue to reflect consumption of
223 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
224 * an entry the host calls this function to update the queue's internal
225 * pointers. This routine returns the number of entries that were consumed by
226 * the HBA.
227 **/
228static uint32_t
229lpfc_sli4_mq_release(struct lpfc_queue *q)
230{
James Smart2e90f4b2011-12-13 13:22:37 -0500231 /* sanity check on queue memory */
232 if (unlikely(!q))
233 return 0;
234
James Smart4f774512009-05-22 14:52:35 -0400235 /* Clear the mailbox pointer for completion */
236 q->phba->mbox = NULL;
237 q->hba_index = ((q->hba_index + 1) % q->entry_count);
238 return 1;
239}
240
241/**
242 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
243 * @q: The Event Queue to get the first valid EQE from
244 *
245 * This routine will get the first valid Event Queue Entry from @q, update
246 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
247 * the Queue (no more work to do), or the Queue is full of EQEs that have been
248 * processed, but not popped back to the HBA then this routine will return NULL.
249 **/
250static struct lpfc_eqe *
251lpfc_sli4_eq_get(struct lpfc_queue *q)
252{
James Smart2e90f4b2011-12-13 13:22:37 -0500253 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400254 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500255
256 /* sanity check on queue memory */
257 if (unlikely(!q))
258 return NULL;
259 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400260
261 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400262 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400263 return NULL;
264 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400265 idx = ((q->hba_index + 1) % q->entry_count);
266 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400267 return NULL;
268
James Smart027140e2012-08-03 12:35:44 -0400269 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400270
271 /*
272 * insert barrier for instruction interlock : data from the hardware
273 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800274 * upon. Speculative instructions were allowing a bcopy at the start
275 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
276 * after our return, to copy data before the valid bit check above
277 * was done. As such, some of the copied data was stale. The barrier
278 * ensures the check is before any data is copied.
James Smart27f344e2014-05-07 17:16:46 -0400279 */
280 mb();
James Smart4f774512009-05-22 14:52:35 -0400281 return eqe;
282}
283
284/**
James Smartba20c852012-08-03 12:36:52 -0400285 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
286 * @q: The Event Queue to disable interrupts
287 *
288 **/
289static inline void
290lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
291{
292 struct lpfc_register doorbell;
293
294 doorbell.word0 = 0;
295 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
296 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
297 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
298 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
299 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
300 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
301}
302
303/**
James Smart4f774512009-05-22 14:52:35 -0400304 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
305 * @q: The Event Queue that the host has completed processing for.
306 * @arm: Indicates whether the host wants to arms this CQ.
307 *
308 * This routine will mark all Event Queue Entries on @q, from the last
309 * known completed entry to the last entry that was processed, as completed
310 * by clearing the valid bit for each completion queue entry. Then it will
311 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
312 * The internal host index in the @q will be updated by this routine to indicate
313 * that the host has finished processing the entries. The @arm parameter
314 * indicates that the queue should be rearmed when ringing the doorbell.
315 *
316 * This function will return the number of EQEs that were popped.
317 **/
318uint32_t
319lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
320{
321 uint32_t released = 0;
322 struct lpfc_eqe *temp_eqe;
323 struct lpfc_register doorbell;
324
James Smart2e90f4b2011-12-13 13:22:37 -0500325 /* sanity check on queue memory */
326 if (unlikely(!q))
327 return 0;
328
James Smart4f774512009-05-22 14:52:35 -0400329 /* while there are valid entries */
330 while (q->hba_index != q->host_index) {
331 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400332 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400333 released++;
334 q->host_index = ((q->host_index + 1) % q->entry_count);
335 }
336 if (unlikely(released == 0 && !arm))
337 return 0;
338
339 /* ring doorbell for number popped */
340 doorbell.word0 = 0;
341 if (arm) {
342 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
343 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
344 }
345 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
346 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500347 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
348 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
349 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400350 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500351 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
352 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
353 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400354 return released;
355}
356
357/**
358 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
359 * @q: The Completion Queue to get the first valid CQE from
360 *
361 * This routine will get the first valid Completion Queue Entry from @q, update
362 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
363 * the Queue (no more work to do), or the Queue is full of CQEs that have been
364 * processed, but not popped back to the HBA then this routine will return NULL.
365 **/
366static struct lpfc_cqe *
367lpfc_sli4_cq_get(struct lpfc_queue *q)
368{
369 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400370 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400371
James Smart2e90f4b2011-12-13 13:22:37 -0500372 /* sanity check on queue memory */
373 if (unlikely(!q))
374 return NULL;
375
James Smart4f774512009-05-22 14:52:35 -0400376 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400377 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400378 return NULL;
379 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400380 idx = ((q->hba_index + 1) % q->entry_count);
381 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400382 return NULL;
383
384 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400385 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400386
387 /*
388 * insert barrier for instruction interlock : data from the hardware
389 * must have the valid bit checked before it can be copied and acted
James Smart2ea259e2017-02-12 13:52:27 -0800390 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
391 * instructions allowing action on content before valid bit checked,
392 * add barrier here as well. May not be needed as "content" is a
393 * single 32-bit entity here (vs multi word structure for cq's).
James Smart27f344e2014-05-07 17:16:46 -0400394 */
395 mb();
James Smart4f774512009-05-22 14:52:35 -0400396 return cqe;
397}
398
399/**
400 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
401 * @q: The Completion Queue that the host has completed processing for.
402 * @arm: Indicates whether the host wants to arms this CQ.
403 *
404 * This routine will mark all Completion queue entries on @q, from the last
405 * known completed entry to the last entry that was processed, as completed
406 * by clearing the valid bit for each completion queue entry. Then it will
407 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
408 * The internal host index in the @q will be updated by this routine to indicate
409 * that the host has finished processing the entries. The @arm parameter
410 * indicates that the queue should be rearmed when ringing the doorbell.
411 *
412 * This function will return the number of CQEs that were released.
413 **/
414uint32_t
415lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
416{
417 uint32_t released = 0;
418 struct lpfc_cqe *temp_qe;
419 struct lpfc_register doorbell;
420
James Smart2e90f4b2011-12-13 13:22:37 -0500421 /* sanity check on queue memory */
422 if (unlikely(!q))
423 return 0;
James Smart4f774512009-05-22 14:52:35 -0400424 /* while there are valid entries */
425 while (q->hba_index != q->host_index) {
426 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400427 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400428 released++;
429 q->host_index = ((q->host_index + 1) % q->entry_count);
430 }
431 if (unlikely(released == 0 && !arm))
432 return 0;
433
434 /* ring doorbell for number popped */
435 doorbell.word0 = 0;
436 if (arm)
437 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
438 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
439 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500440 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
441 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
442 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400443 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
444 return released;
445}
446
447/**
448 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
449 * @q: The Header Receive Queue to operate on.
450 * @wqe: The Receive Queue Entry to put on the Receive queue.
451 *
452 * This routine will copy the contents of @wqe to the next available entry on
453 * the @q. This function will then ring the Receive Queue Doorbell to signal the
454 * HBA to start processing the Receive Queue Entry. This function returns the
455 * index that the rqe was copied to if successful. If no entries are available
456 * on @q then this function will return -ENOMEM.
457 * The caller is expected to hold the hbalock when calling this routine.
458 **/
459static int
460lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
461 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
462{
James Smart2e90f4b2011-12-13 13:22:37 -0500463 struct lpfc_rqe *temp_hrqe;
464 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400465 struct lpfc_register doorbell;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500466 int put_index;
James Smart4f774512009-05-22 14:52:35 -0400467
James Smart2e90f4b2011-12-13 13:22:37 -0500468 /* sanity check on queue memory */
469 if (unlikely(!hq) || unlikely(!dq))
470 return -ENOMEM;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500471 put_index = hq->host_index;
James Smart2e90f4b2011-12-13 13:22:37 -0500472 temp_hrqe = hq->qe[hq->host_index].rqe;
473 temp_drqe = dq->qe[dq->host_index].rqe;
474
James Smart4f774512009-05-22 14:52:35 -0400475 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
476 return -EINVAL;
477 if (hq->host_index != dq->host_index)
478 return -EINVAL;
479 /* If the host has not yet processed the next entry then we are done */
480 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
481 return -EBUSY;
482 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
483 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
484
485 /* Update the host index to point to the next slot */
486 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
487 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
488
489 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400490 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400491 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500492 if (hq->db_format == LPFC_DB_RING_FORMAT) {
493 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
494 hq->entry_repost);
495 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
496 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
497 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
498 hq->entry_repost);
499 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
500 hq->host_index);
501 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
502 } else {
503 return -EINVAL;
504 }
505 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400506 }
507 return put_index;
508}
509
510/**
511 * lpfc_sli4_rq_release - Updates internal hba index for RQ
512 * @q: The Header Receive Queue to operate on.
513 *
514 * This routine will update the HBA index of a queue to reflect consumption of
515 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
516 * consumed an entry the host calls this function to update the queue's
517 * internal pointers. This routine returns the number of entries that were
518 * consumed by the HBA.
519 **/
520static uint32_t
521lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
522{
James Smart2e90f4b2011-12-13 13:22:37 -0500523 /* sanity check on queue memory */
524 if (unlikely(!hq) || unlikely(!dq))
525 return 0;
526
James Smart4f774512009-05-22 14:52:35 -0400527 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
528 return 0;
529 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
530 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
531 return 1;
532}
533
James Smarte59058c2008-08-24 21:49:00 -0400534/**
James Smart3621a712009-04-06 18:47:14 -0400535 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400536 * @phba: Pointer to HBA context object.
537 * @pring: Pointer to driver SLI ring object.
538 *
539 * This function returns pointer to next command iocb entry
540 * in the command ring. The caller must hold hbalock to prevent
541 * other threads consume the next command iocb.
542 * SLI-2/SLI-3 provide different sized iocbs.
543 **/
James Smarted957682007-06-17 19:56:37 -0500544static inline IOCB_t *
545lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
546{
James Smart7e56aa22012-08-03 12:35:34 -0400547 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
548 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500549}
550
James Smarte59058c2008-08-24 21:49:00 -0400551/**
James Smart3621a712009-04-06 18:47:14 -0400552 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400553 * @phba: Pointer to HBA context object.
554 * @pring: Pointer to driver SLI ring object.
555 *
556 * This function returns pointer to next response iocb entry
557 * in the response ring. The caller must hold hbalock to make sure
558 * that no other thread consume the next response iocb.
559 * SLI-2/SLI-3 provide different sized iocbs.
560 **/
James Smarted957682007-06-17 19:56:37 -0500561static inline IOCB_t *
562lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
563{
James Smart7e56aa22012-08-03 12:35:34 -0400564 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
565 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500566}
567
James Smarte59058c2008-08-24 21:49:00 -0400568/**
James Smart3621a712009-04-06 18:47:14 -0400569 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400570 * @phba: Pointer to HBA context object.
571 *
572 * This function is called with hbalock held. This function
573 * allocates a new driver iocb object from the iocb pool. If the
574 * allocation is successful, it returns pointer to the newly
575 * allocated iocb object else it returns NULL.
576 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400577struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500578__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400579{
580 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
581 struct lpfc_iocbq * iocbq = NULL;
582
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100583 lockdep_assert_held(&phba->hbalock);
584
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400585 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400586 if (iocbq)
587 phba->iocb_cnt++;
588 if (phba->iocb_cnt > phba->iocb_max)
589 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400590 return iocbq;
591}
592
James Smarte59058c2008-08-24 21:49:00 -0400593/**
James Smartda0436e2009-05-22 14:51:39 -0400594 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
595 * @phba: Pointer to HBA context object.
596 * @xritag: XRI value.
597 *
598 * This function clears the sglq pointer from the array of acive
599 * sglq's. The xritag that is passed in is used to index into the
600 * array. Before the xritag can be used it needs to be adjusted
601 * by subtracting the xribase.
602 *
603 * Returns sglq ponter = success, NULL = Failure.
604 **/
605static struct lpfc_sglq *
606__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
607{
James Smartda0436e2009-05-22 14:51:39 -0400608 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400609
610 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
611 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400612 return sglq;
613}
614
615/**
616 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
617 * @phba: Pointer to HBA context object.
618 * @xritag: XRI value.
619 *
620 * This function returns the sglq pointer from the array of acive
621 * sglq's. The xritag that is passed in is used to index into the
622 * array. Before the xritag can be used it needs to be adjusted
623 * by subtracting the xribase.
624 *
625 * Returns sglq ponter = success, NULL = Failure.
626 **/
James Smart0f65ff62010-02-26 14:14:23 -0500627struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400628__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
629{
James Smartda0436e2009-05-22 14:51:39 -0400630 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400631
632 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400633 return sglq;
634}
635
636/**
James Smart1151e3e2011-02-16 12:39:35 -0500637 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500638 * @phba: Pointer to HBA context object.
639 * @xritag: xri used in this exchange.
640 * @rrq: The RRQ to be cleared.
641 *
James Smart19ca7602010-11-20 23:11:55 -0500642 **/
James Smart1151e3e2011-02-16 12:39:35 -0500643void
644lpfc_clr_rrq_active(struct lpfc_hba *phba,
645 uint16_t xritag,
646 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500647{
James Smart1151e3e2011-02-16 12:39:35 -0500648 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500649
James Smart1151e3e2011-02-16 12:39:35 -0500650 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
651 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500652
653 /* The target DID could have been swapped (cable swap)
654 * we should use the ndlp from the findnode if it is
655 * available.
656 */
James Smart1151e3e2011-02-16 12:39:35 -0500657 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500658 ndlp = rrq->ndlp;
659
James Smart1151e3e2011-02-16 12:39:35 -0500660 if (!ndlp)
661 goto out;
662
James Smartcff261f2013-12-17 20:29:47 -0500663 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500664 rrq->send_rrq = 0;
665 rrq->xritag = 0;
666 rrq->rrq_stop_time = 0;
667 }
James Smart1151e3e2011-02-16 12:39:35 -0500668out:
James Smart19ca7602010-11-20 23:11:55 -0500669 mempool_free(rrq, phba->rrq_pool);
670}
671
672/**
673 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
674 * @phba: Pointer to HBA context object.
675 *
676 * This function is called with hbalock held. This function
677 * Checks if stop_time (ratov from setting rrq active) has
678 * been reached, if it has and the send_rrq flag is set then
679 * it will call lpfc_send_rrq. If the send_rrq flag is not set
680 * then it will just call the routine to clear the rrq and
681 * free the rrq resource.
682 * The timer is set to the next rrq that is going to expire before
683 * leaving the routine.
684 *
685 **/
686void
687lpfc_handle_rrq_active(struct lpfc_hba *phba)
688{
689 struct lpfc_node_rrq *rrq;
690 struct lpfc_node_rrq *nextrrq;
691 unsigned long next_time;
692 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500693 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500694
695 spin_lock_irqsave(&phba->hbalock, iflags);
696 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400697 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500698 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500699 &phba->active_rrq_list, list) {
700 if (time_after(jiffies, rrq->rrq_stop_time))
701 list_move(&rrq->list, &send_rrq);
702 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500703 next_time = rrq->rrq_stop_time;
704 }
705 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500706 if ((!list_empty(&phba->active_rrq_list)) &&
707 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500708 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500709 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
710 list_del(&rrq->list);
711 if (!rrq->send_rrq)
712 /* this call will free the rrq */
713 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
714 else if (lpfc_send_rrq(phba, rrq)) {
715 /* if we send the rrq then the completion handler
716 * will clear the bit in the xribitmap.
717 */
718 lpfc_clr_rrq_active(phba, rrq->xritag,
719 rrq);
720 }
721 }
James Smart19ca7602010-11-20 23:11:55 -0500722}
723
724/**
725 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
726 * @vport: Pointer to vport context object.
727 * @xri: The xri used in the exchange.
728 * @did: The targets DID for this exchange.
729 *
730 * returns NULL = rrq not found in the phba->active_rrq_list.
731 * rrq = rrq for this xri and target.
732 **/
733struct lpfc_node_rrq *
734lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
735{
736 struct lpfc_hba *phba = vport->phba;
737 struct lpfc_node_rrq *rrq;
738 struct lpfc_node_rrq *nextrrq;
739 unsigned long iflags;
740
741 if (phba->sli_rev != LPFC_SLI_REV4)
742 return NULL;
743 spin_lock_irqsave(&phba->hbalock, iflags);
744 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
745 if (rrq->vport == vport && rrq->xritag == xri &&
746 rrq->nlp_DID == did){
747 list_del(&rrq->list);
748 spin_unlock_irqrestore(&phba->hbalock, iflags);
749 return rrq;
750 }
751 }
752 spin_unlock_irqrestore(&phba->hbalock, iflags);
753 return NULL;
754}
755
756/**
757 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
758 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500759 * @ndlp: Pointer to the lpfc_node_list structure.
760 * If ndlp is NULL Remove all active RRQs for this vport from the
761 * phba->active_rrq_list and clear the rrq.
762 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500763 **/
764void
James Smart1151e3e2011-02-16 12:39:35 -0500765lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500766
767{
768 struct lpfc_hba *phba = vport->phba;
769 struct lpfc_node_rrq *rrq;
770 struct lpfc_node_rrq *nextrrq;
771 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500772 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500773
774 if (phba->sli_rev != LPFC_SLI_REV4)
775 return;
James Smart1151e3e2011-02-16 12:39:35 -0500776 if (!ndlp) {
777 lpfc_sli4_vport_delete_els_xri_aborted(vport);
778 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500779 }
James Smart1151e3e2011-02-16 12:39:35 -0500780 spin_lock_irqsave(&phba->hbalock, iflags);
781 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
782 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
783 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500784 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500785
786 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
787 list_del(&rrq->list);
788 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
789 }
James Smart19ca7602010-11-20 23:11:55 -0500790}
791
792/**
James Smart1151e3e2011-02-16 12:39:35 -0500793 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500794 * @phba: Pointer to HBA context object.
795 * @ndlp: Targets nodelist pointer for this exchange.
796 * @xritag the xri in the bitmap to test.
797 *
798 * This function is called with hbalock held. This function
799 * returns 0 = rrq not active for this xri
800 * 1 = rrq is valid for this xri.
801 **/
James Smart1151e3e2011-02-16 12:39:35 -0500802int
803lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500804 uint16_t xritag)
805{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100806 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -0500807 if (!ndlp)
808 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500809 if (!ndlp->active_rrqs_xri_bitmap)
810 return 0;
811 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500812 return 1;
813 else
814 return 0;
815}
816
817/**
818 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
819 * @phba: Pointer to HBA context object.
820 * @ndlp: nodelist pointer for this target.
821 * @xritag: xri used in this exchange.
822 * @rxid: Remote Exchange ID.
823 * @send_rrq: Flag used to determine if we should send rrq els cmd.
824 *
825 * This function takes the hbalock.
826 * The active bit is always set in the active rrq xri_bitmap even
827 * if there is no slot avaiable for the other rrq information.
828 *
829 * returns 0 rrq actived for this xri
830 * < 0 No memory or invalid ndlp.
831 **/
832int
833lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500834 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500835{
James Smart19ca7602010-11-20 23:11:55 -0500836 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500837 struct lpfc_node_rrq *rrq;
838 int empty;
839
840 if (!ndlp)
841 return -EINVAL;
842
843 if (!phba->cfg_enable_rrq)
844 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500845
846 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500847 if (phba->pport->load_flag & FC_UNLOADING) {
848 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
849 goto out;
850 }
851
852 /*
853 * set the active bit even if there is no mem available.
854 */
855 if (NLP_CHK_FREE_REQ(ndlp))
856 goto out;
857
858 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
859 goto out;
860
James Smartcff261f2013-12-17 20:29:47 -0500861 if (!ndlp->active_rrqs_xri_bitmap)
862 goto out;
863
864 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -0500865 goto out;
866
James Smart19ca7602010-11-20 23:11:55 -0500867 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500868 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
869 if (!rrq) {
870 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
871 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
872 " DID:0x%x Send:%d\n",
873 xritag, rxid, ndlp->nlp_DID, send_rrq);
874 return -EINVAL;
875 }
James Smarte5771b42013-03-01 16:37:14 -0500876 if (phba->cfg_enable_rrq == 1)
877 rrq->send_rrq = send_rrq;
878 else
879 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500880 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -0400881 rrq->rrq_stop_time = jiffies +
882 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -0500883 rrq->ndlp = ndlp;
884 rrq->nlp_DID = ndlp->nlp_DID;
885 rrq->vport = ndlp->vport;
886 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500887 spin_lock_irqsave(&phba->hbalock, iflags);
888 empty = list_empty(&phba->active_rrq_list);
889 list_add_tail(&rrq->list, &phba->active_rrq_list);
890 phba->hba_flag |= HBA_RRQ_ACTIVE;
891 if (empty)
892 lpfc_worker_wake_up(phba);
893 spin_unlock_irqrestore(&phba->hbalock, iflags);
894 return 0;
895out:
896 spin_unlock_irqrestore(&phba->hbalock, iflags);
897 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
898 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
899 " DID:0x%x Send:%d\n",
900 xritag, rxid, ndlp->nlp_DID, send_rrq);
901 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500902}
903
904/**
James Smartda0436e2009-05-22 14:51:39 -0400905 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
906 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500907 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400908 *
James Smartdafe8ce2014-09-03 12:56:40 -0400909 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400910 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400911 * list is not empty then it is successful, it returns pointer to the newly
912 * allocated sglq object else it returns NULL.
913 **/
914static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500915__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400916{
917 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
918 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500919 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500920 struct lpfc_scsi_buf *lpfc_cmd;
921 struct lpfc_nodelist *ndlp;
922 int found = 0;
923
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100924 lockdep_assert_held(&phba->hbalock);
925
James Smart19ca7602010-11-20 23:11:55 -0500926 if (piocbq->iocb_flag & LPFC_IO_FCP) {
927 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
928 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500929 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -0400930 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -0500931 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -0400932 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
933 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
934 ndlp = NULL;
935 else
936 ndlp = piocbq->context_un.ndlp;
937 } else {
James Smart19ca7602010-11-20 23:11:55 -0500938 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -0400939 }
James Smart19ca7602010-11-20 23:11:55 -0500940
James Smartda0436e2009-05-22 14:51:39 -0400941 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500942 start_sglq = sglq;
943 while (!found) {
944 if (!sglq)
945 return NULL;
James Smartee0f4fe2012-05-09 21:19:14 -0400946 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500947 /* This xri has an rrq outstanding for this DID.
948 * put it back in the list and get another xri.
949 */
950 list_add_tail(&sglq->list, lpfc_sgl_list);
951 sglq = NULL;
952 list_remove_head(lpfc_sgl_list, sglq,
953 struct lpfc_sglq, list);
954 if (sglq == start_sglq) {
955 sglq = NULL;
956 break;
957 } else
958 continue;
959 }
960 sglq->ndlp = ndlp;
961 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400962 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500963 sglq->state = SGL_ALLOCATED;
964 }
James Smartda0436e2009-05-22 14:51:39 -0400965 return sglq;
966}
967
968/**
James Smart3621a712009-04-06 18:47:14 -0400969 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400970 * @phba: Pointer to HBA context object.
971 *
972 * This function is called with no lock held. This function
973 * allocates a new driver iocb object from the iocb pool. If the
974 * allocation is successful, it returns pointer to the newly
975 * allocated iocb object else it returns NULL.
976 **/
James Smart2e0fef82007-06-17 19:56:36 -0500977struct lpfc_iocbq *
978lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500979{
James Smart2e0fef82007-06-17 19:56:36 -0500980 struct lpfc_iocbq * iocbq = NULL;
981 unsigned long iflags;
982
983 spin_lock_irqsave(&phba->hbalock, iflags);
984 iocbq = __lpfc_sli_get_iocbq(phba);
985 spin_unlock_irqrestore(&phba->hbalock, iflags);
986 return iocbq;
987}
988
James Smarte59058c2008-08-24 21:49:00 -0400989/**
James Smart4f774512009-05-22 14:52:35 -0400990 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
991 * @phba: Pointer to HBA context object.
992 * @iocbq: Pointer to driver iocb object.
993 *
994 * This function is called with hbalock held to release driver
995 * iocb object to the iocb pool. The iotag in the iocb object
996 * does not change for each use of the iocb object. This function
997 * clears all other fields of the iocb object when it is freed.
998 * The sqlq structure that holds the xritag and phys and virtual
999 * mappings for the scatter gather list is retrieved from the
1000 * active array of sglq. The get of the sglq pointer also clears
1001 * the entry in the array. If the status of the IO indiactes that
1002 * this IO was aborted then the sglq entry it put on the
1003 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1004 * IO has good status or fails for any other reason then the sglq
1005 * entry is added to the free list (lpfc_sgl_list).
1006 **/
1007static void
1008__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1009{
1010 struct lpfc_sglq *sglq;
1011 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001012 unsigned long iflag = 0;
1013 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -04001014
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001015 lockdep_assert_held(&phba->hbalock);
1016
James Smart4f774512009-05-22 14:52:35 -04001017 if (iocbq->sli4_xritag == NO_XRI)
1018 sglq = NULL;
1019 else
James Smart6d368e52011-05-24 11:44:12 -04001020 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1021
James Smart0e9bb8d2013-03-01 16:35:12 -05001022
James Smart4f774512009-05-22 14:52:35 -04001023 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -05001024 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1025 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -04001026 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1027 iflag);
1028 list_add(&sglq->list,
1029 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1030 spin_unlock_irqrestore(
1031 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001032 } else {
James Smartdafe8ce2014-09-03 12:56:40 -04001033 spin_lock_irqsave(&pring->ring_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001034 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001035 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001036 list_add_tail(&sglq->list,
1037 &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04001038 spin_unlock_irqrestore(&pring->ring_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001039
1040 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001041 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001042 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001043 }
James Smart4f774512009-05-22 14:52:35 -04001044 }
1045
1046
1047 /*
1048 * Clean all volatile data fields, preserve iotag and node struct.
1049 */
1050 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001051 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001052 iocbq->sli4_xritag = NO_XRI;
1053 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1054}
1055
James Smart2a9bf3d2010-06-07 15:24:45 -04001056
James Smart4f774512009-05-22 14:52:35 -04001057/**
James Smart3772a992009-05-22 14:50:54 -04001058 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1059 * @phba: Pointer to HBA context object.
1060 * @iocbq: Pointer to driver iocb object.
1061 *
1062 * This function is called with hbalock held to release driver
1063 * iocb object to the iocb pool. The iotag in the iocb object
1064 * does not change for each use of the iocb object. This function
1065 * clears all other fields of the iocb object when it is freed.
1066 **/
1067static void
1068__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1069{
1070 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1071
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001072 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001073
1074 /*
James Smart3772a992009-05-22 14:50:54 -04001075 * Clean all volatile data fields, preserve iotag and node struct.
1076 */
1077 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1078 iocbq->sli4_xritag = NO_XRI;
1079 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1080}
1081
1082/**
James Smart3621a712009-04-06 18:47:14 -04001083 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001084 * @phba: Pointer to HBA context object.
1085 * @iocbq: Pointer to driver iocb object.
1086 *
1087 * This function is called with hbalock held to release driver
1088 * iocb object to the iocb pool. The iotag in the iocb object
1089 * does not change for each use of the iocb object. This function
1090 * clears all other fields of the iocb object when it is freed.
1091 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001092static void
James Smart2e0fef82007-06-17 19:56:36 -05001093__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1094{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001095 lockdep_assert_held(&phba->hbalock);
1096
James Smart3772a992009-05-22 14:50:54 -04001097 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001098 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001099}
1100
James Smarte59058c2008-08-24 21:49:00 -04001101/**
James Smart3621a712009-04-06 18:47:14 -04001102 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001103 * @phba: Pointer to HBA context object.
1104 * @iocbq: Pointer to driver iocb object.
1105 *
1106 * This function is called with no lock held to release the iocb to
1107 * iocb pool.
1108 **/
James Smart2e0fef82007-06-17 19:56:36 -05001109void
1110lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1111{
1112 unsigned long iflags;
1113
1114 /*
1115 * Clean all volatile data fields, preserve iotag and node struct.
1116 */
1117 spin_lock_irqsave(&phba->hbalock, iflags);
1118 __lpfc_sli_release_iocbq(phba, iocbq);
1119 spin_unlock_irqrestore(&phba->hbalock, iflags);
1120}
1121
James Smarte59058c2008-08-24 21:49:00 -04001122/**
James Smarta257bf92009-04-06 18:48:10 -04001123 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1124 * @phba: Pointer to HBA context object.
1125 * @iocblist: List of IOCBs.
1126 * @ulpstatus: ULP status in IOCB command field.
1127 * @ulpWord4: ULP word-4 in IOCB command field.
1128 *
1129 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1130 * on the list by invoking the complete callback function associated with the
1131 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1132 * fields.
1133 **/
1134void
1135lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1136 uint32_t ulpstatus, uint32_t ulpWord4)
1137{
1138 struct lpfc_iocbq *piocb;
1139
1140 while (!list_empty(iocblist)) {
1141 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001142 if (!piocb->iocb_cmpl)
1143 lpfc_sli_release_iocbq(phba, piocb);
1144 else {
1145 piocb->iocb.ulpStatus = ulpstatus;
1146 piocb->iocb.un.ulpWord[4] = ulpWord4;
1147 (piocb->iocb_cmpl) (phba, piocb, piocb);
1148 }
1149 }
1150 return;
1151}
1152
1153/**
James Smart3621a712009-04-06 18:47:14 -04001154 * lpfc_sli_iocb_cmd_type - Get the iocb type
1155 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001156 *
1157 * This function is called by ring event handler function to get the iocb type.
1158 * This function translates the iocb command to an iocb command type used to
1159 * decide the final disposition of each completed IOCB.
1160 * The function returns
1161 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1162 * LPFC_SOL_IOCB if it is a solicited iocb completion
1163 * LPFC_ABORT_IOCB if it is an abort iocb
1164 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1165 *
1166 * The caller is not required to hold any lock.
1167 **/
dea31012005-04-17 16:05:31 -05001168static lpfc_iocb_type
1169lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1170{
1171 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1172
1173 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1174 return 0;
1175
1176 switch (iocb_cmnd) {
1177 case CMD_XMIT_SEQUENCE_CR:
1178 case CMD_XMIT_SEQUENCE_CX:
1179 case CMD_XMIT_BCAST_CN:
1180 case CMD_XMIT_BCAST_CX:
1181 case CMD_ELS_REQUEST_CR:
1182 case CMD_ELS_REQUEST_CX:
1183 case CMD_CREATE_XRI_CR:
1184 case CMD_CREATE_XRI_CX:
1185 case CMD_GET_RPI_CN:
1186 case CMD_XMIT_ELS_RSP_CX:
1187 case CMD_GET_RPI_CR:
1188 case CMD_FCP_IWRITE_CR:
1189 case CMD_FCP_IWRITE_CX:
1190 case CMD_FCP_IREAD_CR:
1191 case CMD_FCP_IREAD_CX:
1192 case CMD_FCP_ICMND_CR:
1193 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001194 case CMD_FCP_TSEND_CX:
1195 case CMD_FCP_TRSP_CX:
1196 case CMD_FCP_TRECEIVE_CX:
1197 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001198 case CMD_ADAPTER_MSG:
1199 case CMD_ADAPTER_DUMP:
1200 case CMD_XMIT_SEQUENCE64_CR:
1201 case CMD_XMIT_SEQUENCE64_CX:
1202 case CMD_XMIT_BCAST64_CN:
1203 case CMD_XMIT_BCAST64_CX:
1204 case CMD_ELS_REQUEST64_CR:
1205 case CMD_ELS_REQUEST64_CX:
1206 case CMD_FCP_IWRITE64_CR:
1207 case CMD_FCP_IWRITE64_CX:
1208 case CMD_FCP_IREAD64_CR:
1209 case CMD_FCP_IREAD64_CX:
1210 case CMD_FCP_ICMND64_CR:
1211 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001212 case CMD_FCP_TSEND64_CX:
1213 case CMD_FCP_TRSP64_CX:
1214 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001215 case CMD_GEN_REQUEST64_CR:
1216 case CMD_GEN_REQUEST64_CX:
1217 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001218 case DSSCMD_IWRITE64_CR:
1219 case DSSCMD_IWRITE64_CX:
1220 case DSSCMD_IREAD64_CR:
1221 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001222 type = LPFC_SOL_IOCB;
1223 break;
1224 case CMD_ABORT_XRI_CN:
1225 case CMD_ABORT_XRI_CX:
1226 case CMD_CLOSE_XRI_CN:
1227 case CMD_CLOSE_XRI_CX:
1228 case CMD_XRI_ABORTED_CX:
1229 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001230 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001231 type = LPFC_ABORT_IOCB;
1232 break;
1233 case CMD_RCV_SEQUENCE_CX:
1234 case CMD_RCV_ELS_REQ_CX:
1235 case CMD_RCV_SEQUENCE64_CX:
1236 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001237 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001238 case CMD_IOCB_RCV_SEQ64_CX:
1239 case CMD_IOCB_RCV_ELS64_CX:
1240 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001241 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001242 type = LPFC_UNSOL_IOCB;
1243 break;
James Smart3163f722008-02-08 18:50:25 -05001244 case CMD_IOCB_XMIT_MSEQ64_CR:
1245 case CMD_IOCB_XMIT_MSEQ64_CX:
1246 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1247 case CMD_IOCB_RCV_ELS_LIST64_CX:
1248 case CMD_IOCB_CLOSE_EXTENDED_CN:
1249 case CMD_IOCB_ABORT_EXTENDED_CN:
1250 case CMD_IOCB_RET_HBQE64_CN:
1251 case CMD_IOCB_FCP_IBIDIR64_CR:
1252 case CMD_IOCB_FCP_IBIDIR64_CX:
1253 case CMD_IOCB_FCP_ITASKMGT64_CX:
1254 case CMD_IOCB_LOGENTRY_CN:
1255 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1256 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001257 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001258 type = LPFC_UNKNOWN_IOCB;
1259 break;
dea31012005-04-17 16:05:31 -05001260 default:
1261 type = LPFC_UNKNOWN_IOCB;
1262 break;
1263 }
1264
1265 return type;
1266}
1267
James Smarte59058c2008-08-24 21:49:00 -04001268/**
James Smart3621a712009-04-06 18:47:14 -04001269 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001270 * @phba: Pointer to HBA context object.
1271 *
1272 * This function is called from SLI initialization code
1273 * to configure every ring of the HBA's SLI interface. The
1274 * caller is not required to hold any lock. This function issues
1275 * a config_ring mailbox command for each ring.
1276 * This function returns zero if successful else returns a negative
1277 * error code.
1278 **/
dea31012005-04-17 16:05:31 -05001279static int
James Smarted957682007-06-17 19:56:37 -05001280lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001281{
1282 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001283 LPFC_MBOXQ_t *pmb;
1284 MAILBOX_t *pmbox;
1285 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001286
James Smarted957682007-06-17 19:56:37 -05001287 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1288 if (!pmb)
1289 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001290 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001291 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001292 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001293 lpfc_config_ring(phba, i, pmb);
1294 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1295 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001296 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001297 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001298 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1299 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001300 rc, pmbox->mbxCommand,
1301 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001302 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001303 ret = -ENXIO;
1304 break;
dea31012005-04-17 16:05:31 -05001305 }
1306 }
James Smarted957682007-06-17 19:56:37 -05001307 mempool_free(pmb, phba->mbox_mem_pool);
1308 return ret;
dea31012005-04-17 16:05:31 -05001309}
1310
James Smarte59058c2008-08-24 21:49:00 -04001311/**
James Smart3621a712009-04-06 18:47:14 -04001312 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001313 * @phba: Pointer to HBA context object.
1314 * @pring: Pointer to driver SLI ring object.
1315 * @piocb: Pointer to the driver iocb object.
1316 *
1317 * This function is called with hbalock held. The function adds the
1318 * new iocb to txcmplq of the given ring. This function always returns
1319 * 0. If this function is called for ELS ring, this function checks if
1320 * there is a vport associated with the ELS command. This function also
1321 * starts els_tmofunc timer if this is an ELS command.
1322 **/
dea31012005-04-17 16:05:31 -05001323static int
James Smart2e0fef82007-06-17 19:56:36 -05001324lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1325 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001326{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001327 lockdep_assert_held(&phba->hbalock);
1328
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001329 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001330
dea31012005-04-17 16:05:31 -05001331 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001332 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001333
James Smart92d7f7b2007-06-17 19:56:38 -05001334 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1335 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001336 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1337 BUG_ON(!piocb->vport);
1338 if (!(piocb->vport->load_flag & FC_UNLOADING))
1339 mod_timer(&piocb->vport->els_tmofunc,
1340 jiffies +
1341 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1342 }
dea31012005-04-17 16:05:31 -05001343
James Smart2e0fef82007-06-17 19:56:36 -05001344 return 0;
dea31012005-04-17 16:05:31 -05001345}
1346
James Smarte59058c2008-08-24 21:49:00 -04001347/**
James Smart3621a712009-04-06 18:47:14 -04001348 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001349 * @phba: Pointer to HBA context object.
1350 * @pring: Pointer to driver SLI ring object.
1351 *
1352 * This function is called with hbalock held to get next
1353 * iocb in txq of the given ring. If there is any iocb in
1354 * the txq, the function returns first iocb in the list after
1355 * removing the iocb from the list, else it returns NULL.
1356 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001357struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001358lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001359{
dea31012005-04-17 16:05:31 -05001360 struct lpfc_iocbq *cmd_iocb;
1361
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001362 lockdep_assert_held(&phba->hbalock);
1363
James Smart858c9f62007-06-17 19:56:39 -05001364 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001365 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001366}
1367
James Smarte59058c2008-08-24 21:49:00 -04001368/**
James Smart3621a712009-04-06 18:47:14 -04001369 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001370 * @phba: Pointer to HBA context object.
1371 * @pring: Pointer to driver SLI ring object.
1372 *
1373 * This function is called with hbalock held and the caller must post the
1374 * iocb without releasing the lock. If the caller releases the lock,
1375 * iocb slot returned by the function is not guaranteed to be available.
1376 * The function returns pointer to the next available iocb slot if there
1377 * is available slot in the ring, else it returns NULL.
1378 * If the get index of the ring is ahead of the put index, the function
1379 * will post an error attention event to the worker thread to take the
1380 * HBA to offline state.
1381 **/
dea31012005-04-17 16:05:31 -05001382static IOCB_t *
1383lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1384{
James Smart34b02dc2008-08-24 21:49:55 -04001385 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001386 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001387
1388 lockdep_assert_held(&phba->hbalock);
1389
James Smart7e56aa22012-08-03 12:35:34 -04001390 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1391 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1392 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001393
James Smart7e56aa22012-08-03 12:35:34 -04001394 if (unlikely(pring->sli.sli3.local_getidx ==
1395 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001396
James Smart7e56aa22012-08-03 12:35:34 -04001397 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001398
James Smart7e56aa22012-08-03 12:35:34 -04001399 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001400 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001401 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001402 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001403 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001404 pring->sli.sli3.local_getidx,
1405 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001406
James Smart2e0fef82007-06-17 19:56:36 -05001407 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001408 /*
1409 * All error attention handlers are posted to
1410 * worker thread
1411 */
1412 phba->work_ha |= HA_ERATT;
1413 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001414
James Smart5e9d9b82008-06-14 22:52:53 -04001415 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001416
1417 return NULL;
1418 }
1419
James Smart7e56aa22012-08-03 12:35:34 -04001420 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001421 return NULL;
1422 }
1423
James Smarted957682007-06-17 19:56:37 -05001424 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001425}
1426
James Smarte59058c2008-08-24 21:49:00 -04001427/**
James Smart3621a712009-04-06 18:47:14 -04001428 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001429 * @phba: Pointer to HBA context object.
1430 * @iocbq: Pointer to driver iocb object.
1431 *
1432 * This function gets an iotag for the iocb. If there is no unused iotag and
1433 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1434 * array and assigns a new iotag.
1435 * The function returns the allocated iotag if successful, else returns zero.
1436 * Zero is not a valid iotag.
1437 * The caller is not required to hold any lock.
1438 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001439uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001440lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001441{
James Smart2e0fef82007-06-17 19:56:36 -05001442 struct lpfc_iocbq **new_arr;
1443 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001444 size_t new_len;
1445 struct lpfc_sli *psli = &phba->sli;
1446 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001447
James Smart2e0fef82007-06-17 19:56:36 -05001448 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001449 iotag = psli->last_iotag;
1450 if(++iotag < psli->iocbq_lookup_len) {
1451 psli->last_iotag = iotag;
1452 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001453 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001454 iocbq->iotag = iotag;
1455 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001456 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001457 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1458 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001459 spin_unlock_irq(&phba->hbalock);
1460 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001461 GFP_KERNEL);
1462 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001463 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001464 old_arr = psli->iocbq_lookup;
1465 if (new_len <= psli->iocbq_lookup_len) {
1466 /* highly unprobable case */
1467 kfree(new_arr);
1468 iotag = psli->last_iotag;
1469 if(++iotag < psli->iocbq_lookup_len) {
1470 psli->last_iotag = iotag;
1471 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001472 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001473 iocbq->iotag = iotag;
1474 return iotag;
1475 }
James Smart2e0fef82007-06-17 19:56:36 -05001476 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001477 return 0;
1478 }
1479 if (psli->iocbq_lookup)
1480 memcpy(new_arr, old_arr,
1481 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001482 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001483 psli->iocbq_lookup = new_arr;
1484 psli->iocbq_lookup_len = new_len;
1485 psli->last_iotag = iotag;
1486 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001487 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001488 iocbq->iotag = iotag;
1489 kfree(old_arr);
1490 return iotag;
1491 }
James Smart8f6d98d2006-08-01 07:34:00 -04001492 } else
James Smart2e0fef82007-06-17 19:56:36 -05001493 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001494
James Smartbc739052010-08-04 16:11:18 -04001495 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001496 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1497 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001498
James Bottomley604a3e32005-10-29 10:28:33 -05001499 return 0;
dea31012005-04-17 16:05:31 -05001500}
1501
James Smarte59058c2008-08-24 21:49:00 -04001502/**
James Smart3621a712009-04-06 18:47:14 -04001503 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001504 * @phba: Pointer to HBA context object.
1505 * @pring: Pointer to driver SLI ring object.
1506 * @iocb: Pointer to iocb slot in the ring.
1507 * @nextiocb: Pointer to driver iocb object which need to be
1508 * posted to firmware.
1509 *
1510 * This function is called with hbalock held to post a new iocb to
1511 * the firmware. This function copies the new iocb to ring iocb slot and
1512 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1513 * a completion call back for this iocb else the function will free the
1514 * iocb object.
1515 **/
dea31012005-04-17 16:05:31 -05001516static void
1517lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1518 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1519{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001520 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001521 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001522 * Set up an iotag
dea31012005-04-17 16:05:31 -05001523 */
James Bottomley604a3e32005-10-29 10:28:33 -05001524 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001525
James Smarte2a0a9d2008-12-04 22:40:02 -05001526
James Smarta58cbd52007-08-02 11:09:43 -04001527 if (pring->ringno == LPFC_ELS_RING) {
1528 lpfc_debugfs_slow_ring_trc(phba,
1529 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1530 *(((uint32_t *) &nextiocb->iocb) + 4),
1531 *(((uint32_t *) &nextiocb->iocb) + 6),
1532 *(((uint32_t *) &nextiocb->iocb) + 7));
1533 }
1534
dea31012005-04-17 16:05:31 -05001535 /*
1536 * Issue iocb command to adapter
1537 */
James Smart92d7f7b2007-06-17 19:56:38 -05001538 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001539 wmb();
1540 pring->stats.iocb_cmd++;
1541
1542 /*
1543 * If there is no completion routine to call, we can release the
1544 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1545 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1546 */
1547 if (nextiocb->iocb_cmpl)
1548 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001549 else
James Smart2e0fef82007-06-17 19:56:36 -05001550 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001551
1552 /*
1553 * Let the HBA know what IOCB slot will be the next one the
1554 * driver will put a command into.
1555 */
James Smart7e56aa22012-08-03 12:35:34 -04001556 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1557 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001558}
1559
James Smarte59058c2008-08-24 21:49:00 -04001560/**
James Smart3621a712009-04-06 18:47:14 -04001561 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001562 * @phba: Pointer to HBA context object.
1563 * @pring: Pointer to driver SLI ring object.
1564 *
1565 * The caller is not required to hold any lock for calling this function.
1566 * This function updates the chip attention bits for the ring to inform firmware
1567 * that there are pending work to be done for this ring and requests an
1568 * interrupt when there is space available in the ring. This function is
1569 * called when the driver is unable to post more iocbs to the ring due
1570 * to unavailability of space in the ring.
1571 **/
dea31012005-04-17 16:05:31 -05001572static void
James Smart2e0fef82007-06-17 19:56:36 -05001573lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001574{
1575 int ringno = pring->ringno;
1576
1577 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1578
1579 wmb();
1580
1581 /*
1582 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1583 * The HBA will tell us when an IOCB entry is available.
1584 */
1585 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1586 readl(phba->CAregaddr); /* flush */
1587
1588 pring->stats.iocb_cmd_full++;
1589}
1590
James Smarte59058c2008-08-24 21:49:00 -04001591/**
James Smart3621a712009-04-06 18:47:14 -04001592 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001593 * @phba: Pointer to HBA context object.
1594 * @pring: Pointer to driver SLI ring object.
1595 *
1596 * This function updates the chip attention register bit for the
1597 * given ring to inform HBA that there is more work to be done
1598 * in this ring. The caller is not required to hold any lock.
1599 **/
dea31012005-04-17 16:05:31 -05001600static void
James Smart2e0fef82007-06-17 19:56:36 -05001601lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001602{
1603 int ringno = pring->ringno;
1604
1605 /*
1606 * Tell the HBA that there is work to do in this ring.
1607 */
James Smart34b02dc2008-08-24 21:49:55 -04001608 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1609 wmb();
1610 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1611 readl(phba->CAregaddr); /* flush */
1612 }
dea31012005-04-17 16:05:31 -05001613}
1614
James Smarte59058c2008-08-24 21:49:00 -04001615/**
James Smart3621a712009-04-06 18:47:14 -04001616 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001617 * @phba: Pointer to HBA context object.
1618 * @pring: Pointer to driver SLI ring object.
1619 *
1620 * This function is called with hbalock held to post pending iocbs
1621 * in the txq to the firmware. This function is called when driver
1622 * detects space available in the ring.
1623 **/
dea31012005-04-17 16:05:31 -05001624static void
James Smart2e0fef82007-06-17 19:56:36 -05001625lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001626{
1627 IOCB_t *iocb;
1628 struct lpfc_iocbq *nextiocb;
1629
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001630 lockdep_assert_held(&phba->hbalock);
1631
dea31012005-04-17 16:05:31 -05001632 /*
1633 * Check to see if:
1634 * (a) there is anything on the txq to send
1635 * (b) link is up
1636 * (c) link attention events can be processed (fcp ring only)
1637 * (d) IOCB processing is not blocked by the outstanding mbox command.
1638 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001639
1640 if (lpfc_is_link_up(phba) &&
1641 (!list_empty(&pring->txq)) &&
dea31012005-04-17 16:05:31 -05001642 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001643 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001644
1645 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1646 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1647 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1648
1649 if (iocb)
1650 lpfc_sli_update_ring(phba, pring);
1651 else
1652 lpfc_sli_update_full_ring(phba, pring);
1653 }
1654
1655 return;
1656}
1657
James Smarte59058c2008-08-24 21:49:00 -04001658/**
James Smart3621a712009-04-06 18:47:14 -04001659 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001660 * @phba: Pointer to HBA context object.
1661 * @hbqno: HBQ number.
1662 *
1663 * This function is called with hbalock held to get the next
1664 * available slot for the given HBQ. If there is free slot
1665 * available for the HBQ it will return pointer to the next available
1666 * HBQ entry else it will return NULL.
1667 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001668static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001669lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1670{
1671 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1672
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001673 lockdep_assert_held(&phba->hbalock);
1674
James Smarted957682007-06-17 19:56:37 -05001675 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1676 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1677 hbqp->next_hbqPutIdx = 0;
1678
1679 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001680 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001681 uint32_t getidx = le32_to_cpu(raw_index);
1682
1683 hbqp->local_hbqGetIdx = getidx;
1684
1685 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1686 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001687 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001688 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001689 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001690 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001691 hbqp->entry_count);
1692
1693 phba->link_state = LPFC_HBA_ERROR;
1694 return NULL;
1695 }
1696
1697 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1698 return NULL;
1699 }
1700
James Smart51ef4c22007-08-02 11:10:31 -04001701 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1702 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001703}
1704
James Smarte59058c2008-08-24 21:49:00 -04001705/**
James Smart3621a712009-04-06 18:47:14 -04001706 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001707 * @phba: Pointer to HBA context object.
1708 *
1709 * This function is called with no lock held to free all the
1710 * hbq buffers while uninitializing the SLI interface. It also
1711 * frees the HBQ buffers returned by the firmware but not yet
1712 * processed by the upper layers.
1713 **/
James Smarted957682007-06-17 19:56:37 -05001714void
1715lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1716{
James Smart92d7f7b2007-06-17 19:56:38 -05001717 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1718 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001719 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001720 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001721 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001722
James Smart51ef4c22007-08-02 11:10:31 -04001723 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001724 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001725 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001726 for (i = 0; i < hbq_count; ++i) {
1727 list_for_each_entry_safe(dmabuf, next_dmabuf,
1728 &phba->hbqs[i].hbq_buffer_list, list) {
1729 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1730 list_del(&hbq_buf->dbuf.list);
1731 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1732 }
James Smarta8adb832007-10-27 13:37:53 -04001733 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001734 }
James Smart3163f722008-02-08 18:50:25 -05001735 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001736 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1737 list) {
James Smart3163f722008-02-08 18:50:25 -05001738 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1739 list_del(&hbq_buf->dbuf.list);
1740 if (hbq_buf->tag == -1) {
1741 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1742 (phba, hbq_buf);
1743 } else {
1744 hbqno = hbq_buf->tag >> 16;
1745 if (hbqno >= LPFC_MAX_HBQS)
1746 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1747 (phba, hbq_buf);
1748 else
1749 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1750 hbq_buf);
1751 }
1752 }
1753
1754 /* Mark the HBQs not in use */
1755 phba->hbq_in_use = 0;
1756 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001757}
1758
James Smarte59058c2008-08-24 21:49:00 -04001759/**
James Smart3621a712009-04-06 18:47:14 -04001760 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001761 * @phba: Pointer to HBA context object.
1762 * @hbqno: HBQ number.
1763 * @hbq_buf: Pointer to HBQ buffer.
1764 *
1765 * This function is called with the hbalock held to post a
1766 * hbq buffer to the firmware. If the function finds an empty
1767 * slot in the HBQ, it will post the buffer. The function will return
1768 * pointer to the hbq entry if it successfully post the buffer
1769 * else it will return NULL.
1770 **/
James Smart3772a992009-05-22 14:50:54 -04001771static int
James Smarted957682007-06-17 19:56:37 -05001772lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001773 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001774{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001775 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001776 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1777}
1778
1779/**
1780 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1781 * @phba: Pointer to HBA context object.
1782 * @hbqno: HBQ number.
1783 * @hbq_buf: Pointer to HBQ buffer.
1784 *
1785 * This function is called with the hbalock held to post a hbq buffer to the
1786 * firmware. If the function finds an empty slot in the HBQ, it will post the
1787 * buffer and place it on the hbq_buffer_list. The function will return zero if
1788 * it successfully post the buffer else it will return an error.
1789 **/
1790static int
1791lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1792 struct hbq_dmabuf *hbq_buf)
1793{
James Smarted957682007-06-17 19:56:37 -05001794 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001795 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001796
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001797 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05001798 /* Get next HBQ entry slot to use */
1799 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1800 if (hbqe) {
1801 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1802
James Smart92d7f7b2007-06-17 19:56:38 -05001803 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1804 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001805 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001806 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001807 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1808 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1809 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001810 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1811 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001812 /* flush */
James Smarted957682007-06-17 19:56:37 -05001813 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001814 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001815 return 0;
1816 } else
1817 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001818}
1819
James Smart4f774512009-05-22 14:52:35 -04001820/**
1821 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1822 * @phba: Pointer to HBA context object.
1823 * @hbqno: HBQ number.
1824 * @hbq_buf: Pointer to HBQ buffer.
1825 *
1826 * This function is called with the hbalock held to post an RQE to the SLI4
1827 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1828 * the hbq_buffer_list and return zero, otherwise it will return an error.
1829 **/
1830static int
1831lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1832 struct hbq_dmabuf *hbq_buf)
1833{
1834 int rc;
1835 struct lpfc_rqe hrqe;
1836 struct lpfc_rqe drqe;
1837
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001838 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04001839 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1840 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1841 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1842 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1843 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1844 &hrqe, &drqe);
1845 if (rc < 0)
1846 return rc;
1847 hbq_buf->tag = rc;
1848 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/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001865static struct lpfc_hbq_init lpfc_extra_hbq = {
1866 .rn = 1,
1867 .entry_count = 200,
1868 .mask_count = 0,
1869 .profile = 0,
1870 .ring_mask = (1 << LPFC_EXTRA_RING),
1871 .buffer_count = 0,
1872 .init_count = 0,
1873 .add_count = 5,
1874};
1875
James Smarte59058c2008-08-24 21:49:00 -04001876/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001877struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001878 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001879 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001880};
1881
James Smarte59058c2008-08-24 21:49:00 -04001882/**
James Smart3621a712009-04-06 18:47:14 -04001883 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001884 * @phba: Pointer to HBA context object.
1885 * @hbqno: HBQ number.
1886 * @count: Number of HBQ buffers to be posted.
1887 *
James Smartd7c255b2008-08-24 21:50:00 -04001888 * This function is called with no lock held to post more hbq buffers to the
1889 * given HBQ. The function returns the number of HBQ buffers successfully
1890 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001891 **/
James Smart311464e2007-08-02 11:10:37 -04001892static int
James Smart92d7f7b2007-06-17 19:56:38 -05001893lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1894{
James Smartd7c255b2008-08-24 21:50:00 -04001895 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001896 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001897 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001898 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001899 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001900 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001901
James Smartd7c255b2008-08-24 21:50:00 -04001902 if ((phba->hbqs[hbqno].buffer_count + count) >
1903 lpfc_hbq_defs[hbqno]->entry_count)
1904 count = lpfc_hbq_defs[hbqno]->entry_count -
1905 phba->hbqs[hbqno].buffer_count;
1906 if (!count)
1907 return 0;
1908 /* Allocate HBQ entries */
1909 for (i = 0; i < count; i++) {
1910 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1911 if (!hbq_buffer)
1912 break;
1913 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1914 }
James Smart3163f722008-02-08 18:50:25 -05001915 /* Check whether HBQ is still in use */
1916 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001917 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001918 goto err;
1919 while (!list_empty(&hbq_buf_list)) {
1920 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1921 dbuf.list);
1922 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1923 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001924 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001925 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001926 posted++;
1927 } else
James Smart51ef4c22007-08-02 11:10:31 -04001928 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001929 }
James Smart3163f722008-02-08 18:50:25 -05001930 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001931 return posted;
1932err:
1933 spin_unlock_irqrestore(&phba->hbalock, flags);
1934 while (!list_empty(&hbq_buf_list)) {
1935 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1936 dbuf.list);
1937 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1938 }
James Smart92d7f7b2007-06-17 19:56:38 -05001939 return 0;
James Smarted957682007-06-17 19:56:37 -05001940}
1941
James Smarte59058c2008-08-24 21:49:00 -04001942/**
James Smart3621a712009-04-06 18:47:14 -04001943 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001944 * @phba: Pointer to HBA context object.
1945 * @qno: HBQ number.
1946 *
1947 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001948 * is called with no lock held. The function returns the number of HBQ entries
1949 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001950 **/
James Smarted957682007-06-17 19:56:37 -05001951int
James Smart92d7f7b2007-06-17 19:56:38 -05001952lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001953{
James Smartdef9c7a2009-12-21 17:02:28 -05001954 if (phba->sli_rev == LPFC_SLI_REV4)
1955 return 0;
1956 else
1957 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1958 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001959}
1960
James Smarte59058c2008-08-24 21:49:00 -04001961/**
James Smart3621a712009-04-06 18:47:14 -04001962 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001963 * @phba: Pointer to HBA context object.
1964 * @qno: HBQ queue number.
1965 *
1966 * This function is called from SLI initialization code path with
1967 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001968 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001969 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001970static int
James Smart92d7f7b2007-06-17 19:56:38 -05001971lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001972{
James Smartdef9c7a2009-12-21 17:02:28 -05001973 if (phba->sli_rev == LPFC_SLI_REV4)
1974 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001975 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001976 else
1977 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1978 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001979}
1980
James Smarte59058c2008-08-24 21:49:00 -04001981/**
James Smart3772a992009-05-22 14:50:54 -04001982 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1983 * @phba: Pointer to HBA context object.
1984 * @hbqno: HBQ number.
1985 *
1986 * This function removes the first hbq buffer on an hbq list and returns a
1987 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1988 **/
1989static struct hbq_dmabuf *
1990lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1991{
1992 struct lpfc_dmabuf *d_buf;
1993
1994 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1995 if (!d_buf)
1996 return NULL;
1997 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1998}
1999
2000/**
James Smart3621a712009-04-06 18:47:14 -04002001 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002002 * @phba: Pointer to HBA context object.
2003 * @tag: Tag of the hbq buffer.
2004 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002005 * This function searches for the hbq buffer associated with the given tag in
2006 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2007 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002008 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002009static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002010lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2011{
James Smart92d7f7b2007-06-17 19:56:38 -05002012 struct lpfc_dmabuf *d_buf;
2013 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002014 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002015
James Smart51ef4c22007-08-02 11:10:31 -04002016 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002017 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002018 return NULL;
2019
James Smart3772a992009-05-22 14:50:54 -04002020 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002021 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002022 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002023 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002024 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002025 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002026 }
2027 }
James Smart3772a992009-05-22 14:50:54 -04002028 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002029 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002030 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002031 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002032 return NULL;
James Smarted957682007-06-17 19:56:37 -05002033}
2034
James Smarte59058c2008-08-24 21:49:00 -04002035/**
James Smart3621a712009-04-06 18:47:14 -04002036 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002037 * @phba: Pointer to HBA context object.
2038 * @hbq_buffer: Pointer to HBQ buffer.
2039 *
2040 * This function is called with hbalock. This function gives back
2041 * the hbq buffer to firmware. If the HBQ does not have space to
2042 * post the buffer, it will free the buffer.
2043 **/
James Smarted957682007-06-17 19:56:37 -05002044void
James Smart51ef4c22007-08-02 11:10:31 -04002045lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002046{
2047 uint32_t hbqno;
2048
James Smart51ef4c22007-08-02 11:10:31 -04002049 if (hbq_buffer) {
2050 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002051 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002052 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002053 }
2054}
2055
James Smarte59058c2008-08-24 21:49:00 -04002056/**
James Smart3621a712009-04-06 18:47:14 -04002057 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002058 * @mbxCommand: mailbox command code.
2059 *
2060 * This function is called by the mailbox event handler function to verify
2061 * that the completed mailbox command is a legitimate mailbox command. If the
2062 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2063 * and the mailbox event handler will take the HBA offline.
2064 **/
dea31012005-04-17 16:05:31 -05002065static int
2066lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2067{
2068 uint8_t ret;
2069
2070 switch (mbxCommand) {
2071 case MBX_LOAD_SM:
2072 case MBX_READ_NV:
2073 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002074 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002075 case MBX_RUN_BIU_DIAG:
2076 case MBX_INIT_LINK:
2077 case MBX_DOWN_LINK:
2078 case MBX_CONFIG_LINK:
2079 case MBX_CONFIG_RING:
2080 case MBX_RESET_RING:
2081 case MBX_READ_CONFIG:
2082 case MBX_READ_RCONFIG:
2083 case MBX_READ_SPARM:
2084 case MBX_READ_STATUS:
2085 case MBX_READ_RPI:
2086 case MBX_READ_XRI:
2087 case MBX_READ_REV:
2088 case MBX_READ_LNK_STAT:
2089 case MBX_REG_LOGIN:
2090 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002091 case MBX_CLEAR_LA:
2092 case MBX_DUMP_MEMORY:
2093 case MBX_DUMP_CONTEXT:
2094 case MBX_RUN_DIAGS:
2095 case MBX_RESTART:
2096 case MBX_UPDATE_CFG:
2097 case MBX_DOWN_LOAD:
2098 case MBX_DEL_LD_ENTRY:
2099 case MBX_RUN_PROGRAM:
2100 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002101 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002102 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002103 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002104 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002105 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002106 case MBX_LOAD_AREA:
2107 case MBX_RUN_BIU_DIAG64:
2108 case MBX_CONFIG_PORT:
2109 case MBX_READ_SPARM64:
2110 case MBX_READ_RPI64:
2111 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002112 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002113 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002114 case MBX_SET_DEBUG:
2115 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002116 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002117 case MBX_REG_VPI:
2118 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002119 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002120 case MBX_PORT_CAPABILITIES:
2121 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002122 case MBX_SLI4_CONFIG:
2123 case MBX_SLI4_REQ_FTRS:
2124 case MBX_REG_FCFI:
2125 case MBX_UNREG_FCFI:
2126 case MBX_REG_VFI:
2127 case MBX_UNREG_VFI:
2128 case MBX_INIT_VPI:
2129 case MBX_INIT_VFI:
2130 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002131 case MBX_READ_EVENT_LOG_STATUS:
2132 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002133 case MBX_SECURITY_MGMT:
2134 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002135 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002136 ret = mbxCommand;
2137 break;
2138 default:
2139 ret = MBX_SHUTDOWN;
2140 break;
2141 }
James Smart2e0fef82007-06-17 19:56:36 -05002142 return ret;
dea31012005-04-17 16:05:31 -05002143}
James Smarte59058c2008-08-24 21:49:00 -04002144
2145/**
James Smart3621a712009-04-06 18:47:14 -04002146 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002147 * @phba: Pointer to HBA context object.
2148 * @pmboxq: Pointer to mailbox command.
2149 *
2150 * This is completion handler function for mailbox commands issued from
2151 * lpfc_sli_issue_mbox_wait function. This function is called by the
2152 * mailbox event handler function with no lock held. This function
2153 * will wake up thread waiting on the wait queue pointed by context1
2154 * of the mailbox.
2155 **/
James Smart04c68492009-05-22 14:52:52 -04002156void
James Smart2e0fef82007-06-17 19:56:36 -05002157lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002158{
2159 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002160 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002161
2162 /*
2163 * If pdone_q is empty, the driver thread gave up waiting and
2164 * continued running.
2165 */
James Smart7054a602007-04-25 09:52:34 -04002166 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002167 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002168 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2169 if (pdone_q)
2170 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002171 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002172 return;
2173}
2174
James Smarte59058c2008-08-24 21:49:00 -04002175
2176/**
James Smart3621a712009-04-06 18:47:14 -04002177 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002178 * @phba: Pointer to HBA context object.
2179 * @pmb: Pointer to mailbox object.
2180 *
2181 * This function is the default mailbox completion handler. It
2182 * frees the memory resources associated with the completed mailbox
2183 * command. If the completed command is a REG_LOGIN mailbox command,
2184 * this function will issue a UREG_LOGIN to re-claim the RPI.
2185 **/
dea31012005-04-17 16:05:31 -05002186void
James Smart2e0fef82007-06-17 19:56:36 -05002187lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002188{
James Smartd439d282010-09-29 11:18:45 -04002189 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002190 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002191 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002192 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002193 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002194 int rc;
2195
dea31012005-04-17 16:05:31 -05002196 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002197
dea31012005-04-17 16:05:31 -05002198 if (mp) {
2199 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2200 kfree(mp);
2201 }
James Smart7054a602007-04-25 09:52:34 -04002202
2203 /*
2204 * If a REG_LOGIN succeeded after node is destroyed or node
2205 * is in re-discovery driver need to cleanup the RPI.
2206 */
James Smart2e0fef82007-06-17 19:56:36 -05002207 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002208 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2209 !pmb->u.mb.mbxStatus) {
2210 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002211 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002212 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002213 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002214 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002215 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2216 if (rc != MBX_NOT_FINISHED)
2217 return;
2218 }
2219
James Smart695a8142010-01-26 23:08:03 -05002220 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2221 !(phba->pport->load_flag & FC_UNLOADING) &&
2222 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002223 shost = lpfc_shost_from_vport(vport);
2224 spin_lock_irq(shost->host_lock);
2225 vport->vpi_state |= LPFC_VPI_REGISTERED;
2226 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2227 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002228 }
2229
James Smartd439d282010-09-29 11:18:45 -04002230 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2231 ndlp = (struct lpfc_nodelist *)pmb->context2;
2232 lpfc_nlp_put(ndlp);
2233 pmb->context2 = NULL;
2234 }
2235
James Smartdcf2a4e2010-09-29 11:18:53 -04002236 /* Check security permission status on INIT_LINK mailbox command */
2237 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2238 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2239 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2240 "2860 SLI authentication is required "
2241 "for INIT_LINK but has not done yet\n");
2242
James Smart04c68492009-05-22 14:52:52 -04002243 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2244 lpfc_sli4_mbox_cmd_free(phba, pmb);
2245 else
2246 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002247}
James Smartbe6bb942015-04-07 15:07:22 -04002248 /**
2249 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2250 * @phba: Pointer to HBA context object.
2251 * @pmb: Pointer to mailbox object.
2252 *
2253 * This function is the unreg rpi mailbox completion handler. It
2254 * frees the memory resources associated with the completed mailbox
2255 * command. An additional refrenece is put on the ndlp to prevent
2256 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2257 * the unreg mailbox command completes, this routine puts the
2258 * reference back.
2259 *
2260 **/
2261void
2262lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2263{
2264 struct lpfc_vport *vport = pmb->vport;
2265 struct lpfc_nodelist *ndlp;
2266
2267 ndlp = pmb->context1;
2268 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2269 if (phba->sli_rev == LPFC_SLI_REV4 &&
2270 (bf_get(lpfc_sli_intf_if_type,
2271 &phba->sli4_hba.sli_intf) ==
2272 LPFC_SLI_INTF_IF_TYPE_2)) {
2273 if (ndlp) {
2274 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2275 "0010 UNREG_LOGIN vpi:%x "
2276 "rpi:%x DID:%x map:%x %p\n",
2277 vport->vpi, ndlp->nlp_rpi,
2278 ndlp->nlp_DID,
2279 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002280 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002281 lpfc_nlp_put(ndlp);
2282 }
2283 }
2284 }
2285
2286 mempool_free(pmb, phba->mbox_mem_pool);
2287}
dea31012005-04-17 16:05:31 -05002288
James Smarte59058c2008-08-24 21:49:00 -04002289/**
James Smart3621a712009-04-06 18:47:14 -04002290 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002291 * @phba: Pointer to HBA context object.
2292 *
2293 * This function is called with no lock held. This function processes all
2294 * the completed mailbox commands and gives it to upper layers. The interrupt
2295 * service routine processes mailbox completion interrupt and adds completed
2296 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2297 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2298 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2299 * function returns the mailbox commands to the upper layer by calling the
2300 * completion handler function of each mailbox.
2301 **/
dea31012005-04-17 16:05:31 -05002302int
James Smart2e0fef82007-06-17 19:56:36 -05002303lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002304{
James Smart92d7f7b2007-06-17 19:56:38 -05002305 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002306 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002307 int rc;
2308 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002309
2310 phba->sli.slistat.mbox_event++;
2311
James Smart92d7f7b2007-06-17 19:56:38 -05002312 /* Get all completed mailboxe buffers into the cmplq */
2313 spin_lock_irq(&phba->hbalock);
2314 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2315 spin_unlock_irq(&phba->hbalock);
2316
dea31012005-04-17 16:05:31 -05002317 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002318 do {
2319 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2320 if (pmb == NULL)
2321 break;
2322
James Smart04c68492009-05-22 14:52:52 -04002323 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002324
James Smart858c9f62007-06-17 19:56:39 -05002325 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2326 if (pmb->vport) {
2327 lpfc_debugfs_disc_trc(pmb->vport,
2328 LPFC_DISC_TRC_MBOX_VPORT,
2329 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2330 (uint32_t)pmbox->mbxCommand,
2331 pmbox->un.varWords[0],
2332 pmbox->un.varWords[1]);
2333 }
2334 else {
2335 lpfc_debugfs_disc_trc(phba->pport,
2336 LPFC_DISC_TRC_MBOX,
2337 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2338 (uint32_t)pmbox->mbxCommand,
2339 pmbox->un.varWords[0],
2340 pmbox->un.varWords[1]);
2341 }
2342 }
2343
dea31012005-04-17 16:05:31 -05002344 /*
2345 * It is a fatal error if unknown mbox command completion.
2346 */
2347 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2348 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002349 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002350 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002351 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002352 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002353 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002354 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002355 lpfc_sli_config_mbox_subsys_get(phba,
2356 pmb),
2357 lpfc_sli_config_mbox_opcode_get(phba,
2358 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002359 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002360 phba->work_hs = HS_FFER3;
2361 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002362 continue;
dea31012005-04-17 16:05:31 -05002363 }
2364
dea31012005-04-17 16:05:31 -05002365 if (pmbox->mbxStatus) {
2366 phba->sli.slistat.mbox_stat_err++;
2367 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2368 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002369 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002370 LOG_MBOX | LOG_SLI,
2371 "(%d):0305 Mbox cmd cmpl "
2372 "error - RETRYing Data: x%x "
2373 "(x%x/x%x) x%x x%x x%x\n",
2374 pmb->vport ? pmb->vport->vpi : 0,
2375 pmbox->mbxCommand,
2376 lpfc_sli_config_mbox_subsys_get(phba,
2377 pmb),
2378 lpfc_sli_config_mbox_opcode_get(phba,
2379 pmb),
2380 pmbox->mbxStatus,
2381 pmbox->un.varWords[0],
2382 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002383 pmbox->mbxStatus = 0;
2384 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002385 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002386 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002387 continue;
dea31012005-04-17 16:05:31 -05002388 }
2389 }
2390
2391 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002392 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002393 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002394 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2395 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002396 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002397 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002398 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2399 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002400 pmb->mbox_cmpl,
2401 *((uint32_t *) pmbox),
2402 pmbox->un.varWords[0],
2403 pmbox->un.varWords[1],
2404 pmbox->un.varWords[2],
2405 pmbox->un.varWords[3],
2406 pmbox->un.varWords[4],
2407 pmbox->un.varWords[5],
2408 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002409 pmbox->un.varWords[7],
2410 pmbox->un.varWords[8],
2411 pmbox->un.varWords[9],
2412 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002413
James Smart92d7f7b2007-06-17 19:56:38 -05002414 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002415 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002416 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002417 return 0;
dea31012005-04-17 16:05:31 -05002418}
James Smart92d7f7b2007-06-17 19:56:38 -05002419
James Smarte59058c2008-08-24 21:49:00 -04002420/**
James Smart3621a712009-04-06 18:47:14 -04002421 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002422 * @phba: Pointer to HBA context object.
2423 * @pring: Pointer to driver SLI ring object.
2424 * @tag: buffer tag.
2425 *
2426 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2427 * is set in the tag the buffer is posted for a particular exchange,
2428 * the function will return the buffer without replacing the buffer.
2429 * If the buffer is for unsolicited ELS or CT traffic, this function
2430 * returns the buffer and also posts another buffer to the firmware.
2431 **/
James Smart76bb24e2007-10-27 13:38:00 -04002432static struct lpfc_dmabuf *
2433lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002434 struct lpfc_sli_ring *pring,
2435 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002436{
James Smart9f1e1b52008-12-04 22:39:40 -05002437 struct hbq_dmabuf *hbq_entry;
2438
James Smart76bb24e2007-10-27 13:38:00 -04002439 if (tag & QUE_BUFTAG_BIT)
2440 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002441 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2442 if (!hbq_entry)
2443 return NULL;
2444 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002445}
James Smart57127f12007-10-27 13:37:05 -04002446
James Smart3772a992009-05-22 14:50:54 -04002447/**
2448 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2449 * @phba: Pointer to HBA context object.
2450 * @pring: Pointer to driver SLI ring object.
2451 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2452 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2453 * @fch_type: the type for the first frame of the sequence.
2454 *
2455 * This function is called with no lock held. This function uses the r_ctl and
2456 * type of the received sequence to find the correct callback function to call
2457 * to process the sequence.
2458 **/
2459static int
2460lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2461 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2462 uint32_t fch_type)
2463{
2464 int i;
2465
2466 /* unSolicited Responses */
2467 if (pring->prt[0].profile) {
2468 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2469 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2470 saveq);
2471 return 1;
2472 }
2473 /* We must search, based on rctl / type
2474 for the right routine */
2475 for (i = 0; i < pring->num_mask; i++) {
2476 if ((pring->prt[i].rctl == fch_r_ctl) &&
2477 (pring->prt[i].type == fch_type)) {
2478 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2479 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2480 (phba, pring, saveq);
2481 return 1;
2482 }
2483 }
2484 return 0;
2485}
James Smarte59058c2008-08-24 21:49:00 -04002486
2487/**
James Smart3621a712009-04-06 18:47:14 -04002488 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002489 * @phba: Pointer to HBA context object.
2490 * @pring: Pointer to driver SLI ring object.
2491 * @saveq: Pointer to the unsolicited iocb.
2492 *
2493 * This function is called with no lock held by the ring event handler
2494 * when there is an unsolicited iocb posted to the response ring by the
2495 * firmware. This function gets the buffer associated with the iocbs
2496 * and calls the event handler for the ring. This function handles both
2497 * qring buffers and hbq buffers.
2498 * When the function returns 1 the caller can free the iocb object otherwise
2499 * upper layer functions will free the iocb objects.
2500 **/
dea31012005-04-17 16:05:31 -05002501static int
2502lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2503 struct lpfc_iocbq *saveq)
2504{
2505 IOCB_t * irsp;
2506 WORD5 * w5p;
2507 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002508 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002509 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002510
dea31012005-04-17 16:05:31 -05002511 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002512
2513 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2514 if (pring->lpfc_sli_rcv_async_status)
2515 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2516 else
2517 lpfc_printf_log(phba,
2518 KERN_WARNING,
2519 LOG_SLI,
2520 "0316 Ring %d handler: unexpected "
2521 "ASYNC_STATUS iocb received evt_code "
2522 "0x%x\n",
2523 pring->ringno,
2524 irsp->un.asyncstat.evt_code);
2525 return 1;
2526 }
2527
James Smart3163f722008-02-08 18:50:25 -05002528 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2529 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2530 if (irsp->ulpBdeCount > 0) {
2531 dmzbuf = lpfc_sli_get_buff(phba, pring,
2532 irsp->un.ulpWord[3]);
2533 lpfc_in_buf_free(phba, dmzbuf);
2534 }
2535
2536 if (irsp->ulpBdeCount > 1) {
2537 dmzbuf = lpfc_sli_get_buff(phba, pring,
2538 irsp->unsli3.sli3Words[3]);
2539 lpfc_in_buf_free(phba, dmzbuf);
2540 }
2541
2542 if (irsp->ulpBdeCount > 2) {
2543 dmzbuf = lpfc_sli_get_buff(phba, pring,
2544 irsp->unsli3.sli3Words[7]);
2545 lpfc_in_buf_free(phba, dmzbuf);
2546 }
2547
2548 return 1;
2549 }
2550
James Smart92d7f7b2007-06-17 19:56:38 -05002551 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002552 if (irsp->ulpBdeCount != 0) {
2553 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002554 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002555 if (!saveq->context2)
2556 lpfc_printf_log(phba,
2557 KERN_ERR,
2558 LOG_SLI,
2559 "0341 Ring %d Cannot find buffer for "
2560 "an unsolicited iocb. tag 0x%x\n",
2561 pring->ringno,
2562 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002563 }
2564 if (irsp->ulpBdeCount == 2) {
2565 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002566 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002567 if (!saveq->context3)
2568 lpfc_printf_log(phba,
2569 KERN_ERR,
2570 LOG_SLI,
2571 "0342 Ring %d Cannot find buffer for an"
2572 " unsolicited iocb. tag 0x%x\n",
2573 pring->ringno,
2574 irsp->unsli3.sli3Words[7]);
2575 }
2576 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002577 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002578 if (irsp->ulpBdeCount != 0) {
2579 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2580 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002581 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002582 lpfc_printf_log(phba,
2583 KERN_ERR,
2584 LOG_SLI,
2585 "0343 Ring %d Cannot find "
2586 "buffer for an unsolicited iocb"
2587 ". tag 0x%x\n", pring->ringno,
2588 irsp->un.ulpWord[3]);
2589 }
2590 if (irsp->ulpBdeCount == 2) {
2591 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2592 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002593 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002594 lpfc_printf_log(phba,
2595 KERN_ERR,
2596 LOG_SLI,
2597 "0344 Ring %d Cannot find "
2598 "buffer for an unsolicited "
2599 "iocb. tag 0x%x\n",
2600 pring->ringno,
2601 irsp->unsli3.sli3Words[7]);
2602 }
2603 }
James Smart92d7f7b2007-06-17 19:56:38 -05002604 }
James Smart9c2face2008-01-11 01:53:18 -05002605 if (irsp->ulpBdeCount != 0 &&
2606 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2607 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2608 int found = 0;
2609
2610 /* search continue save q for same XRI */
2611 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002612 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2613 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002614 list_add_tail(&saveq->list, &iocbq->list);
2615 found = 1;
2616 break;
2617 }
2618 }
2619 if (!found)
2620 list_add_tail(&saveq->clist,
2621 &pring->iocb_continue_saveq);
2622 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2623 list_del_init(&iocbq->clist);
2624 saveq = iocbq;
2625 irsp = &(saveq->iocb);
2626 } else
2627 return 0;
2628 }
2629 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2630 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2631 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002632 Rctl = FC_RCTL_ELS_REQ;
2633 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002634 } else {
2635 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2636 Rctl = w5p->hcsw.Rctl;
2637 Type = w5p->hcsw.Type;
2638
2639 /* Firmware Workaround */
2640 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2641 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2642 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002643 Rctl = FC_RCTL_ELS_REQ;
2644 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002645 w5p->hcsw.Rctl = Rctl;
2646 w5p->hcsw.Type = Type;
2647 }
2648 }
James Smart92d7f7b2007-06-17 19:56:38 -05002649
James Smart3772a992009-05-22 14:50:54 -04002650 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002651 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002652 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002653 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002654 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002655
James Smart92d7f7b2007-06-17 19:56:38 -05002656 return 1;
dea31012005-04-17 16:05:31 -05002657}
2658
James Smarte59058c2008-08-24 21:49:00 -04002659/**
James Smart3621a712009-04-06 18:47:14 -04002660 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002661 * @phba: Pointer to HBA context object.
2662 * @pring: Pointer to driver SLI ring object.
2663 * @prspiocb: Pointer to response iocb object.
2664 *
2665 * This function looks up the iocb_lookup table to get the command iocb
2666 * corresponding to the given response iocb using the iotag of the
2667 * response iocb. This function is called with the hbalock held.
2668 * This function returns the command iocb object if it finds the command
2669 * iocb else returns NULL.
2670 **/
dea31012005-04-17 16:05:31 -05002671static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002672lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2673 struct lpfc_sli_ring *pring,
2674 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002675{
dea31012005-04-17 16:05:31 -05002676 struct lpfc_iocbq *cmd_iocb = NULL;
2677 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002678 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002679
James Bottomley604a3e32005-10-29 10:28:33 -05002680 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002681
James Bottomley604a3e32005-10-29 10:28:33 -05002682 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2683 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002684 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart89533e92016-10-13 15:06:15 -07002685 /* remove from txcmpl queue list */
2686 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002687 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart89533e92016-10-13 15:06:15 -07002688 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002689 }
dea31012005-04-17 16:05:31 -05002690 }
2691
dea31012005-04-17 16:05:31 -05002692 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002693 "0317 iotag x%x is out of "
James Bottomley604a3e32005-10-29 10:28:33 -05002694 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002695 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002696 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002697 return NULL;
2698}
2699
James Smarte59058c2008-08-24 21:49:00 -04002700/**
James Smart3772a992009-05-22 14:50:54 -04002701 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2702 * @phba: Pointer to HBA context object.
2703 * @pring: Pointer to driver SLI ring object.
2704 * @iotag: IOCB tag.
2705 *
2706 * This function looks up the iocb_lookup table to get the command iocb
2707 * corresponding to the given iotag. This function is called with the
2708 * hbalock held.
2709 * This function returns the command iocb object if it finds the command
2710 * iocb else returns NULL.
2711 **/
2712static struct lpfc_iocbq *
2713lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2714 struct lpfc_sli_ring *pring, uint16_t iotag)
2715{
2716 struct lpfc_iocbq *cmd_iocb;
2717
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002718 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002719 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2720 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002721 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2722 /* remove from txcmpl queue list */
2723 list_del_init(&cmd_iocb->list);
2724 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002725 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002726 }
James Smart3772a992009-05-22 14:50:54 -04002727 }
James Smart89533e92016-10-13 15:06:15 -07002728
James Smart3772a992009-05-22 14:50:54 -04002729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart89533e92016-10-13 15:06:15 -07002730 "0372 iotag x%x is out of range: max iotag (x%x)\n",
James Smart3772a992009-05-22 14:50:54 -04002731 iotag, phba->sli.last_iotag);
2732 return NULL;
2733}
2734
2735/**
James Smart3621a712009-04-06 18:47:14 -04002736 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002737 * @phba: Pointer to HBA context object.
2738 * @pring: Pointer to driver SLI ring object.
2739 * @saveq: Pointer to the response iocb to be processed.
2740 *
2741 * This function is called by the ring event handler for non-fcp
2742 * rings when there is a new response iocb in the response ring.
2743 * The caller is not required to hold any locks. This function
2744 * gets the command iocb associated with the response iocb and
2745 * calls the completion handler for the command iocb. If there
2746 * is no completion handler, the function will free the resources
2747 * associated with command iocb. If the response iocb is for
2748 * an already aborted command iocb, the status of the completion
2749 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2750 * This function always returns 1.
2751 **/
dea31012005-04-17 16:05:31 -05002752static int
James Smart2e0fef82007-06-17 19:56:36 -05002753lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002754 struct lpfc_iocbq *saveq)
2755{
James Smart2e0fef82007-06-17 19:56:36 -05002756 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002757 int rc = 1;
2758 unsigned long iflag;
2759
2760 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002761 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002762 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002763 spin_unlock_irqrestore(&phba->hbalock, iflag);
2764
dea31012005-04-17 16:05:31 -05002765 if (cmdiocbp) {
2766 if (cmdiocbp->iocb_cmpl) {
2767 /*
James Smartea2151b2008-09-07 11:52:10 -04002768 * If an ELS command failed send an event to mgmt
2769 * application.
2770 */
2771 if (saveq->iocb.ulpStatus &&
2772 (pring->ringno == LPFC_ELS_RING) &&
2773 (cmdiocbp->iocb.ulpCommand ==
2774 CMD_ELS_REQUEST64_CR))
2775 lpfc_send_els_failure_event(phba,
2776 cmdiocbp, saveq);
2777
2778 /*
dea31012005-04-17 16:05:31 -05002779 * Post all ELS completions to the worker thread.
2780 * All other are passed to the completion callback.
2781 */
2782 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002783 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2784 (cmdiocbp->iocb_flag &
2785 LPFC_DRIVER_ABORTED)) {
2786 spin_lock_irqsave(&phba->hbalock,
2787 iflag);
James Smart07951072007-04-25 09:51:38 -04002788 cmdiocbp->iocb_flag &=
2789 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002790 spin_unlock_irqrestore(&phba->hbalock,
2791 iflag);
James Smart07951072007-04-25 09:51:38 -04002792 saveq->iocb.ulpStatus =
2793 IOSTAT_LOCAL_REJECT;
2794 saveq->iocb.un.ulpWord[4] =
2795 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002796
2797 /* Firmware could still be in progress
2798 * of DMAing payload, so don't free data
2799 * buffer till after a hbeat.
2800 */
James Smart341af102010-01-26 23:07:37 -05002801 spin_lock_irqsave(&phba->hbalock,
2802 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002803 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002804 spin_unlock_irqrestore(&phba->hbalock,
2805 iflag);
2806 }
James Smart0f65ff62010-02-26 14:14:23 -05002807 if (phba->sli_rev == LPFC_SLI_REV4) {
2808 if (saveq->iocb_flag &
2809 LPFC_EXCHANGE_BUSY) {
2810 /* Set cmdiocb flag for the
2811 * exchange busy so sgl (xri)
2812 * will not be released until
2813 * the abort xri is received
2814 * from hba.
2815 */
2816 spin_lock_irqsave(
2817 &phba->hbalock, iflag);
2818 cmdiocbp->iocb_flag |=
2819 LPFC_EXCHANGE_BUSY;
2820 spin_unlock_irqrestore(
2821 &phba->hbalock, iflag);
2822 }
2823 if (cmdiocbp->iocb_flag &
2824 LPFC_DRIVER_ABORTED) {
2825 /*
2826 * Clear LPFC_DRIVER_ABORTED
2827 * bit in case it was driver
2828 * initiated abort.
2829 */
2830 spin_lock_irqsave(
2831 &phba->hbalock, iflag);
2832 cmdiocbp->iocb_flag &=
2833 ~LPFC_DRIVER_ABORTED;
2834 spin_unlock_irqrestore(
2835 &phba->hbalock, iflag);
2836 cmdiocbp->iocb.ulpStatus =
2837 IOSTAT_LOCAL_REJECT;
2838 cmdiocbp->iocb.un.ulpWord[4] =
2839 IOERR_ABORT_REQUESTED;
2840 /*
2841 * For SLI4, irsiocb contains
2842 * NO_XRI in sli_xritag, it
2843 * shall not affect releasing
2844 * sgl (xri) process.
2845 */
2846 saveq->iocb.ulpStatus =
2847 IOSTAT_LOCAL_REJECT;
2848 saveq->iocb.un.ulpWord[4] =
2849 IOERR_SLI_ABORTED;
2850 spin_lock_irqsave(
2851 &phba->hbalock, iflag);
2852 saveq->iocb_flag |=
2853 LPFC_DELAY_MEM_FREE;
2854 spin_unlock_irqrestore(
2855 &phba->hbalock, iflag);
2856 }
James Smart07951072007-04-25 09:51:38 -04002857 }
dea31012005-04-17 16:05:31 -05002858 }
James Smart2e0fef82007-06-17 19:56:36 -05002859 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002860 } else
2861 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002862 } else {
2863 /*
2864 * Unknown initiating command based on the response iotag.
2865 * This could be the case on the ELS ring because of
2866 * lpfc_els_abort().
2867 */
2868 if (pring->ringno != LPFC_ELS_RING) {
2869 /*
2870 * Ring <ringno> handler: unexpected completion IoTag
2871 * <IoTag>
2872 */
James Smarta257bf92009-04-06 18:48:10 -04002873 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002874 "0322 Ring %d handler: "
2875 "unexpected completion IoTag x%x "
2876 "Data: x%x x%x x%x x%x\n",
2877 pring->ringno,
2878 saveq->iocb.ulpIoTag,
2879 saveq->iocb.ulpStatus,
2880 saveq->iocb.un.ulpWord[4],
2881 saveq->iocb.ulpCommand,
2882 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002883 }
2884 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002885
dea31012005-04-17 16:05:31 -05002886 return rc;
2887}
2888
James Smarte59058c2008-08-24 21:49:00 -04002889/**
James Smart3621a712009-04-06 18:47:14 -04002890 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002891 * @phba: Pointer to HBA context object.
2892 * @pring: Pointer to driver SLI ring object.
2893 *
2894 * This function is called from the iocb ring event handlers when
2895 * put pointer is ahead of the get pointer for a ring. This function signal
2896 * an error attention condition to the worker thread and the worker
2897 * thread will transition the HBA to offline state.
2898 **/
James Smart2e0fef82007-06-17 19:56:36 -05002899static void
2900lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002901{
James Smart34b02dc2008-08-24 21:49:55 -04002902 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002903 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002904 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002905 * rsp ring <portRspMax>
2906 */
2907 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002908 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002909 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002910 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002911 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002912
James Smart2e0fef82007-06-17 19:56:36 -05002913 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002914
2915 /*
2916 * All error attention handlers are posted to
2917 * worker thread
2918 */
2919 phba->work_ha |= HA_ERATT;
2920 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002921
James Smart5e9d9b82008-06-14 22:52:53 -04002922 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002923
2924 return;
2925}
2926
James Smarte59058c2008-08-24 21:49:00 -04002927/**
James Smart3621a712009-04-06 18:47:14 -04002928 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002929 * @ptr: Pointer to address of HBA context object.
2930 *
2931 * This function is invoked by the Error Attention polling timer when the
2932 * timer times out. It will check the SLI Error Attention register for
2933 * possible attention events. If so, it will post an Error Attention event
2934 * and wake up worker thread to process it. Otherwise, it will set up the
2935 * Error Attention polling timer for the next poll.
2936 **/
2937void lpfc_poll_eratt(unsigned long ptr)
2938{
2939 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04002940 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04002941 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002942
2943 phba = (struct lpfc_hba *)ptr;
2944
James Smartaa6fbb72012-08-03 12:36:03 -04002945 /* Here we will also keep track of interrupts per sec of the hba */
2946 sli_intr = phba->sli.slistat.sli_intr;
2947
2948 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2949 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2950 sli_intr);
2951 else
2952 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2953
James Smart65791f12016-07-06 12:35:56 -07002954 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
2955 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04002956 phba->sli.slistat.sli_ips = cnt;
2957
2958 phba->sli.slistat.sli_prev_intr = sli_intr;
2959
James Smart93996272008-08-24 21:50:30 -04002960 /* Check chip HA register for error event */
2961 eratt = lpfc_sli_check_eratt(phba);
2962
2963 if (eratt)
2964 /* Tell the worker thread there is work to do */
2965 lpfc_worker_wake_up(phba);
2966 else
2967 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04002968 mod_timer(&phba->eratt_poll,
2969 jiffies +
James Smart65791f12016-07-06 12:35:56 -07002970 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04002971 return;
2972}
2973
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002974
James Smarte59058c2008-08-24 21:49:00 -04002975/**
James Smart3621a712009-04-06 18:47:14 -04002976 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002977 * @phba: Pointer to HBA context object.
2978 * @pring: Pointer to driver SLI ring object.
2979 * @mask: Host attention register mask for this ring.
2980 *
2981 * This function is called from the interrupt context when there is a ring
2982 * event for the fcp ring. The caller does not hold any lock.
2983 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002984 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002985 * LE bit set. The function will call the completion handler of the command iocb
2986 * if the response iocb indicates a completion for a command iocb or it is
2987 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2988 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002989 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002990 * to check it explicitly.
2991 */
2992int
James Smart2e0fef82007-06-17 19:56:36 -05002993lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2994 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002995{
James Smart34b02dc2008-08-24 21:49:55 -04002996 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002997 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002998 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002999 struct lpfc_iocbq *cmdiocbq = NULL;
3000 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003001 uint32_t status;
3002 uint32_t portRspPut, portRspMax;
3003 int rc = 1;
3004 lpfc_iocb_type type;
3005 unsigned long iflag;
3006 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003007
James Smart2e0fef82007-06-17 19:56:36 -05003008 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003009 pring->stats.iocb_event++;
3010
dea31012005-04-17 16:05:31 -05003011 /*
3012 * The next available response entry should never exceed the maximum
3013 * entries. If it does, treat it as an adapter hardware error.
3014 */
James Smart7e56aa22012-08-03 12:35:34 -04003015 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003016 portRspPut = le32_to_cpu(pgp->rspPutInx);
3017 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003018 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003019 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003020 return 1;
3021 }
James Smart45ed1192009-10-02 15:17:02 -04003022 if (phba->fcp_ring_in_use) {
3023 spin_unlock_irqrestore(&phba->hbalock, iflag);
3024 return 1;
3025 } else
3026 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003027
3028 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003029 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003030 /*
3031 * Fetch an entry off the ring and copy it into a local data
3032 * structure. The copy involves a byte-swap since the
3033 * network byte order and pci byte orders are different.
3034 */
James Smarted957682007-06-17 19:56:37 -05003035 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003036 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003037
James Smart7e56aa22012-08-03 12:35:34 -04003038 if (++pring->sli.sli3.rspidx >= portRspMax)
3039 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003040
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003041 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3042 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003043 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003044 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003045 irsp = &rspiocbq.iocb;
3046
dea31012005-04-17 16:05:31 -05003047 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3048 pring->stats.iocb_rsp++;
3049 rsp_cmpl++;
3050
3051 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003052 /*
3053 * If resource errors reported from HBA, reduce
3054 * queuedepths of the SCSI device.
3055 */
3056 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003057 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3058 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003059 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003060 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003061 spin_lock_irqsave(&phba->hbalock, iflag);
3062 }
3063
dea31012005-04-17 16:05:31 -05003064 /* Rsp ring <ringno> error: IOCB */
3065 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003066 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003067 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003068 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003069 irsp->un.ulpWord[0],
3070 irsp->un.ulpWord[1],
3071 irsp->un.ulpWord[2],
3072 irsp->un.ulpWord[3],
3073 irsp->un.ulpWord[4],
3074 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003075 *(uint32_t *)&irsp->un1,
3076 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003077 }
3078
3079 switch (type) {
3080 case LPFC_ABORT_IOCB:
3081 case LPFC_SOL_IOCB:
3082 /*
3083 * Idle exchange closed via ABTS from port. No iocb
3084 * resources need to be recovered.
3085 */
3086 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003087 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003088 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003089 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003090 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003091 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003092 break;
3093 }
3094
James Bottomley604a3e32005-10-29 10:28:33 -05003095 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3096 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003097 if (unlikely(!cmdiocbq))
3098 break;
3099 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3100 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3101 if (cmdiocbq->iocb_cmpl) {
3102 spin_unlock_irqrestore(&phba->hbalock, iflag);
3103 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3104 &rspiocbq);
3105 spin_lock_irqsave(&phba->hbalock, iflag);
3106 }
dea31012005-04-17 16:05:31 -05003107 break;
James Smarta4bc3372006-12-02 13:34:16 -05003108 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003109 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003110 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003111 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003112 break;
dea31012005-04-17 16:05:31 -05003113 default:
3114 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3115 char adaptermsg[LPFC_MAX_ADPTMSG];
3116 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3117 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3118 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003119 dev_warn(&((phba->pcidev)->dev),
3120 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003121 phba->brd_no, adaptermsg);
3122 } else {
3123 /* Unknown IOCB command */
3124 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003125 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003126 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003127 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003128 irsp->ulpStatus,
3129 irsp->ulpIoTag,
3130 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003131 }
3132 break;
3133 }
3134
3135 /*
3136 * The response IOCB has been processed. Update the ring
3137 * pointer in SLIM. If the port response put pointer has not
3138 * been updated, sync the pgp->rspPutInx and fetch the new port
3139 * response put pointer.
3140 */
James Smart7e56aa22012-08-03 12:35:34 -04003141 writel(pring->sli.sli3.rspidx,
3142 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003143
James Smart7e56aa22012-08-03 12:35:34 -04003144 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003145 portRspPut = le32_to_cpu(pgp->rspPutInx);
3146 }
3147
3148 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3149 pring->stats.iocb_rsp_full++;
3150 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3151 writel(status, phba->CAregaddr);
3152 readl(phba->CAregaddr);
3153 }
3154 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3155 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3156 pring->stats.iocb_cmd_empty++;
3157
3158 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003159 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003160 lpfc_sli_resume_iocb(phba, pring);
3161
3162 if ((pring->lpfc_sli_cmd_available))
3163 (pring->lpfc_sli_cmd_available) (phba, pring);
3164
3165 }
3166
James Smart45ed1192009-10-02 15:17:02 -04003167 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003168 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003169 return rc;
3170}
3171
James Smarte59058c2008-08-24 21:49:00 -04003172/**
James Smart3772a992009-05-22 14:50:54 -04003173 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3174 * @phba: Pointer to HBA context object.
3175 * @pring: Pointer to driver SLI ring object.
3176 * @rspiocbp: Pointer to driver response IOCB object.
3177 *
3178 * This function is called from the worker thread when there is a slow-path
3179 * response IOCB to process. This function chains all the response iocbs until
3180 * seeing the iocb with the LE bit set. The function will call
3181 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3182 * completion of a command iocb. The function will call the
3183 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3184 * The function frees the resources or calls the completion handler if this
3185 * iocb is an abort completion. The function returns NULL when the response
3186 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3187 * this function shall chain the iocb on to the iocb_continueq and return the
3188 * response iocb passed in.
3189 **/
3190static struct lpfc_iocbq *
3191lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3192 struct lpfc_iocbq *rspiocbp)
3193{
3194 struct lpfc_iocbq *saveq;
3195 struct lpfc_iocbq *cmdiocbp;
3196 struct lpfc_iocbq *next_iocb;
3197 IOCB_t *irsp = NULL;
3198 uint32_t free_saveq;
3199 uint8_t iocb_cmd_type;
3200 lpfc_iocb_type type;
3201 unsigned long iflag;
3202 int rc;
3203
3204 spin_lock_irqsave(&phba->hbalock, iflag);
3205 /* First add the response iocb to the countinueq list */
3206 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3207 pring->iocb_continueq_cnt++;
3208
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003209 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003210 irsp = &rspiocbp->iocb;
3211 if (irsp->ulpLe) {
3212 /*
3213 * By default, the driver expects to free all resources
3214 * associated with this iocb completion.
3215 */
3216 free_saveq = 1;
3217 saveq = list_get_first(&pring->iocb_continueq,
3218 struct lpfc_iocbq, list);
3219 irsp = &(saveq->iocb);
3220 list_del_init(&pring->iocb_continueq);
3221 pring->iocb_continueq_cnt = 0;
3222
3223 pring->stats.iocb_rsp++;
3224
3225 /*
3226 * If resource errors reported from HBA, reduce
3227 * queuedepths of the SCSI device.
3228 */
3229 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003230 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3231 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003232 spin_unlock_irqrestore(&phba->hbalock, iflag);
3233 phba->lpfc_rampdown_queue_depth(phba);
3234 spin_lock_irqsave(&phba->hbalock, iflag);
3235 }
3236
3237 if (irsp->ulpStatus) {
3238 /* Rsp ring <ringno> error: IOCB */
3239 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3240 "0328 Rsp Ring %d error: "
3241 "IOCB Data: "
3242 "x%x x%x x%x x%x "
3243 "x%x x%x x%x x%x "
3244 "x%x x%x x%x x%x "
3245 "x%x x%x x%x x%x\n",
3246 pring->ringno,
3247 irsp->un.ulpWord[0],
3248 irsp->un.ulpWord[1],
3249 irsp->un.ulpWord[2],
3250 irsp->un.ulpWord[3],
3251 irsp->un.ulpWord[4],
3252 irsp->un.ulpWord[5],
3253 *(((uint32_t *) irsp) + 6),
3254 *(((uint32_t *) irsp) + 7),
3255 *(((uint32_t *) irsp) + 8),
3256 *(((uint32_t *) irsp) + 9),
3257 *(((uint32_t *) irsp) + 10),
3258 *(((uint32_t *) irsp) + 11),
3259 *(((uint32_t *) irsp) + 12),
3260 *(((uint32_t *) irsp) + 13),
3261 *(((uint32_t *) irsp) + 14),
3262 *(((uint32_t *) irsp) + 15));
3263 }
3264
3265 /*
3266 * Fetch the IOCB command type and call the correct completion
3267 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3268 * get freed back to the lpfc_iocb_list by the discovery
3269 * kernel thread.
3270 */
3271 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3272 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3273 switch (type) {
3274 case LPFC_SOL_IOCB:
3275 spin_unlock_irqrestore(&phba->hbalock, iflag);
3276 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3277 spin_lock_irqsave(&phba->hbalock, iflag);
3278 break;
3279
3280 case LPFC_UNSOL_IOCB:
3281 spin_unlock_irqrestore(&phba->hbalock, iflag);
3282 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3283 spin_lock_irqsave(&phba->hbalock, iflag);
3284 if (!rc)
3285 free_saveq = 0;
3286 break;
3287
3288 case LPFC_ABORT_IOCB:
3289 cmdiocbp = NULL;
3290 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3291 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3292 saveq);
3293 if (cmdiocbp) {
3294 /* Call the specified completion routine */
3295 if (cmdiocbp->iocb_cmpl) {
3296 spin_unlock_irqrestore(&phba->hbalock,
3297 iflag);
3298 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3299 saveq);
3300 spin_lock_irqsave(&phba->hbalock,
3301 iflag);
3302 } else
3303 __lpfc_sli_release_iocbq(phba,
3304 cmdiocbp);
3305 }
3306 break;
3307
3308 case LPFC_UNKNOWN_IOCB:
3309 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3310 char adaptermsg[LPFC_MAX_ADPTMSG];
3311 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3312 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3313 MAX_MSG_DATA);
3314 dev_warn(&((phba->pcidev)->dev),
3315 "lpfc%d: %s\n",
3316 phba->brd_no, adaptermsg);
3317 } else {
3318 /* Unknown IOCB command */
3319 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3320 "0335 Unknown IOCB "
3321 "command Data: x%x "
3322 "x%x x%x x%x\n",
3323 irsp->ulpCommand,
3324 irsp->ulpStatus,
3325 irsp->ulpIoTag,
3326 irsp->ulpContext);
3327 }
3328 break;
3329 }
3330
3331 if (free_saveq) {
3332 list_for_each_entry_safe(rspiocbp, next_iocb,
3333 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003334 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003335 __lpfc_sli_release_iocbq(phba, rspiocbp);
3336 }
3337 __lpfc_sli_release_iocbq(phba, saveq);
3338 }
3339 rspiocbp = NULL;
3340 }
3341 spin_unlock_irqrestore(&phba->hbalock, iflag);
3342 return rspiocbp;
3343}
3344
3345/**
3346 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003347 * @phba: Pointer to HBA context object.
3348 * @pring: Pointer to driver SLI ring object.
3349 * @mask: Host attention register mask for this ring.
3350 *
James Smart3772a992009-05-22 14:50:54 -04003351 * This routine wraps the actual slow_ring event process routine from the
3352 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003353 **/
James Smart3772a992009-05-22 14:50:54 -04003354void
James Smart2e0fef82007-06-17 19:56:36 -05003355lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3356 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003357{
James Smart3772a992009-05-22 14:50:54 -04003358 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3359}
3360
3361/**
3362 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3363 * @phba: Pointer to HBA context object.
3364 * @pring: Pointer to driver SLI ring object.
3365 * @mask: Host attention register mask for this ring.
3366 *
3367 * This function is called from the worker thread when there is a ring event
3368 * for non-fcp rings. The caller does not hold any lock. The function will
3369 * remove each response iocb in the response ring and calls the handle
3370 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3371 **/
3372static void
3373lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3374 struct lpfc_sli_ring *pring, uint32_t mask)
3375{
James Smart34b02dc2008-08-24 21:49:55 -04003376 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003377 IOCB_t *entry;
3378 IOCB_t *irsp = NULL;
3379 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003380 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003381 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003382 uint32_t status;
dea31012005-04-17 16:05:31 -05003383
James Smart34b02dc2008-08-24 21:49:55 -04003384 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003385 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003386 pring->stats.iocb_event++;
3387
dea31012005-04-17 16:05:31 -05003388 /*
3389 * The next available response entry should never exceed the maximum
3390 * entries. If it does, treat it as an adapter hardware error.
3391 */
James Smart7e56aa22012-08-03 12:35:34 -04003392 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003393 portRspPut = le32_to_cpu(pgp->rspPutInx);
3394 if (portRspPut >= portRspMax) {
3395 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003396 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003397 * rsp ring <portRspMax>
3398 */
James Smarted957682007-06-17 19:56:37 -05003399 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003400 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003401 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003402 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003403
James Smart2e0fef82007-06-17 19:56:36 -05003404 phba->link_state = LPFC_HBA_ERROR;
3405 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003406
3407 phba->work_hs = HS_FFER3;
3408 lpfc_handle_eratt(phba);
3409
James Smart3772a992009-05-22 14:50:54 -04003410 return;
dea31012005-04-17 16:05:31 -05003411 }
3412
3413 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003414 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003415 /*
3416 * Build a completion list and call the appropriate handler.
3417 * The process is to get the next available response iocb, get
3418 * a free iocb from the list, copy the response data into the
3419 * free iocb, insert to the continuation list, and update the
3420 * next response index to slim. This process makes response
3421 * iocb's in the ring available to DMA as fast as possible but
3422 * pays a penalty for a copy operation. Since the iocb is
3423 * only 32 bytes, this penalty is considered small relative to
3424 * the PCI reads for register values and a slim write. When
3425 * the ulpLe field is set, the entire Command has been
3426 * received.
3427 */
James Smarted957682007-06-17 19:56:37 -05003428 entry = lpfc_resp_iocb(phba, pring);
3429
James Smart858c9f62007-06-17 19:56:39 -05003430 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003431 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003432 if (rspiocbp == NULL) {
3433 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003434 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003435 break;
3436 }
3437
James Smarted957682007-06-17 19:56:37 -05003438 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3439 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003440 irsp = &rspiocbp->iocb;
3441
James Smart7e56aa22012-08-03 12:35:34 -04003442 if (++pring->sli.sli3.rspidx >= portRspMax)
3443 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003444
James Smarta58cbd52007-08-02 11:09:43 -04003445 if (pring->ringno == LPFC_ELS_RING) {
3446 lpfc_debugfs_slow_ring_trc(phba,
3447 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3448 *(((uint32_t *) irsp) + 4),
3449 *(((uint32_t *) irsp) + 6),
3450 *(((uint32_t *) irsp) + 7));
3451 }
3452
James Smart7e56aa22012-08-03 12:35:34 -04003453 writel(pring->sli.sli3.rspidx,
3454 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003455
James Smart3772a992009-05-22 14:50:54 -04003456 spin_unlock_irqrestore(&phba->hbalock, iflag);
3457 /* Handle the response IOCB */
3458 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3459 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003460
3461 /*
3462 * If the port response put pointer has not been updated, sync
3463 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3464 * response put pointer.
3465 */
James Smart7e56aa22012-08-03 12:35:34 -04003466 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003467 portRspPut = le32_to_cpu(pgp->rspPutInx);
3468 }
James Smart7e56aa22012-08-03 12:35:34 -04003469 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003470
James Smart92d7f7b2007-06-17 19:56:38 -05003471 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003472 /* At least one response entry has been freed */
3473 pring->stats.iocb_rsp_full++;
3474 /* SET RxRE_RSP in Chip Att register */
3475 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3476 writel(status, phba->CAregaddr);
3477 readl(phba->CAregaddr); /* flush */
3478 }
3479 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3480 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3481 pring->stats.iocb_cmd_empty++;
3482
3483 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003484 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003485 lpfc_sli_resume_iocb(phba, pring);
3486
3487 if ((pring->lpfc_sli_cmd_available))
3488 (pring->lpfc_sli_cmd_available) (phba, pring);
3489
3490 }
3491
James Smart2e0fef82007-06-17 19:56:36 -05003492 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003493 return;
dea31012005-04-17 16:05:31 -05003494}
3495
James Smarte59058c2008-08-24 21:49:00 -04003496/**
James Smart4f774512009-05-22 14:52:35 -04003497 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3498 * @phba: Pointer to HBA context object.
3499 * @pring: Pointer to driver SLI ring object.
3500 * @mask: Host attention register mask for this ring.
3501 *
3502 * This function is called from the worker thread when there is a pending
3503 * ELS response iocb on the driver internal slow-path response iocb worker
3504 * queue. The caller does not hold any lock. The function will remove each
3505 * response iocb from the response worker queue and calls the handle
3506 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3507 **/
3508static void
3509lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3510 struct lpfc_sli_ring *pring, uint32_t mask)
3511{
3512 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003513 struct hbq_dmabuf *dmabuf;
3514 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003515 unsigned long iflag;
3516
James Smart45ed1192009-10-02 15:17:02 -04003517 spin_lock_irqsave(&phba->hbalock, iflag);
3518 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3519 spin_unlock_irqrestore(&phba->hbalock, iflag);
3520 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003521 /* Get the response iocb from the head of work queue */
3522 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003523 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003524 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003525 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003526
3527 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3528 case CQE_CODE_COMPL_WQE:
3529 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3530 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003531 /* Translate ELS WCQE to response IOCBQ */
3532 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3533 irspiocbq);
3534 if (irspiocbq)
3535 lpfc_sli_sp_handle_rspiocb(phba, pring,
3536 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003537 break;
3538 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003539 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003540 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3541 cq_event);
3542 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3543 break;
3544 default:
3545 break;
3546 }
James Smart4f774512009-05-22 14:52:35 -04003547 }
3548}
3549
3550/**
James Smart3621a712009-04-06 18:47:14 -04003551 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003552 * @phba: Pointer to HBA context object.
3553 * @pring: Pointer to driver SLI ring object.
3554 *
3555 * This function aborts all iocbs in the given ring and frees all the iocb
3556 * objects in txq. This function issues an abort iocb for all the iocb commands
3557 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3558 * the return of this function. The caller is not required to hold any locks.
3559 **/
James Smart2e0fef82007-06-17 19:56:36 -05003560void
dea31012005-04-17 16:05:31 -05003561lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3562{
James Smart2534ba72007-04-25 09:52:20 -04003563 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003564 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003565
James Smart92d7f7b2007-06-17 19:56:38 -05003566 if (pring->ringno == LPFC_ELS_RING) {
3567 lpfc_fabric_abort_hba(phba);
3568 }
3569
dea31012005-04-17 16:05:31 -05003570 /* Error everything on txq and txcmplq
3571 * First do the txq.
3572 */
James Smartdb55fba2014-04-04 13:52:02 -04003573 if (phba->sli_rev >= LPFC_SLI_REV4) {
3574 spin_lock_irq(&pring->ring_lock);
3575 list_splice_init(&pring->txq, &completions);
3576 pring->txq_cnt = 0;
3577 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003578
James Smartdb55fba2014-04-04 13:52:02 -04003579 spin_lock_irq(&phba->hbalock);
3580 /* Next issue ABTS for everything on the txcmplq */
3581 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3582 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3583 spin_unlock_irq(&phba->hbalock);
3584 } else {
3585 spin_lock_irq(&phba->hbalock);
3586 list_splice_init(&pring->txq, &completions);
3587 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003588
James Smartdb55fba2014-04-04 13:52:02 -04003589 /* Next issue ABTS for everything on the txcmplq */
3590 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3591 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3592 spin_unlock_irq(&phba->hbalock);
3593 }
James Smart2534ba72007-04-25 09:52:20 -04003594
James Smarta257bf92009-04-06 18:48:10 -04003595 /* Cancel all the IOCBs from the completions list */
3596 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3597 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003598}
3599
James Smarte59058c2008-08-24 21:49:00 -04003600/**
James Smartdb55fba2014-04-04 13:52:02 -04003601 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3602 * @phba: Pointer to HBA context object.
3603 * @pring: Pointer to driver SLI ring object.
3604 *
3605 * This function aborts all iocbs in FCP rings and frees all the iocb
3606 * objects in txq. This function issues an abort iocb for all the iocb commands
3607 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3608 * the return of this function. The caller is not required to hold any locks.
3609 **/
3610void
3611lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3612{
3613 struct lpfc_sli *psli = &phba->sli;
3614 struct lpfc_sli_ring *pring;
3615 uint32_t i;
3616
3617 /* Look on all the FCP Rings for the iotag */
3618 if (phba->sli_rev >= LPFC_SLI_REV4) {
3619 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3620 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3621 lpfc_sli_abort_iocb_ring(phba, pring);
3622 }
3623 } else {
3624 pring = &psli->ring[psli->fcp_ring];
3625 lpfc_sli_abort_iocb_ring(phba, pring);
3626 }
3627}
3628
3629
3630/**
James Smart3621a712009-04-06 18:47:14 -04003631 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003632 * @phba: Pointer to HBA context object.
3633 *
3634 * This function flushes all iocbs in the fcp ring and frees all the iocb
3635 * objects in txq and txcmplq. This function will not issue abort iocbs
3636 * for all the iocb commands in txcmplq, they will just be returned with
3637 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3638 * slot has been permanently disabled.
3639 **/
3640void
3641lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3642{
3643 LIST_HEAD(txq);
3644 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003645 struct lpfc_sli *psli = &phba->sli;
3646 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003647 uint32_t i;
James Smarta8e497d2008-08-24 21:50:11 -04003648
3649 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003650 /* Indicate the I/O queues are flushed */
3651 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003652 spin_unlock_irq(&phba->hbalock);
3653
James Smartdb55fba2014-04-04 13:52:02 -04003654 /* Look on all the FCP Rings for the iotag */
3655 if (phba->sli_rev >= LPFC_SLI_REV4) {
3656 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3657 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
James Smarta8e497d2008-08-24 21:50:11 -04003658
James Smartdb55fba2014-04-04 13:52:02 -04003659 spin_lock_irq(&pring->ring_lock);
3660 /* Retrieve everything on txq */
3661 list_splice_init(&pring->txq, &txq);
3662 /* Retrieve everything on the txcmplq */
3663 list_splice_init(&pring->txcmplq, &txcmplq);
3664 pring->txq_cnt = 0;
3665 pring->txcmplq_cnt = 0;
3666 spin_unlock_irq(&pring->ring_lock);
3667
3668 /* Flush the txq */
3669 lpfc_sli_cancel_iocbs(phba, &txq,
3670 IOSTAT_LOCAL_REJECT,
3671 IOERR_SLI_DOWN);
3672 /* Flush the txcmpq */
3673 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3674 IOSTAT_LOCAL_REJECT,
3675 IOERR_SLI_DOWN);
3676 }
3677 } else {
3678 pring = &psli->ring[psli->fcp_ring];
3679
3680 spin_lock_irq(&phba->hbalock);
3681 /* Retrieve everything on txq */
3682 list_splice_init(&pring->txq, &txq);
3683 /* Retrieve everything on the txcmplq */
3684 list_splice_init(&pring->txcmplq, &txcmplq);
3685 pring->txq_cnt = 0;
3686 pring->txcmplq_cnt = 0;
3687 spin_unlock_irq(&phba->hbalock);
3688
3689 /* Flush the txq */
3690 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3691 IOERR_SLI_DOWN);
3692 /* Flush the txcmpq */
3693 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3694 IOERR_SLI_DOWN);
3695 }
James Smarta8e497d2008-08-24 21:50:11 -04003696}
3697
3698/**
James Smart3772a992009-05-22 14:50:54 -04003699 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003700 * @phba: Pointer to HBA context object.
3701 * @mask: Bit mask to be checked.
3702 *
3703 * This function reads the host status register and compares
3704 * with the provided bit mask to check if HBA completed
3705 * the restart. This function will wait in a loop for the
3706 * HBA to complete restart. If the HBA does not restart within
3707 * 15 iterations, the function will reset the HBA again. The
3708 * function returns 1 when HBA fail to restart otherwise returns
3709 * zero.
3710 **/
James Smart3772a992009-05-22 14:50:54 -04003711static int
3712lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003713{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003714 uint32_t status;
3715 int i = 0;
3716 int retval = 0;
dea31012005-04-17 16:05:31 -05003717
Jamie Wellnitz41415862006-02-28 19:25:27 -05003718 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003719 if (lpfc_readl(phba->HSregaddr, &status))
3720 return 1;
dea31012005-04-17 16:05:31 -05003721
Jamie Wellnitz41415862006-02-28 19:25:27 -05003722 /*
3723 * Check status register every 100ms for 5 retries, then every
3724 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3725 * every 2.5 sec for 4.
3726 * Break our of the loop if errors occurred during init.
3727 */
3728 while (((status & mask) != mask) &&
3729 !(status & HS_FFERM) &&
3730 i++ < 20) {
dea31012005-04-17 16:05:31 -05003731
Jamie Wellnitz41415862006-02-28 19:25:27 -05003732 if (i <= 5)
3733 msleep(10);
3734 else if (i <= 10)
3735 msleep(500);
3736 else
3737 msleep(2500);
dea31012005-04-17 16:05:31 -05003738
Jamie Wellnitz41415862006-02-28 19:25:27 -05003739 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003740 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003741 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003742 lpfc_sli_brdrestart(phba);
3743 }
3744 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003745 if (lpfc_readl(phba->HSregaddr, &status)) {
3746 retval = 1;
3747 break;
3748 }
dea31012005-04-17 16:05:31 -05003749 }
dea31012005-04-17 16:05:31 -05003750
Jamie Wellnitz41415862006-02-28 19:25:27 -05003751 /* Check to see if any errors occurred during init */
3752 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003753 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3754 "2751 Adapter failed to restart, "
3755 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3756 status,
3757 readl(phba->MBslimaddr + 0xa8),
3758 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003759 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003760 retval = 1;
3761 }
dea31012005-04-17 16:05:31 -05003762
Jamie Wellnitz41415862006-02-28 19:25:27 -05003763 return retval;
dea31012005-04-17 16:05:31 -05003764}
3765
James Smartda0436e2009-05-22 14:51:39 -04003766/**
3767 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3768 * @phba: Pointer to HBA context object.
3769 * @mask: Bit mask to be checked.
3770 *
3771 * This function checks the host status register to check if HBA is
3772 * ready. This function will wait in a loop for the HBA to be ready
3773 * If the HBA is not ready , the function will will reset the HBA PCI
3774 * function again. The function returns 1 when HBA fail to be ready
3775 * otherwise returns zero.
3776 **/
3777static int
3778lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3779{
3780 uint32_t status;
3781 int retval = 0;
3782
3783 /* Read the HBA Host Status Register */
3784 status = lpfc_sli4_post_status_check(phba);
3785
3786 if (status) {
3787 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3788 lpfc_sli_brdrestart(phba);
3789 status = lpfc_sli4_post_status_check(phba);
3790 }
3791
3792 /* Check to see if any errors occurred during init */
3793 if (status) {
3794 phba->link_state = LPFC_HBA_ERROR;
3795 retval = 1;
3796 } else
3797 phba->sli4_hba.intr_enable = 0;
3798
3799 return retval;
3800}
3801
3802/**
3803 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3804 * @phba: Pointer to HBA context object.
3805 * @mask: Bit mask to be checked.
3806 *
3807 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3808 * from the API jump table function pointer from the lpfc_hba struct.
3809 **/
3810int
3811lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3812{
3813 return phba->lpfc_sli_brdready(phba, mask);
3814}
3815
James Smart92908312006-03-07 15:04:13 -05003816#define BARRIER_TEST_PATTERN (0xdeadbeef)
3817
James Smarte59058c2008-08-24 21:49:00 -04003818/**
James Smart3621a712009-04-06 18:47:14 -04003819 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003820 * @phba: Pointer to HBA context object.
3821 *
James Smart1b511972011-12-13 13:23:09 -05003822 * This function is called before resetting an HBA. This function is called
3823 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003824 **/
James Smart2e0fef82007-06-17 19:56:36 -05003825void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003826{
James Smart65a29c12006-07-06 15:50:50 -04003827 uint32_t __iomem *resp_buf;
3828 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003829 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003830 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003831 int i;
3832 uint8_t hdrtype;
3833
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003834 lockdep_assert_held(&phba->hbalock);
3835
James Smart92908312006-03-07 15:04:13 -05003836 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3837 if (hdrtype != 0x80 ||
3838 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3839 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3840 return;
3841
3842 /*
3843 * Tell the other part of the chip to suspend temporarily all
3844 * its DMA activity.
3845 */
James Smart65a29c12006-07-06 15:50:50 -04003846 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003847
3848 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003849 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3850 return;
James Smart92908312006-03-07 15:04:13 -05003851 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3852 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003853 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003854
James Smart9940b972011-03-11 16:06:12 -05003855 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3856 return;
3857 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003858 /* Clear Chip error bit */
3859 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003860 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003861 }
3862
3863 mbox = 0;
3864 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3865 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3866
3867 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003868 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003869 writel(mbox, mbox_buf);
3870
James Smart9940b972011-03-11 16:06:12 -05003871 for (i = 0; i < 50; i++) {
3872 if (lpfc_readl((resp_buf + 1), &resp_data))
3873 return;
3874 if (resp_data != ~(BARRIER_TEST_PATTERN))
3875 mdelay(1);
3876 else
3877 break;
3878 }
3879 resp_data = 0;
3880 if (lpfc_readl((resp_buf + 1), &resp_data))
3881 return;
3882 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003883 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003884 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003885 goto restore_hc;
3886 else
3887 goto clear_errat;
3888 }
3889
3890 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003891 resp_data = 0;
3892 for (i = 0; i < 500; i++) {
3893 if (lpfc_readl(resp_buf, &resp_data))
3894 return;
3895 if (resp_data != mbox)
3896 mdelay(1);
3897 else
3898 break;
3899 }
James Smart92908312006-03-07 15:04:13 -05003900
3901clear_errat:
3902
James Smart9940b972011-03-11 16:06:12 -05003903 while (++i < 500) {
3904 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3905 return;
3906 if (!(ha_copy & HA_ERATT))
3907 mdelay(1);
3908 else
3909 break;
3910 }
James Smart92908312006-03-07 15:04:13 -05003911
3912 if (readl(phba->HAregaddr) & HA_ERATT) {
3913 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003914 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003915 }
3916
3917restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003918 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003919 writel(hc_copy, phba->HCregaddr);
3920 readl(phba->HCregaddr); /* flush */
3921}
3922
James Smarte59058c2008-08-24 21:49:00 -04003923/**
James Smart3621a712009-04-06 18:47:14 -04003924 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003925 * @phba: Pointer to HBA context object.
3926 *
3927 * This function issues a kill_board mailbox command and waits for
3928 * the error attention interrupt. This function is called for stopping
3929 * the firmware processing. The caller is not required to hold any
3930 * locks. This function calls lpfc_hba_down_post function to free
3931 * any pending commands after the kill. The function will return 1 when it
3932 * fails to kill the board else will return 0.
3933 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003934int
James Smart2e0fef82007-06-17 19:56:36 -05003935lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003936{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003937 struct lpfc_sli *psli;
3938 LPFC_MBOXQ_t *pmb;
3939 uint32_t status;
3940 uint32_t ha_copy;
3941 int retval;
3942 int i = 0;
3943
3944 psli = &phba->sli;
3945
3946 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003947 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003948 "0329 Kill HBA Data: x%x x%x\n",
3949 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003950
James Smart98c9ea52007-10-27 13:37:33 -04003951 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3952 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003953 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003954
3955 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003956 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003957 if (lpfc_readl(phba->HCregaddr, &status)) {
3958 spin_unlock_irq(&phba->hbalock);
3959 mempool_free(pmb, phba->mbox_mem_pool);
3960 return 1;
3961 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003962 status &= ~HC_ERINT_ENA;
3963 writel(status, phba->HCregaddr);
3964 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003965 phba->link_flag |= LS_IGNORE_ERATT;
3966 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003967
3968 lpfc_kill_board(phba, pmb);
3969 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3970 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3971
3972 if (retval != MBX_SUCCESS) {
3973 if (retval != MBX_BUSY)
3974 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003975 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3976 "2752 KILL_BOARD command failed retval %d\n",
3977 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003978 spin_lock_irq(&phba->hbalock);
3979 phba->link_flag &= ~LS_IGNORE_ERATT;
3980 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003981 return 1;
3982 }
3983
James Smartf4b4c682009-05-22 14:53:12 -04003984 spin_lock_irq(&phba->hbalock);
3985 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3986 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003987
Jamie Wellnitz41415862006-02-28 19:25:27 -05003988 mempool_free(pmb, phba->mbox_mem_pool);
3989
3990 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3991 * attention every 100ms for 3 seconds. If we don't get ERATT after
3992 * 3 seconds we still set HBA_ERROR state because the status of the
3993 * board is now undefined.
3994 */
James Smart9940b972011-03-11 16:06:12 -05003995 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3996 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003997 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3998 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003999 if (lpfc_readl(phba->HAregaddr, &ha_copy))
4000 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004001 }
4002
4003 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004004 if (ha_copy & HA_ERATT) {
4005 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004006 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004007 }
James Smart2e0fef82007-06-17 19:56:36 -05004008 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004009 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004010 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004011 phba->link_flag &= ~LS_IGNORE_ERATT;
4012 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004013
Jamie Wellnitz41415862006-02-28 19:25:27 -05004014 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004015 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004016
James Smart2e0fef82007-06-17 19:56:36 -05004017 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004018}
4019
James Smarte59058c2008-08-24 21:49:00 -04004020/**
James Smart3772a992009-05-22 14:50:54 -04004021 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004022 * @phba: Pointer to HBA context object.
4023 *
4024 * This function resets the HBA by writing HC_INITFF to the control
4025 * register. After the HBA resets, this function resets all the iocb ring
4026 * indices. This function disables PCI layer parity checking during
4027 * the reset.
4028 * This function returns 0 always.
4029 * The caller is not required to hold any locks.
4030 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004031int
James Smart2e0fef82007-06-17 19:56:36 -05004032lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004033{
4034 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004035 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004036 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004037 int i;
dea31012005-04-17 16:05:31 -05004038
Jamie Wellnitz41415862006-02-28 19:25:27 -05004039 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004040
Jamie Wellnitz41415862006-02-28 19:25:27 -05004041 /* Reset HBA */
4042 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004043 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004044 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05004045
4046 /* perform board reset */
4047 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004048 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004049 phba->pport->fc_myDID = 0;
4050 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05004051
Jamie Wellnitz41415862006-02-28 19:25:27 -05004052 /* Turn off parity checking and serr during the physical reset */
4053 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4054 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4055 (cfg_value &
4056 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4057
James Smart3772a992009-05-22 14:50:54 -04004058 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4059
Jamie Wellnitz41415862006-02-28 19:25:27 -05004060 /* Now toggle INITFF bit in the Host Control Register */
4061 writel(HC_INITFF, phba->HCregaddr);
4062 mdelay(1);
4063 readl(phba->HCregaddr); /* flush */
4064 writel(0, phba->HCregaddr);
4065 readl(phba->HCregaddr); /* flush */
4066
4067 /* Restore PCI cmd register */
4068 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004069
4070 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004071 for (i = 0; i < psli->num_rings; i++) {
4072 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05004073 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004074 pring->sli.sli3.rspidx = 0;
4075 pring->sli.sli3.next_cmdidx = 0;
4076 pring->sli.sli3.local_getidx = 0;
4077 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004078 pring->missbufcnt = 0;
4079 }
dea31012005-04-17 16:05:31 -05004080
James Smart2e0fef82007-06-17 19:56:36 -05004081 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004082 return 0;
4083}
4084
James Smarte59058c2008-08-24 21:49:00 -04004085/**
James Smartda0436e2009-05-22 14:51:39 -04004086 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4087 * @phba: Pointer to HBA context object.
4088 *
4089 * This function resets a SLI4 HBA. This function disables PCI layer parity
4090 * checking during resets the device. The caller is not required to hold
4091 * any locks.
4092 *
4093 * This function returns 0 always.
4094 **/
4095int
4096lpfc_sli4_brdreset(struct lpfc_hba *phba)
4097{
4098 struct lpfc_sli *psli = &phba->sli;
4099 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004100 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004101
4102 /* Reset HBA */
4103 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004104 "0295 Reset HBA Data: x%x x%x x%x\n",
4105 phba->pport->port_state, psli->sli_flag,
4106 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004107
4108 /* perform board reset */
4109 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004110 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004111 phba->pport->fc_myDID = 0;
4112 phba->pport->fc_prevDID = 0;
4113
James Smartda0436e2009-05-22 14:51:39 -04004114 spin_lock_irq(&phba->hbalock);
4115 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4116 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004117 spin_unlock_irq(&phba->hbalock);
4118
James Smart02936352014-04-04 13:52:12 -04004119 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4120 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4121 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4122 return rc;
4123 }
4124
James Smartda0436e2009-05-22 14:51:39 -04004125 /* Now physically reset the device */
4126 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4127 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004128
4129 /* Turn off parity checking and serr during the physical reset */
4130 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4131 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4132 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4133
James Smart88318812012-09-29 11:29:29 -04004134 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004135 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04004136 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004137
James Smartbe858b62010-12-15 17:57:20 -05004138 /* Restore PCI cmd register */
4139 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4140
James Smart27b01b82012-05-09 21:19:44 -04004141 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004142}
4143
4144/**
4145 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004146 * @phba: Pointer to HBA context object.
4147 *
4148 * This function is called in the SLI initialization code path to
4149 * restart the HBA. The caller is not required to hold any lock.
4150 * This function writes MBX_RESTART mailbox command to the SLIM and
4151 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4152 * function to free any pending commands. The function enables
4153 * POST only during the first initialization. The function returns zero.
4154 * The function does not guarantee completion of MBX_RESTART mailbox
4155 * command before the return of this function.
4156 **/
James Smartda0436e2009-05-22 14:51:39 -04004157static int
4158lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004159{
4160 MAILBOX_t *mb;
4161 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004162 volatile uint32_t word0;
4163 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004164 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004165
James Smart2e0fef82007-06-17 19:56:36 -05004166 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004167
James Smart0d878412009-10-02 15:16:56 -04004168 /* Take PCIe device Advanced Error Reporting (AER) state */
4169 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4170
Jamie Wellnitz41415862006-02-28 19:25:27 -05004171 psli = &phba->sli;
4172
4173 /* Restart HBA */
4174 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004175 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004176 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004177
4178 word0 = 0;
4179 mb = (MAILBOX_t *) &word0;
4180 mb->mbxCommand = MBX_RESTART;
4181 mb->mbxHc = 1;
4182
James Smart92908312006-03-07 15:04:13 -05004183 lpfc_reset_barrier(phba);
4184
Jamie Wellnitz41415862006-02-28 19:25:27 -05004185 to_slim = phba->MBslimaddr;
4186 writel(*(uint32_t *) mb, to_slim);
4187 readl(to_slim); /* flush */
4188
4189 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004190 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004191 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004192 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004193 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004194 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004195 writel(*(uint32_t *) mb, to_slim);
4196 readl(to_slim); /* flush */
4197
4198 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004199 phba->pport->stopped = 0;
4200 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004201 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004202 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004203
James Smart64ba8812006-08-02 15:24:34 -04004204 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4205 psli->stats_start = get_seconds();
4206
James Smarteaf15d52008-12-04 22:39:29 -05004207 /* Give the INITFF and Post time to settle. */
4208 mdelay(100);
dea31012005-04-17 16:05:31 -05004209
James Smart0d878412009-10-02 15:16:56 -04004210 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4211 if (hba_aer_enabled)
4212 pci_disable_pcie_error_reporting(phba->pcidev);
4213
Jamie Wellnitz41415862006-02-28 19:25:27 -05004214 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004215
4216 return 0;
4217}
4218
James Smarte59058c2008-08-24 21:49:00 -04004219/**
James Smartda0436e2009-05-22 14:51:39 -04004220 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4221 * @phba: Pointer to HBA context object.
4222 *
4223 * This function is called in the SLI initialization code path to restart
4224 * a SLI4 HBA. The caller is not required to hold any lock.
4225 * At the end of the function, it calls lpfc_hba_down_post function to
4226 * free any pending commands.
4227 **/
4228static int
4229lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4230{
4231 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004232 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004233 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004234
4235 /* Restart HBA */
4236 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4237 "0296 Restart HBA Data: x%x x%x\n",
4238 phba->pport->port_state, psli->sli_flag);
4239
James Smart75baf692010-06-08 18:31:21 -04004240 /* Take PCIe device Advanced Error Reporting (AER) state */
4241 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4242
James Smart27b01b82012-05-09 21:19:44 -04004243 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004244
4245 spin_lock_irq(&phba->hbalock);
4246 phba->pport->stopped = 0;
4247 phba->link_state = LPFC_INIT_START;
4248 phba->hba_flag = 0;
4249 spin_unlock_irq(&phba->hbalock);
4250
4251 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4252 psli->stats_start = get_seconds();
4253
James Smart75baf692010-06-08 18:31:21 -04004254 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4255 if (hba_aer_enabled)
4256 pci_disable_pcie_error_reporting(phba->pcidev);
4257
James Smartda0436e2009-05-22 14:51:39 -04004258 lpfc_hba_down_post(phba);
4259
James Smart27b01b82012-05-09 21:19:44 -04004260 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004261}
4262
4263/**
4264 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4265 * @phba: Pointer to HBA context object.
4266 *
4267 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4268 * API jump table function pointer from the lpfc_hba struct.
4269**/
4270int
4271lpfc_sli_brdrestart(struct lpfc_hba *phba)
4272{
4273 return phba->lpfc_sli_brdrestart(phba);
4274}
4275
4276/**
James Smart3621a712009-04-06 18:47:14 -04004277 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004278 * @phba: Pointer to HBA context object.
4279 *
4280 * This function is called after a HBA restart to wait for successful
4281 * restart of the HBA. Successful restart of the HBA is indicated by
4282 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4283 * iteration, the function will restart the HBA again. The function returns
4284 * zero if HBA successfully restarted else returns negative error code.
4285 **/
dea31012005-04-17 16:05:31 -05004286static int
4287lpfc_sli_chipset_init(struct lpfc_hba *phba)
4288{
4289 uint32_t status, i = 0;
4290
4291 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004292 if (lpfc_readl(phba->HSregaddr, &status))
4293 return -EIO;
dea31012005-04-17 16:05:31 -05004294
4295 /* Check status register to see what current state is */
4296 i = 0;
4297 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4298
James Smartdcf2a4e2010-09-29 11:18:53 -04004299 /* Check every 10ms for 10 retries, then every 100ms for 90
4300 * retries, then every 1 sec for 50 retires for a total of
4301 * ~60 seconds before reset the board again and check every
4302 * 1 sec for 50 retries. The up to 60 seconds before the
4303 * board ready is required by the Falcon FIPS zeroization
4304 * complete, and any reset the board in between shall cause
4305 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004306 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004307 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004308 /* Adapter failed to init, timeout, status reg
4309 <status> */
James Smarted957682007-06-17 19:56:37 -05004310 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004311 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004312 "timeout, status reg x%x, "
4313 "FW Data: A8 x%x AC x%x\n", status,
4314 readl(phba->MBslimaddr + 0xa8),
4315 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004316 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004317 return -ETIMEDOUT;
4318 }
4319
4320 /* Check to see if any errors occurred during init */
4321 if (status & HS_FFERM) {
4322 /* ERROR: During chipset initialization */
4323 /* Adapter failed to init, chipset, status reg
4324 <status> */
James Smarted957682007-06-17 19:56:37 -05004325 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004326 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004327 "chipset, status reg x%x, "
4328 "FW Data: A8 x%x AC x%x\n", status,
4329 readl(phba->MBslimaddr + 0xa8),
4330 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004331 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004332 return -EIO;
4333 }
4334
James Smartdcf2a4e2010-09-29 11:18:53 -04004335 if (i <= 10)
dea31012005-04-17 16:05:31 -05004336 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004337 else if (i <= 100)
4338 msleep(100);
4339 else
4340 msleep(1000);
dea31012005-04-17 16:05:31 -05004341
James Smartdcf2a4e2010-09-29 11:18:53 -04004342 if (i == 150) {
4343 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004344 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004345 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004346 }
4347 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004348 if (lpfc_readl(phba->HSregaddr, &status))
4349 return -EIO;
dea31012005-04-17 16:05:31 -05004350 }
4351
4352 /* Check to see if any errors occurred during init */
4353 if (status & HS_FFERM) {
4354 /* ERROR: During chipset initialization */
4355 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004357 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004358 "status reg x%x, "
4359 "FW Data: A8 x%x AC x%x\n", status,
4360 readl(phba->MBslimaddr + 0xa8),
4361 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004362 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004363 return -EIO;
4364 }
4365
4366 /* Clear all interrupt enable conditions */
4367 writel(0, phba->HCregaddr);
4368 readl(phba->HCregaddr); /* flush */
4369
4370 /* setup host attn register */
4371 writel(0xffffffff, phba->HAregaddr);
4372 readl(phba->HAregaddr); /* flush */
4373 return 0;
4374}
4375
James Smarte59058c2008-08-24 21:49:00 -04004376/**
James Smart3621a712009-04-06 18:47:14 -04004377 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004378 *
4379 * This function calculates and returns the number of HBQs required to be
4380 * configured.
4381 **/
James Smart78b2d852007-08-02 11:10:21 -04004382int
James Smarted957682007-06-17 19:56:37 -05004383lpfc_sli_hbq_count(void)
4384{
James Smart92d7f7b2007-06-17 19:56:38 -05004385 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004386}
4387
James Smarte59058c2008-08-24 21:49:00 -04004388/**
James Smart3621a712009-04-06 18:47:14 -04004389 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004390 *
4391 * This function adds the number of hbq entries in every HBQ to get
4392 * the total number of hbq entries required for the HBA and returns
4393 * the total count.
4394 **/
James Smarted957682007-06-17 19:56:37 -05004395static int
4396lpfc_sli_hbq_entry_count(void)
4397{
4398 int hbq_count = lpfc_sli_hbq_count();
4399 int count = 0;
4400 int i;
4401
4402 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004403 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004404 return count;
4405}
4406
James Smarte59058c2008-08-24 21:49:00 -04004407/**
James Smart3621a712009-04-06 18:47:14 -04004408 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004409 *
4410 * This function calculates amount of memory required for all hbq entries
4411 * to be configured and returns the total memory required.
4412 **/
dea31012005-04-17 16:05:31 -05004413int
James Smarted957682007-06-17 19:56:37 -05004414lpfc_sli_hbq_size(void)
4415{
4416 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4417}
4418
James Smarte59058c2008-08-24 21:49:00 -04004419/**
James Smart3621a712009-04-06 18:47:14 -04004420 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004421 * @phba: Pointer to HBA context object.
4422 *
4423 * This function is called during the SLI initialization to configure
4424 * all the HBQs and post buffers to the HBQ. The caller is not
4425 * required to hold any locks. This function will return zero if successful
4426 * else it will return negative error code.
4427 **/
James Smarted957682007-06-17 19:56:37 -05004428static int
4429lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4430{
4431 int hbq_count = lpfc_sli_hbq_count();
4432 LPFC_MBOXQ_t *pmb;
4433 MAILBOX_t *pmbox;
4434 uint32_t hbqno;
4435 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004436
James Smart92d7f7b2007-06-17 19:56:38 -05004437 /* Get a Mailbox buffer to setup mailbox
4438 * commands for HBA initialization
4439 */
James Smarted957682007-06-17 19:56:37 -05004440 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4441
4442 if (!pmb)
4443 return -ENOMEM;
4444
James Smart04c68492009-05-22 14:52:52 -04004445 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004446
4447 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4448 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004449 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004450
4451 hbq_entry_index = 0;
4452 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4453 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4454 phba->hbqs[hbqno].hbqPutIdx = 0;
4455 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4456 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004457 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004458 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4459 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004460 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4461
4462 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4463 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4464 mbxStatus <status>, ring <num> */
4465
4466 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004467 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004468 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004469 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004470 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004471 pmbox->mbxStatus, hbqno);
4472
4473 phba->link_state = LPFC_HBA_ERROR;
4474 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004475 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004476 }
4477 }
4478 phba->hbq_count = hbq_count;
4479
James Smarted957682007-06-17 19:56:37 -05004480 mempool_free(pmb, phba->mbox_mem_pool);
4481
James Smart92d7f7b2007-06-17 19:56:38 -05004482 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004483 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4484 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004485 return 0;
4486}
4487
James Smarte59058c2008-08-24 21:49:00 -04004488/**
James Smart4f774512009-05-22 14:52:35 -04004489 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4490 * @phba: Pointer to HBA context object.
4491 *
4492 * This function is called during the SLI initialization to configure
4493 * all the HBQs and post buffers to the HBQ. The caller is not
4494 * required to hold any locks. This function will return zero if successful
4495 * else it will return negative error code.
4496 **/
4497static int
4498lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4499{
4500 phba->hbq_in_use = 1;
4501 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4502 phba->hbq_count = 1;
4503 /* Initially populate or replenish the HBQs */
4504 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4505 return 0;
4506}
4507
4508/**
James Smart3621a712009-04-06 18:47:14 -04004509 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004510 * @phba: Pointer to HBA context object.
4511 * @sli_mode: sli mode - 2/3
4512 *
4513 * This function is called by the sli intialization code path
4514 * to issue config_port mailbox command. This function restarts the
4515 * HBA firmware and issues a config_port mailbox command to configure
4516 * the SLI interface in the sli mode specified by sli_mode
4517 * variable. The caller is not required to hold any locks.
4518 * The function returns 0 if successful, else returns negative error
4519 * code.
4520 **/
James Smart93996272008-08-24 21:50:30 -04004521int
4522lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004523{
4524 LPFC_MBOXQ_t *pmb;
4525 uint32_t resetcount = 0, rc = 0, done = 0;
4526
4527 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4528 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004529 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004530 return -ENOMEM;
4531 }
4532
James Smarted957682007-06-17 19:56:37 -05004533 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004534 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004535 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004536 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004537 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004538 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004539 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004540 rc = lpfc_sli_chipset_init(phba);
4541 if (rc)
4542 break;
4543
James Smart2e0fef82007-06-17 19:56:36 -05004544 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004545 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004546 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004547 resetcount++;
4548
James Smarted957682007-06-17 19:56:37 -05004549 /* Call pre CONFIG_PORT mailbox command initialization. A
4550 * value of 0 means the call was successful. Any other
4551 * nonzero value is a failure, but if ERESTART is returned,
4552 * the driver may reset the HBA and try again.
4553 */
dea31012005-04-17 16:05:31 -05004554 rc = lpfc_config_port_prep(phba);
4555 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004556 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004557 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004558 } else if (rc)
dea31012005-04-17 16:05:31 -05004559 break;
James Smart6d368e52011-05-24 11:44:12 -04004560
James Smart2e0fef82007-06-17 19:56:36 -05004561 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004562 lpfc_config_port(phba, pmb);
4563 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004564 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4565 LPFC_SLI3_HBQ_ENABLED |
4566 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004567 LPFC_SLI3_BG_ENABLED |
4568 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004569 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004570 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004571 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004572 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004573 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004574 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004575 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004576 spin_unlock_irq(&phba->hbalock);
4577 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004578 } else {
4579 /* Allow asynchronous mailbox command to go through */
4580 spin_lock_irq(&phba->hbalock);
4581 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4582 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004583 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004584
4585 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4586 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4587 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4588 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004589 }
dea31012005-04-17 16:05:31 -05004590 }
James Smarted957682007-06-17 19:56:37 -05004591 if (!done) {
4592 rc = -EINVAL;
4593 goto do_prep_failed;
4594 }
James Smart04c68492009-05-22 14:52:52 -04004595 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4596 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004597 rc = -ENXIO;
4598 goto do_prep_failed;
4599 }
James Smart04c68492009-05-22 14:52:52 -04004600 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004601 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004602 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4603 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4604 phba->max_vpi : phba->max_vports;
4605
James Smart34b02dc2008-08-24 21:49:55 -04004606 } else
4607 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004608 phba->fips_level = 0;
4609 phba->fips_spec_rev = 0;
4610 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004611 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004612 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4613 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4614 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4615 "2850 Security Crypto Active. FIPS x%d "
4616 "(Spec Rev: x%d)",
4617 phba->fips_level, phba->fips_spec_rev);
4618 }
4619 if (pmb->u.mb.un.varCfgPort.sec_err) {
4620 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4621 "2856 Config Port Security Crypto "
4622 "Error: x%x ",
4623 pmb->u.mb.un.varCfgPort.sec_err);
4624 }
James Smart04c68492009-05-22 14:52:52 -04004625 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004626 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004627 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004628 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004629
4630 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4631 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004632
4633 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004634 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004635 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4636 else
4637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4638 "0443 Adapter did not grant "
4639 "BlockGuard\n");
4640 }
James Smart34b02dc2008-08-24 21:49:55 -04004641 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004642 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004643 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004644 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004645 }
James Smart92d7f7b2007-06-17 19:56:38 -05004646do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004647 mempool_free(pmb, phba->mbox_mem_pool);
4648 return rc;
4649}
4650
James Smarte59058c2008-08-24 21:49:00 -04004651
4652/**
James Smart3621a712009-04-06 18:47:14 -04004653 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004654 * @phba: Pointer to HBA context object.
4655 *
4656 * This function is the main SLI intialization function. This function
4657 * is called by the HBA intialization code, HBA reset code and HBA
4658 * error attention handler code. Caller is not required to hold any
4659 * locks. This function issues config_port mailbox command to configure
4660 * the SLI, setup iocb rings and HBQ rings. In the end the function
4661 * calls the config_port_post function to issue init_link mailbox
4662 * command and to start the discovery. The function will return zero
4663 * if successful, else it will return negative error code.
4664 **/
James Smarted957682007-06-17 19:56:37 -05004665int
4666lpfc_sli_hba_setup(struct lpfc_hba *phba)
4667{
4668 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004669 int mode = 3, i;
4670 int longs;
James Smarted957682007-06-17 19:56:37 -05004671
James Smart12247e82016-07-06 12:36:09 -07004672 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05004673 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004674 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004675 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004676 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004677 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07004678 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004679 break;
4680 }
James Smarted957682007-06-17 19:56:37 -05004681 mode = 2;
4682 break;
4683 case 0:
4684 case 3:
4685 break;
4686 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004687 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004688 "1819 Unrecognized sli_mode parameter: %d.\n",
4689 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004690
4691 break;
4692 }
James Smartb5c53952016-03-31 14:12:30 -07004693 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004694
James Smart93996272008-08-24 21:50:30 -04004695 rc = lpfc_sli_config_port(phba, mode);
4696
James Smart12247e82016-07-06 12:36:09 -07004697 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004698 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004699 "1820 Unable to select SLI-3. "
4700 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004701 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004702 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07004703 else if (rc && mode == 2)
4704 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05004705 if (rc)
dea31012005-04-17 16:05:31 -05004706 goto lpfc_sli_hba_setup_error;
4707
James Smart0d878412009-10-02 15:16:56 -04004708 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4709 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4710 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4711 if (!rc) {
4712 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4713 "2709 This device supports "
4714 "Advanced Error Reporting (AER)\n");
4715 spin_lock_irq(&phba->hbalock);
4716 phba->hba_flag |= HBA_AER_ENABLED;
4717 spin_unlock_irq(&phba->hbalock);
4718 } else {
4719 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4720 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004721 "Advanced Error Reporting (AER): %d\n",
4722 rc);
James Smart0d878412009-10-02 15:16:56 -04004723 phba->cfg_aer_support = 0;
4724 }
4725 }
4726
James Smarted957682007-06-17 19:56:37 -05004727 if (phba->sli_rev == 3) {
4728 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4729 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004730 } else {
4731 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4732 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004733 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004734 }
4735
4736 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004737 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4738 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004739 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004740
4741 if (rc)
4742 goto lpfc_sli_hba_setup_error;
4743
James Smart6d368e52011-05-24 11:44:12 -04004744 /* Initialize VPIs. */
4745 if (phba->sli_rev == LPFC_SLI_REV3) {
4746 /*
4747 * The VPI bitmask and physical ID array are allocated
4748 * and initialized once only - at driver load. A port
4749 * reset doesn't need to reinitialize this memory.
4750 */
4751 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4752 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4753 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4754 GFP_KERNEL);
4755 if (!phba->vpi_bmask) {
4756 rc = -ENOMEM;
4757 goto lpfc_sli_hba_setup_error;
4758 }
4759
4760 phba->vpi_ids = kzalloc(
4761 (phba->max_vpi+1) * sizeof(uint16_t),
4762 GFP_KERNEL);
4763 if (!phba->vpi_ids) {
4764 kfree(phba->vpi_bmask);
4765 rc = -ENOMEM;
4766 goto lpfc_sli_hba_setup_error;
4767 }
4768 for (i = 0; i < phba->max_vpi; i++)
4769 phba->vpi_ids[i] = i;
4770 }
4771 }
4772
James Smart93996272008-08-24 21:50:30 -04004773 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004774 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4775 rc = lpfc_sli_hbq_setup(phba);
4776 if (rc)
4777 goto lpfc_sli_hba_setup_error;
4778 }
James Smart04c68492009-05-22 14:52:52 -04004779 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004780 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004781 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004782
4783 rc = lpfc_config_port_post(phba);
4784 if (rc)
4785 goto lpfc_sli_hba_setup_error;
4786
James Smarted957682007-06-17 19:56:37 -05004787 return rc;
4788
James Smart92d7f7b2007-06-17 19:56:38 -05004789lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004790 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004792 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004793 return rc;
4794}
4795
James Smartda0436e2009-05-22 14:51:39 -04004796/**
4797 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4798 * @phba: Pointer to HBA context object.
4799 * @mboxq: mailbox pointer.
4800 * This function issue a dump mailbox command to read config region
4801 * 23 and parse the records in the region and populate driver
4802 * data structure.
4803 **/
4804static int
James Smartff78d8f2011-12-13 13:21:35 -05004805lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004806{
James Smartff78d8f2011-12-13 13:21:35 -05004807 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004808 struct lpfc_dmabuf *mp;
4809 struct lpfc_mqe *mqe;
4810 uint32_t data_length;
4811 int rc;
4812
4813 /* Program the default value of vlan_id and fc_map */
4814 phba->valid_vlan = 0;
4815 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4816 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4817 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4818
James Smartff78d8f2011-12-13 13:21:35 -05004819 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4820 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004821 return -ENOMEM;
4822
James Smartff78d8f2011-12-13 13:21:35 -05004823 mqe = &mboxq->u.mqe;
4824 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4825 rc = -ENOMEM;
4826 goto out_free_mboxq;
4827 }
4828
James Smartda0436e2009-05-22 14:51:39 -04004829 mp = (struct lpfc_dmabuf *) mboxq->context1;
4830 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4831
4832 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4833 "(%d):2571 Mailbox cmd x%x Status x%x "
4834 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4835 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4836 "CQ: x%x x%x x%x x%x\n",
4837 mboxq->vport ? mboxq->vport->vpi : 0,
4838 bf_get(lpfc_mqe_command, mqe),
4839 bf_get(lpfc_mqe_status, mqe),
4840 mqe->un.mb_words[0], mqe->un.mb_words[1],
4841 mqe->un.mb_words[2], mqe->un.mb_words[3],
4842 mqe->un.mb_words[4], mqe->un.mb_words[5],
4843 mqe->un.mb_words[6], mqe->un.mb_words[7],
4844 mqe->un.mb_words[8], mqe->un.mb_words[9],
4845 mqe->un.mb_words[10], mqe->un.mb_words[11],
4846 mqe->un.mb_words[12], mqe->un.mb_words[13],
4847 mqe->un.mb_words[14], mqe->un.mb_words[15],
4848 mqe->un.mb_words[16], mqe->un.mb_words[50],
4849 mboxq->mcqe.word0,
4850 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4851 mboxq->mcqe.trailer);
4852
4853 if (rc) {
4854 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4855 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004856 rc = -EIO;
4857 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004858 }
4859 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004860 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004861 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4862 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004863 rc = -EIO;
4864 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004865 }
James Smartda0436e2009-05-22 14:51:39 -04004866
4867 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4868 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4869 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004870 rc = 0;
4871
4872out_free_mboxq:
4873 mempool_free(mboxq, phba->mbox_mem_pool);
4874 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004875}
4876
4877/**
4878 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4879 * @phba: pointer to lpfc hba data structure.
4880 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4881 * @vpd: pointer to the memory to hold resulting port vpd data.
4882 * @vpd_size: On input, the number of bytes allocated to @vpd.
4883 * On output, the number of data bytes in @vpd.
4884 *
4885 * This routine executes a READ_REV SLI4 mailbox command. In
4886 * addition, this routine gets the port vpd data.
4887 *
4888 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004889 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004890 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004891 **/
4892static int
4893lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4894 uint8_t *vpd, uint32_t *vpd_size)
4895{
4896 int rc = 0;
4897 uint32_t dma_size;
4898 struct lpfc_dmabuf *dmabuf;
4899 struct lpfc_mqe *mqe;
4900
4901 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4902 if (!dmabuf)
4903 return -ENOMEM;
4904
4905 /*
4906 * Get a DMA buffer for the vpd data resulting from the READ_REV
4907 * mailbox command.
4908 */
4909 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04004910 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
4911 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04004912 if (!dmabuf->virt) {
4913 kfree(dmabuf);
4914 return -ENOMEM;
4915 }
James Smartda0436e2009-05-22 14:51:39 -04004916
4917 /*
4918 * The SLI4 implementation of READ_REV conflicts at word1,
4919 * bits 31:16 and SLI4 adds vpd functionality not present
4920 * in SLI3. This code corrects the conflicts.
4921 */
4922 lpfc_read_rev(phba, mboxq);
4923 mqe = &mboxq->u.mqe;
4924 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4925 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4926 mqe->un.read_rev.word1 &= 0x0000FFFF;
4927 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4928 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4929
4930 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4931 if (rc) {
4932 dma_free_coherent(&phba->pcidev->dev, dma_size,
4933 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004934 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004935 return -EIO;
4936 }
4937
James Smartda0436e2009-05-22 14:51:39 -04004938 /*
4939 * The available vpd length cannot be bigger than the
4940 * DMA buffer passed to the port. Catch the less than
4941 * case and update the caller's size.
4942 */
4943 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4944 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4945
James Smartd7c47992010-06-08 18:31:54 -04004946 memcpy(vpd, dmabuf->virt, *vpd_size);
4947
James Smartda0436e2009-05-22 14:51:39 -04004948 dma_free_coherent(&phba->pcidev->dev, dma_size,
4949 dmabuf->virt, dmabuf->phys);
4950 kfree(dmabuf);
4951 return 0;
4952}
4953
4954/**
James Smartcd1c8302011-10-10 21:33:25 -04004955 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4956 * @phba: pointer to lpfc hba data structure.
4957 *
4958 * This routine retrieves SLI4 device physical port name this PCI function
4959 * is attached to.
4960 *
4961 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004962 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04004963 * otherwise - failed to retrieve physical port name
4964 **/
4965static int
4966lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4967{
4968 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004969 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4970 struct lpfc_controller_attribute *cntl_attr;
4971 struct lpfc_mbx_get_port_name *get_port_name;
4972 void *virtaddr = NULL;
4973 uint32_t alloclen, reqlen;
4974 uint32_t shdr_status, shdr_add_status;
4975 union lpfc_sli4_cfg_shdr *shdr;
4976 char cport_name = 0;
4977 int rc;
4978
4979 /* We assume nothing at this point */
4980 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4981 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4982
4983 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4984 if (!mboxq)
4985 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004986 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004987 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4988 lpfc_sli4_read_config(phba);
4989 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4990 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004991
4992 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4993 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4994 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4995 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4996 LPFC_SLI4_MBX_NEMBED);
4997 if (alloclen < reqlen) {
4998 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4999 "3084 Allocated DMA memory size (%d) is "
5000 "less than the requested DMA memory size "
5001 "(%d)\n", alloclen, reqlen);
5002 rc = -ENOMEM;
5003 goto out_free_mboxq;
5004 }
5005 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5006 virtaddr = mboxq->sge_array->addr[0];
5007 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5008 shdr = &mbx_cntl_attr->cfg_shdr;
5009 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5010 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5011 if (shdr_status || shdr_add_status || rc) {
5012 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5013 "3085 Mailbox x%x (x%x/x%x) failed, "
5014 "rc:x%x, status:x%x, add_status:x%x\n",
5015 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5016 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5017 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5018 rc, shdr_status, shdr_add_status);
5019 rc = -ENXIO;
5020 goto out_free_mboxq;
5021 }
5022 cntl_attr = &mbx_cntl_attr->cntl_attr;
5023 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5024 phba->sli4_hba.lnk_info.lnk_tp =
5025 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5026 phba->sli4_hba.lnk_info.lnk_no =
5027 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5028 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5029 "3086 lnk_type:%d, lnk_numb:%d\n",
5030 phba->sli4_hba.lnk_info.lnk_tp,
5031 phba->sli4_hba.lnk_info.lnk_no);
5032
5033retrieve_ppname:
5034 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5035 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5036 sizeof(struct lpfc_mbx_get_port_name) -
5037 sizeof(struct lpfc_sli4_cfg_mhdr),
5038 LPFC_SLI4_MBX_EMBED);
5039 get_port_name = &mboxq->u.mqe.un.get_port_name;
5040 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5041 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5042 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5043 phba->sli4_hba.lnk_info.lnk_tp);
5044 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5045 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5046 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5047 if (shdr_status || shdr_add_status || rc) {
5048 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5049 "3087 Mailbox x%x (x%x/x%x) failed: "
5050 "rc:x%x, status:x%x, add_status:x%x\n",
5051 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5052 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5053 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5054 rc, shdr_status, shdr_add_status);
5055 rc = -ENXIO;
5056 goto out_free_mboxq;
5057 }
5058 switch (phba->sli4_hba.lnk_info.lnk_no) {
5059 case LPFC_LINK_NUMBER_0:
5060 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5061 &get_port_name->u.response);
5062 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5063 break;
5064 case LPFC_LINK_NUMBER_1:
5065 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5066 &get_port_name->u.response);
5067 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5068 break;
5069 case LPFC_LINK_NUMBER_2:
5070 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5071 &get_port_name->u.response);
5072 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5073 break;
5074 case LPFC_LINK_NUMBER_3:
5075 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5076 &get_port_name->u.response);
5077 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5078 break;
5079 default:
5080 break;
5081 }
5082
5083 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5084 phba->Port[0] = cport_name;
5085 phba->Port[1] = '\0';
5086 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5087 "3091 SLI get port name: %s\n", phba->Port);
5088 }
5089
5090out_free_mboxq:
5091 if (rc != MBX_TIMEOUT) {
5092 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5093 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5094 else
5095 mempool_free(mboxq, phba->mbox_mem_pool);
5096 }
5097 return rc;
5098}
5099
5100/**
James Smartda0436e2009-05-22 14:51:39 -04005101 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5102 * @phba: pointer to lpfc hba data structure.
5103 *
5104 * This routine is called to explicitly arm the SLI4 device's completion and
5105 * event queues
5106 **/
5107static void
5108lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5109{
James Smart962bc512013-01-03 15:44:00 -05005110 int fcp_eqidx;
James Smartda0436e2009-05-22 14:51:39 -04005111
5112 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5113 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04005114 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05005115 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04005116 do {
James Smart2e90f4b2011-12-13 13:22:37 -05005117 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
5118 LPFC_QUEUE_REARM);
James Smart67d12732012-08-03 12:36:13 -04005119 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
James Smart2e90f4b2011-12-13 13:22:37 -05005120 }
James Smart1ba981f2014-02-20 09:56:45 -05005121
James Smartf38fa0b2014-04-04 13:52:21 -04005122 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005123 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5124
James Smart67d12732012-08-03 12:36:13 -04005125 if (phba->sli4_hba.hba_eq) {
5126 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05005127 fcp_eqidx++)
James Smart67d12732012-08-03 12:36:13 -04005128 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
James Smart2e90f4b2011-12-13 13:22:37 -05005129 LPFC_QUEUE_REARM);
5130 }
James Smart1ba981f2014-02-20 09:56:45 -05005131
5132 if (phba->cfg_fof)
5133 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005134}
5135
5136/**
James Smart6d368e52011-05-24 11:44:12 -04005137 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5138 * @phba: Pointer to HBA context object.
5139 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005140 * @extnt_count: buffer to hold port available extent count.
5141 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005142 *
James Smartb76f2dc2011-07-22 18:37:42 -04005143 * This function calls the port and retrievs the number of available
5144 * extents and their size for a particular extent type.
5145 *
5146 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005147 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005148int
James Smart6d368e52011-05-24 11:44:12 -04005149lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5150 uint16_t *extnt_count, uint16_t *extnt_size)
5151{
5152 int rc = 0;
5153 uint32_t length;
5154 uint32_t mbox_tmo;
5155 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5156 LPFC_MBOXQ_t *mbox;
5157
5158 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5159 if (!mbox)
5160 return -ENOMEM;
5161
5162 /* Find out how many extents are available for this resource type */
5163 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5164 sizeof(struct lpfc_sli4_cfg_mhdr));
5165 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5166 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5167 length, LPFC_SLI4_MBX_EMBED);
5168
5169 /* Send an extents count of 0 - the GET doesn't use it. */
5170 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5171 LPFC_SLI4_MBX_EMBED);
5172 if (unlikely(rc)) {
5173 rc = -EIO;
5174 goto err_exit;
5175 }
5176
5177 if (!phba->sli4_hba.intr_enable)
5178 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5179 else {
James Smarta183a152011-10-10 21:32:43 -04005180 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005181 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5182 }
5183 if (unlikely(rc)) {
5184 rc = -EIO;
5185 goto err_exit;
5186 }
5187
5188 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5189 if (bf_get(lpfc_mbox_hdr_status,
5190 &rsrc_info->header.cfg_shdr.response)) {
5191 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5192 "2930 Failed to get resource extents "
5193 "Status 0x%x Add'l Status 0x%x\n",
5194 bf_get(lpfc_mbox_hdr_status,
5195 &rsrc_info->header.cfg_shdr.response),
5196 bf_get(lpfc_mbox_hdr_add_status,
5197 &rsrc_info->header.cfg_shdr.response));
5198 rc = -EIO;
5199 goto err_exit;
5200 }
5201
5202 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5203 &rsrc_info->u.rsp);
5204 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5205 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005206
5207 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5208 "3162 Retrieved extents type-%d from port: count:%d, "
5209 "size:%d\n", type, *extnt_count, *extnt_size);
5210
5211err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005212 mempool_free(mbox, phba->mbox_mem_pool);
5213 return rc;
5214}
5215
5216/**
5217 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5218 * @phba: Pointer to HBA context object.
5219 * @type: The extent type to check.
5220 *
5221 * This function reads the current available extents from the port and checks
5222 * if the extent count or extent size has changed since the last access.
5223 * Callers use this routine post port reset to understand if there is a
5224 * extent reprovisioning requirement.
5225 *
5226 * Returns:
5227 * -Error: error indicates problem.
5228 * 1: Extent count or size has changed.
5229 * 0: No changes.
5230 **/
5231static int
5232lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5233{
5234 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5235 uint16_t size_diff, rsrc_ext_size;
5236 int rc = 0;
5237 struct lpfc_rsrc_blks *rsrc_entry;
5238 struct list_head *rsrc_blk_list = NULL;
5239
5240 size_diff = 0;
5241 curr_ext_cnt = 0;
5242 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5243 &rsrc_ext_cnt,
5244 &rsrc_ext_size);
5245 if (unlikely(rc))
5246 return -EIO;
5247
5248 switch (type) {
5249 case LPFC_RSC_TYPE_FCOE_RPI:
5250 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5251 break;
5252 case LPFC_RSC_TYPE_FCOE_VPI:
5253 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5254 break;
5255 case LPFC_RSC_TYPE_FCOE_XRI:
5256 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5257 break;
5258 case LPFC_RSC_TYPE_FCOE_VFI:
5259 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5260 break;
5261 default:
5262 break;
5263 }
5264
5265 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5266 curr_ext_cnt++;
5267 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5268 size_diff++;
5269 }
5270
5271 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5272 rc = 1;
5273
5274 return rc;
5275}
5276
5277/**
5278 * lpfc_sli4_cfg_post_extnts -
5279 * @phba: Pointer to HBA context object.
5280 * @extnt_cnt - number of available extents.
5281 * @type - the extent type (rpi, xri, vfi, vpi).
5282 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5283 * @mbox - pointer to the caller's allocated mailbox structure.
5284 *
5285 * This function executes the extents allocation request. It also
5286 * takes care of the amount of memory needed to allocate or get the
5287 * allocated extents. It is the caller's responsibility to evaluate
5288 * the response.
5289 *
5290 * Returns:
5291 * -Error: Error value describes the condition found.
5292 * 0: if successful
5293 **/
5294static int
James Smart8a9d2e82012-05-09 21:16:12 -04005295lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005296 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5297{
5298 int rc = 0;
5299 uint32_t req_len;
5300 uint32_t emb_len;
5301 uint32_t alloc_len, mbox_tmo;
5302
5303 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005304 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005305
5306 /*
5307 * Calculate the size of an embedded mailbox. The uint32_t
5308 * accounts for extents-specific word.
5309 */
5310 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5311 sizeof(uint32_t);
5312
5313 /*
5314 * Presume the allocation and response will fit into an embedded
5315 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5316 */
5317 *emb = LPFC_SLI4_MBX_EMBED;
5318 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005319 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005320 sizeof(union lpfc_sli4_cfg_shdr) +
5321 sizeof(uint32_t);
5322 *emb = LPFC_SLI4_MBX_NEMBED;
5323 }
5324
5325 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5326 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5327 req_len, *emb);
5328 if (alloc_len < req_len) {
5329 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005330 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005331 "less than the requested DMA memory "
5332 "size (x%x)\n", alloc_len, req_len);
5333 return -ENOMEM;
5334 }
James Smart8a9d2e82012-05-09 21:16:12 -04005335 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005336 if (unlikely(rc))
5337 return -EIO;
5338
5339 if (!phba->sli4_hba.intr_enable)
5340 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5341 else {
James Smarta183a152011-10-10 21:32:43 -04005342 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005343 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5344 }
5345
5346 if (unlikely(rc))
5347 rc = -EIO;
5348 return rc;
5349}
5350
5351/**
5352 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5353 * @phba: Pointer to HBA context object.
5354 * @type: The resource extent type to allocate.
5355 *
5356 * This function allocates the number of elements for the specified
5357 * resource type.
5358 **/
5359static int
5360lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5361{
5362 bool emb = false;
5363 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5364 uint16_t rsrc_id, rsrc_start, j, k;
5365 uint16_t *ids;
5366 int i, rc;
5367 unsigned long longs;
5368 unsigned long *bmask;
5369 struct lpfc_rsrc_blks *rsrc_blks;
5370 LPFC_MBOXQ_t *mbox;
5371 uint32_t length;
5372 struct lpfc_id_range *id_array = NULL;
5373 void *virtaddr = NULL;
5374 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5375 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5376 struct list_head *ext_blk_list;
5377
5378 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5379 &rsrc_cnt,
5380 &rsrc_size);
5381 if (unlikely(rc))
5382 return -EIO;
5383
5384 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5385 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5386 "3009 No available Resource Extents "
5387 "for resource type 0x%x: Count: 0x%x, "
5388 "Size 0x%x\n", type, rsrc_cnt,
5389 rsrc_size);
5390 return -ENOMEM;
5391 }
5392
James Smart8a9d2e82012-05-09 21:16:12 -04005393 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5394 "2903 Post resource extents type-0x%x: "
5395 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005396
5397 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5398 if (!mbox)
5399 return -ENOMEM;
5400
James Smart8a9d2e82012-05-09 21:16:12 -04005401 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005402 if (unlikely(rc)) {
5403 rc = -EIO;
5404 goto err_exit;
5405 }
5406
5407 /*
5408 * Figure out where the response is located. Then get local pointers
5409 * to the response data. The port does not guarantee to respond to
5410 * all extents counts request so update the local variable with the
5411 * allocated count from the port.
5412 */
5413 if (emb == LPFC_SLI4_MBX_EMBED) {
5414 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5415 id_array = &rsrc_ext->u.rsp.id[0];
5416 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5417 } else {
5418 virtaddr = mbox->sge_array->addr[0];
5419 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5420 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5421 id_array = &n_rsrc->id;
5422 }
5423
5424 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5425 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5426
5427 /*
5428 * Based on the resource size and count, correct the base and max
5429 * resource values.
5430 */
5431 length = sizeof(struct lpfc_rsrc_blks);
5432 switch (type) {
5433 case LPFC_RSC_TYPE_FCOE_RPI:
5434 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5435 sizeof(unsigned long),
5436 GFP_KERNEL);
5437 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5438 rc = -ENOMEM;
5439 goto err_exit;
5440 }
5441 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5442 sizeof(uint16_t),
5443 GFP_KERNEL);
5444 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5445 kfree(phba->sli4_hba.rpi_bmask);
5446 rc = -ENOMEM;
5447 goto err_exit;
5448 }
5449
5450 /*
5451 * The next_rpi was initialized with the maximum available
5452 * count but the port may allocate a smaller number. Catch
5453 * that case and update the next_rpi.
5454 */
5455 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5456
5457 /* Initialize local ptrs for common extent processing later. */
5458 bmask = phba->sli4_hba.rpi_bmask;
5459 ids = phba->sli4_hba.rpi_ids;
5460 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5461 break;
5462 case LPFC_RSC_TYPE_FCOE_VPI:
5463 phba->vpi_bmask = kzalloc(longs *
5464 sizeof(unsigned long),
5465 GFP_KERNEL);
5466 if (unlikely(!phba->vpi_bmask)) {
5467 rc = -ENOMEM;
5468 goto err_exit;
5469 }
5470 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5471 sizeof(uint16_t),
5472 GFP_KERNEL);
5473 if (unlikely(!phba->vpi_ids)) {
5474 kfree(phba->vpi_bmask);
5475 rc = -ENOMEM;
5476 goto err_exit;
5477 }
5478
5479 /* Initialize local ptrs for common extent processing later. */
5480 bmask = phba->vpi_bmask;
5481 ids = phba->vpi_ids;
5482 ext_blk_list = &phba->lpfc_vpi_blk_list;
5483 break;
5484 case LPFC_RSC_TYPE_FCOE_XRI:
5485 phba->sli4_hba.xri_bmask = kzalloc(longs *
5486 sizeof(unsigned long),
5487 GFP_KERNEL);
5488 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5489 rc = -ENOMEM;
5490 goto err_exit;
5491 }
James Smart8a9d2e82012-05-09 21:16:12 -04005492 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005493 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5494 sizeof(uint16_t),
5495 GFP_KERNEL);
5496 if (unlikely(!phba->sli4_hba.xri_ids)) {
5497 kfree(phba->sli4_hba.xri_bmask);
5498 rc = -ENOMEM;
5499 goto err_exit;
5500 }
5501
5502 /* Initialize local ptrs for common extent processing later. */
5503 bmask = phba->sli4_hba.xri_bmask;
5504 ids = phba->sli4_hba.xri_ids;
5505 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5506 break;
5507 case LPFC_RSC_TYPE_FCOE_VFI:
5508 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5509 sizeof(unsigned long),
5510 GFP_KERNEL);
5511 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5512 rc = -ENOMEM;
5513 goto err_exit;
5514 }
5515 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5516 sizeof(uint16_t),
5517 GFP_KERNEL);
5518 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5519 kfree(phba->sli4_hba.vfi_bmask);
5520 rc = -ENOMEM;
5521 goto err_exit;
5522 }
5523
5524 /* Initialize local ptrs for common extent processing later. */
5525 bmask = phba->sli4_hba.vfi_bmask;
5526 ids = phba->sli4_hba.vfi_ids;
5527 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5528 break;
5529 default:
5530 /* Unsupported Opcode. Fail call. */
5531 id_array = NULL;
5532 bmask = NULL;
5533 ids = NULL;
5534 ext_blk_list = NULL;
5535 goto err_exit;
5536 }
5537
5538 /*
5539 * Complete initializing the extent configuration with the
5540 * allocated ids assigned to this function. The bitmask serves
5541 * as an index into the array and manages the available ids. The
5542 * array just stores the ids communicated to the port via the wqes.
5543 */
5544 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5545 if ((i % 2) == 0)
5546 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5547 &id_array[k]);
5548 else
5549 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5550 &id_array[k]);
5551
5552 rsrc_blks = kzalloc(length, GFP_KERNEL);
5553 if (unlikely(!rsrc_blks)) {
5554 rc = -ENOMEM;
5555 kfree(bmask);
5556 kfree(ids);
5557 goto err_exit;
5558 }
5559 rsrc_blks->rsrc_start = rsrc_id;
5560 rsrc_blks->rsrc_size = rsrc_size;
5561 list_add_tail(&rsrc_blks->list, ext_blk_list);
5562 rsrc_start = rsrc_id;
5563 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5564 phba->sli4_hba.scsi_xri_start = rsrc_start +
5565 lpfc_sli4_get_els_iocb_cnt(phba);
5566
5567 while (rsrc_id < (rsrc_start + rsrc_size)) {
5568 ids[j] = rsrc_id;
5569 rsrc_id++;
5570 j++;
5571 }
5572 /* Entire word processed. Get next word.*/
5573 if ((i % 2) == 1)
5574 k++;
5575 }
5576 err_exit:
5577 lpfc_sli4_mbox_cmd_free(phba, mbox);
5578 return rc;
5579}
5580
5581/**
5582 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5583 * @phba: Pointer to HBA context object.
5584 * @type: the extent's type.
5585 *
5586 * This function deallocates all extents of a particular resource type.
5587 * SLI4 does not allow for deallocating a particular extent range. It
5588 * is the caller's responsibility to release all kernel memory resources.
5589 **/
5590static int
5591lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5592{
5593 int rc;
5594 uint32_t length, mbox_tmo = 0;
5595 LPFC_MBOXQ_t *mbox;
5596 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5597 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5598
5599 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5600 if (!mbox)
5601 return -ENOMEM;
5602
5603 /*
5604 * This function sends an embedded mailbox because it only sends the
5605 * the resource type. All extents of this type are released by the
5606 * port.
5607 */
5608 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5609 sizeof(struct lpfc_sli4_cfg_mhdr));
5610 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5611 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5612 length, LPFC_SLI4_MBX_EMBED);
5613
5614 /* Send an extents count of 0 - the dealloc doesn't use it. */
5615 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5616 LPFC_SLI4_MBX_EMBED);
5617 if (unlikely(rc)) {
5618 rc = -EIO;
5619 goto out_free_mbox;
5620 }
5621 if (!phba->sli4_hba.intr_enable)
5622 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5623 else {
James Smarta183a152011-10-10 21:32:43 -04005624 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005625 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5626 }
5627 if (unlikely(rc)) {
5628 rc = -EIO;
5629 goto out_free_mbox;
5630 }
5631
5632 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5633 if (bf_get(lpfc_mbox_hdr_status,
5634 &dealloc_rsrc->header.cfg_shdr.response)) {
5635 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5636 "2919 Failed to release resource extents "
5637 "for type %d - Status 0x%x Add'l Status 0x%x. "
5638 "Resource memory not released.\n",
5639 type,
5640 bf_get(lpfc_mbox_hdr_status,
5641 &dealloc_rsrc->header.cfg_shdr.response),
5642 bf_get(lpfc_mbox_hdr_add_status,
5643 &dealloc_rsrc->header.cfg_shdr.response));
5644 rc = -EIO;
5645 goto out_free_mbox;
5646 }
5647
5648 /* Release kernel memory resources for the specific type. */
5649 switch (type) {
5650 case LPFC_RSC_TYPE_FCOE_VPI:
5651 kfree(phba->vpi_bmask);
5652 kfree(phba->vpi_ids);
5653 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5654 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5655 &phba->lpfc_vpi_blk_list, list) {
5656 list_del_init(&rsrc_blk->list);
5657 kfree(rsrc_blk);
5658 }
James Smart16a3a202013-04-17 20:14:38 -04005659 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005660 break;
5661 case LPFC_RSC_TYPE_FCOE_XRI:
5662 kfree(phba->sli4_hba.xri_bmask);
5663 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005664 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5665 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5666 list_del_init(&rsrc_blk->list);
5667 kfree(rsrc_blk);
5668 }
5669 break;
5670 case LPFC_RSC_TYPE_FCOE_VFI:
5671 kfree(phba->sli4_hba.vfi_bmask);
5672 kfree(phba->sli4_hba.vfi_ids);
5673 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5674 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5675 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5676 list_del_init(&rsrc_blk->list);
5677 kfree(rsrc_blk);
5678 }
5679 break;
5680 case LPFC_RSC_TYPE_FCOE_RPI:
5681 /* RPI bitmask and physical id array are cleaned up earlier. */
5682 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5683 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5684 list_del_init(&rsrc_blk->list);
5685 kfree(rsrc_blk);
5686 }
5687 break;
5688 default:
5689 break;
5690 }
5691
5692 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5693
5694 out_free_mbox:
5695 mempool_free(mbox, phba->mbox_mem_pool);
5696 return rc;
5697}
5698
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08005699static void
James Smart7bdedb32016-07-06 12:36:00 -07005700lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5701 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07005702{
James Smart65791f12016-07-06 12:35:56 -07005703 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07005704
James Smart65791f12016-07-06 12:35:56 -07005705 len = sizeof(struct lpfc_mbx_set_feature) -
5706 sizeof(struct lpfc_sli4_cfg_mhdr);
5707 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5708 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5709 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07005710
James Smart7bdedb32016-07-06 12:36:00 -07005711 switch (feature) {
5712 case LPFC_SET_UE_RECOVERY:
5713 bf_set(lpfc_mbx_set_feature_UER,
5714 &mbox->u.mqe.un.set_feature, 1);
5715 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5716 mbox->u.mqe.un.set_feature.param_len = 8;
5717 break;
5718 case LPFC_SET_MDS_DIAGS:
5719 bf_set(lpfc_mbx_set_feature_mds,
5720 &mbox->u.mqe.un.set_feature, 1);
5721 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5722 &mbox->u.mqe.un.set_feature, 0);
5723 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5724 mbox->u.mqe.un.set_feature.param_len = 8;
5725 break;
James Smart65791f12016-07-06 12:35:56 -07005726 }
James Smart7bdedb32016-07-06 12:36:00 -07005727
5728 return;
James Smart65791f12016-07-06 12:35:56 -07005729}
5730
James Smart6d368e52011-05-24 11:44:12 -04005731/**
5732 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5733 * @phba: Pointer to HBA context object.
5734 *
5735 * This function allocates all SLI4 resource identifiers.
5736 **/
5737int
5738lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5739{
5740 int i, rc, error = 0;
5741 uint16_t count, base;
5742 unsigned long longs;
5743
James Smartff78d8f2011-12-13 13:21:35 -05005744 if (!phba->sli4_hba.rpi_hdrs_in_use)
5745 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005746 if (phba->sli4_hba.extents_in_use) {
5747 /*
5748 * The port supports resource extents. The XRI, VPI, VFI, RPI
5749 * resource extent count must be read and allocated before
5750 * provisioning the resource id arrays.
5751 */
5752 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5753 LPFC_IDX_RSRC_RDY) {
5754 /*
5755 * Extent-based resources are set - the driver could
5756 * be in a port reset. Figure out if any corrective
5757 * actions need to be taken.
5758 */
5759 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5760 LPFC_RSC_TYPE_FCOE_VFI);
5761 if (rc != 0)
5762 error++;
5763 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5764 LPFC_RSC_TYPE_FCOE_VPI);
5765 if (rc != 0)
5766 error++;
5767 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5768 LPFC_RSC_TYPE_FCOE_XRI);
5769 if (rc != 0)
5770 error++;
5771 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5772 LPFC_RSC_TYPE_FCOE_RPI);
5773 if (rc != 0)
5774 error++;
5775
5776 /*
5777 * It's possible that the number of resources
5778 * provided to this port instance changed between
5779 * resets. Detect this condition and reallocate
5780 * resources. Otherwise, there is no action.
5781 */
5782 if (error) {
5783 lpfc_printf_log(phba, KERN_INFO,
5784 LOG_MBOX | LOG_INIT,
5785 "2931 Detected extent resource "
5786 "change. Reallocating all "
5787 "extents.\n");
5788 rc = lpfc_sli4_dealloc_extent(phba,
5789 LPFC_RSC_TYPE_FCOE_VFI);
5790 rc = lpfc_sli4_dealloc_extent(phba,
5791 LPFC_RSC_TYPE_FCOE_VPI);
5792 rc = lpfc_sli4_dealloc_extent(phba,
5793 LPFC_RSC_TYPE_FCOE_XRI);
5794 rc = lpfc_sli4_dealloc_extent(phba,
5795 LPFC_RSC_TYPE_FCOE_RPI);
5796 } else
5797 return 0;
5798 }
5799
5800 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5801 if (unlikely(rc))
5802 goto err_exit;
5803
5804 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5805 if (unlikely(rc))
5806 goto err_exit;
5807
5808 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5809 if (unlikely(rc))
5810 goto err_exit;
5811
5812 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5813 if (unlikely(rc))
5814 goto err_exit;
5815 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5816 LPFC_IDX_RSRC_RDY);
5817 return rc;
5818 } else {
5819 /*
5820 * The port does not support resource extents. The XRI, VPI,
5821 * VFI, RPI resource ids were determined from READ_CONFIG.
5822 * Just allocate the bitmasks and provision the resource id
5823 * arrays. If a port reset is active, the resources don't
5824 * need any action - just exit.
5825 */
5826 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005827 LPFC_IDX_RSRC_RDY) {
5828 lpfc_sli4_dealloc_resource_identifiers(phba);
5829 lpfc_sli4_remove_rpis(phba);
5830 }
James Smart6d368e52011-05-24 11:44:12 -04005831 /* RPIs. */
5832 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05005833 if (count <= 0) {
5834 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5835 "3279 Invalid provisioning of "
5836 "rpi:%d\n", count);
5837 rc = -EINVAL;
5838 goto err_exit;
5839 }
James Smart6d368e52011-05-24 11:44:12 -04005840 base = phba->sli4_hba.max_cfg_param.rpi_base;
5841 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5842 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5843 sizeof(unsigned long),
5844 GFP_KERNEL);
5845 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5846 rc = -ENOMEM;
5847 goto err_exit;
5848 }
5849 phba->sli4_hba.rpi_ids = kzalloc(count *
5850 sizeof(uint16_t),
5851 GFP_KERNEL);
5852 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5853 rc = -ENOMEM;
5854 goto free_rpi_bmask;
5855 }
5856
5857 for (i = 0; i < count; i++)
5858 phba->sli4_hba.rpi_ids[i] = base + i;
5859
5860 /* VPIs. */
5861 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05005862 if (count <= 0) {
5863 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5864 "3280 Invalid provisioning of "
5865 "vpi:%d\n", count);
5866 rc = -EINVAL;
5867 goto free_rpi_ids;
5868 }
James Smart6d368e52011-05-24 11:44:12 -04005869 base = phba->sli4_hba.max_cfg_param.vpi_base;
5870 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5871 phba->vpi_bmask = kzalloc(longs *
5872 sizeof(unsigned long),
5873 GFP_KERNEL);
5874 if (unlikely(!phba->vpi_bmask)) {
5875 rc = -ENOMEM;
5876 goto free_rpi_ids;
5877 }
5878 phba->vpi_ids = kzalloc(count *
5879 sizeof(uint16_t),
5880 GFP_KERNEL);
5881 if (unlikely(!phba->vpi_ids)) {
5882 rc = -ENOMEM;
5883 goto free_vpi_bmask;
5884 }
5885
5886 for (i = 0; i < count; i++)
5887 phba->vpi_ids[i] = base + i;
5888
5889 /* XRIs. */
5890 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05005891 if (count <= 0) {
5892 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5893 "3281 Invalid provisioning of "
5894 "xri:%d\n", count);
5895 rc = -EINVAL;
5896 goto free_vpi_ids;
5897 }
James Smart6d368e52011-05-24 11:44:12 -04005898 base = phba->sli4_hba.max_cfg_param.xri_base;
5899 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5900 phba->sli4_hba.xri_bmask = kzalloc(longs *
5901 sizeof(unsigned long),
5902 GFP_KERNEL);
5903 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5904 rc = -ENOMEM;
5905 goto free_vpi_ids;
5906 }
James Smart41899be2012-03-01 22:34:19 -05005907 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005908 phba->sli4_hba.xri_ids = kzalloc(count *
5909 sizeof(uint16_t),
5910 GFP_KERNEL);
5911 if (unlikely(!phba->sli4_hba.xri_ids)) {
5912 rc = -ENOMEM;
5913 goto free_xri_bmask;
5914 }
5915
5916 for (i = 0; i < count; i++)
5917 phba->sli4_hba.xri_ids[i] = base + i;
5918
5919 /* VFIs. */
5920 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05005921 if (count <= 0) {
5922 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5923 "3282 Invalid provisioning of "
5924 "vfi:%d\n", count);
5925 rc = -EINVAL;
5926 goto free_xri_ids;
5927 }
James Smart6d368e52011-05-24 11:44:12 -04005928 base = phba->sli4_hba.max_cfg_param.vfi_base;
5929 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5930 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5931 sizeof(unsigned long),
5932 GFP_KERNEL);
5933 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5934 rc = -ENOMEM;
5935 goto free_xri_ids;
5936 }
5937 phba->sli4_hba.vfi_ids = kzalloc(count *
5938 sizeof(uint16_t),
5939 GFP_KERNEL);
5940 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5941 rc = -ENOMEM;
5942 goto free_vfi_bmask;
5943 }
5944
5945 for (i = 0; i < count; i++)
5946 phba->sli4_hba.vfi_ids[i] = base + i;
5947
5948 /*
5949 * Mark all resources ready. An HBA reset doesn't need
5950 * to reset the initialization.
5951 */
5952 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5953 LPFC_IDX_RSRC_RDY);
5954 return 0;
5955 }
5956
5957 free_vfi_bmask:
5958 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01005959 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005960 free_xri_ids:
5961 kfree(phba->sli4_hba.xri_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01005962 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005963 free_xri_bmask:
5964 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01005965 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005966 free_vpi_ids:
5967 kfree(phba->vpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01005968 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005969 free_vpi_bmask:
5970 kfree(phba->vpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01005971 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005972 free_rpi_ids:
5973 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassucd60be42017-01-11 11:06:42 +01005974 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005975 free_rpi_bmask:
5976 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassucd60be42017-01-11 11:06:42 +01005977 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005978 err_exit:
5979 return rc;
5980}
5981
5982/**
5983 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5984 * @phba: Pointer to HBA context object.
5985 *
5986 * This function allocates the number of elements for the specified
5987 * resource type.
5988 **/
5989int
5990lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5991{
5992 if (phba->sli4_hba.extents_in_use) {
5993 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5994 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5995 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5996 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5997 } else {
5998 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04005999 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04006000 kfree(phba->vpi_ids);
6001 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6002 kfree(phba->sli4_hba.xri_bmask);
6003 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006004 kfree(phba->sli4_hba.vfi_bmask);
6005 kfree(phba->sli4_hba.vfi_ids);
6006 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6007 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6008 }
6009
6010 return 0;
6011}
6012
6013/**
James Smartb76f2dc2011-07-22 18:37:42 -04006014 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6015 * @phba: Pointer to HBA context object.
6016 * @type: The resource extent type.
6017 * @extnt_count: buffer to hold port extent count response
6018 * @extnt_size: buffer to hold port extent size response.
6019 *
6020 * This function calls the port to read the host allocated extents
6021 * for a particular type.
6022 **/
6023int
6024lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6025 uint16_t *extnt_cnt, uint16_t *extnt_size)
6026{
6027 bool emb;
6028 int rc = 0;
6029 uint16_t curr_blks = 0;
6030 uint32_t req_len, emb_len;
6031 uint32_t alloc_len, mbox_tmo;
6032 struct list_head *blk_list_head;
6033 struct lpfc_rsrc_blks *rsrc_blk;
6034 LPFC_MBOXQ_t *mbox;
6035 void *virtaddr = NULL;
6036 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6037 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6038 union lpfc_sli4_cfg_shdr *shdr;
6039
6040 switch (type) {
6041 case LPFC_RSC_TYPE_FCOE_VPI:
6042 blk_list_head = &phba->lpfc_vpi_blk_list;
6043 break;
6044 case LPFC_RSC_TYPE_FCOE_XRI:
6045 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6046 break;
6047 case LPFC_RSC_TYPE_FCOE_VFI:
6048 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6049 break;
6050 case LPFC_RSC_TYPE_FCOE_RPI:
6051 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6052 break;
6053 default:
6054 return -EIO;
6055 }
6056
6057 /* Count the number of extents currently allocatd for this type. */
6058 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6059 if (curr_blks == 0) {
6060 /*
6061 * The GET_ALLOCATED mailbox does not return the size,
6062 * just the count. The size should be just the size
6063 * stored in the current allocated block and all sizes
6064 * for an extent type are the same so set the return
6065 * value now.
6066 */
6067 *extnt_size = rsrc_blk->rsrc_size;
6068 }
6069 curr_blks++;
6070 }
6071
James Smartb76f2dc2011-07-22 18:37:42 -04006072 /*
6073 * Calculate the size of an embedded mailbox. The uint32_t
6074 * accounts for extents-specific word.
6075 */
6076 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6077 sizeof(uint32_t);
6078
6079 /*
6080 * Presume the allocation and response will fit into an embedded
6081 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6082 */
6083 emb = LPFC_SLI4_MBX_EMBED;
6084 req_len = emb_len;
6085 if (req_len > emb_len) {
6086 req_len = curr_blks * sizeof(uint16_t) +
6087 sizeof(union lpfc_sli4_cfg_shdr) +
6088 sizeof(uint32_t);
6089 emb = LPFC_SLI4_MBX_NEMBED;
6090 }
6091
6092 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6093 if (!mbox)
6094 return -ENOMEM;
6095 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6096
6097 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6098 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6099 req_len, emb);
6100 if (alloc_len < req_len) {
6101 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6102 "2983 Allocated DMA memory size (x%x) is "
6103 "less than the requested DMA memory "
6104 "size (x%x)\n", alloc_len, req_len);
6105 rc = -ENOMEM;
6106 goto err_exit;
6107 }
6108 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6109 if (unlikely(rc)) {
6110 rc = -EIO;
6111 goto err_exit;
6112 }
6113
6114 if (!phba->sli4_hba.intr_enable)
6115 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6116 else {
James Smarta183a152011-10-10 21:32:43 -04006117 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006118 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6119 }
6120
6121 if (unlikely(rc)) {
6122 rc = -EIO;
6123 goto err_exit;
6124 }
6125
6126 /*
6127 * Figure out where the response is located. Then get local pointers
6128 * to the response data. The port does not guarantee to respond to
6129 * all extents counts request so update the local variable with the
6130 * allocated count from the port.
6131 */
6132 if (emb == LPFC_SLI4_MBX_EMBED) {
6133 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6134 shdr = &rsrc_ext->header.cfg_shdr;
6135 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6136 } else {
6137 virtaddr = mbox->sge_array->addr[0];
6138 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6139 shdr = &n_rsrc->cfg_shdr;
6140 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6141 }
6142
6143 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6144 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6145 "2984 Failed to read allocated resources "
6146 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6147 type,
6148 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6149 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6150 rc = -EIO;
6151 goto err_exit;
6152 }
6153 err_exit:
6154 lpfc_sli4_mbox_cmd_free(phba, mbox);
6155 return rc;
6156}
6157
6158/**
James Smart8a9d2e82012-05-09 21:16:12 -04006159 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
6160 * @phba: pointer to lpfc hba data structure.
6161 *
6162 * This routine walks the list of els buffers that have been allocated and
6163 * repost them to the port by using SGL block post. This is needed after a
6164 * pci_function_reset/warm_start or start. It attempts to construct blocks
6165 * of els buffer sgls which contains contiguous xris and uses the non-embedded
6166 * SGL block post mailbox commands to post them to the port. For single els
6167 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6168 * mailbox command for posting.
6169 *
6170 * Returns: 0 = success, non-zero failure.
6171 **/
6172static int
6173lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
6174{
6175 struct lpfc_sglq *sglq_entry = NULL;
6176 struct lpfc_sglq *sglq_entry_next = NULL;
6177 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart711ea882013-04-17 20:18:29 -04006178 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006179 int last_xritag = NO_XRI;
James Smartdafe8ce2014-09-03 12:56:40 -04006180 struct lpfc_sli_ring *pring;
James Smart8a9d2e82012-05-09 21:16:12 -04006181 LIST_HEAD(prep_sgl_list);
6182 LIST_HEAD(blck_sgl_list);
6183 LIST_HEAD(allc_sgl_list);
6184 LIST_HEAD(post_sgl_list);
6185 LIST_HEAD(free_sgl_list);
6186
James Smartdafe8ce2014-09-03 12:56:40 -04006187 pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart38c20672013-03-01 16:37:44 -05006188 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04006189 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04006190 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04006191 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05006192 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006193
James Smart711ea882013-04-17 20:18:29 -04006194 total_cnt = phba->sli4_hba.els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006195 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6196 &allc_sgl_list, list) {
6197 list_del_init(&sglq_entry->list);
6198 block_cnt++;
6199 if ((last_xritag != NO_XRI) &&
6200 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6201 /* a hole in xri block, form a sgl posting block */
6202 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6203 post_cnt = block_cnt - 1;
6204 /* prepare list for next posting block */
6205 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6206 block_cnt = 1;
6207 } else {
6208 /* prepare list for next posting block */
6209 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6210 /* enough sgls for non-embed sgl mbox command */
6211 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6212 list_splice_init(&prep_sgl_list,
6213 &blck_sgl_list);
6214 post_cnt = block_cnt;
6215 block_cnt = 0;
6216 }
6217 }
6218 num_posted++;
6219
6220 /* keep track of last sgl's xritag */
6221 last_xritag = sglq_entry->sli4_xritag;
6222
6223 /* end of repost sgl list condition for els buffers */
6224 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6225 if (post_cnt == 0) {
6226 list_splice_init(&prep_sgl_list,
6227 &blck_sgl_list);
6228 post_cnt = block_cnt;
6229 } else if (block_cnt == 1) {
6230 status = lpfc_sli4_post_sgl(phba,
6231 sglq_entry->phys, 0,
6232 sglq_entry->sli4_xritag);
6233 if (!status) {
6234 /* successful, put sgl to posted list */
6235 list_add_tail(&sglq_entry->list,
6236 &post_sgl_list);
6237 } else {
6238 /* Failure, put sgl to free list */
6239 lpfc_printf_log(phba, KERN_WARNING,
6240 LOG_SLI,
6241 "3159 Failed to post els "
6242 "sgl, xritag:x%x\n",
6243 sglq_entry->sli4_xritag);
6244 list_add_tail(&sglq_entry->list,
6245 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006246 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006247 }
6248 }
6249 }
6250
6251 /* continue until a nembed page worth of sgls */
6252 if (post_cnt == 0)
6253 continue;
6254
6255 /* post the els buffer list sgls as a block */
6256 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6257 post_cnt);
6258
6259 if (!status) {
6260 /* success, put sgl list to posted sgl list */
6261 list_splice_init(&blck_sgl_list, &post_sgl_list);
6262 } else {
6263 /* Failure, put sgl list to free sgl list */
6264 sglq_entry_first = list_first_entry(&blck_sgl_list,
6265 struct lpfc_sglq,
6266 list);
6267 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6268 "3160 Failed to post els sgl-list, "
6269 "xritag:x%x-x%x\n",
6270 sglq_entry_first->sli4_xritag,
6271 (sglq_entry_first->sli4_xritag +
6272 post_cnt - 1));
6273 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006274 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006275 }
6276
6277 /* don't reset xirtag due to hole in xri block */
6278 if (block_cnt == 0)
6279 last_xritag = NO_XRI;
6280
6281 /* reset els sgl post count for next round of posting */
6282 post_cnt = 0;
6283 }
James Smart711ea882013-04-17 20:18:29 -04006284 /* update the number of XRIs posted for ELS */
6285 phba->sli4_hba.els_xri_cnt = total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006286
6287 /* free the els sgls failed to post */
6288 lpfc_free_sgl_list(phba, &free_sgl_list);
6289
6290 /* push els sgls posted to the availble list */
6291 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006292 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04006293 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04006294 list_splice_init(&post_sgl_list,
6295 &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04006296 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05006297 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006298 } else {
6299 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6300 "3161 Failure to post els sgl to port.\n");
6301 return -EIO;
6302 }
6303 return 0;
6304}
6305
James Smart61bda8f2016-10-13 15:06:05 -07006306void
6307lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6308{
6309 uint32_t len;
6310
6311 len = sizeof(struct lpfc_mbx_set_host_data) -
6312 sizeof(struct lpfc_sli4_cfg_mhdr);
6313 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6314 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6315 LPFC_SLI4_MBX_EMBED);
6316
6317 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
James Smartb2fd1032016-12-19 15:07:21 -08006318 mbox->u.mqe.un.set_host_data.param_len =
6319 LPFC_HOST_OS_DRIVER_VERSION_SIZE;
James Smart61bda8f2016-10-13 15:06:05 -07006320 snprintf(mbox->u.mqe.un.set_host_data.data,
6321 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6322 "Linux %s v"LPFC_DRIVER_VERSION,
6323 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6324}
6325
James Smart8a9d2e82012-05-09 21:16:12 -04006326/**
James Smartda0436e2009-05-22 14:51:39 -04006327 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6328 * @phba: Pointer to HBA context object.
6329 *
6330 * This function is the main SLI4 device intialization PCI function. This
6331 * function is called by the HBA intialization code, HBA reset code and
6332 * HBA error attention handler code. Caller is not required to hold any
6333 * locks.
6334 **/
6335int
6336lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6337{
6338 int rc;
6339 LPFC_MBOXQ_t *mboxq;
6340 struct lpfc_mqe *mqe;
6341 uint8_t *vpd;
6342 uint32_t vpd_size;
6343 uint32_t ftr_rsp = 0;
6344 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6345 struct lpfc_vport *vport = phba->pport;
6346 struct lpfc_dmabuf *mp;
6347
6348 /* Perform a PCI function reset to start from clean */
6349 rc = lpfc_pci_function_reset(phba);
6350 if (unlikely(rc))
6351 return -ENODEV;
6352
6353 /* Check the HBA Host Status Register for readyness */
6354 rc = lpfc_sli4_post_status_check(phba);
6355 if (unlikely(rc))
6356 return -ENODEV;
6357 else {
6358 spin_lock_irq(&phba->hbalock);
6359 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6360 spin_unlock_irq(&phba->hbalock);
6361 }
6362
6363 /*
6364 * Allocate a single mailbox container for initializing the
6365 * port.
6366 */
6367 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6368 if (!mboxq)
6369 return -ENOMEM;
6370
James Smartda0436e2009-05-22 14:51:39 -04006371 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006372 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006373 vpd = kzalloc(vpd_size, GFP_KERNEL);
6374 if (!vpd) {
6375 rc = -ENOMEM;
6376 goto out_free_mbox;
6377 }
6378
6379 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006380 if (unlikely(rc)) {
6381 kfree(vpd);
6382 goto out_free_mbox;
6383 }
James Smart572709e2013-07-15 18:32:43 -04006384
James Smartda0436e2009-05-22 14:51:39 -04006385 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006386 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006387 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006388 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006389 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6390 } else {
James Smart76a95d72010-11-20 23:11:48 -05006391 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006392 }
James Smart45ed1192009-10-02 15:17:02 -04006393
6394 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6395 LPFC_DCBX_CEE_MODE)
6396 phba->hba_flag |= HBA_FIP_SUPPORT;
6397 else
6398 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6399
James Smart4f2e66c2012-05-09 21:17:07 -04006400 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6401
James Smartc31098c2011-04-16 11:03:33 -04006402 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006403 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6404 "0376 READ_REV Error. SLI Level %d "
6405 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006406 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006407 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006408 kfree(vpd);
6409 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006410 }
James Smartcd1c8302011-10-10 21:33:25 -04006411
6412 /*
James Smartff78d8f2011-12-13 13:21:35 -05006413 * Continue initialization with default values even if driver failed
6414 * to read FCoE param config regions, only read parameters if the
6415 * board is FCoE
6416 */
6417 if (phba->hba_flag & HBA_FCOE_MODE &&
6418 lpfc_sli4_read_fcoe_params(phba))
6419 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6420 "2570 Failed to read FCoE parameters\n");
6421
6422 /*
James Smartcd1c8302011-10-10 21:33:25 -04006423 * Retrieve sli4 device physical port name, failure of doing it
6424 * is considered as non-fatal.
6425 */
6426 rc = lpfc_sli4_retrieve_pport_name(phba);
6427 if (!rc)
6428 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6429 "3080 Successful retrieving SLI4 device "
6430 "physical port name: %s.\n", phba->Port);
6431
James Smartda0436e2009-05-22 14:51:39 -04006432 /*
6433 * Evaluate the read rev and vpd data. Populate the driver
6434 * state with the results. If this routine fails, the failure
6435 * is not fatal as the driver will use generic values.
6436 */
6437 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6438 if (unlikely(!rc)) {
6439 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6440 "0377 Error %d parsing vpd. "
6441 "Using defaults.\n", rc);
6442 rc = 0;
6443 }
James Smart76a95d72010-11-20 23:11:48 -05006444 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006445
James Smartf1126682009-06-10 17:22:44 -04006446 /* Save information as VPD data */
6447 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6448 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6449 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6450 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6451 &mqe->un.read_rev);
6452 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6453 &mqe->un.read_rev);
6454 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6455 &mqe->un.read_rev);
6456 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6457 &mqe->un.read_rev);
6458 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6459 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6460 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6461 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6462 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6463 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6464 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6465 "(%d):0380 READ_REV Status x%x "
6466 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6467 mboxq->vport ? mboxq->vport->vpi : 0,
6468 bf_get(lpfc_mqe_status, mqe),
6469 phba->vpd.rev.opFwName,
6470 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6471 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006472
James Smart572709e2013-07-15 18:32:43 -04006473 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6474 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6475 if (phba->pport->cfg_lun_queue_depth > rc) {
6476 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6477 "3362 LUN queue depth changed from %d to %d\n",
6478 phba->pport->cfg_lun_queue_depth, rc);
6479 phba->pport->cfg_lun_queue_depth = rc;
6480 }
6481
James Smart65791f12016-07-06 12:35:56 -07006482 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006483 LPFC_SLI_INTF_IF_TYPE_0) {
6484 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6485 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6486 if (rc == MBX_SUCCESS) {
6487 phba->hba_flag |= HBA_RECOVERABLE_UE;
6488 /* Set 1Sec interval to detect UE */
6489 phba->eratt_poll_interval = 1;
6490 phba->sli4_hba.ue_to_sr = bf_get(
6491 lpfc_mbx_set_feature_UESR,
6492 &mboxq->u.mqe.un.set_feature);
6493 phba->sli4_hba.ue_to_rp = bf_get(
6494 lpfc_mbx_set_feature_UERP,
6495 &mboxq->u.mqe.un.set_feature);
6496 }
6497 }
6498
6499 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6500 /* Enable MDS Diagnostics only if the SLI Port supports it */
6501 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6502 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6503 if (rc != MBX_SUCCESS)
6504 phba->mds_diags_support = 0;
6505 }
James Smart572709e2013-07-15 18:32:43 -04006506
James Smartda0436e2009-05-22 14:51:39 -04006507 /*
6508 * Discover the port's supported feature set and match it against the
6509 * hosts requests.
6510 */
6511 lpfc_request_features(phba, mboxq);
6512 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6513 if (unlikely(rc)) {
6514 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006515 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006516 }
6517
6518 /*
6519 * The port must support FCP initiator mode as this is the
6520 * only mode running in the host.
6521 */
6522 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6523 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6524 "0378 No support for fcpi mode.\n");
6525 ftr_rsp++;
6526 }
James Smartfedd3b72011-02-16 12:39:24 -05006527 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6528 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6529 else
6530 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006531 /*
6532 * If the port cannot support the host's requested features
6533 * then turn off the global config parameters to disable the
6534 * feature in the driver. This is not a fatal error.
6535 */
James Smartbf086112011-08-21 21:48:13 -04006536 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6537 if (phba->cfg_enable_bg) {
6538 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6539 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6540 else
6541 ftr_rsp++;
6542 }
James Smartda0436e2009-05-22 14:51:39 -04006543
6544 if (phba->max_vpi && phba->cfg_enable_npiv &&
6545 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6546 ftr_rsp++;
6547
6548 if (ftr_rsp) {
6549 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6550 "0379 Feature Mismatch Data: x%08x %08x "
6551 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6552 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6553 phba->cfg_enable_npiv, phba->max_vpi);
6554 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6555 phba->cfg_enable_bg = 0;
6556 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6557 phba->cfg_enable_npiv = 0;
6558 }
6559
6560 /* These SLI3 features are assumed in SLI4 */
6561 spin_lock_irq(&phba->hbalock);
6562 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6563 spin_unlock_irq(&phba->hbalock);
6564
James Smart6d368e52011-05-24 11:44:12 -04006565 /*
6566 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6567 * calls depends on these resources to complete port setup.
6568 */
6569 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6570 if (rc) {
6571 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6572 "2920 Failed to alloc Resource IDs "
6573 "rc = x%x\n", rc);
6574 goto out_free_mbox;
6575 }
6576
James Smart61bda8f2016-10-13 15:06:05 -07006577 lpfc_set_host_data(phba, mboxq);
6578
6579 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6580 if (rc) {
6581 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6582 "2134 Failed to set host os driver version %x",
6583 rc);
6584 }
6585
James Smartda0436e2009-05-22 14:51:39 -04006586 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006587 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6588 if (rc) {
6589 phba->link_state = LPFC_HBA_ERROR;
6590 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006591 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006592 }
6593
James Smartda0436e2009-05-22 14:51:39 -04006594 mboxq->vport = vport;
6595 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6596 mp = (struct lpfc_dmabuf *) mboxq->context1;
6597 if (rc == MBX_SUCCESS) {
6598 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6599 rc = 0;
6600 }
6601
6602 /*
6603 * This memory was allocated by the lpfc_read_sparam routine. Release
6604 * it to the mbuf pool.
6605 */
6606 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6607 kfree(mp);
6608 mboxq->context1 = NULL;
6609 if (unlikely(rc)) {
6610 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6611 "0382 READ_SPARAM command failed "
6612 "status %d, mbxStatus x%x\n",
6613 rc, bf_get(lpfc_mqe_status, mqe));
6614 phba->link_state = LPFC_HBA_ERROR;
6615 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006616 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006617 }
6618
James Smart05580562011-05-24 11:40:48 -04006619 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006620
6621 /* Update the fc_host data structures with new wwn. */
6622 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6623 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6624
James Smart8a9d2e82012-05-09 21:16:12 -04006625 /* update host els and scsi xri-sgl sizes and mappings */
6626 rc = lpfc_sli4_xri_sgl_update(phba);
6627 if (unlikely(rc)) {
6628 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6629 "1400 Failed to update xri-sgl size and "
6630 "mapping: %d\n", rc);
6631 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006632 }
6633
James Smart8a9d2e82012-05-09 21:16:12 -04006634 /* register the els sgl pool to the port */
6635 rc = lpfc_sli4_repost_els_sgl_list(phba);
6636 if (unlikely(rc)) {
6637 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6638 "0582 Error %d during els sgl post "
6639 "operation\n", rc);
6640 rc = -ENODEV;
6641 goto out_free_mbox;
6642 }
6643
6644 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006645 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6646 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006647 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006648 "0383 Error %d during scsi sgl post "
6649 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006650 /* Some Scsi buffers were moved to the abort scsi list */
6651 /* A pci function reset will repost them */
6652 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006653 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006654 }
6655
6656 /* Post the rpi header region to the device. */
6657 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6658 if (unlikely(rc)) {
6659 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6660 "0393 Error %d during rpi post operation\n",
6661 rc);
6662 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006663 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006664 }
James Smart97f2ecf2012-03-01 22:35:23 -05006665 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006666
James Smart5350d872011-10-10 21:33:49 -04006667 /* Create all the SLI4 queues */
6668 rc = lpfc_sli4_queue_create(phba);
6669 if (rc) {
6670 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6671 "3089 Failed to allocate queues\n");
6672 rc = -ENODEV;
6673 goto out_stop_timers;
6674 }
James Smartda0436e2009-05-22 14:51:39 -04006675 /* Set up all the queues to the device */
6676 rc = lpfc_sli4_queue_setup(phba);
6677 if (unlikely(rc)) {
6678 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6679 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006680 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006681 }
6682
6683 /* Arm the CQs and then EQs on device */
6684 lpfc_sli4_arm_cqeq_intr(phba);
6685
6686 /* Indicate device interrupt mode */
6687 phba->sli4_hba.intr_enable = 1;
6688
6689 /* Allow asynchronous mailbox command to go through */
6690 spin_lock_irq(&phba->hbalock);
6691 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6692 spin_unlock_irq(&phba->hbalock);
6693
6694 /* Post receive buffers to the device */
6695 lpfc_sli4_rb_setup(phba);
6696
James Smartfc2b9892010-02-26 14:15:29 -05006697 /* Reset HBA FCF states after HBA reset */
6698 phba->fcf.fcf_flag = 0;
6699 phba->fcf.current_rec.flag = 0;
6700
James Smartda0436e2009-05-22 14:51:39 -04006701 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006702 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006703 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04006704
6705 /* Start heart beat timer */
6706 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006707 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006708 phba->hb_outstanding = 0;
6709 phba->last_completion_time = jiffies;
6710
6711 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04006712 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07006713 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04006714
James Smart75baf692010-06-08 18:31:21 -04006715 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6716 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6717 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6718 if (!rc) {
6719 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6720 "2829 This device supports "
6721 "Advanced Error Reporting (AER)\n");
6722 spin_lock_irq(&phba->hbalock);
6723 phba->hba_flag |= HBA_AER_ENABLED;
6724 spin_unlock_irq(&phba->hbalock);
6725 } else {
6726 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6727 "2830 This device does not support "
6728 "Advanced Error Reporting (AER)\n");
6729 phba->cfg_aer_support = 0;
6730 }
James Smart0a96e972011-07-22 18:37:28 -04006731 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006732 }
6733
James Smart76a95d72010-11-20 23:11:48 -05006734 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6735 /*
6736 * The FC Port needs to register FCFI (index 0)
6737 */
6738 lpfc_reg_fcfi(phba, mboxq);
6739 mboxq->vport = phba->pport;
6740 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006741 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006742 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006743 rc = 0;
6744 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6745 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006746
6747 /* Check if the port is configured to be disabled */
6748 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006749 }
James Smart026abb82011-12-13 13:20:45 -05006750
James Smartda0436e2009-05-22 14:51:39 -04006751 /*
6752 * The port is ready, set the host's link state to LINK_DOWN
6753 * in preparation for link interrupts.
6754 */
James Smartda0436e2009-05-22 14:51:39 -04006755 spin_lock_irq(&phba->hbalock);
6756 phba->link_state = LPFC_LINK_DOWN;
6757 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006758 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6759 (phba->hba_flag & LINK_DISABLED)) {
6760 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6761 "3103 Adapter Link is disabled.\n");
6762 lpfc_down_link(phba, mboxq);
6763 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6764 if (rc != MBX_SUCCESS) {
6765 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6766 "3104 Adapter failed to issue "
6767 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6768 goto out_unset_queue;
6769 }
6770 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006771 /* don't perform init_link on SLI4 FC port loopback test */
6772 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6773 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6774 if (rc)
6775 goto out_unset_queue;
6776 }
James Smart5350d872011-10-10 21:33:49 -04006777 }
6778 mempool_free(mboxq, phba->mbox_mem_pool);
6779 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006780out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006781 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006782 lpfc_sli4_queue_unset(phba);
6783out_destroy_queue:
6784 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006785out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006786 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006787out_free_mbox:
6788 mempool_free(mboxq, phba->mbox_mem_pool);
6789 return rc;
6790}
James Smarte59058c2008-08-24 21:49:00 -04006791
6792/**
James Smart3621a712009-04-06 18:47:14 -04006793 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006794 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006795 *
James Smarte59058c2008-08-24 21:49:00 -04006796 * This is the callback function for mailbox timer. The mailbox
6797 * timer is armed when a new mailbox command is issued and the timer
6798 * is deleted when the mailbox complete. The function is called by
6799 * the kernel timer code when a mailbox does not complete within
6800 * expected time. This function wakes up the worker thread to
6801 * process the mailbox timeout and returns. All the processing is
6802 * done by the worker thread function lpfc_mbox_timeout_handler.
6803 **/
dea31012005-04-17 16:05:31 -05006804void
6805lpfc_mbox_timeout(unsigned long ptr)
6806{
James Smart92d7f7b2007-06-17 19:56:38 -05006807 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006808 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006809 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006810
James Smart2e0fef82007-06-17 19:56:36 -05006811 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006812 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006813 if (!tmo_posted)
6814 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6815 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6816
James Smart5e9d9b82008-06-14 22:52:53 -04006817 if (!tmo_posted)
6818 lpfc_worker_wake_up(phba);
6819 return;
dea31012005-04-17 16:05:31 -05006820}
6821
James Smarte8d3c3b2013-10-10 12:21:30 -04006822/**
6823 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6824 * are pending
6825 * @phba: Pointer to HBA context object.
6826 *
6827 * This function checks if any mailbox completions are present on the mailbox
6828 * completion queue.
6829 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04006830static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04006831lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6832{
6833
6834 uint32_t idx;
6835 struct lpfc_queue *mcq;
6836 struct lpfc_mcqe *mcqe;
6837 bool pending_completions = false;
6838
6839 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6840 return false;
6841
6842 /* Check for completions on mailbox completion queue */
6843
6844 mcq = phba->sli4_hba.mbx_cq;
6845 idx = mcq->hba_index;
6846 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6847 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6848 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6849 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6850 pending_completions = true;
6851 break;
6852 }
6853 idx = (idx + 1) % mcq->entry_count;
6854 if (mcq->hba_index == idx)
6855 break;
6856 }
6857 return pending_completions;
6858
6859}
6860
6861/**
6862 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6863 * that were missed.
6864 * @phba: Pointer to HBA context object.
6865 *
6866 * For sli4, it is possible to miss an interrupt. As such mbox completions
6867 * maybe missed causing erroneous mailbox timeouts to occur. This function
6868 * checks to see if mbox completions are on the mailbox completion queue
6869 * and will process all the completions associated with the eq for the
6870 * mailbox completion queue.
6871 **/
6872bool
6873lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6874{
6875
6876 uint32_t eqidx;
6877 struct lpfc_queue *fpeq = NULL;
6878 struct lpfc_eqe *eqe;
6879 bool mbox_pending;
6880
6881 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6882 return false;
6883
6884 /* Find the eq associated with the mcq */
6885
6886 if (phba->sli4_hba.hba_eq)
6887 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6888 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6889 phba->sli4_hba.mbx_cq->assoc_qid) {
6890 fpeq = phba->sli4_hba.hba_eq[eqidx];
6891 break;
6892 }
6893 if (!fpeq)
6894 return false;
6895
6896 /* Turn off interrupts from this EQ */
6897
6898 lpfc_sli4_eq_clr_intr(fpeq);
6899
6900 /* Check to see if a mbox completion is pending */
6901
6902 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6903
6904 /*
6905 * If a mbox completion is pending, process all the events on EQ
6906 * associated with the mbox completion queue (this could include
6907 * mailbox commands, async events, els commands, receive queue data
6908 * and fcp commands)
6909 */
6910
6911 if (mbox_pending)
6912 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6913 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6914 fpeq->EQ_processed++;
6915 }
6916
6917 /* Always clear and re-arm the EQ */
6918
6919 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6920
6921 return mbox_pending;
6922
6923}
James Smarte59058c2008-08-24 21:49:00 -04006924
6925/**
James Smart3621a712009-04-06 18:47:14 -04006926 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006927 * @phba: Pointer to HBA context object.
6928 *
6929 * This function is called from worker thread when a mailbox command times out.
6930 * The caller is not required to hold any locks. This function will reset the
6931 * HBA and recover all the pending commands.
6932 **/
dea31012005-04-17 16:05:31 -05006933void
6934lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6935{
James Smart2e0fef82007-06-17 19:56:36 -05006936 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04006937 MAILBOX_t *mb = NULL;
6938
James Smart1dcb58e2007-04-25 09:51:30 -04006939 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006940
James Smarte8d3c3b2013-10-10 12:21:30 -04006941 /* If the mailbox completed, process the completion and return */
6942 if (lpfc_sli4_process_missed_mbox_completions(phba))
6943 return;
6944
James Smarteb016562014-09-03 12:58:06 -04006945 if (pmbox != NULL)
6946 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04006947 /* Check the pmbox pointer first. There is a race condition
6948 * between the mbox timeout handler getting executed in the
6949 * worklist and the mailbox actually completing. When this
6950 * race condition occurs, the mbox_active will be NULL.
6951 */
6952 spin_lock_irq(&phba->hbalock);
6953 if (pmbox == NULL) {
6954 lpfc_printf_log(phba, KERN_WARNING,
6955 LOG_MBOX | LOG_SLI,
6956 "0353 Active Mailbox cleared - mailbox timeout "
6957 "exiting\n");
6958 spin_unlock_irq(&phba->hbalock);
6959 return;
6960 }
6961
dea31012005-04-17 16:05:31 -05006962 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006963 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006964 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006965 mb->mbxCommand,
6966 phba->pport->port_state,
6967 phba->sli.sli_flag,
6968 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006969 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006970
James Smart1dcb58e2007-04-25 09:51:30 -04006971 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6972 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006973 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006974 */
James Smart2e0fef82007-06-17 19:56:36 -05006975 spin_lock_irq(&phba->pport->work_port_lock);
6976 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6977 spin_unlock_irq(&phba->pport->work_port_lock);
6978 spin_lock_irq(&phba->hbalock);
6979 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006980 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006981 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006982
James Smartdb55fba2014-04-04 13:52:02 -04006983 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04006984
6985 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006986 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006987
6988 /* Reset the HBA device */
6989 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006990}
6991
James Smarte59058c2008-08-24 21:49:00 -04006992/**
James Smart3772a992009-05-22 14:50:54 -04006993 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006994 * @phba: Pointer to HBA context object.
6995 * @pmbox: Pointer to mailbox object.
6996 * @flag: Flag indicating how the mailbox need to be processed.
6997 *
6998 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006999 * to submit a mailbox command to firmware with SLI-3 interface spec. This
7000 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04007001 * The mailbox command can be submitted in polling mode, in which case
7002 * this function will wait in a polling loop for the completion of the
7003 * mailbox.
7004 * If the mailbox is submitted in no_wait mode (not polling) the
7005 * function will submit the command and returns immediately without waiting
7006 * for the mailbox completion. The no_wait is supported only when HBA
7007 * is in SLI2/SLI3 mode - interrupts are enabled.
7008 * The SLI interface allows only one mailbox pending at a time. If the
7009 * mailbox is issued in polling mode and there is already a mailbox
7010 * pending, then the function will return an error. If the mailbox is issued
7011 * in NO_WAIT mode and there is a mailbox pending already, the function
7012 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
7013 * The sli layer owns the mailbox object until the completion of mailbox
7014 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7015 * return codes the caller owns the mailbox command after the return of
7016 * the function.
7017 **/
James Smart3772a992009-05-22 14:50:54 -04007018static int
7019lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7020 uint32_t flag)
dea31012005-04-17 16:05:31 -05007021{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007022 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007023 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007024 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007025 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007026 int i;
James Smart09372822008-01-11 01:52:54 -05007027 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007028 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007029 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007030 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007031 int processing_queue = 0;
7032
7033 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7034 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007035 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007036 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007037 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7038 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7039 return MBX_SUCCESS;
7040 }
James Smart58da1ff2008-04-07 10:15:56 -04007041 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007042 pmbox = lpfc_mbox_get(phba);
7043 if (!pmbox) {
7044 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7045 return MBX_SUCCESS;
7046 }
7047 }
dea31012005-04-17 16:05:31 -05007048
James Smarted957682007-06-17 19:56:37 -05007049 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007050 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007051 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007052 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007053 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007054 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007055 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007056 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007057 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007058 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007059 }
7060 }
7061
Linas Vepstas8d63f372007-02-14 14:28:36 -06007062 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007063 if (unlikely(pci_channel_offline(phba->pcidev))) {
7064 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7065 goto out_not_finished;
7066 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007067
James Smarta257bf92009-04-06 18:48:10 -04007068 /* If HBA has a deferred error attention, fail the iocb. */
7069 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7070 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7071 goto out_not_finished;
7072 }
7073
dea31012005-04-17 16:05:31 -05007074 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007075
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007076 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007077 status = MBX_SUCCESS;
7078
James Smart2e0fef82007-06-17 19:56:36 -05007079 if (phba->link_state == LPFC_HBA_ERROR) {
7080 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007081
7082 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007083 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7084 "(%d):0311 Mailbox command x%x cannot "
7085 "issue Data: x%x x%x\n",
7086 pmbox->vport ? pmbox->vport->vpi : 0,
7087 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007088 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007089 }
7090
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007091 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007092 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7093 !(hc_copy & HC_MBINT_ENA)) {
7094 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7095 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007096 "(%d):2528 Mailbox command x%x cannot "
7097 "issue Data: x%x x%x\n",
7098 pmbox->vport ? pmbox->vport->vpi : 0,
7099 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007100 goto out_not_finished;
7101 }
James Smart92908312006-03-07 15:04:13 -05007102 }
7103
dea31012005-04-17 16:05:31 -05007104 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7105 /* Polling for a mbox command when another one is already active
7106 * is not allowed in SLI. Also, the driver must have established
7107 * SLI2 mode to queue and process multiple mbox commands.
7108 */
7109
7110 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007111 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007112
7113 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007114 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7115 "(%d):2529 Mailbox command x%x "
7116 "cannot issue Data: x%x x%x\n",
7117 pmbox->vport ? pmbox->vport->vpi : 0,
7118 pmbox->u.mb.mbxCommand,
7119 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007120 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007121 }
7122
James Smart3772a992009-05-22 14:50:54 -04007123 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007124 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007125 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007126 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7127 "(%d):2530 Mailbox command x%x "
7128 "cannot issue Data: x%x x%x\n",
7129 pmbox->vport ? pmbox->vport->vpi : 0,
7130 pmbox->u.mb.mbxCommand,
7131 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007132 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007133 }
7134
dea31012005-04-17 16:05:31 -05007135 /* Another mailbox command is still being processed, queue this
7136 * command to be processed later.
7137 */
7138 lpfc_mbox_put(phba, pmbox);
7139
7140 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007141 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007142 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007143 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007144 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007145 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007146 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007147
7148 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007149 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007150
James Smart858c9f62007-06-17 19:56:39 -05007151 if (pmbox->vport) {
7152 lpfc_debugfs_disc_trc(pmbox->vport,
7153 LPFC_DISC_TRC_MBOX_VPORT,
7154 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007155 (uint32_t)mbx->mbxCommand,
7156 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007157 }
7158 else {
7159 lpfc_debugfs_disc_trc(phba->pport,
7160 LPFC_DISC_TRC_MBOX,
7161 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007162 (uint32_t)mbx->mbxCommand,
7163 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007164 }
7165
James Smart2e0fef82007-06-17 19:56:36 -05007166 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007167 }
7168
dea31012005-04-17 16:05:31 -05007169 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7170
7171 /* If we are not polling, we MUST be in SLI2 mode */
7172 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007173 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007174 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007175 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007176 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007177 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007178 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7179 "(%d):2531 Mailbox command x%x "
7180 "cannot issue Data: x%x x%x\n",
7181 pmbox->vport ? pmbox->vport->vpi : 0,
7182 pmbox->u.mb.mbxCommand,
7183 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007184 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007185 }
7186 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007187 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7188 1000);
7189 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007190 }
7191
7192 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007193 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007194 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007195 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007196 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007197 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007198 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007199
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007200 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007201 if (pmbox->vport) {
7202 lpfc_debugfs_disc_trc(pmbox->vport,
7203 LPFC_DISC_TRC_MBOX_VPORT,
7204 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007205 (uint32_t)mbx->mbxCommand,
7206 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007207 }
7208 else {
7209 lpfc_debugfs_disc_trc(phba->pport,
7210 LPFC_DISC_TRC_MBOX,
7211 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007212 (uint32_t)mbx->mbxCommand,
7213 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007214 }
7215 }
7216
dea31012005-04-17 16:05:31 -05007217 psli->slistat.mbox_cmd++;
7218 evtctr = psli->slistat.mbox_event;
7219
7220 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007221 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007222
James Smart3772a992009-05-22 14:50:54 -04007223 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007224 /* Populate mbox extension offset word. */
7225 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007226 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007227 = (uint8_t *)phba->mbox_ext
7228 - (uint8_t *)phba->mbox;
7229 }
7230
7231 /* Copy the mailbox extension data */
7232 if (pmbox->in_ext_byte_len && pmbox->context2) {
7233 lpfc_sli_pcimem_bcopy(pmbox->context2,
7234 (uint8_t *)phba->mbox_ext,
7235 pmbox->in_ext_byte_len);
7236 }
7237 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007238 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007239 } else {
James Smart7a470272010-03-15 11:25:20 -04007240 /* Populate mbox extension offset word. */
7241 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007242 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007243 = MAILBOX_HBA_EXT_OFFSET;
7244
7245 /* Copy the mailbox extension data */
7246 if (pmbox->in_ext_byte_len && pmbox->context2) {
7247 lpfc_memcpy_to_slim(phba->MBslimaddr +
7248 MAILBOX_HBA_EXT_OFFSET,
7249 pmbox->context2, pmbox->in_ext_byte_len);
7250
7251 }
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007252 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007253 /* copy command data into host mbox for cmpl */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007254 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007255 }
7256
7257 /* First copy mbox command data to HBA SLIM, skip past first
7258 word */
7259 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007260 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007261 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7262
7263 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007264 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007265 to_slim = phba->MBslimaddr;
7266 writel(ldata, to_slim);
7267 readl(to_slim); /* flush */
7268
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007269 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007270 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007271 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007272 }
7273 }
7274
7275 wmb();
dea31012005-04-17 16:05:31 -05007276
7277 switch (flag) {
7278 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007279 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007280 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007281 /* Interrupt board to do it */
7282 writel(CA_MBATT, phba->CAregaddr);
7283 readl(phba->CAregaddr); /* flush */
7284 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007285 break;
7286
7287 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007288 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007289 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007290 /* Interrupt board to do it */
7291 writel(CA_MBATT, phba->CAregaddr);
7292 readl(phba->CAregaddr); /* flush */
7293
James Smart3772a992009-05-22 14:50:54 -04007294 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007295 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007296 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007297 word0 = le32_to_cpu(word0);
7298 } else {
7299 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007300 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7301 spin_unlock_irqrestore(&phba->hbalock,
7302 drvr_flag);
7303 goto out_not_finished;
7304 }
dea31012005-04-17 16:05:31 -05007305 }
7306
7307 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007308 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7309 spin_unlock_irqrestore(&phba->hbalock,
7310 drvr_flag);
7311 goto out_not_finished;
7312 }
James Smarta183a152011-10-10 21:32:43 -04007313 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7314 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007315 i = 0;
dea31012005-04-17 16:05:31 -05007316 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007317 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7318 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007319 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007320 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007321 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007322 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007323 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007324 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007325 }
7326
7327 /* Check if we took a mbox interrupt while we were
7328 polling */
7329 if (((word0 & OWN_CHIP) != OWN_CHIP)
7330 && (evtctr != psli->slistat.mbox_event))
7331 break;
7332
James Smart09372822008-01-11 01:52:54 -05007333 if (i++ > 10) {
7334 spin_unlock_irqrestore(&phba->hbalock,
7335 drvr_flag);
7336 msleep(1);
7337 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7338 }
dea31012005-04-17 16:05:31 -05007339
James Smart3772a992009-05-22 14:50:54 -04007340 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007341 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007342 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007343 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007344 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007345 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007346 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007347 /* Check real SLIM for any errors */
7348 slimword0 = readl(phba->MBslimaddr);
7349 slimmb = (MAILBOX_t *) & slimword0;
7350 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7351 && slimmb->mbxStatus) {
7352 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007353 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007354 word0 = slimword0;
7355 }
7356 }
7357 } else {
7358 /* First copy command data */
7359 word0 = readl(phba->MBslimaddr);
7360 }
7361 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007362 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7363 spin_unlock_irqrestore(&phba->hbalock,
7364 drvr_flag);
7365 goto out_not_finished;
7366 }
dea31012005-04-17 16:05:31 -05007367 }
7368
James Smart3772a992009-05-22 14:50:54 -04007369 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007370 /* copy results back to user */
James Smart2ea259e2017-02-12 13:52:27 -08007371 lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
7372 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007373 /* Copy the mailbox extension data */
7374 if (pmbox->out_ext_byte_len && pmbox->context2) {
7375 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7376 pmbox->context2,
7377 pmbox->out_ext_byte_len);
7378 }
dea31012005-04-17 16:05:31 -05007379 } else {
7380 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007381 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
James Smart2ea259e2017-02-12 13:52:27 -08007382 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007383 /* Copy the mailbox extension data */
7384 if (pmbox->out_ext_byte_len && pmbox->context2) {
7385 lpfc_memcpy_from_slim(pmbox->context2,
7386 phba->MBslimaddr +
7387 MAILBOX_HBA_EXT_OFFSET,
7388 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007389 }
7390 }
7391
7392 writel(HA_MBATT, phba->HAregaddr);
7393 readl(phba->HAregaddr); /* flush */
7394
7395 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007396 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007397 }
7398
James Smart2e0fef82007-06-17 19:56:36 -05007399 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7400 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007401
7402out_not_finished:
7403 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007404 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007405 lpfc_mbox_cmpl_put(phba, pmbox);
7406 }
7407 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007408}
7409
James Smarte59058c2008-08-24 21:49:00 -04007410/**
James Smartf1126682009-06-10 17:22:44 -04007411 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7412 * @phba: Pointer to HBA context object.
7413 *
7414 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7415 * the driver internal pending mailbox queue. It will then try to wait out the
7416 * possible outstanding mailbox command before return.
7417 *
7418 * Returns:
7419 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7420 * the outstanding mailbox command timed out.
7421 **/
7422static int
7423lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7424{
7425 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007426 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007427 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007428
7429 /* Mark the asynchronous mailbox command posting as blocked */
7430 spin_lock_irq(&phba->hbalock);
7431 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007432 /* Determine how long we might wait for the active mailbox
7433 * command to be gracefully completed by firmware.
7434 */
James Smarta183a152011-10-10 21:32:43 -04007435 if (phba->sli.mbox_active)
7436 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7437 phba->sli.mbox_active) *
7438 1000) + jiffies;
7439 spin_unlock_irq(&phba->hbalock);
7440
James Smarte8d3c3b2013-10-10 12:21:30 -04007441 /* Make sure the mailbox is really active */
7442 if (timeout)
7443 lpfc_sli4_process_missed_mbox_completions(phba);
7444
James Smartf1126682009-06-10 17:22:44 -04007445 /* Wait for the outstnading mailbox command to complete */
7446 while (phba->sli.mbox_active) {
7447 /* Check active mailbox complete status every 2ms */
7448 msleep(2);
7449 if (time_after(jiffies, timeout)) {
7450 /* Timeout, marked the outstanding cmd not complete */
7451 rc = 1;
7452 break;
7453 }
7454 }
7455
7456 /* Can not cleanly block async mailbox command, fails it */
7457 if (rc) {
7458 spin_lock_irq(&phba->hbalock);
7459 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7460 spin_unlock_irq(&phba->hbalock);
7461 }
7462 return rc;
7463}
7464
7465/**
7466 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7467 * @phba: Pointer to HBA context object.
7468 *
7469 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7470 * commands from the driver internal pending mailbox queue. It makes sure
7471 * that there is no outstanding mailbox command before resuming posting
7472 * asynchronous mailbox commands. If, for any reason, there is outstanding
7473 * mailbox command, it will try to wait it out before resuming asynchronous
7474 * mailbox command posting.
7475 **/
7476static void
7477lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7478{
7479 struct lpfc_sli *psli = &phba->sli;
7480
7481 spin_lock_irq(&phba->hbalock);
7482 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7483 /* Asynchronous mailbox posting is not blocked, do nothing */
7484 spin_unlock_irq(&phba->hbalock);
7485 return;
7486 }
7487
7488 /* Outstanding synchronous mailbox command is guaranteed to be done,
7489 * successful or timeout, after timing-out the outstanding mailbox
7490 * command shall always be removed, so just unblock posting async
7491 * mailbox command and resume
7492 */
7493 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7494 spin_unlock_irq(&phba->hbalock);
7495
7496 /* wake up worker thread to post asynchronlous mailbox command */
7497 lpfc_worker_wake_up(phba);
7498}
7499
7500/**
James Smart2d843ed2012-09-29 11:29:06 -04007501 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7502 * @phba: Pointer to HBA context object.
7503 * @mboxq: Pointer to mailbox object.
7504 *
7505 * The function waits for the bootstrap mailbox register ready bit from
7506 * port for twice the regular mailbox command timeout value.
7507 *
7508 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7509 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7510 **/
7511static int
7512lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7513{
7514 uint32_t db_ready;
7515 unsigned long timeout;
7516 struct lpfc_register bmbx_reg;
7517
7518 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7519 * 1000) + jiffies;
7520
7521 do {
7522 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7523 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7524 if (!db_ready)
7525 msleep(2);
7526
7527 if (time_after(jiffies, timeout))
7528 return MBXERR_ERROR;
7529 } while (!db_ready);
7530
7531 return 0;
7532}
7533
7534/**
James Smartda0436e2009-05-22 14:51:39 -04007535 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7536 * @phba: Pointer to HBA context object.
7537 * @mboxq: Pointer to mailbox object.
7538 *
7539 * The function posts a mailbox to the port. The mailbox is expected
7540 * to be comletely filled in and ready for the port to operate on it.
7541 * This routine executes a synchronous completion operation on the
7542 * mailbox by polling for its completion.
7543 *
7544 * The caller must not be holding any locks when calling this routine.
7545 *
7546 * Returns:
7547 * MBX_SUCCESS - mailbox posted successfully
7548 * Any of the MBX error values.
7549 **/
7550static int
7551lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7552{
7553 int rc = MBX_SUCCESS;
7554 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007555 uint32_t mcqe_status;
7556 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007557 struct lpfc_sli *psli = &phba->sli;
7558 struct lpfc_mqe *mb = &mboxq->u.mqe;
7559 struct lpfc_bmbx_create *mbox_rgn;
7560 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007561
7562 /*
7563 * Only one mailbox can be active to the bootstrap mailbox region
7564 * at a time and there is no queueing provided.
7565 */
7566 spin_lock_irqsave(&phba->hbalock, iflag);
7567 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7568 spin_unlock_irqrestore(&phba->hbalock, iflag);
7569 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007570 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007571 "cannot issue Data: x%x x%x\n",
7572 mboxq->vport ? mboxq->vport->vpi : 0,
7573 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007574 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7575 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007576 psli->sli_flag, MBX_POLL);
7577 return MBXERR_ERROR;
7578 }
7579 /* The server grabs the token and owns it until release */
7580 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7581 phba->sli.mbox_active = mboxq;
7582 spin_unlock_irqrestore(&phba->hbalock, iflag);
7583
James Smart2d843ed2012-09-29 11:29:06 -04007584 /* wait for bootstrap mbox register for readyness */
7585 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7586 if (rc)
7587 goto exit;
7588
James Smartda0436e2009-05-22 14:51:39 -04007589 /*
7590 * Initialize the bootstrap memory region to avoid stale data areas
7591 * in the mailbox post. Then copy the caller's mailbox contents to
7592 * the bmbx mailbox region.
7593 */
7594 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7595 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7596 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7597 sizeof(struct lpfc_mqe));
7598
7599 /* Post the high mailbox dma address to the port and wait for ready. */
7600 dma_address = &phba->sli4_hba.bmbx.dma_address;
7601 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7602
James Smart2d843ed2012-09-29 11:29:06 -04007603 /* wait for bootstrap mbox register for hi-address write done */
7604 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7605 if (rc)
7606 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007607
7608 /* Post the low mailbox dma address to the port. */
7609 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007610
James Smart2d843ed2012-09-29 11:29:06 -04007611 /* wait for bootstrap mbox register for low address write done */
7612 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7613 if (rc)
7614 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007615
7616 /*
7617 * Read the CQ to ensure the mailbox has completed.
7618 * If so, update the mailbox status so that the upper layers
7619 * can complete the request normally.
7620 */
7621 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7622 sizeof(struct lpfc_mqe));
7623 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7624 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7625 sizeof(struct lpfc_mcqe));
7626 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007627 /*
7628 * When the CQE status indicates a failure and the mailbox status
7629 * indicates success then copy the CQE status into the mailbox status
7630 * (and prefix it with x4000).
7631 */
James Smartda0436e2009-05-22 14:51:39 -04007632 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007633 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7634 bf_set(lpfc_mqe_status, mb,
7635 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007636 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007637 } else
7638 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007639
7640 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007641 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007642 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7643 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007644 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7645 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7646 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007647 bf_get(lpfc_mqe_status, mb),
7648 mb->un.mb_words[0], mb->un.mb_words[1],
7649 mb->un.mb_words[2], mb->un.mb_words[3],
7650 mb->un.mb_words[4], mb->un.mb_words[5],
7651 mb->un.mb_words[6], mb->un.mb_words[7],
7652 mb->un.mb_words[8], mb->un.mb_words[9],
7653 mb->un.mb_words[10], mb->un.mb_words[11],
7654 mb->un.mb_words[12], mboxq->mcqe.word0,
7655 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7656 mboxq->mcqe.trailer);
7657exit:
7658 /* We are holding the token, no needed for lock when release */
7659 spin_lock_irqsave(&phba->hbalock, iflag);
7660 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7661 phba->sli.mbox_active = NULL;
7662 spin_unlock_irqrestore(&phba->hbalock, iflag);
7663 return rc;
7664}
7665
7666/**
7667 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7668 * @phba: Pointer to HBA context object.
7669 * @pmbox: Pointer to mailbox object.
7670 * @flag: Flag indicating how the mailbox need to be processed.
7671 *
7672 * This function is called by discovery code and HBA management code to submit
7673 * a mailbox command to firmware with SLI-4 interface spec.
7674 *
7675 * Return codes the caller owns the mailbox command after the return of the
7676 * function.
7677 **/
7678static int
7679lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7680 uint32_t flag)
7681{
7682 struct lpfc_sli *psli = &phba->sli;
7683 unsigned long iflags;
7684 int rc;
7685
James Smartb76f2dc2011-07-22 18:37:42 -04007686 /* dump from issue mailbox command if setup */
7687 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7688
James Smart8fa38512009-07-19 10:01:03 -04007689 rc = lpfc_mbox_dev_check(phba);
7690 if (unlikely(rc)) {
7691 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007692 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007693 "cannot issue Data: x%x x%x\n",
7694 mboxq->vport ? mboxq->vport->vpi : 0,
7695 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007696 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7697 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007698 psli->sli_flag, flag);
7699 goto out_not_finished;
7700 }
7701
James Smartda0436e2009-05-22 14:51:39 -04007702 /* Detect polling mode and jump to a handler */
7703 if (!phba->sli4_hba.intr_enable) {
7704 if (flag == MBX_POLL)
7705 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7706 else
7707 rc = -EIO;
7708 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007709 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007710 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007711 "(x%x/x%x) failure: "
7712 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7713 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007714 mboxq->vport ? mboxq->vport->vpi : 0,
7715 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007716 lpfc_sli_config_mbox_subsys_get(phba,
7717 mboxq),
7718 lpfc_sli_config_mbox_opcode_get(phba,
7719 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007720 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7721 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7722 bf_get(lpfc_mcqe_ext_status,
7723 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007724 psli->sli_flag, flag);
7725 return rc;
7726 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007727 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7728 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007729 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007730 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007731 mboxq->vport ? mboxq->vport->vpi : 0,
7732 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007733 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7734 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007735 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007736 /* Try to block the asynchronous mailbox posting */
7737 rc = lpfc_sli4_async_mbox_block(phba);
7738 if (!rc) {
7739 /* Successfully blocked, now issue sync mbox cmd */
7740 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7741 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007742 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007743 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007744 "(%d):2597 Sync Mailbox command "
7745 "x%x (x%x/x%x) failure: "
7746 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7747 "Data: x%x x%x\n,",
7748 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007749 mboxq->u.mb.mbxCommand,
7750 lpfc_sli_config_mbox_subsys_get(phba,
7751 mboxq),
7752 lpfc_sli_config_mbox_opcode_get(phba,
7753 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007754 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7755 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7756 bf_get(lpfc_mcqe_ext_status,
7757 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007758 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007759 /* Unblock the async mailbox posting afterward */
7760 lpfc_sli4_async_mbox_unblock(phba);
7761 }
7762 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007763 }
7764
7765 /* Now, interrupt mode asynchrous mailbox command */
7766 rc = lpfc_mbox_cmd_check(phba, mboxq);
7767 if (rc) {
7768 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007769 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007770 "cannot issue Data: x%x x%x\n",
7771 mboxq->vport ? mboxq->vport->vpi : 0,
7772 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007773 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7774 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007775 psli->sli_flag, flag);
7776 goto out_not_finished;
7777 }
James Smartda0436e2009-05-22 14:51:39 -04007778
7779 /* Put the mailbox command to the driver internal FIFO */
7780 psli->slistat.mbox_busy++;
7781 spin_lock_irqsave(&phba->hbalock, iflags);
7782 lpfc_mbox_put(phba, mboxq);
7783 spin_unlock_irqrestore(&phba->hbalock, iflags);
7784 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7785 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007786 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007787 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7788 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007789 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7790 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007791 phba->pport->port_state,
7792 psli->sli_flag, MBX_NOWAIT);
7793 /* Wake up worker thread to transport mailbox command from head */
7794 lpfc_worker_wake_up(phba);
7795
7796 return MBX_BUSY;
7797
7798out_not_finished:
7799 return MBX_NOT_FINISHED;
7800}
7801
7802/**
7803 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7804 * @phba: Pointer to HBA context object.
7805 *
7806 * This function is called by worker thread to send a mailbox command to
7807 * SLI4 HBA firmware.
7808 *
7809 **/
7810int
7811lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7812{
7813 struct lpfc_sli *psli = &phba->sli;
7814 LPFC_MBOXQ_t *mboxq;
7815 int rc = MBX_SUCCESS;
7816 unsigned long iflags;
7817 struct lpfc_mqe *mqe;
7818 uint32_t mbx_cmnd;
7819
7820 /* Check interrupt mode before post async mailbox command */
7821 if (unlikely(!phba->sli4_hba.intr_enable))
7822 return MBX_NOT_FINISHED;
7823
7824 /* Check for mailbox command service token */
7825 spin_lock_irqsave(&phba->hbalock, iflags);
7826 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7827 spin_unlock_irqrestore(&phba->hbalock, iflags);
7828 return MBX_NOT_FINISHED;
7829 }
7830 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7831 spin_unlock_irqrestore(&phba->hbalock, iflags);
7832 return MBX_NOT_FINISHED;
7833 }
7834 if (unlikely(phba->sli.mbox_active)) {
7835 spin_unlock_irqrestore(&phba->hbalock, iflags);
7836 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7837 "0384 There is pending active mailbox cmd\n");
7838 return MBX_NOT_FINISHED;
7839 }
7840 /* Take the mailbox command service token */
7841 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7842
7843 /* Get the next mailbox command from head of queue */
7844 mboxq = lpfc_mbox_get(phba);
7845
7846 /* If no more mailbox command waiting for post, we're done */
7847 if (!mboxq) {
7848 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7849 spin_unlock_irqrestore(&phba->hbalock, iflags);
7850 return MBX_SUCCESS;
7851 }
7852 phba->sli.mbox_active = mboxq;
7853 spin_unlock_irqrestore(&phba->hbalock, iflags);
7854
7855 /* Check device readiness for posting mailbox command */
7856 rc = lpfc_mbox_dev_check(phba);
7857 if (unlikely(rc))
7858 /* Driver clean routine will clean up pending mailbox */
7859 goto out_not_finished;
7860
7861 /* Prepare the mbox command to be posted */
7862 mqe = &mboxq->u.mqe;
7863 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7864
7865 /* Start timer for the mbox_tmo and log some mailbox post messages */
7866 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04007867 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007868
7869 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007870 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007871 "x%x x%x\n",
7872 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007873 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7874 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007875 phba->pport->port_state, psli->sli_flag);
7876
7877 if (mbx_cmnd != MBX_HEARTBEAT) {
7878 if (mboxq->vport) {
7879 lpfc_debugfs_disc_trc(mboxq->vport,
7880 LPFC_DISC_TRC_MBOX_VPORT,
7881 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7882 mbx_cmnd, mqe->un.mb_words[0],
7883 mqe->un.mb_words[1]);
7884 } else {
7885 lpfc_debugfs_disc_trc(phba->pport,
7886 LPFC_DISC_TRC_MBOX,
7887 "MBOX Send: cmd:x%x mb:x%x x%x",
7888 mbx_cmnd, mqe->un.mb_words[0],
7889 mqe->un.mb_words[1]);
7890 }
7891 }
7892 psli->slistat.mbox_cmd++;
7893
7894 /* Post the mailbox command to the port */
7895 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7896 if (rc != MBX_SUCCESS) {
7897 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007898 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007899 "cannot issue Data: x%x x%x\n",
7900 mboxq->vport ? mboxq->vport->vpi : 0,
7901 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007902 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7903 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007904 psli->sli_flag, MBX_NOWAIT);
7905 goto out_not_finished;
7906 }
7907
7908 return rc;
7909
7910out_not_finished:
7911 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007912 if (phba->sli.mbox_active) {
7913 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7914 __lpfc_mbox_cmpl_put(phba, mboxq);
7915 /* Release the token */
7916 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7917 phba->sli.mbox_active = NULL;
7918 }
James Smartda0436e2009-05-22 14:51:39 -04007919 spin_unlock_irqrestore(&phba->hbalock, iflags);
7920
7921 return MBX_NOT_FINISHED;
7922}
7923
7924/**
7925 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7926 * @phba: Pointer to HBA context object.
7927 * @pmbox: Pointer to mailbox object.
7928 * @flag: Flag indicating how the mailbox need to be processed.
7929 *
7930 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7931 * the API jump table function pointer from the lpfc_hba struct.
7932 *
7933 * Return codes the caller owns the mailbox command after the return of the
7934 * function.
7935 **/
7936int
7937lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7938{
7939 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7940}
7941
7942/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007943 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007944 * @phba: The hba struct for which this call is being executed.
7945 * @dev_grp: The HBA PCI-Device group number.
7946 *
7947 * This routine sets up the mbox interface API function jump table in @phba
7948 * struct.
7949 * Returns: 0 - success, -ENODEV - failure.
7950 **/
7951int
7952lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7953{
7954
7955 switch (dev_grp) {
7956 case LPFC_PCI_DEV_LP:
7957 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7958 phba->lpfc_sli_handle_slow_ring_event =
7959 lpfc_sli_handle_slow_ring_event_s3;
7960 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7961 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7962 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7963 break;
7964 case LPFC_PCI_DEV_OC:
7965 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7966 phba->lpfc_sli_handle_slow_ring_event =
7967 lpfc_sli_handle_slow_ring_event_s4;
7968 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7969 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7970 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7971 break;
7972 default:
7973 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7974 "1420 Invalid HBA PCI-device group: 0x%x\n",
7975 dev_grp);
7976 return -ENODEV;
7977 break;
7978 }
7979 return 0;
7980}
7981
7982/**
James Smart3621a712009-04-06 18:47:14 -04007983 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007984 * @phba: Pointer to HBA context object.
7985 * @pring: Pointer to driver SLI ring object.
7986 * @piocb: Pointer to address of newly added command iocb.
7987 *
7988 * This function is called with hbalock held to add a command
7989 * iocb to the txq when SLI layer cannot submit the command iocb
7990 * to the ring.
7991 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007992void
James Smart92d7f7b2007-06-17 19:56:38 -05007993__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007994 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007995{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01007996 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007997 /* Insert the caller's iocb in the txq tail for later processing. */
7998 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05007999}
8000
James Smarte59058c2008-08-24 21:49:00 -04008001/**
James Smart3621a712009-04-06 18:47:14 -04008002 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04008003 * @phba: Pointer to HBA context object.
8004 * @pring: Pointer to driver SLI ring object.
8005 * @piocb: Pointer to address of newly added command iocb.
8006 *
8007 * This function is called with hbalock held before a new
8008 * iocb is submitted to the firmware. This function checks
8009 * txq to flush the iocbs in txq to Firmware before
8010 * submitting new iocbs to the Firmware.
8011 * If there are iocbs in the txq which need to be submitted
8012 * to firmware, lpfc_sli_next_iocb returns the first element
8013 * of the txq after dequeuing it from txq.
8014 * If there is no iocb in the txq then the function will return
8015 * *piocb and *piocb is set to NULL. Caller needs to check
8016 * *piocb to find if there are more commands in the txq.
8017 **/
dea31012005-04-17 16:05:31 -05008018static struct lpfc_iocbq *
8019lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008020 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008021{
8022 struct lpfc_iocbq * nextiocb;
8023
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008024 lockdep_assert_held(&phba->hbalock);
8025
dea31012005-04-17 16:05:31 -05008026 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8027 if (!nextiocb) {
8028 nextiocb = *piocb;
8029 *piocb = NULL;
8030 }
8031
8032 return nextiocb;
8033}
8034
James Smarte59058c2008-08-24 21:49:00 -04008035/**
James Smart3772a992009-05-22 14:50:54 -04008036 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008037 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008038 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008039 * @piocb: Pointer to command iocb.
8040 * @flag: Flag indicating if this command can be put into txq.
8041 *
James Smart3772a992009-05-22 14:50:54 -04008042 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8043 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8044 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8045 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8046 * this function allows only iocbs for posting buffers. This function finds
8047 * next available slot in the command ring and posts the command to the
8048 * available slot and writes the port attention register to request HBA start
8049 * processing new iocb. If there is no slot available in the ring and
8050 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8051 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008052 *
James Smart3772a992009-05-22 14:50:54 -04008053 * This function is called with hbalock held. The function will return success
8054 * after it successfully submit the iocb to firmware or after adding to the
8055 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008056 **/
James Smart98c9ea52007-10-27 13:37:33 -04008057static int
James Smart3772a992009-05-22 14:50:54 -04008058__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008059 struct lpfc_iocbq *piocb, uint32_t flag)
8060{
8061 struct lpfc_iocbq *nextiocb;
8062 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04008063 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05008064
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008065 lockdep_assert_held(&phba->hbalock);
8066
James Smart92d7f7b2007-06-17 19:56:38 -05008067 if (piocb->iocb_cmpl && (!piocb->vport) &&
8068 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8069 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8070 lpfc_printf_log(phba, KERN_ERR,
8071 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008072 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008073 piocb->iocb.ulpCommand);
8074 dump_stack();
8075 return IOCB_ERROR;
8076 }
8077
8078
Linas Vepstas8d63f372007-02-14 14:28:36 -06008079 /* If the PCI channel is in offline state, do not post iocbs. */
8080 if (unlikely(pci_channel_offline(phba->pcidev)))
8081 return IOCB_ERROR;
8082
James Smarta257bf92009-04-06 18:48:10 -04008083 /* If HBA has a deferred error attention, fail the iocb. */
8084 if (unlikely(phba->hba_flag & DEFER_ERATT))
8085 return IOCB_ERROR;
8086
dea31012005-04-17 16:05:31 -05008087 /*
8088 * We should never get an IOCB if we are in a < LINK_DOWN state
8089 */
James Smart2e0fef82007-06-17 19:56:36 -05008090 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008091 return IOCB_ERROR;
8092
8093 /*
8094 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008095 * outstanding event.
dea31012005-04-17 16:05:31 -05008096 */
James Smart0b727fe2007-10-27 13:37:25 -04008097 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008098 goto iocb_busy;
8099
James Smart2e0fef82007-06-17 19:56:36 -05008100 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008101 /*
James Smart2680eea2007-04-25 09:52:55 -04008102 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008103 * can be issued if the link is not up.
8104 */
8105 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008106 case CMD_GEN_REQUEST64_CR:
8107 case CMD_GEN_REQUEST64_CX:
8108 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8109 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008110 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008111 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8112 MENLO_TRANSPORT_TYPE))
8113
8114 goto iocb_busy;
8115 break;
dea31012005-04-17 16:05:31 -05008116 case CMD_QUE_RING_BUF_CN:
8117 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008118 /*
8119 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8120 * completion, iocb_cmpl MUST be 0.
8121 */
8122 if (piocb->iocb_cmpl)
8123 piocb->iocb_cmpl = NULL;
8124 /*FALLTHROUGH*/
8125 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008126 case CMD_CLOSE_XRI_CN:
8127 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008128 break;
8129 default:
8130 goto iocb_busy;
8131 }
8132
8133 /*
8134 * For FCP commands, we must be in a state where we can process link
8135 * attention events.
8136 */
8137 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05008138 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008139 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008140 }
dea31012005-04-17 16:05:31 -05008141
dea31012005-04-17 16:05:31 -05008142 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8143 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8144 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8145
8146 if (iocb)
8147 lpfc_sli_update_ring(phba, pring);
8148 else
8149 lpfc_sli_update_full_ring(phba, pring);
8150
8151 if (!piocb)
8152 return IOCB_SUCCESS;
8153
8154 goto out_busy;
8155
8156 iocb_busy:
8157 pring->stats.iocb_cmd_delay++;
8158
8159 out_busy:
8160
8161 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008162 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008163 return IOCB_SUCCESS;
8164 }
8165
8166 return IOCB_BUSY;
8167}
8168
James Smart3772a992009-05-22 14:50:54 -04008169/**
James Smart4f774512009-05-22 14:52:35 -04008170 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8171 * @phba: Pointer to HBA context object.
8172 * @piocb: Pointer to command iocb.
8173 * @sglq: Pointer to the scatter gather queue object.
8174 *
8175 * This routine converts the bpl or bde that is in the IOCB
8176 * to a sgl list for the sli4 hardware. The physical address
8177 * of the bpl/bde is converted back to a virtual address.
8178 * If the IOCB contains a BPL then the list of BDE's is
8179 * converted to sli4_sge's. If the IOCB contains a single
8180 * BDE then it is converted to a single sli_sge.
8181 * The IOCB is still in cpu endianess so the contents of
8182 * the bpl can be used without byte swapping.
8183 *
8184 * Returns valid XRI = Success, NO_XRI = Failure.
8185**/
8186static uint16_t
8187lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8188 struct lpfc_sglq *sglq)
8189{
8190 uint16_t xritag = NO_XRI;
8191 struct ulp_bde64 *bpl = NULL;
8192 struct ulp_bde64 bde;
8193 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008194 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008195 IOCB_t *icmd;
8196 int numBdes = 0;
8197 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008198 uint32_t offset = 0; /* accumulated offset in the sg request list */
8199 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008200
8201 if (!piocbq || !sglq)
8202 return xritag;
8203
8204 sgl = (struct sli4_sge *)sglq->sgl;
8205 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008206 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8207 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008208 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8209 numBdes = icmd->un.genreq64.bdl.bdeSize /
8210 sizeof(struct ulp_bde64);
8211 /* The addrHigh and addrLow fields within the IOCB
8212 * have not been byteswapped yet so there is no
8213 * need to swap them back.
8214 */
James Smart1b511972011-12-13 13:23:09 -05008215 if (piocbq->context3)
8216 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8217 else
8218 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008219
James Smart1b511972011-12-13 13:23:09 -05008220 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008221 if (!bpl)
8222 return xritag;
8223
8224 for (i = 0; i < numBdes; i++) {
8225 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008226 sgl->addr_hi = bpl->addrHigh;
8227 sgl->addr_lo = bpl->addrLow;
8228
James Smart05580562011-05-24 11:40:48 -04008229 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008230 if ((i+1) == numBdes)
8231 bf_set(lpfc_sli4_sge_last, sgl, 1);
8232 else
8233 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008234 /* swap the size field back to the cpu so we
8235 * can assign it to the sgl.
8236 */
8237 bde.tus.w = le32_to_cpu(bpl->tus.w);
8238 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008239 /* The offsets in the sgl need to be accumulated
8240 * separately for the request and reply lists.
8241 * The request is always first, the reply follows.
8242 */
8243 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8244 /* add up the reply sg entries */
8245 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8246 inbound++;
8247 /* first inbound? reset the offset */
8248 if (inbound == 1)
8249 offset = 0;
8250 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008251 bf_set(lpfc_sli4_sge_type, sgl,
8252 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008253 offset += bde.tus.f.bdeSize;
8254 }
James Smart546fc852011-03-11 16:06:29 -05008255 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008256 bpl++;
8257 sgl++;
8258 }
8259 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8260 /* The addrHigh and addrLow fields of the BDE have not
8261 * been byteswapped yet so they need to be swapped
8262 * before putting them in the sgl.
8263 */
8264 sgl->addr_hi =
8265 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8266 sgl->addr_lo =
8267 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008268 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008269 bf_set(lpfc_sli4_sge_last, sgl, 1);
8270 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008271 sgl->sge_len =
8272 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008273 }
8274 return sglq->sli4_xritag;
8275}
8276
8277/**
James Smart4f774512009-05-22 14:52:35 -04008278 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8279 * @phba: Pointer to HBA context object.
8280 * @piocb: Pointer to command iocb.
8281 * @wqe: Pointer to the work queue entry.
8282 *
8283 * This routine converts the iocb command to its Work Queue Entry
8284 * equivalent. The wqe pointer should not have any fields set when
8285 * this routine is called because it will memcpy over them.
8286 * This routine does not set the CQ_ID or the WQEC bits in the
8287 * wqe.
8288 *
8289 * Returns: 0 = Success, IOCB_ERROR = Failure.
8290 **/
8291static int
8292lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8293 union lpfc_wqe *wqe)
8294{
James Smart5ffc2662009-11-18 15:39:44 -05008295 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008296 uint8_t ct = 0;
8297 uint32_t fip;
8298 uint32_t abort_tag;
8299 uint8_t command_type = ELS_COMMAND_NON_FIP;
8300 uint8_t cmnd;
8301 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008302 uint16_t abrt_iotag;
8303 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008304 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008305 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008306 int numBdes, i;
8307 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008308 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008309 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008310 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008311
James Smart45ed1192009-10-02 15:17:02 -04008312 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008313 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008314 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008315 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008316 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008317 command_type = ELS_COMMAND_FIP;
8318 else
8319 command_type = ELS_COMMAND_NON_FIP;
8320
James Smartb5c53952016-03-31 14:12:30 -07008321 if (phba->fcp_embed_io)
8322 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008323 /* Some of the fields are in the right position already */
8324 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartf0d9bcc2010-10-22 11:07:09 -04008325 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart28d7f3d2014-05-21 08:05:28 -04008326 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07008327
8328 abort_tag = (uint32_t) iocbq->iotag;
8329 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008330 /* words0-2 bpl convert bde */
8331 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008332 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8333 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008334 bpl = (struct ulp_bde64 *)
8335 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8336 if (!bpl)
8337 return IOCB_ERROR;
8338
8339 /* Should already be byte swapped. */
8340 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8341 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8342 /* swap the size field back to the cpu so we
8343 * can assign it to the sgl.
8344 */
8345 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008346 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8347 total_len = 0;
8348 for (i = 0; i < numBdes; i++) {
8349 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8350 total_len += bde.tus.f.bdeSize;
8351 }
James Smart4f774512009-05-22 14:52:35 -04008352 } else
James Smart5ffc2662009-11-18 15:39:44 -05008353 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008354
8355 iocbq->iocb.ulpIoTag = iocbq->iotag;
8356 cmnd = iocbq->iocb.ulpCommand;
8357
8358 switch (iocbq->iocb.ulpCommand) {
8359 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008360 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8361 ndlp = iocbq->context_un.ndlp;
8362 else
8363 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008364 if (!iocbq->iocb.ulpLe) {
8365 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8366 "2007 Only Limited Edition cmd Format"
8367 " supported 0x%x\n",
8368 iocbq->iocb.ulpCommand);
8369 return IOCB_ERROR;
8370 }
James Smartff78d8f2011-12-13 13:21:35 -05008371
James Smart5ffc2662009-11-18 15:39:44 -05008372 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008373 /* Els_reguest64 has a TMO */
8374 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8375 iocbq->iocb.ulpTimeout);
8376 /* Need a VF for word 4 set the vf bit*/
8377 bf_set(els_req64_vf, &wqe->els_req, 0);
8378 /* And a VFID for word 12 */
8379 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008380 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008381 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8382 iocbq->iocb.ulpContext);
8383 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8384 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008385 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008386 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008387 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8388 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008389 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8390 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008391 if_type = bf_get(lpfc_sli_intf_if_type,
8392 &phba->sli4_hba.sli_intf);
8393 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008394 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008395 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008396 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008397 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008398 *pcmd == ELS_CMD_PLOGI)) {
8399 bf_set(els_req64_sp, &wqe->els_req, 1);
8400 bf_set(els_req64_sid, &wqe->els_req,
8401 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008402 if ((*pcmd == ELS_CMD_FLOGI) &&
8403 !(phba->fc_topology ==
8404 LPFC_TOPOLOGY_LOOP))
8405 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008406 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8407 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008408 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008409 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008410 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8411 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8412 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8413 }
James Smartc8685952009-11-18 15:39:16 -05008414 }
James Smart6d368e52011-05-24 11:44:12 -04008415 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8416 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008417 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8418 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8419 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8420 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8421 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8422 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008423 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008424 break;
James Smart5ffc2662009-11-18 15:39:44 -05008425 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008426 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8427 iocbq->iocb.un.ulpWord[3]);
8428 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008429 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008430 /* The entire sequence is transmitted for this IOCB */
8431 xmit_len = total_len;
8432 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008433 if (phba->link_flag & LS_LOOPBACK_MODE)
8434 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008435 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008436 /* word3 iocb=io_tag32 wqe=reserved */
8437 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008438 /* word4 relative_offset memcpy */
8439 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008440 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8441 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8442 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8443 LPFC_WQE_IOD_WRITE);
8444 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8445 LPFC_WQE_LENLOC_WORD12);
8446 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008447 wqe->xmit_sequence.xmit_len = xmit_len;
8448 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008449 break;
James Smart4f774512009-05-22 14:52:35 -04008450 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008451 /* word3 iocb=iotag32 wqe=seq_payload_len */
8452 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008453 /* word4 iocb=rsvd wqe=rsvd */
8454 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8455 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008456 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008457 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008458 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8459 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8460 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8461 LPFC_WQE_LENLOC_WORD3);
8462 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008463 break;
James Smart4f774512009-05-22 14:52:35 -04008464 case CMD_FCP_IWRITE64_CR:
8465 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008466 /* word3 iocb=iotag wqe=payload_offset_len */
8467 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008468 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8469 xmit_len + sizeof(struct fcp_rsp));
8470 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8471 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008472 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8473 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8474 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8475 iocbq->iocb.ulpFCP2Rcvy);
8476 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8477 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008478 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8479 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8480 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008481 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008482 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008483 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8484 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008485 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8486 if (iocbq->priority) {
8487 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8488 (iocbq->priority << 1));
8489 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008490 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8491 (phba->cfg_XLanePriority << 1));
8492 }
8493 }
James Smartb5c53952016-03-31 14:12:30 -07008494 /* Note, word 10 is already initialized to 0 */
8495
8496 if (phba->fcp_embed_io) {
8497 struct lpfc_scsi_buf *lpfc_cmd;
8498 struct sli4_sge *sgl;
8499 union lpfc_wqe128 *wqe128;
8500 struct fcp_cmnd *fcp_cmnd;
8501 uint32_t *ptr;
8502
8503 /* 128 byte wqe support here */
8504 wqe128 = (union lpfc_wqe128 *)wqe;
8505
8506 lpfc_cmd = iocbq->context1;
8507 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8508 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8509
8510 /* Word 0-2 - FCP_CMND */
8511 wqe128->generic.bde.tus.f.bdeFlags =
8512 BUFF_TYPE_BDE_IMMED;
8513 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8514 wqe128->generic.bde.addrHigh = 0;
8515 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8516
8517 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8518
8519 /* Word 22-29 FCP CMND Payload */
8520 ptr = &wqe128->words[22];
8521 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8522 }
James Smart7851fe22011-07-22 18:36:52 -04008523 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008524 case CMD_FCP_IREAD64_CR:
8525 /* word3 iocb=iotag wqe=payload_offset_len */
8526 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008527 bf_set(payload_offset_len, &wqe->fcp_iread,
8528 xmit_len + sizeof(struct fcp_rsp));
8529 bf_set(cmd_buff_len, &wqe->fcp_iread,
8530 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008531 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8532 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8533 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8534 iocbq->iocb.ulpFCP2Rcvy);
8535 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008536 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008537 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8538 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8539 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008540 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008541 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008542 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8543 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008544 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8545 if (iocbq->priority) {
8546 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8547 (iocbq->priority << 1));
8548 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008549 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8550 (phba->cfg_XLanePriority << 1));
8551 }
8552 }
James Smartb5c53952016-03-31 14:12:30 -07008553 /* Note, word 10 is already initialized to 0 */
8554
8555 if (phba->fcp_embed_io) {
8556 struct lpfc_scsi_buf *lpfc_cmd;
8557 struct sli4_sge *sgl;
8558 union lpfc_wqe128 *wqe128;
8559 struct fcp_cmnd *fcp_cmnd;
8560 uint32_t *ptr;
8561
8562 /* 128 byte wqe support here */
8563 wqe128 = (union lpfc_wqe128 *)wqe;
8564
8565 lpfc_cmd = iocbq->context1;
8566 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8567 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8568
8569 /* Word 0-2 - FCP_CMND */
8570 wqe128->generic.bde.tus.f.bdeFlags =
8571 BUFF_TYPE_BDE_IMMED;
8572 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8573 wqe128->generic.bde.addrHigh = 0;
8574 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8575
8576 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8577
8578 /* Word 22-29 FCP CMND Payload */
8579 ptr = &wqe128->words[22];
8580 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8581 }
James Smart7851fe22011-07-22 18:36:52 -04008582 break;
James Smartf1126682009-06-10 17:22:44 -04008583 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008584 /* word3 iocb=iotag wqe=payload_offset_len */
8585 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8586 bf_set(payload_offset_len, &wqe->fcp_icmd,
8587 xmit_len + sizeof(struct fcp_rsp));
8588 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8589 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008590 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008591 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008592 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008593 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8594 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8595 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8596 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8597 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008598 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8599 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008600 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8601 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008602 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8603 if (iocbq->priority) {
8604 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8605 (iocbq->priority << 1));
8606 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008607 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8608 (phba->cfg_XLanePriority << 1));
8609 }
8610 }
James Smartb5c53952016-03-31 14:12:30 -07008611 /* Note, word 10 is already initialized to 0 */
8612
8613 if (phba->fcp_embed_io) {
8614 struct lpfc_scsi_buf *lpfc_cmd;
8615 struct sli4_sge *sgl;
8616 union lpfc_wqe128 *wqe128;
8617 struct fcp_cmnd *fcp_cmnd;
8618 uint32_t *ptr;
8619
8620 /* 128 byte wqe support here */
8621 wqe128 = (union lpfc_wqe128 *)wqe;
8622
8623 lpfc_cmd = iocbq->context1;
8624 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8625 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8626
8627 /* Word 0-2 - FCP_CMND */
8628 wqe128->generic.bde.tus.f.bdeFlags =
8629 BUFF_TYPE_BDE_IMMED;
8630 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8631 wqe128->generic.bde.addrHigh = 0;
8632 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8633
8634 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8635
8636 /* Word 22-29 FCP CMND Payload */
8637 ptr = &wqe128->words[22];
8638 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8639 }
James Smart7851fe22011-07-22 18:36:52 -04008640 break;
James Smart4f774512009-05-22 14:52:35 -04008641 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008642 /* For this command calculate the xmit length of the
8643 * request bde.
8644 */
8645 xmit_len = 0;
8646 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8647 sizeof(struct ulp_bde64);
8648 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008649 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008650 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8651 break;
James Smart63e801c2010-11-20 23:14:19 -05008652 xmit_len += bde.tus.f.bdeSize;
8653 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008654 /* word3 iocb=IO_TAG wqe=request_payload_len */
8655 wqe->gen_req.request_payload_len = xmit_len;
8656 /* word4 iocb=parameter wqe=relative_offset memcpy */
8657 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008658 /* word6 context tag copied in memcpy */
8659 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8660 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8661 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8662 "2015 Invalid CT %x command 0x%x\n",
8663 ct, iocbq->iocb.ulpCommand);
8664 return IOCB_ERROR;
8665 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008666 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8667 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8668 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8669 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8670 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8671 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8672 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8673 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008674 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008675 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008676 break;
James Smart4f774512009-05-22 14:52:35 -04008677 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008678 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008679 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008680 /* word3 iocb=iotag32 wqe=response_payload_len */
8681 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008682 /* word4 */
8683 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008684 /* word5 iocb=rsvd wge=did */
8685 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008686 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8687
8688 if_type = bf_get(lpfc_sli_intf_if_type,
8689 &phba->sli4_hba.sli_intf);
8690 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8691 if (iocbq->vport->fc_flag & FC_PT2PT) {
8692 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8693 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8694 iocbq->vport->fc_myDID);
8695 if (iocbq->vport->fc_myDID == Fabric_DID) {
8696 bf_set(wqe_els_did,
8697 &wqe->xmit_els_rsp.wqe_dest, 0);
8698 }
8699 }
8700 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008701 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8702 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8703 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8704 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008705 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008706 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008707 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008708 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008709 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8710 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8711 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8712 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8713 LPFC_WQE_LENLOC_WORD3);
8714 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008715 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8716 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008717 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8718 iocbq->context2)->virt);
8719 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008720 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8721 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008722 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008723 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8724 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008725 phba->vpi_ids[phba->pport->vpi]);
8726 }
James Smart4f774512009-05-22 14:52:35 -04008727 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008728 break;
James Smart4f774512009-05-22 14:52:35 -04008729 case CMD_CLOSE_XRI_CN:
8730 case CMD_ABORT_XRI_CN:
8731 case CMD_ABORT_XRI_CX:
8732 /* words 0-2 memcpy should be 0 rserved */
8733 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008734 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8735 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8736 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8737 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8738 } else
8739 fip = 0;
8740
8741 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008742 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008743 * The link is down, or the command was ELS_FIP
8744 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008745 * on the wire.
8746 */
8747 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8748 else
8749 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8750 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008751 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8752 wqe->abort_cmd.rsrvd5 = 0;
8753 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008754 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8755 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008756 /*
8757 * The abort handler will send us CMD_ABORT_XRI_CN or
8758 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8759 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008760 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8761 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8762 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8763 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008764 cmnd = CMD_ABORT_XRI_CX;
8765 command_type = OTHER_COMMAND;
8766 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008767 break;
James Smart6669f9b2009-10-02 15:16:45 -04008768 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008769 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008770 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008771 * we re-construct this WQE here based on information in
8772 * iocbq from scratch.
8773 */
8774 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008775 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008776 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008777 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8778 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008779 LPFC_ABTS_UNSOL_INT) {
8780 /* ABTS sent by initiator to CT exchange, the
8781 * RX_ID field will be filled with the newly
8782 * allocated responder XRI.
8783 */
8784 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8785 iocbq->sli4_xritag);
8786 } else {
8787 /* ABTS sent by responder to CT exchange, the
8788 * RX_ID field will be filled with the responder
8789 * RX_ID from ABTS.
8790 */
8791 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008792 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008793 }
James Smart6669f9b2009-10-02 15:16:45 -04008794 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8795 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008796
8797 /* Use CT=VPI */
8798 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8799 ndlp->nlp_DID);
8800 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8801 iocbq->iocb.ulpContext);
8802 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008803 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008804 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008805 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8806 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8807 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008808 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8809 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008810 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8811 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8812 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8813 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8814 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8815 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8816 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8817 }
8818
James Smart7851fe22011-07-22 18:36:52 -04008819 break;
James Smart4f774512009-05-22 14:52:35 -04008820 case CMD_XRI_ABORTED_CX:
8821 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008822 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8823 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8824 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8825 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8826 default:
8827 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8828 "2014 Invalid command 0x%x\n",
8829 iocbq->iocb.ulpCommand);
8830 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008831 break;
James Smart4f774512009-05-22 14:52:35 -04008832 }
James Smart6d368e52011-05-24 11:44:12 -04008833
James Smart8012cc32012-10-31 14:44:49 -04008834 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8835 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8836 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8837 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8838 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8839 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8840 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8841 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008842 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8843 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8844 wqe->generic.wqe_com.abort_tag = abort_tag;
8845 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8846 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8847 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8848 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008849 return 0;
8850}
8851
8852/**
8853 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8854 * @phba: Pointer to HBA context object.
8855 * @ring_number: SLI ring number to issue iocb on.
8856 * @piocb: Pointer to command iocb.
8857 * @flag: Flag indicating if this command can be put into txq.
8858 *
8859 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8860 * an iocb command to an HBA with SLI-4 interface spec.
8861 *
8862 * This function is called with hbalock held. The function will return success
8863 * after it successfully submit the iocb to firmware or after adding to the
8864 * txq.
8865 **/
8866static int
8867__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8868 struct lpfc_iocbq *piocb, uint32_t flag)
8869{
8870 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07008871 union lpfc_wqe *wqe;
8872 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05008873 struct lpfc_queue *wq;
James Smart4f774512009-05-22 14:52:35 -04008874 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008875
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008876 lockdep_assert_held(&phba->hbalock);
8877
James Smartb5c53952016-03-31 14:12:30 -07008878 /*
8879 * The WQE can be either 64 or 128 bytes,
8880 * so allocate space on the stack assuming the largest.
8881 */
8882 wqe = (union lpfc_wqe *)&wqe128;
8883
James Smart4f774512009-05-22 14:52:35 -04008884 if (piocb->sli4_xritag == NO_XRI) {
8885 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008886 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008887 sglq = NULL;
8888 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05008889 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04008890 if (!(flag & SLI_IOCB_RET_IOCB)) {
8891 __lpfc_sli_ringtx_put(phba,
8892 pring, piocb);
8893 return IOCB_SUCCESS;
8894 } else {
8895 return IOCB_BUSY;
8896 }
8897 } else {
James Smart6d368e52011-05-24 11:44:12 -04008898 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008899 if (!sglq) {
8900 if (!(flag & SLI_IOCB_RET_IOCB)) {
8901 __lpfc_sli_ringtx_put(phba,
8902 pring,
8903 piocb);
8904 return IOCB_SUCCESS;
8905 } else
8906 return IOCB_BUSY;
8907 }
8908 }
James Smart4f774512009-05-22 14:52:35 -04008909 }
James Smart2ea259e2017-02-12 13:52:27 -08008910 } else if (piocb->iocb_flag & LPFC_IO_FCP)
James Smart6d368e52011-05-24 11:44:12 -04008911 /* These IO's already have an XRI and a mapped sgl. */
8912 sglq = NULL;
James Smart2ea259e2017-02-12 13:52:27 -08008913 else {
James Smart6d368e52011-05-24 11:44:12 -04008914 /*
8915 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008916 * sglq is on the active list
8917 */
James Smartedccdc12013-01-03 15:43:45 -05008918 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04008919 if (!sglq)
8920 return IOCB_ERROR;
8921 }
8922
8923 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008924 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008925 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008926 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008927 return IOCB_ERROR;
8928 }
8929
James Smartb5c53952016-03-31 14:12:30 -07008930 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04008931 return IOCB_ERROR;
8932
James Smart341af102010-01-26 23:07:37 -05008933 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
James Smart1ba981f2014-02-20 09:56:45 -05008934 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartf38fa0b2014-04-04 13:52:21 -04008935 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
James Smart1ba981f2014-02-20 09:56:45 -05008936 wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
8937 } else {
8938 wq = phba->sli4_hba.oas_wq;
8939 }
James Smartb5c53952016-03-31 14:12:30 -07008940 if (lpfc_sli4_wq_put(wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008941 return IOCB_ERROR;
8942 } else {
James Smartea714f32013-04-17 20:18:47 -04008943 if (unlikely(!phba->sli4_hba.els_wq))
8944 return IOCB_ERROR;
James Smartb5c53952016-03-31 14:12:30 -07008945 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008946 return IOCB_ERROR;
8947 }
8948 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8949
8950 return 0;
8951}
8952
8953/**
James Smart3772a992009-05-22 14:50:54 -04008954 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8955 *
8956 * This routine wraps the actual lockless version for issusing IOCB function
8957 * pointer from the lpfc_hba struct.
8958 *
8959 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07008960 * IOCB_ERROR - Error
8961 * IOCB_SUCCESS - Success
8962 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04008963 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008964int
James Smart3772a992009-05-22 14:50:54 -04008965__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8966 struct lpfc_iocbq *piocb, uint32_t flag)
8967{
8968 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8969}
8970
8971/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008972 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008973 * @phba: The hba struct for which this call is being executed.
8974 * @dev_grp: The HBA PCI-Device group number.
8975 *
8976 * This routine sets up the SLI interface API function jump table in @phba
8977 * struct.
8978 * Returns: 0 - success, -ENODEV - failure.
8979 **/
8980int
8981lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8982{
8983
8984 switch (dev_grp) {
8985 case LPFC_PCI_DEV_LP:
8986 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8987 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8988 break;
James Smart4f774512009-05-22 14:52:35 -04008989 case LPFC_PCI_DEV_OC:
8990 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8991 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8992 break;
James Smart3772a992009-05-22 14:50:54 -04008993 default:
8994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8995 "1419 Invalid HBA PCI-device group: 0x%x\n",
8996 dev_grp);
8997 return -ENODEV;
8998 break;
8999 }
9000 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
9001 return 0;
9002}
James Smart92d7f7b2007-06-17 19:56:38 -05009003
James Smarta1efe162015-05-21 13:55:20 -04009004/**
9005 * lpfc_sli_calc_ring - Calculates which ring to use
9006 * @phba: Pointer to HBA context object.
9007 * @ring_number: Initial ring
9008 * @piocb: Pointer to command iocb.
9009 *
9010 * For SLI4, FCP IO can deferred to one fo many WQs, based on
9011 * fcp_wqidx, thus we need to calculate the corresponding ring.
9012 * Since ABORTS must go on the same WQ of the command they are
9013 * aborting, we use command's fcp_wqidx.
9014 */
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08009015static int
James Smart9bd2bff52014-09-03 12:57:30 -04009016lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
9017 struct lpfc_iocbq *piocb)
9018{
James Smart8b0dff12015-05-22 10:42:38 -04009019 if (phba->sli_rev < LPFC_SLI_REV4)
9020 return ring_number;
9021
9022 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
9023 if (!(phba->cfg_fof) ||
9024 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
9025 if (unlikely(!phba->sli4_hba.fcp_wq))
9026 return LPFC_HBA_ERROR;
9027 /*
9028 * for abort iocb fcp_wqidx should already
9029 * be setup based on what work queue we used.
9030 */
9031 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
9032 piocb->fcp_wqidx =
9033 lpfc_sli4_scmd_to_wqidx_distr(phba,
9034 piocb->context1);
9035 ring_number = MAX_SLI3_CONFIGURED_RINGS +
9036 piocb->fcp_wqidx;
9037 } else {
9038 if (unlikely(!phba->sli4_hba.oas_wq))
9039 return LPFC_HBA_ERROR;
9040 piocb->fcp_wqidx = 0;
9041 ring_number = LPFC_FCP_OAS_RING;
James Smart9bd2bff52014-09-03 12:57:30 -04009042 }
9043 }
9044 return ring_number;
9045}
9046
James Smarte59058c2008-08-24 21:49:00 -04009047/**
James Smart3621a712009-04-06 18:47:14 -04009048 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009049 * @phba: Pointer to HBA context object.
9050 * @pring: Pointer to driver SLI ring object.
9051 * @piocb: Pointer to command iocb.
9052 * @flag: Flag indicating if this command can be put into txq.
9053 *
9054 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9055 * function. This function gets the hbalock and calls
9056 * __lpfc_sli_issue_iocb function and will return the error returned
9057 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9058 * functions which do not hold hbalock.
9059 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009060int
James Smart3772a992009-05-22 14:50:54 -04009061lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009062 struct lpfc_iocbq *piocb, uint32_t flag)
9063{
James Smartba20c852012-08-03 12:36:52 -04009064 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009065 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009066 struct lpfc_queue *fpeq;
9067 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009068 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009069 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009070
James Smart7e56aa22012-08-03 12:35:34 -04009071 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -04009072 ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
9073 if (unlikely(ring_number == LPFC_HBA_ERROR))
9074 return IOCB_ERROR;
9075 idx = piocb->fcp_wqidx;
James Smartba20c852012-08-03 12:36:52 -04009076
James Smart9bd2bff52014-09-03 12:57:30 -04009077 pring = &phba->sli.ring[ring_number];
9078 spin_lock_irqsave(&pring->ring_lock, iflags);
9079 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9080 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009081
James Smart9bd2bff52014-09-03 12:57:30 -04009082 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
9083 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009084
James Smart9bd2bff52014-09-03 12:57:30 -04009085 if (atomic_dec_and_test(&fcp_eq_hdl->
9086 fcp_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009087
James Smart9bd2bff52014-09-03 12:57:30 -04009088 /* Get associated EQ with this index */
9089 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009090
James Smart9bd2bff52014-09-03 12:57:30 -04009091 /* Turn off interrupts from this EQ */
9092 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009093
James Smart9bd2bff52014-09-03 12:57:30 -04009094 /*
9095 * Process all the events on FCP EQ
9096 */
9097 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9098 lpfc_sli4_hba_handle_eqe(phba,
9099 eqe, idx);
9100 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009101 }
James Smartba20c852012-08-03 12:36:52 -04009102
James Smart9bd2bff52014-09-03 12:57:30 -04009103 /* Always clear and re-arm the EQ */
9104 lpfc_sli4_eq_release(fpeq,
9105 LPFC_QUEUE_REARM);
9106 }
9107 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009108 }
James Smart7e56aa22012-08-03 12:35:34 -04009109 } else {
9110 /* For now, SLI2/3 will still use hbalock */
9111 spin_lock_irqsave(&phba->hbalock, iflags);
9112 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9113 spin_unlock_irqrestore(&phba->hbalock, iflags);
9114 }
James Smart92d7f7b2007-06-17 19:56:38 -05009115 return rc;
9116}
9117
James Smarte59058c2008-08-24 21:49:00 -04009118/**
James Smart3621a712009-04-06 18:47:14 -04009119 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009120 * @phba: Pointer to HBA context object.
9121 *
9122 * This function is called while driver attaches with the
9123 * HBA to setup the extra ring. The extra ring is used
9124 * only when driver needs to support target mode functionality
9125 * or IP over FC functionalities.
9126 *
9127 * This function is called with no lock held.
9128 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009129static int
9130lpfc_extra_ring_setup( struct lpfc_hba *phba)
9131{
9132 struct lpfc_sli *psli;
9133 struct lpfc_sli_ring *pring;
9134
9135 psli = &phba->sli;
9136
9137 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009138
9139 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009140 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04009141 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9142 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9143 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9144 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009145
James Smarta4bc3372006-12-02 13:34:16 -05009146 /* and give them to the extra ring */
9147 pring = &psli->ring[psli->extra_ring];
9148
James Smart7e56aa22012-08-03 12:35:34 -04009149 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9150 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9151 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9152 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009153
9154 /* Setup default profile for this ring */
9155 pring->iotag_max = 4096;
9156 pring->num_mask = 1;
9157 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009158 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9159 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009160 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9161 return 0;
9162}
9163
James Smartcb69f7d2011-12-13 13:21:57 -05009164/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9165 * @phba: Pointer to HBA context object.
9166 * @iocbq: Pointer to iocb object.
9167 *
9168 * The async_event handler calls this routine when it receives
9169 * an ASYNC_STATUS_CN event from the port. The port generates
9170 * this event when an Abort Sequence request to an rport fails
9171 * twice in succession. The abort could be originated by the
9172 * driver or by the port. The ABTS could have been for an ELS
9173 * or FCP IO. The port only generates this event when an ABTS
9174 * fails to complete after one retry.
9175 */
9176static void
9177lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9178 struct lpfc_iocbq *iocbq)
9179{
9180 struct lpfc_nodelist *ndlp = NULL;
9181 uint16_t rpi = 0, vpi = 0;
9182 struct lpfc_vport *vport = NULL;
9183
9184 /* The rpi in the ulpContext is vport-sensitive. */
9185 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9186 rpi = iocbq->iocb.ulpContext;
9187
9188 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9189 "3092 Port generated ABTS async event "
9190 "on vpi %d rpi %d status 0x%x\n",
9191 vpi, rpi, iocbq->iocb.ulpStatus);
9192
9193 vport = lpfc_find_vport_by_vpid(phba, vpi);
9194 if (!vport)
9195 goto err_exit;
9196 ndlp = lpfc_findnode_rpi(vport, rpi);
9197 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9198 goto err_exit;
9199
9200 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9201 lpfc_sli_abts_recover_port(vport, ndlp);
9202 return;
9203
9204 err_exit:
9205 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9206 "3095 Event Context not found, no "
9207 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9208 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9209 vpi, rpi);
9210}
9211
9212/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9213 * @phba: pointer to HBA context object.
9214 * @ndlp: nodelist pointer for the impacted rport.
9215 * @axri: pointer to the wcqe containing the failed exchange.
9216 *
9217 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9218 * port. The port generates this event when an abort exchange request to an
9219 * rport fails twice in succession with no reply. The abort could be originated
9220 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9221 */
9222void
9223lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9224 struct lpfc_nodelist *ndlp,
9225 struct sli4_wcqe_xri_aborted *axri)
9226{
9227 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009228 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009229
James Smart6b5151f2012-01-18 16:24:06 -05009230 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009231 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9232 "3115 Node Context not found, driver "
9233 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009234 return;
9235 }
9236
James Smartcb69f7d2011-12-13 13:21:57 -05009237 vport = ndlp->vport;
9238 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9239 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009240 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009241 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009242 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009243 bf_get(lpfc_wcqe_xa_status, axri),
9244 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009245
James Smart5c1db2a2012-03-01 22:34:36 -05009246 /*
9247 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9248 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9249 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9250 */
James Smarte3d2b802012-08-14 14:25:43 -04009251 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009252 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9253 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009254 lpfc_sli_abts_recover_port(vport, ndlp);
9255}
9256
James Smarte59058c2008-08-24 21:49:00 -04009257/**
James Smart3621a712009-04-06 18:47:14 -04009258 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009259 * @phba: Pointer to HBA context object.
9260 * @pring: Pointer to driver SLI ring object.
9261 * @iocbq: Pointer to iocb object.
9262 *
9263 * This function is called by the slow ring event handler
9264 * function when there is an ASYNC event iocb in the ring.
9265 * This function is called with no lock held.
9266 * Currently this function handles only temperature related
9267 * ASYNC events. The function decodes the temperature sensor
9268 * event message and posts events for the management applications.
9269 **/
James Smart98c9ea52007-10-27 13:37:33 -04009270static void
James Smart57127f12007-10-27 13:37:05 -04009271lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9272 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9273{
9274 IOCB_t *icmd;
9275 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009276 struct temp_event temp_event_data;
9277 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009278 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009279
9280 icmd = &iocbq->iocb;
9281 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009282
James Smartcb69f7d2011-12-13 13:21:57 -05009283 switch (evt_code) {
9284 case ASYNC_TEMP_WARN:
9285 case ASYNC_TEMP_SAFE:
9286 temp_event_data.data = (uint32_t) icmd->ulpContext;
9287 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9288 if (evt_code == ASYNC_TEMP_WARN) {
9289 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9290 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9291 "0347 Adapter is very hot, please take "
9292 "corrective action. temperature : %d Celsius\n",
9293 (uint32_t) icmd->ulpContext);
9294 } else {
9295 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9296 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9297 "0340 Adapter temperature is OK now. "
9298 "temperature : %d Celsius\n",
9299 (uint32_t) icmd->ulpContext);
9300 }
9301
9302 /* Send temperature change event to applications */
9303 shost = lpfc_shost_from_vport(phba->pport);
9304 fc_host_post_vendor_event(shost, fc_get_event_number(),
9305 sizeof(temp_event_data), (char *) &temp_event_data,
9306 LPFC_NL_VENDOR_ID);
9307 break;
9308 case ASYNC_STATUS_CN:
9309 lpfc_sli_abts_err_handler(phba, iocbq);
9310 break;
9311 default:
James Smarta257bf92009-04-06 18:48:10 -04009312 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009313 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009314 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009315 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009316 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9317 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9318 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9319 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009320 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009321 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9322 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9323 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9324 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9325
James Smartcb69f7d2011-12-13 13:21:57 -05009326 break;
James Smart57127f12007-10-27 13:37:05 -04009327 }
James Smart57127f12007-10-27 13:37:05 -04009328}
9329
9330
James Smarte59058c2008-08-24 21:49:00 -04009331/**
James Smart3621a712009-04-06 18:47:14 -04009332 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009333 * @phba: Pointer to HBA context object.
9334 *
9335 * lpfc_sli_setup sets up rings of the SLI interface with
9336 * number of iocbs per ring and iotags. This function is
9337 * called while driver attach to the HBA and before the
9338 * interrupts are enabled. So there is no need for locking.
9339 *
9340 * This function always returns 0.
9341 **/
dea31012005-04-17 16:05:31 -05009342int
9343lpfc_sli_setup(struct lpfc_hba *phba)
9344{
James Smarted957682007-06-17 19:56:37 -05009345 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009346 struct lpfc_sli *psli = &phba->sli;
9347 struct lpfc_sli_ring *pring;
9348
James Smart2a76a282012-08-03 12:35:54 -04009349 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9350 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04009351 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05009352 psli->sli_flag = 0;
9353 psli->fcp_ring = LPFC_FCP_RING;
9354 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05009355 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05009356
James Bottomley604a3e32005-10-29 10:28:33 -05009357 psli->iocbq_lookup = NULL;
9358 psli->iocbq_lookup_len = 0;
9359 psli->last_iotag = 0;
9360
dea31012005-04-17 16:05:31 -05009361 for (i = 0; i < psli->num_rings; i++) {
9362 pring = &psli->ring[i];
9363 switch (i) {
9364 case LPFC_FCP_RING: /* ring 0 - FCP */
9365 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009366 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9367 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9368 pring->sli.sli3.numCiocb +=
9369 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9370 pring->sli.sli3.numRiocb +=
9371 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9372 pring->sli.sli3.numCiocb +=
9373 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9374 pring->sli.sli3.numRiocb +=
9375 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9376 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009377 SLI3_IOCB_CMD_SIZE :
9378 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009379 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009380 SLI3_IOCB_RSP_SIZE :
9381 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009382 pring->iotag_ctr = 0;
9383 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009384 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009385 pring->fast_iotag = pring->iotag_max;
9386 pring->num_mask = 0;
9387 break;
James Smarta4bc3372006-12-02 13:34:16 -05009388 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009389 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009390 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9391 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9392 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009393 SLI3_IOCB_CMD_SIZE :
9394 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009395 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009396 SLI3_IOCB_RSP_SIZE :
9397 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009398 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009399 pring->num_mask = 0;
9400 break;
9401 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9402 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009403 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9404 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9405 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009406 SLI3_IOCB_CMD_SIZE :
9407 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009408 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009409 SLI3_IOCB_RSP_SIZE :
9410 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009411 pring->fast_iotag = 0;
9412 pring->iotag_ctr = 0;
9413 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009414 pring->lpfc_sli_rcv_async_status =
9415 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009416 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009417 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009418 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9419 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009420 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009421 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009422 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009423 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9424 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009425 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009426 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009427 pring->prt[2].profile = 0; /* Mask 2 */
9428 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009429 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009430 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009431 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009432 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009433 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009434 pring->prt[3].profile = 0; /* Mask 3 */
9435 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009436 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009437 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009438 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009439 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009440 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009441 break;
9442 }
James Smart7e56aa22012-08-03 12:35:34 -04009443 totiocbsize += (pring->sli.sli3.numCiocb *
9444 pring->sli.sli3.sizeCiocb) +
9445 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009446 }
James Smarted957682007-06-17 19:56:37 -05009447 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009448 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009449 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9450 "SLI2 SLIM Data: x%x x%lx\n",
9451 phba->brd_no, totiocbsize,
9452 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009453 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009454 if (phba->cfg_multi_ring_support == 2)
9455 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009456
9457 return 0;
9458}
9459
James Smarte59058c2008-08-24 21:49:00 -04009460/**
James Smart3621a712009-04-06 18:47:14 -04009461 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009462 * @phba: Pointer to HBA context object.
9463 *
9464 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9465 * ring. This function also initializes ring indices of each ring.
9466 * This function is called during the initialization of the SLI
9467 * interface of an HBA.
9468 * This function is called with no lock held and always returns
9469 * 1.
9470 **/
dea31012005-04-17 16:05:31 -05009471int
James Smart2e0fef82007-06-17 19:56:36 -05009472lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009473{
9474 struct lpfc_sli *psli;
9475 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009476 int i;
dea31012005-04-17 16:05:31 -05009477
9478 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009479 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009480 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009481 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009482 /* Initialize list headers for txq and txcmplq as double linked lists */
9483 for (i = 0; i < psli->num_rings; i++) {
9484 pring = &psli->ring[i];
9485 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009486 pring->sli.sli3.next_cmdidx = 0;
9487 pring->sli.sli3.local_getidx = 0;
9488 pring->sli.sli3.cmdidx = 0;
James Smart68e814f2014-05-21 08:04:59 -04009489 pring->flag = 0;
dea31012005-04-17 16:05:31 -05009490 INIT_LIST_HEAD(&pring->txq);
9491 INIT_LIST_HEAD(&pring->txcmplq);
9492 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009493 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009494 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04009495 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009496 }
James Smart2e0fef82007-06-17 19:56:36 -05009497 spin_unlock_irq(&phba->hbalock);
9498 return 1;
dea31012005-04-17 16:05:31 -05009499}
9500
James Smarte59058c2008-08-24 21:49:00 -04009501/**
James Smart04c68492009-05-22 14:52:52 -04009502 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9503 * @phba: Pointer to HBA context object.
9504 *
9505 * This routine flushes the mailbox command subsystem. It will unconditionally
9506 * flush all the mailbox commands in the three possible stages in the mailbox
9507 * command sub-system: pending mailbox command queue; the outstanding mailbox
9508 * command; and completed mailbox command queue. It is caller's responsibility
9509 * to make sure that the driver is in the proper state to flush the mailbox
9510 * command sub-system. Namely, the posting of mailbox commands into the
9511 * pending mailbox command queue from the various clients must be stopped;
9512 * either the HBA is in a state that it will never works on the outstanding
9513 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9514 * mailbox command has been completed.
9515 **/
9516static void
9517lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9518{
9519 LIST_HEAD(completions);
9520 struct lpfc_sli *psli = &phba->sli;
9521 LPFC_MBOXQ_t *pmb;
9522 unsigned long iflag;
9523
9524 /* Flush all the mailbox commands in the mbox system */
9525 spin_lock_irqsave(&phba->hbalock, iflag);
9526 /* The pending mailbox command queue */
9527 list_splice_init(&phba->sli.mboxq, &completions);
9528 /* The outstanding active mailbox command */
9529 if (psli->mbox_active) {
9530 list_add_tail(&psli->mbox_active->list, &completions);
9531 psli->mbox_active = NULL;
9532 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9533 }
9534 /* The completed mailbox command queue */
9535 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9536 spin_unlock_irqrestore(&phba->hbalock, iflag);
9537
9538 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9539 while (!list_empty(&completions)) {
9540 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9541 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9542 if (pmb->mbox_cmpl)
9543 pmb->mbox_cmpl(phba, pmb);
9544 }
9545}
9546
9547/**
James Smart3621a712009-04-06 18:47:14 -04009548 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009549 * @vport: Pointer to virtual port object.
9550 *
9551 * lpfc_sli_host_down is called to clean up the resources
9552 * associated with a vport before destroying virtual
9553 * port data structures.
9554 * This function does following operations:
9555 * - Free discovery resources associated with this virtual
9556 * port.
9557 * - Free iocbs associated with this virtual port in
9558 * the txq.
9559 * - Send abort for all iocb commands associated with this
9560 * vport in txcmplq.
9561 *
9562 * This function is called with no lock held and always returns 1.
9563 **/
dea31012005-04-17 16:05:31 -05009564int
James Smart92d7f7b2007-06-17 19:56:38 -05009565lpfc_sli_host_down(struct lpfc_vport *vport)
9566{
James Smart858c9f62007-06-17 19:56:39 -05009567 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009568 struct lpfc_hba *phba = vport->phba;
9569 struct lpfc_sli *psli = &phba->sli;
9570 struct lpfc_sli_ring *pring;
9571 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009572 int i;
9573 unsigned long flags = 0;
9574 uint16_t prev_pring_flag;
9575
9576 lpfc_cleanup_discovery_resources(vport);
9577
9578 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009579 for (i = 0; i < psli->num_rings; i++) {
9580 pring = &psli->ring[i];
9581 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009582 /* Only slow rings */
9583 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009584 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009585 /* Set the lpfc data pending flag */
9586 set_bit(LPFC_DATA_READY, &phba->data_flags);
9587 }
James Smart92d7f7b2007-06-17 19:56:38 -05009588 /*
9589 * Error everything on the txq since these iocbs have not been
9590 * given to the FW yet.
9591 */
James Smart92d7f7b2007-06-17 19:56:38 -05009592 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9593 if (iocb->vport != vport)
9594 continue;
James Smart858c9f62007-06-17 19:56:39 -05009595 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009596 }
9597
9598 /* Next issue ABTS for everything on the txcmplq */
9599 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9600 list) {
9601 if (iocb->vport != vport)
9602 continue;
9603 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9604 }
9605
9606 pring->flag = prev_pring_flag;
9607 }
9608
9609 spin_unlock_irqrestore(&phba->hbalock, flags);
9610
James Smarta257bf92009-04-06 18:48:10 -04009611 /* Cancel all the IOCBs from the completions list */
9612 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9613 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009614 return 1;
9615}
9616
James Smarte59058c2008-08-24 21:49:00 -04009617/**
James Smart3621a712009-04-06 18:47:14 -04009618 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009619 * @phba: Pointer to HBA context object.
9620 *
9621 * This function cleans up all iocb, buffers, mailbox commands
9622 * while shutting down the HBA. This function is called with no
9623 * lock held and always returns 1.
9624 * This function does the following to cleanup driver resources:
9625 * - Free discovery resources for each virtual port
9626 * - Cleanup any pending fabric iocbs
9627 * - Iterate through the iocb txq and free each entry
9628 * in the list.
9629 * - Free up any buffer posted to the HBA
9630 * - Free mailbox commands in the mailbox queue.
9631 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009632int
James Smart2e0fef82007-06-17 19:56:36 -05009633lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009634{
James Smart2534ba72007-04-25 09:52:20 -04009635 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009636 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009637 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009638 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009639 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009640 int i;
9641
9642 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009643 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009644
dea31012005-04-17 16:05:31 -05009645 lpfc_hba_down_prep(phba);
9646
James Smart92d7f7b2007-06-17 19:56:38 -05009647 lpfc_fabric_abort_hba(phba);
9648
James Smart2e0fef82007-06-17 19:56:36 -05009649 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009650 for (i = 0; i < psli->num_rings; i++) {
9651 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009652 /* Only slow rings */
9653 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009654 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009655 /* Set the lpfc data pending flag */
9656 set_bit(LPFC_DATA_READY, &phba->data_flags);
9657 }
dea31012005-04-17 16:05:31 -05009658
9659 /*
9660 * Error everything on the txq since these iocbs have not been
9661 * given to the FW yet.
9662 */
James Smart2534ba72007-04-25 09:52:20 -04009663 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009664 }
James Smart2e0fef82007-06-17 19:56:36 -05009665 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009666
James Smarta257bf92009-04-06 18:48:10 -04009667 /* Cancel all the IOCBs from the completions list */
9668 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9669 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009670
James Smart0ff10d42008-01-11 01:52:36 -05009671 spin_lock_irqsave(&phba->hbalock, flags);
9672 list_splice_init(&phba->elsbuf, &completions);
9673 phba->elsbuf_cnt = 0;
9674 phba->elsbuf_prev_cnt = 0;
9675 spin_unlock_irqrestore(&phba->hbalock, flags);
9676
9677 while (!list_empty(&completions)) {
9678 list_remove_head(&completions, buf_ptr,
9679 struct lpfc_dmabuf, list);
9680 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9681 kfree(buf_ptr);
9682 }
9683
dea31012005-04-17 16:05:31 -05009684 /* Return any active mbox cmds */
9685 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009686
James Smartda0436e2009-05-22 14:51:39 -04009687 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009688 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009689 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009690
James Smartda0436e2009-05-22 14:51:39 -04009691 return 1;
9692}
James Smart92d7f7b2007-06-17 19:56:38 -05009693
James Smartda0436e2009-05-22 14:51:39 -04009694/**
James Smart3621a712009-04-06 18:47:14 -04009695 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009696 * @srcp: Source memory pointer.
9697 * @destp: Destination memory pointer.
9698 * @cnt: Number of words required to be copied.
9699 *
9700 * This function is used for copying data between driver memory
9701 * and the SLI memory. This function also changes the endianness
9702 * of each word if native endianness is different from SLI
9703 * endianness. This function can be called with or without
9704 * lock.
9705 **/
dea31012005-04-17 16:05:31 -05009706void
9707lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9708{
9709 uint32_t *src = srcp;
9710 uint32_t *dest = destp;
9711 uint32_t ldata;
9712 int i;
9713
9714 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9715 ldata = *src;
9716 ldata = le32_to_cpu(ldata);
9717 *dest = ldata;
9718 src++;
9719 dest++;
9720 }
9721}
9722
James Smarte59058c2008-08-24 21:49:00 -04009723
9724/**
James Smarta0c87cb2009-07-19 10:01:10 -04009725 * lpfc_sli_bemem_bcopy - SLI memory copy function
9726 * @srcp: Source memory pointer.
9727 * @destp: Destination memory pointer.
9728 * @cnt: Number of words required to be copied.
9729 *
9730 * This function is used for copying data between a data structure
9731 * with big endian representation to local endianness.
9732 * This function can be called with or without lock.
9733 **/
9734void
9735lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9736{
9737 uint32_t *src = srcp;
9738 uint32_t *dest = destp;
9739 uint32_t ldata;
9740 int i;
9741
9742 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9743 ldata = *src;
9744 ldata = be32_to_cpu(ldata);
9745 *dest = ldata;
9746 src++;
9747 dest++;
9748 }
9749}
9750
9751/**
James Smart3621a712009-04-06 18:47:14 -04009752 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009753 * @phba: Pointer to HBA context object.
9754 * @pring: Pointer to driver SLI ring object.
9755 * @mp: Pointer to driver buffer object.
9756 *
9757 * This function is called with no lock held.
9758 * It always return zero after adding the buffer to the postbufq
9759 * buffer list.
9760 **/
dea31012005-04-17 16:05:31 -05009761int
James Smart2e0fef82007-06-17 19:56:36 -05009762lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9763 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009764{
9765 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9766 later */
James Smart2e0fef82007-06-17 19:56:36 -05009767 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009768 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009769 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009770 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009771 return 0;
9772}
9773
James Smarte59058c2008-08-24 21:49:00 -04009774/**
James Smart3621a712009-04-06 18:47:14 -04009775 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009776 * @phba: Pointer to HBA context object.
9777 *
9778 * When HBQ is enabled, buffers are searched based on tags. This function
9779 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9780 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9781 * does not conflict with tags of buffer posted for unsolicited events.
9782 * The function returns the allocated tag. The function is called with
9783 * no locks held.
9784 **/
James Smart76bb24e2007-10-27 13:38:00 -04009785uint32_t
9786lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9787{
9788 spin_lock_irq(&phba->hbalock);
9789 phba->buffer_tag_count++;
9790 /*
9791 * Always set the QUE_BUFTAG_BIT to distiguish between
9792 * a tag assigned by HBQ.
9793 */
9794 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9795 spin_unlock_irq(&phba->hbalock);
9796 return phba->buffer_tag_count;
9797}
9798
James Smarte59058c2008-08-24 21:49:00 -04009799/**
James Smart3621a712009-04-06 18:47:14 -04009800 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009801 * @phba: Pointer to HBA context object.
9802 * @pring: Pointer to driver SLI ring object.
9803 * @tag: Buffer tag.
9804 *
9805 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9806 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9807 * iocb is posted to the response ring with the tag of the buffer.
9808 * This function searches the pring->postbufq list using the tag
9809 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9810 * iocb. If the buffer is found then lpfc_dmabuf object of the
9811 * buffer is returned to the caller else NULL is returned.
9812 * This function is called with no lock held.
9813 **/
James Smart76bb24e2007-10-27 13:38:00 -04009814struct lpfc_dmabuf *
9815lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9816 uint32_t tag)
9817{
9818 struct lpfc_dmabuf *mp, *next_mp;
9819 struct list_head *slp = &pring->postbufq;
9820
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009821 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009822 spin_lock_irq(&phba->hbalock);
9823 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9824 if (mp->buffer_tag == tag) {
9825 list_del_init(&mp->list);
9826 pring->postbufq_cnt--;
9827 spin_unlock_irq(&phba->hbalock);
9828 return mp;
9829 }
9830 }
9831
9832 spin_unlock_irq(&phba->hbalock);
9833 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009834 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009835 "ring %d Data x%lx x%p x%p x%x\n",
9836 pring->ringno, (unsigned long) tag,
9837 slp->next, slp->prev, pring->postbufq_cnt);
9838
9839 return NULL;
9840}
dea31012005-04-17 16:05:31 -05009841
James Smarte59058c2008-08-24 21:49:00 -04009842/**
James Smart3621a712009-04-06 18:47:14 -04009843 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009844 * @phba: Pointer to HBA context object.
9845 * @pring: Pointer to driver SLI ring object.
9846 * @phys: DMA address of the buffer.
9847 *
9848 * This function searches the buffer list using the dma_address
9849 * of unsolicited event to find the driver's lpfc_dmabuf object
9850 * corresponding to the dma_address. The function returns the
9851 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9852 * This function is called by the ct and els unsolicited event
9853 * handlers to get the buffer associated with the unsolicited
9854 * event.
9855 *
9856 * This function is called with no lock held.
9857 **/
dea31012005-04-17 16:05:31 -05009858struct lpfc_dmabuf *
9859lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9860 dma_addr_t phys)
9861{
9862 struct lpfc_dmabuf *mp, *next_mp;
9863 struct list_head *slp = &pring->postbufq;
9864
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009865 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009866 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009867 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9868 if (mp->phys == phys) {
9869 list_del_init(&mp->list);
9870 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009871 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009872 return mp;
9873 }
9874 }
9875
James Smart2e0fef82007-06-17 19:56:36 -05009876 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009877 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009878 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009879 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009880 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009881 slp->next, slp->prev, pring->postbufq_cnt);
9882 return NULL;
9883}
9884
James Smarte59058c2008-08-24 21:49:00 -04009885/**
James Smart3621a712009-04-06 18:47:14 -04009886 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009887 * @phba: Pointer to HBA context object.
9888 * @cmdiocb: Pointer to driver command iocb object.
9889 * @rspiocb: Pointer to driver response iocb object.
9890 *
9891 * This function is the completion handler for the abort iocbs for
9892 * ELS commands. This function is called from the ELS ring event
9893 * handler with no lock held. This function frees memory resources
9894 * associated with the abort iocb.
9895 **/
dea31012005-04-17 16:05:31 -05009896static void
James Smart2e0fef82007-06-17 19:56:36 -05009897lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9898 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009899{
James Smart2e0fef82007-06-17 19:56:36 -05009900 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009901 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009902 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009903
9904 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009905
9906 /*
9907 * Assume that the port already completed and returned, or
9908 * will return the iocb. Just Log the message.
9909 */
James Smart2680eea2007-04-25 09:52:55 -04009910 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9911 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9912
James Smart2e0fef82007-06-17 19:56:36 -05009913 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009914 if (phba->sli_rev < LPFC_SLI_REV4) {
9915 if (abort_iotag != 0 &&
9916 abort_iotag <= phba->sli.last_iotag)
9917 abort_iocb =
9918 phba->sli.iocbq_lookup[abort_iotag];
9919 } else
9920 /* For sli4 the abort_tag is the XRI,
9921 * so the abort routine puts the iotag of the iocb
9922 * being aborted in the context field of the abort
9923 * IOCB.
9924 */
9925 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009926
James Smart2a9bf3d2010-06-07 15:24:45 -04009927 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9928 "0327 Cannot abort els iocb %p "
9929 "with tag %x context %x, abort status %x, "
9930 "abort code %x\n",
9931 abort_iocb, abort_iotag, abort_context,
9932 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009933
James Smartff78d8f2011-12-13 13:21:35 -05009934 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009935 }
James Bottomley604a3e32005-10-29 10:28:33 -05009936 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009937 return;
9938}
9939
James Smarte59058c2008-08-24 21:49:00 -04009940/**
James Smart3621a712009-04-06 18:47:14 -04009941 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009942 * @phba: Pointer to HBA context object.
9943 * @cmdiocb: Pointer to driver command iocb object.
9944 * @rspiocb: Pointer to driver response iocb object.
9945 *
9946 * The function is called from SLI ring event handler with no
9947 * lock held. This function is the completion handler for ELS commands
9948 * which are aborted. The function frees memory resources used for
9949 * the aborted ELS commands.
9950 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009951static void
9952lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9953 struct lpfc_iocbq *rspiocb)
9954{
9955 IOCB_t *irsp = &rspiocb->iocb;
9956
9957 /* ELS cmd tag <ulpIoTag> completes */
9958 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009959 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009960 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009961 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009962 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009963 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9964 lpfc_ct_free_iocb(phba, cmdiocb);
9965 else
9966 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009967 return;
9968}
9969
James Smarte59058c2008-08-24 21:49:00 -04009970/**
James Smart5af5eee2010-10-22 11:06:38 -04009971 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009972 * @phba: Pointer to HBA context object.
9973 * @pring: Pointer to driver SLI ring object.
9974 * @cmdiocb: Pointer to driver command iocb object.
9975 *
James Smart5af5eee2010-10-22 11:06:38 -04009976 * This function issues an abort iocb for the provided command iocb down to
9977 * the port. Other than the case the outstanding command iocb is an abort
9978 * request, this function issues abort out unconditionally. This function is
9979 * called with hbalock held. The function returns 0 when it fails due to
9980 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009981 **/
James Smart5af5eee2010-10-22 11:06:38 -04009982static int
9983lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009984 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009985{
James Smart2e0fef82007-06-17 19:56:36 -05009986 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009987 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009988 IOCB_t *icmd = NULL;
9989 IOCB_t *iabt = NULL;
James Smart9bd2bff52014-09-03 12:57:30 -04009990 int ring_number;
James Smart5af5eee2010-10-22 11:06:38 -04009991 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009992 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009993
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009994 lockdep_assert_held(&phba->hbalock);
9995
James Smart92d7f7b2007-06-17 19:56:38 -05009996 /*
9997 * There are certain command types we don't want to abort. And we
9998 * don't want to abort commands that are already in the process of
9999 * being aborted.
James Smart07951072007-04-25 09:51:38 -040010000 */
10001 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -050010002 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -050010003 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10004 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -040010005 return 0;
10006
dea31012005-04-17 16:05:31 -050010007 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -050010008 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010009 if (abtsiocbp == NULL)
10010 return 0;
dea31012005-04-17 16:05:31 -050010011
James Smart07951072007-04-25 09:51:38 -040010012 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -050010013 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -040010014 */
10015 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10016
dea31012005-04-17 16:05:31 -050010017 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010018 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10019 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010020 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010021 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010022 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10023 }
James Smartda0436e2009-05-22 14:51:39 -040010024 else
10025 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010026 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010027 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010028
James Smart5ffc2662009-11-18 15:39:44 -050010029 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10030 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -050010031 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10032 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010033 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10034 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010035
James Smart2e0fef82007-06-17 19:56:36 -050010036 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010037 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10038 else
10039 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10040
10041 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smarte6c6acc2016-12-19 15:07:23 -080010042 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010043
James Smarte8b62012007-08-02 11:10:09 -040010044 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10045 "0339 Abort xri x%x, original iotag x%x, "
10046 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010047 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010048 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010049 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010050
10051 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -040010052 ring_number =
10053 lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
10054 if (unlikely(ring_number == LPFC_HBA_ERROR))
10055 return 0;
10056 pring = &phba->sli.ring[ring_number];
James Smart7e56aa22012-08-03 12:35:34 -040010057 /* Note: both hbalock and ring_lock need to be set here */
10058 spin_lock_irqsave(&pring->ring_lock, iflags);
10059 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10060 abtsiocbp, 0);
10061 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10062 } else {
10063 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10064 abtsiocbp, 0);
10065 }
James Smart07951072007-04-25 09:51:38 -040010066
James Smartd7c255b2008-08-24 21:50:00 -040010067 if (retval)
10068 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010069
10070 /*
10071 * Caller to this routine should check for IOCB_ERROR
10072 * and handle it properly. This routine no longer removes
10073 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10074 */
10075 return retval;
10076}
10077
10078/**
10079 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10080 * @phba: Pointer to HBA context object.
10081 * @pring: Pointer to driver SLI ring object.
10082 * @cmdiocb: Pointer to driver command iocb object.
10083 *
10084 * This function issues an abort iocb for the provided command iocb. In case
10085 * of unloading, the abort iocb will not be issued to commands on the ELS
10086 * ring. Instead, the callback function shall be changed to those commands
10087 * so that nothing happens when them finishes. This function is called with
10088 * hbalock held. The function returns 0 when the command iocb is an abort
10089 * request.
10090 **/
10091int
10092lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10093 struct lpfc_iocbq *cmdiocb)
10094{
10095 struct lpfc_vport *vport = cmdiocb->vport;
10096 int retval = IOCB_ERROR;
10097 IOCB_t *icmd = NULL;
10098
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010099 lockdep_assert_held(&phba->hbalock);
10100
James Smart5af5eee2010-10-22 11:06:38 -040010101 /*
10102 * There are certain command types we don't want to abort. And we
10103 * don't want to abort commands that are already in the process of
10104 * being aborted.
10105 */
10106 icmd = &cmdiocb->iocb;
10107 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10108 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10109 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10110 return 0;
10111
10112 /*
10113 * If we're unloading, don't abort iocb on the ELS ring, but change
10114 * the callback so that nothing happens when it finishes.
10115 */
10116 if ((vport->load_flag & FC_UNLOADING) &&
10117 (pring->ringno == LPFC_ELS_RING)) {
10118 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10119 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10120 else
10121 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10122 goto abort_iotag_exit;
10123 }
10124
10125 /* Now, we try to issue the abort to the cmdiocb out */
10126 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10127
James Smart07951072007-04-25 09:51:38 -040010128abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010129 /*
10130 * Caller to this routine should check for IOCB_ERROR
10131 * and handle it properly. This routine no longer removes
10132 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010133 */
James Smart2e0fef82007-06-17 19:56:36 -050010134 return retval;
dea31012005-04-17 16:05:31 -050010135}
10136
James Smarte59058c2008-08-24 21:49:00 -040010137/**
James Smart5af5eee2010-10-22 11:06:38 -040010138 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10139 * @phba: pointer to lpfc HBA data structure.
10140 *
10141 * This routine will abort all pending and outstanding iocbs to an HBA.
10142 **/
10143void
10144lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10145{
10146 struct lpfc_sli *psli = &phba->sli;
10147 struct lpfc_sli_ring *pring;
10148 int i;
10149
10150 for (i = 0; i < psli->num_rings; i++) {
10151 pring = &psli->ring[i];
James Smartdb55fba2014-04-04 13:52:02 -040010152 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010153 }
10154}
10155
10156/**
James Smart3621a712009-04-06 18:47:14 -040010157 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010158 * @iocbq: Pointer to driver iocb object.
10159 * @vport: Pointer to driver virtual port object.
10160 * @tgt_id: SCSI ID of the target.
10161 * @lun_id: LUN ID of the scsi device.
10162 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10163 *
James Smart3621a712009-04-06 18:47:14 -040010164 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010165 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10166 * 0 if the filtering criteria is met for the given iocb and will return
10167 * 1 if the filtering criteria is not met.
10168 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10169 * given iocb is for the SCSI device specified by vport, tgt_id and
10170 * lun_id parameter.
10171 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10172 * given iocb is for the SCSI target specified by vport and tgt_id
10173 * parameters.
10174 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10175 * given iocb is for the SCSI host associated with the given vport.
10176 * This function is called with no locks held.
10177 **/
dea31012005-04-17 16:05:31 -050010178static int
James Smart51ef4c22007-08-02 11:10:31 -040010179lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10180 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010181 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010182{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010183 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010184 int rc = 1;
10185
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010186 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10187 return rc;
10188
James Smart51ef4c22007-08-02 11:10:31 -040010189 if (iocbq->vport != vport)
10190 return rc;
10191
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010192 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010193
James Smart495a7142008-06-14 22:52:59 -040010194 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010195 return rc;
10196
10197 switch (ctx_cmd) {
10198 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010199 if ((lpfc_cmd->rdata->pnode) &&
10200 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10201 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010202 rc = 0;
10203 break;
10204 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010205 if ((lpfc_cmd->rdata->pnode) &&
10206 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010207 rc = 0;
10208 break;
dea31012005-04-17 16:05:31 -050010209 case LPFC_CTX_HOST:
10210 rc = 0;
10211 break;
10212 default:
10213 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010214 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010215 break;
10216 }
10217
10218 return rc;
10219}
10220
James Smarte59058c2008-08-24 21:49:00 -040010221/**
James Smart3621a712009-04-06 18:47:14 -040010222 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010223 * @vport: Pointer to virtual port.
10224 * @tgt_id: SCSI ID of the target.
10225 * @lun_id: LUN ID of the scsi device.
10226 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10227 *
10228 * This function returns number of FCP commands pending for the vport.
10229 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10230 * commands pending on the vport associated with SCSI device specified
10231 * by tgt_id and lun_id parameters.
10232 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10233 * commands pending on the vport associated with SCSI target specified
10234 * by tgt_id parameter.
10235 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10236 * commands pending on the vport.
10237 * This function returns the number of iocbs which satisfy the filter.
10238 * This function is called without any lock held.
10239 **/
dea31012005-04-17 16:05:31 -050010240int
James Smart51ef4c22007-08-02 11:10:31 -040010241lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10242 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010243{
James Smart51ef4c22007-08-02 11:10:31 -040010244 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010245 struct lpfc_iocbq *iocbq;
10246 int sum, i;
dea31012005-04-17 16:05:31 -050010247
Johannes Thumshirn31979002016-07-18 16:06:03 +020010248 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010249 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10250 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010251
James Smart51ef4c22007-08-02 11:10:31 -040010252 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10253 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010254 sum++;
dea31012005-04-17 16:05:31 -050010255 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010256 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010257
dea31012005-04-17 16:05:31 -050010258 return sum;
10259}
10260
James Smarte59058c2008-08-24 21:49:00 -040010261/**
James Smart3621a712009-04-06 18:47:14 -040010262 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010263 * @phba: Pointer to HBA context object
10264 * @cmdiocb: Pointer to command iocb object.
10265 * @rspiocb: Pointer to response iocb object.
10266 *
10267 * This function is called when an aborted FCP iocb completes. This
10268 * function is called by the ring event handler with no lock held.
10269 * This function frees the iocb.
10270 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010271void
James Smart2e0fef82007-06-17 19:56:36 -050010272lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10273 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010274{
James Smartcb69f7d2011-12-13 13:21:57 -050010275 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010276 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010277 "original iotag x%x, abort cmd iotag x%x "
10278 "status 0x%x, reason 0x%x\n",
10279 cmdiocb->iocb.un.acxri.abortContextTag,
10280 cmdiocb->iocb.un.acxri.abortIoTag,
10281 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10282 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010283 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010284 return;
10285}
10286
James Smarte59058c2008-08-24 21:49:00 -040010287/**
James Smart3621a712009-04-06 18:47:14 -040010288 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010289 * @vport: Pointer to virtual port.
10290 * @pring: Pointer to driver SLI ring object.
10291 * @tgt_id: SCSI ID of the target.
10292 * @lun_id: LUN ID of the scsi device.
10293 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10294 *
10295 * This function sends an abort command for every SCSI command
10296 * associated with the given virtual port pending on the ring
10297 * filtered by lpfc_sli_validate_fcp_iocb function.
10298 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10299 * FCP iocbs associated with lun specified by tgt_id and lun_id
10300 * parameters
10301 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10302 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10303 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10304 * FCP iocbs associated with virtual port.
10305 * This function returns number of iocbs it failed to abort.
10306 * This function is called with no locks held.
10307 **/
dea31012005-04-17 16:05:31 -050010308int
James Smart51ef4c22007-08-02 11:10:31 -040010309lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10310 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010311{
James Smart51ef4c22007-08-02 11:10:31 -040010312 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010313 struct lpfc_iocbq *iocbq;
10314 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010315 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010316 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010317 int i;
dea31012005-04-17 16:05:31 -050010318
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010319 for (i = 1; i <= phba->sli.last_iotag; i++) {
10320 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010321
James Smart51ef4c22007-08-02 11:10:31 -040010322 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010323 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010324 continue;
10325
James Smartafbd8d82013-09-06 12:22:13 -040010326 /*
10327 * If the iocbq is already being aborted, don't take a second
10328 * action, but do count it.
10329 */
10330 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10331 continue;
10332
dea31012005-04-17 16:05:31 -050010333 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010334 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010335 if (abtsiocb == NULL) {
10336 errcnt++;
10337 continue;
10338 }
dea31012005-04-17 16:05:31 -050010339
James Smartafbd8d82013-09-06 12:22:13 -040010340 /* indicate the IO is being aborted by the driver. */
10341 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10342
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010343 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010344 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10345 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010346 if (phba->sli_rev == LPFC_SLI_REV4)
10347 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10348 else
10349 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010350 abtsiocb->iocb.ulpLe = 1;
10351 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010352 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010353
James Smart5ffc2662009-11-18 15:39:44 -050010354 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10355 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -050010356 if (iocbq->iocb_flag & LPFC_IO_FCP)
10357 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010358 if (iocbq->iocb_flag & LPFC_IO_FOF)
10359 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010360
James Smart2e0fef82007-06-17 19:56:36 -050010361 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010362 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10363 else
10364 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10365
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010366 /* Setup callback routine and issue the command. */
10367 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010368 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10369 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010370 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010371 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010372 errcnt++;
10373 continue;
10374 }
10375 }
10376
10377 return errcnt;
10378}
10379
James Smarte59058c2008-08-24 21:49:00 -040010380/**
James Smart98912dda2014-04-04 13:52:31 -040010381 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10382 * @vport: Pointer to virtual port.
10383 * @pring: Pointer to driver SLI ring object.
10384 * @tgt_id: SCSI ID of the target.
10385 * @lun_id: LUN ID of the scsi device.
10386 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10387 *
10388 * This function sends an abort command for every SCSI command
10389 * associated with the given virtual port pending on the ring
10390 * filtered by lpfc_sli_validate_fcp_iocb function.
10391 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10392 * FCP iocbs associated with lun specified by tgt_id and lun_id
10393 * parameters
10394 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10395 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10396 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10397 * FCP iocbs associated with virtual port.
10398 * This function returns number of iocbs it aborted .
10399 * This function is called with no locks held right after a taskmgmt
10400 * command is sent.
10401 **/
10402int
10403lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10404 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10405{
10406 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040010407 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040010408 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040010409 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040010410 struct lpfc_iocbq *iocbq;
10411 IOCB_t *icmd;
10412 int sum, i, ret_val;
10413 unsigned long iflags;
10414 struct lpfc_sli_ring *pring_s4;
10415 uint32_t ring_number;
10416
10417 spin_lock_irq(&phba->hbalock);
10418
10419 /* all I/Os are in process of being flushed */
10420 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10421 spin_unlock_irq(&phba->hbalock);
10422 return 0;
10423 }
10424 sum = 0;
10425
10426 for (i = 1; i <= phba->sli.last_iotag; i++) {
10427 iocbq = phba->sli.iocbq_lookup[i];
10428
10429 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10430 cmd) != 0)
10431 continue;
10432
10433 /*
10434 * If the iocbq is already being aborted, don't take a second
10435 * action, but do count it.
10436 */
10437 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10438 continue;
10439
10440 /* issue ABTS for this IOCB based on iotag */
10441 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10442 if (abtsiocbq == NULL)
10443 continue;
10444
10445 icmd = &iocbq->iocb;
10446 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10447 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10448 if (phba->sli_rev == LPFC_SLI_REV4)
10449 abtsiocbq->iocb.un.acxri.abortIoTag =
10450 iocbq->sli4_xritag;
10451 else
10452 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10453 abtsiocbq->iocb.ulpLe = 1;
10454 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10455 abtsiocbq->vport = vport;
10456
10457 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10458 abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
10459 if (iocbq->iocb_flag & LPFC_IO_FCP)
10460 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010461 if (iocbq->iocb_flag & LPFC_IO_FOF)
10462 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040010463
James Smart8c50d252014-09-03 12:58:16 -040010464 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10465 ndlp = lpfc_cmd->rdata->pnode;
10466
10467 if (lpfc_is_link_up(phba) &&
10468 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040010469 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10470 else
10471 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10472
10473 /* Setup callback routine and issue the command. */
10474 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10475
10476 /*
10477 * Indicate the IO is being aborted by the driver and set
10478 * the caller's flag into the aborted IO.
10479 */
10480 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10481
10482 if (phba->sli_rev == LPFC_SLI_REV4) {
10483 ring_number = MAX_SLI3_CONFIGURED_RINGS +
10484 iocbq->fcp_wqidx;
10485 pring_s4 = &phba->sli.ring[ring_number];
10486 /* Note: both hbalock and ring_lock must be set here */
10487 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10488 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10489 abtsiocbq, 0);
10490 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10491 } else {
10492 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10493 abtsiocbq, 0);
10494 }
10495
10496
10497 if (ret_val == IOCB_ERROR)
10498 __lpfc_sli_release_iocbq(phba, abtsiocbq);
10499 else
10500 sum++;
10501 }
10502 spin_unlock_irq(&phba->hbalock);
10503 return sum;
10504}
10505
10506/**
James Smart3621a712009-04-06 18:47:14 -040010507 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040010508 * @phba: Pointer to HBA context object.
10509 * @cmdiocbq: Pointer to command iocb.
10510 * @rspiocbq: Pointer to response iocb.
10511 *
10512 * This function is the completion handler for iocbs issued using
10513 * lpfc_sli_issue_iocb_wait function. This function is called by the
10514 * ring event handler function without any lock held. This function
10515 * can be called from both worker thread context and interrupt
10516 * context. This function also can be called from other thread which
10517 * cleans up the SLI layer objects.
10518 * This function copy the contents of the response iocb to the
10519 * response iocb memory object provided by the caller of
10520 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10521 * sleeps for the iocb completion.
10522 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010523static void
10524lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10525 struct lpfc_iocbq *cmdiocbq,
10526 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050010527{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010528 wait_queue_head_t *pdone_q;
10529 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050010530 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010531
James Smart2e0fef82007-06-17 19:56:36 -050010532 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040010533 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10534
10535 /*
10536 * A time out has occurred for the iocb. If a time out
10537 * completion handler has been supplied, call it. Otherwise,
10538 * just free the iocbq.
10539 */
10540
10541 spin_unlock_irqrestore(&phba->hbalock, iflags);
10542 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10543 cmdiocbq->wait_iocb_cmpl = NULL;
10544 if (cmdiocbq->iocb_cmpl)
10545 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10546 else
10547 lpfc_sli_release_iocbq(phba, cmdiocbq);
10548 return;
10549 }
10550
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010551 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10552 if (cmdiocbq->context2 && rspiocbq)
10553 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10554 &rspiocbq->iocb, sizeof(IOCB_t));
10555
James Smart0f65ff62010-02-26 14:14:23 -050010556 /* Set the exchange busy flag for task management commands */
10557 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10558 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10559 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10560 cur_iocbq);
10561 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10562 }
10563
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010564 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010565 if (pdone_q)
10566 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050010567 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050010568 return;
10569}
10570
James Smarte59058c2008-08-24 21:49:00 -040010571/**
James Smartd11e31d2009-06-10 17:23:06 -040010572 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10573 * @phba: Pointer to HBA context object..
10574 * @piocbq: Pointer to command iocb.
10575 * @flag: Flag to test.
10576 *
10577 * This routine grabs the hbalock and then test the iocb_flag to
10578 * see if the passed in flag is set.
10579 * Returns:
10580 * 1 if flag is set.
10581 * 0 if flag is not set.
10582 **/
10583static int
10584lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10585 struct lpfc_iocbq *piocbq, uint32_t flag)
10586{
10587 unsigned long iflags;
10588 int ret;
10589
10590 spin_lock_irqsave(&phba->hbalock, iflags);
10591 ret = piocbq->iocb_flag & flag;
10592 spin_unlock_irqrestore(&phba->hbalock, iflags);
10593 return ret;
10594
10595}
10596
10597/**
James Smart3621a712009-04-06 18:47:14 -040010598 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040010599 * @phba: Pointer to HBA context object..
10600 * @pring: Pointer to sli ring.
10601 * @piocb: Pointer to command iocb.
10602 * @prspiocbq: Pointer to response iocb.
10603 * @timeout: Timeout in number of seconds.
10604 *
10605 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040010606 * iocb to complete. The iocb_cmpl field of the shall be used
10607 * to handle iocbs which time out. If the field is NULL, the
10608 * function shall free the iocbq structure. If more clean up is
10609 * needed, the caller is expected to provide a completion function
10610 * that will provide the needed clean up. If the iocb command is
10611 * not completed within timeout seconds, the function will either
10612 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10613 * completion function set in the iocb_cmpl field and then return
10614 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10615 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040010616 * The function waits for the iocb completion using an
10617 * non-interruptible wait.
10618 * This function will sleep while waiting for iocb completion.
10619 * So, this function should not be called from any context which
10620 * does not allow sleeping. Due to the same reason, this function
10621 * cannot be called with interrupt disabled.
10622 * This function assumes that the iocb completions occur while
10623 * this function sleep. So, this function cannot be called from
10624 * the thread which process iocb completion for this ring.
10625 * This function clears the iocb_flag of the iocb object before
10626 * issuing the iocb and the iocb completion handler sets this
10627 * flag and wakes this thread when the iocb completes.
10628 * The contents of the response iocb will be copied to prspiocbq
10629 * by the completion handler when the command completes.
10630 * This function returns IOCB_SUCCESS when success.
10631 * This function is called with no lock held.
10632 **/
dea31012005-04-17 16:05:31 -050010633int
James Smart2e0fef82007-06-17 19:56:36 -050010634lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040010635 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050010636 struct lpfc_iocbq *piocb,
10637 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010638 uint32_t timeout)
dea31012005-04-17 16:05:31 -050010639{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010640 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010641 long timeleft, timeout_req = 0;
10642 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010643 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050010644 struct lpfc_iocbq *iocb;
10645 int txq_cnt = 0;
10646 int txcmplq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040010647 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart5a0916b2013-07-15 18:31:42 -040010648 unsigned long iflags;
10649 bool iocb_completed = true;
10650
dea31012005-04-17 16:05:31 -050010651 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010652 * If the caller has provided a response iocbq buffer, then context2
10653 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050010654 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010655 if (prspiocbq) {
10656 if (piocb->context2)
10657 return IOCB_ERROR;
10658 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050010659 }
10660
James Smart5a0916b2013-07-15 18:31:42 -040010661 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010662 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10663 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040010664 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050010665
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010666 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010667 if (lpfc_readl(phba->HCregaddr, &creg_val))
10668 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010669 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10670 writel(creg_val, phba->HCregaddr);
10671 readl(phba->HCregaddr); /* flush */
10672 }
10673
James Smart2a9bf3d2010-06-07 15:24:45 -040010674 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10675 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010676 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040010677 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010678 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040010679 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010680 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040010681 spin_lock_irqsave(&phba->hbalock, iflags);
10682 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050010683
James Smart5a0916b2013-07-15 18:31:42 -040010684 /*
10685 * IOCB timed out. Inform the wake iocb wait
10686 * completion function and set local status
10687 */
10688
10689 iocb_completed = false;
10690 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10691 }
10692 spin_unlock_irqrestore(&phba->hbalock, iflags);
10693 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040010694 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010695 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040010696 /* Note: we are not indicating if the IOCB has a success
10697 * status or not - that's for the caller to check.
10698 * IOCB_SUCCESS means just that the command was sent and
10699 * completed. Not that it completed successfully.
10700 * */
James Smart7054a602007-04-25 09:52:34 -040010701 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010702 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010703 "0338 IOCB wait timeout error - no "
10704 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010705 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040010706 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010707 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010708 "0330 IOCB wake NOT set, "
10709 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010710 timeout, (timeleft / jiffies));
10711 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010712 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010713 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050010714 if (phba->cfg_log_verbose & LOG_SLI) {
10715 list_for_each_entry(iocb, &pring->txq, list) {
10716 txq_cnt++;
10717 }
10718 list_for_each_entry(iocb, &pring->txcmplq, list) {
10719 txcmplq_cnt++;
10720 }
10721 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10722 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10723 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10724 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010725 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010726 } else {
10727 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010728 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010729 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010730 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010731 }
10732
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010733 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010734 if (lpfc_readl(phba->HCregaddr, &creg_val))
10735 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010736 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10737 writel(creg_val, phba->HCregaddr);
10738 readl(phba->HCregaddr); /* flush */
10739 }
10740
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010741 if (prspiocbq)
10742 piocb->context2 = NULL;
10743
10744 piocb->context_un.wait_queue = NULL;
10745 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010746 return retval;
10747}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010748
James Smarte59058c2008-08-24 21:49:00 -040010749/**
James Smart3621a712009-04-06 18:47:14 -040010750 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010751 * @phba: Pointer to HBA context object.
10752 * @pmboxq: Pointer to driver mailbox object.
10753 * @timeout: Timeout in number of seconds.
10754 *
10755 * This function issues the mailbox to firmware and waits for the
10756 * mailbox command to complete. If the mailbox command is not
10757 * completed within timeout seconds, it returns MBX_TIMEOUT.
10758 * The function waits for the mailbox completion using an
10759 * interruptible wait. If the thread is woken up due to a
10760 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10761 * should not free the mailbox resources, if this function returns
10762 * MBX_TIMEOUT.
10763 * This function will sleep while waiting for mailbox completion.
10764 * So, this function should not be called from any context which
10765 * does not allow sleeping. Due to the same reason, this function
10766 * cannot be called with interrupt disabled.
10767 * This function assumes that the mailbox completion occurs while
10768 * this function sleep. So, this function cannot be called from
10769 * the worker thread which processes mailbox completion.
10770 * This function is called in the context of HBA management
10771 * applications.
10772 * This function returns MBX_SUCCESS when successful.
10773 * This function is called with no lock held.
10774 **/
dea31012005-04-17 16:05:31 -050010775int
James Smart2e0fef82007-06-17 19:56:36 -050010776lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010777 uint32_t timeout)
10778{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010779 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040010780 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050010781 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010782 unsigned long flag;
dea31012005-04-17 16:05:31 -050010783
James Smartb230b8a2013-05-31 17:05:27 -040010784 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040010785 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040010786 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050010787
James Smart495a7142008-06-14 22:52:59 -040010788 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010789 /* setup wake call as IOCB callback */
10790 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10791 /* setup context field to pass wait_queue pointer to wake function */
10792 pmboxq->context1 = &done_q;
10793
dea31012005-04-17 16:05:31 -050010794 /* now issue the command */
10795 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010796 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010797 wait_event_interruptible_timeout(done_q,
10798 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040010799 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040010800
James Smart858c9f62007-06-17 19:56:39 -050010801 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010802 /* restore the possible extended buffer for free resource */
10803 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040010804 /*
10805 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10806 * else do not free the resources.
10807 */
James Smartd7c47992010-06-08 18:31:54 -040010808 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010809 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010810 } else {
James Smart7054a602007-04-25 09:52:34 -040010811 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010812 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10813 }
10814 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010815 } else {
10816 /* restore the possible extended buffer for free resource */
10817 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050010818 }
10819
dea31012005-04-17 16:05:31 -050010820 return retval;
10821}
10822
James Smarte59058c2008-08-24 21:49:00 -040010823/**
James Smart3772a992009-05-22 14:50:54 -040010824 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010825 * @phba: Pointer to HBA context.
10826 *
James Smart3772a992009-05-22 14:50:54 -040010827 * This function is called to shutdown the driver's mailbox sub-system.
10828 * It first marks the mailbox sub-system is in a block state to prevent
10829 * the asynchronous mailbox command from issued off the pending mailbox
10830 * command queue. If the mailbox command sub-system shutdown is due to
10831 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10832 * the mailbox sub-system flush routine to forcefully bring down the
10833 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10834 * as with offline or HBA function reset), this routine will wait for the
10835 * outstanding mailbox command to complete before invoking the mailbox
10836 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010837 **/
James Smart3772a992009-05-22 14:50:54 -040010838void
James Smart618a5232012-06-12 13:54:36 -040010839lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010840{
James Smart3772a992009-05-22 14:50:54 -040010841 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010842 unsigned long timeout;
10843
James Smart618a5232012-06-12 13:54:36 -040010844 if (mbx_action == LPFC_MBX_NO_WAIT) {
10845 /* delay 100ms for port state */
10846 msleep(100);
10847 lpfc_sli_mbox_sys_flush(phba);
10848 return;
10849 }
James Smarta183a152011-10-10 21:32:43 -040010850 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010851
James Smart3772a992009-05-22 14:50:54 -040010852 spin_lock_irq(&phba->hbalock);
10853 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010854
10855 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010856 /* Determine how long we might wait for the active mailbox
10857 * command to be gracefully completed by firmware.
10858 */
James Smarta183a152011-10-10 21:32:43 -040010859 if (phba->sli.mbox_active)
10860 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10861 phba->sli.mbox_active) *
10862 1000) + jiffies;
10863 spin_unlock_irq(&phba->hbalock);
10864
James Smart3772a992009-05-22 14:50:54 -040010865 while (phba->sli.mbox_active) {
10866 /* Check active mailbox complete status every 2ms */
10867 msleep(2);
10868 if (time_after(jiffies, timeout))
10869 /* Timeout, let the mailbox flush routine to
10870 * forcefully release active mailbox command
10871 */
10872 break;
10873 }
James Smartd7069f02012-03-01 22:36:29 -050010874 } else
10875 spin_unlock_irq(&phba->hbalock);
10876
James Smart3772a992009-05-22 14:50:54 -040010877 lpfc_sli_mbox_sys_flush(phba);
10878}
10879
10880/**
10881 * lpfc_sli_eratt_read - read sli-3 error attention events
10882 * @phba: Pointer to HBA context.
10883 *
10884 * This function is called to read the SLI3 device error attention registers
10885 * for possible error attention events. The caller must hold the hostlock
10886 * with spin_lock_irq().
10887 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010888 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010889 * Register and returns 0 otherwise.
10890 **/
10891static int
10892lpfc_sli_eratt_read(struct lpfc_hba *phba)
10893{
James Smarted957682007-06-17 19:56:37 -050010894 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010895
James Smart3772a992009-05-22 14:50:54 -040010896 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010897 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10898 goto unplug_err;
10899
James Smart3772a992009-05-22 14:50:54 -040010900 if (ha_copy & HA_ERATT) {
10901 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010902 if (lpfc_sli_read_hs(phba))
10903 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010904
James Smart3772a992009-05-22 14:50:54 -040010905 /* Check if there is a deferred error condition is active */
10906 if ((HS_FFER1 & phba->work_hs) &&
10907 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010908 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010909 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010910 /* Clear all interrupt enable conditions */
10911 writel(0, phba->HCregaddr);
10912 readl(phba->HCregaddr);
10913 }
10914
10915 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010916 phba->work_ha |= HA_ERATT;
10917 /* Indicate polling handles this ERATT */
10918 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010919 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010920 }
James Smart3772a992009-05-22 14:50:54 -040010921 return 0;
James Smart9940b972011-03-11 16:06:12 -050010922
10923unplug_err:
10924 /* Set the driver HS work bitmap */
10925 phba->work_hs |= UNPLUG_ERR;
10926 /* Set the driver HA work bitmap */
10927 phba->work_ha |= HA_ERATT;
10928 /* Indicate polling handles this ERATT */
10929 phba->hba_flag |= HBA_ERATT_HANDLED;
10930 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010931}
10932
James Smarte59058c2008-08-24 21:49:00 -040010933/**
James Smartda0436e2009-05-22 14:51:39 -040010934 * lpfc_sli4_eratt_read - read sli-4 error attention events
10935 * @phba: Pointer to HBA context.
10936 *
10937 * This function is called to read the SLI4 device error attention registers
10938 * for possible error attention events. The caller must hold the hostlock
10939 * with spin_lock_irq().
10940 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010941 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010942 * Register and returns 0 otherwise.
10943 **/
10944static int
10945lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10946{
10947 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010948 uint32_t if_type, portsmphr;
10949 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010950
James Smart2fcee4b2010-12-15 17:57:46 -050010951 /*
10952 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010953 * registers for error attention. This can be changed later.
10954 */
James Smart2fcee4b2010-12-15 17:57:46 -050010955 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10956 switch (if_type) {
10957 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010958 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10959 &uerr_sta_lo) ||
10960 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10961 &uerr_sta_hi)) {
10962 phba->work_hs |= UNPLUG_ERR;
10963 phba->work_ha |= HA_ERATT;
10964 phba->hba_flag |= HBA_ERATT_HANDLED;
10965 return 1;
10966 }
James Smart2fcee4b2010-12-15 17:57:46 -050010967 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10968 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10969 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10970 "1423 HBA Unrecoverable error: "
10971 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10972 "ue_mask_lo_reg=0x%x, "
10973 "ue_mask_hi_reg=0x%x\n",
10974 uerr_sta_lo, uerr_sta_hi,
10975 phba->sli4_hba.ue_mask_lo,
10976 phba->sli4_hba.ue_mask_hi);
10977 phba->work_status[0] = uerr_sta_lo;
10978 phba->work_status[1] = uerr_sta_hi;
10979 phba->work_ha |= HA_ERATT;
10980 phba->hba_flag |= HBA_ERATT_HANDLED;
10981 return 1;
10982 }
10983 break;
10984 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010985 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10986 &portstat_reg.word0) ||
10987 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10988 &portsmphr)){
10989 phba->work_hs |= UNPLUG_ERR;
10990 phba->work_ha |= HA_ERATT;
10991 phba->hba_flag |= HBA_ERATT_HANDLED;
10992 return 1;
10993 }
James Smart2fcee4b2010-12-15 17:57:46 -050010994 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10995 phba->work_status[0] =
10996 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10997 phba->work_status[1] =
10998 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10999 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050011000 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050011001 "port status reg 0x%x, "
11002 "port smphr reg 0x%x, "
11003 "error 1=0x%x, error 2=0x%x\n",
11004 portstat_reg.word0,
11005 portsmphr,
11006 phba->work_status[0],
11007 phba->work_status[1]);
11008 phba->work_ha |= HA_ERATT;
11009 phba->hba_flag |= HBA_ERATT_HANDLED;
11010 return 1;
11011 }
11012 break;
11013 case LPFC_SLI_INTF_IF_TYPE_1:
11014 default:
James Smarta747c9c2009-11-18 15:41:10 -050011015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011016 "2886 HBA Error Attention on unsupported "
11017 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011018 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011019 }
James Smart2fcee4b2010-12-15 17:57:46 -050011020
James Smartda0436e2009-05-22 14:51:39 -040011021 return 0;
11022}
11023
11024/**
James Smart3621a712009-04-06 18:47:14 -040011025 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011026 * @phba: Pointer to HBA context.
11027 *
James Smart3772a992009-05-22 14:50:54 -040011028 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011029 * error attention register bit for error attention events.
11030 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011031 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011032 * Register and returns 0 otherwise.
11033 **/
11034int
11035lpfc_sli_check_eratt(struct lpfc_hba *phba)
11036{
11037 uint32_t ha_copy;
11038
11039 /* If somebody is waiting to handle an eratt, don't process it
11040 * here. The brdkill function will do this.
11041 */
11042 if (phba->link_flag & LS_IGNORE_ERATT)
11043 return 0;
11044
11045 /* Check if interrupt handler handles this ERATT */
11046 spin_lock_irq(&phba->hbalock);
11047 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11048 /* Interrupt handler has handled ERATT */
11049 spin_unlock_irq(&phba->hbalock);
11050 return 0;
11051 }
11052
James Smarta257bf92009-04-06 18:48:10 -040011053 /*
11054 * If there is deferred error attention, do not check for error
11055 * attention
11056 */
11057 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11058 spin_unlock_irq(&phba->hbalock);
11059 return 0;
11060 }
11061
James Smart3772a992009-05-22 14:50:54 -040011062 /* If PCI channel is offline, don't process it */
11063 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011064 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011065 return 0;
11066 }
11067
11068 switch (phba->sli_rev) {
11069 case LPFC_SLI_REV2:
11070 case LPFC_SLI_REV3:
11071 /* Read chip Host Attention (HA) register */
11072 ha_copy = lpfc_sli_eratt_read(phba);
11073 break;
James Smartda0436e2009-05-22 14:51:39 -040011074 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011075 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011076 ha_copy = lpfc_sli4_eratt_read(phba);
11077 break;
James Smart3772a992009-05-22 14:50:54 -040011078 default:
11079 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11080 "0299 Invalid SLI revision (%d)\n",
11081 phba->sli_rev);
11082 ha_copy = 0;
11083 break;
James Smart93996272008-08-24 21:50:30 -040011084 }
11085 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011086
11087 return ha_copy;
11088}
11089
11090/**
11091 * lpfc_intr_state_check - Check device state for interrupt handling
11092 * @phba: Pointer to HBA context.
11093 *
11094 * This inline routine checks whether a device or its PCI slot is in a state
11095 * that the interrupt should be handled.
11096 *
11097 * This function returns 0 if the device or the PCI slot is in a state that
11098 * interrupt should be handled, otherwise -EIO.
11099 */
11100static inline int
11101lpfc_intr_state_check(struct lpfc_hba *phba)
11102{
11103 /* If the pci channel is offline, ignore all the interrupts */
11104 if (unlikely(pci_channel_offline(phba->pcidev)))
11105 return -EIO;
11106
11107 /* Update device level interrupt statistics */
11108 phba->sli.slistat.sli_intr++;
11109
11110 /* Ignore all interrupts during initialization. */
11111 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11112 return -EIO;
11113
James Smart93996272008-08-24 21:50:30 -040011114 return 0;
11115}
11116
11117/**
James Smart3772a992009-05-22 14:50:54 -040011118 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011119 * @irq: Interrupt number.
11120 * @dev_id: The device context pointer.
11121 *
James Smart93996272008-08-24 21:50:30 -040011122 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011123 * service routine when device with SLI-3 interface spec is enabled with
11124 * MSI-X multi-message interrupt mode and there are slow-path events in
11125 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11126 * interrupt mode, this function is called as part of the device-level
11127 * interrupt handler. When the PCI slot is in error recovery or the HBA
11128 * is undergoing initialization, the interrupt handler will not process
11129 * the interrupt. The link attention and ELS ring attention events are
11130 * handled by the worker thread. The interrupt handler signals the worker
11131 * thread and returns for these events. This function is called without
11132 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011133 * structures.
11134 *
11135 * This function returns IRQ_HANDLED when interrupt is handled else it
11136 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011137 **/
dea31012005-04-17 16:05:31 -050011138irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011139lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011140{
James Smart2e0fef82007-06-17 19:56:36 -050011141 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011142 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011143 uint32_t work_ha_copy;
11144 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011145 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011146 uint32_t control;
11147
James Smart92d7f7b2007-06-17 19:56:38 -050011148 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011149 struct lpfc_vport *vport;
11150 struct lpfc_nodelist *ndlp;
11151 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011152 LPFC_MBOXQ_t *pmb;
11153 int rc;
11154
dea31012005-04-17 16:05:31 -050011155 /*
11156 * Get the driver's phba structure from the dev_id and
11157 * assume the HBA is not interrupting.
11158 */
James Smart93996272008-08-24 21:50:30 -040011159 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011160
11161 if (unlikely(!phba))
11162 return IRQ_NONE;
11163
dea31012005-04-17 16:05:31 -050011164 /*
James Smart93996272008-08-24 21:50:30 -040011165 * Stuff needs to be attented to when this function is invoked as an
11166 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011167 */
James Smart93996272008-08-24 21:50:30 -040011168 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011169 /* Check device state for handling interrupt */
11170 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011171 return IRQ_NONE;
11172 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011173 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011174 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11175 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011176 /* If somebody is waiting to handle an eratt don't process it
11177 * here. The brdkill function will do this.
11178 */
11179 if (phba->link_flag & LS_IGNORE_ERATT)
11180 ha_copy &= ~HA_ERATT;
11181 /* Check the need for handling ERATT in interrupt handler */
11182 if (ha_copy & HA_ERATT) {
11183 if (phba->hba_flag & HBA_ERATT_HANDLED)
11184 /* ERATT polling has handled ERATT */
11185 ha_copy &= ~HA_ERATT;
11186 else
11187 /* Indicate interrupt handler handles ERATT */
11188 phba->hba_flag |= HBA_ERATT_HANDLED;
11189 }
James Smarta257bf92009-04-06 18:48:10 -040011190
11191 /*
11192 * If there is deferred error attention, do not check for any
11193 * interrupt.
11194 */
11195 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011196 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011197 return IRQ_NONE;
11198 }
11199
James Smart93996272008-08-24 21:50:30 -040011200 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011201 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11202 goto unplug_error;
11203
James Smarta747c9c2009-11-18 15:41:10 -050011204 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11205 HC_LAINT_ENA | HC_ERINT_ENA),
11206 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011207 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11208 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011209 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011210 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011211 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011212 } else
11213 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011214
dea31012005-04-17 16:05:31 -050011215 work_ha_copy = ha_copy & phba->work_ha_mask;
11216
James Smart93996272008-08-24 21:50:30 -040011217 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011218 if (work_ha_copy & HA_LATT) {
11219 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11220 /*
11221 * Turn off Link Attention interrupts
11222 * until CLEAR_LA done
11223 */
James Smart5b75da22008-12-04 22:39:35 -050011224 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011225 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011226 if (lpfc_readl(phba->HCregaddr, &control))
11227 goto unplug_error;
dea31012005-04-17 16:05:31 -050011228 control &= ~HC_LAINT_ENA;
11229 writel(control, phba->HCregaddr);
11230 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011231 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011232 }
11233 else
11234 work_ha_copy &= ~HA_LATT;
11235 }
11236
James Smart93996272008-08-24 21:50:30 -040011237 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011238 /*
11239 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11240 * the only slow ring.
11241 */
11242 status = (work_ha_copy &
11243 (HA_RXMASK << (4*LPFC_ELS_RING)));
11244 status >>= (4*LPFC_ELS_RING);
11245 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011246 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011247 if (lpfc_readl(phba->HCregaddr, &control))
11248 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011249
11250 lpfc_debugfs_slow_ring_trc(phba,
11251 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11252 control, status,
11253 (uint32_t)phba->sli.slistat.sli_intr);
11254
James Smart858c9f62007-06-17 19:56:39 -050011255 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011256 lpfc_debugfs_slow_ring_trc(phba,
11257 "ISR Disable ring:"
11258 "pwork:x%x hawork:x%x wait:x%x",
11259 phba->work_ha, work_ha_copy,
11260 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011261 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011262
James Smart858c9f62007-06-17 19:56:39 -050011263 control &=
11264 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011265 writel(control, phba->HCregaddr);
11266 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011267 }
James Smarta58cbd52007-08-02 11:09:43 -040011268 else {
11269 lpfc_debugfs_slow_ring_trc(phba,
11270 "ISR slow ring: pwork:"
11271 "x%x hawork:x%x wait:x%x",
11272 phba->work_ha, work_ha_copy,
11273 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011274 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011275 }
James Smart5b75da22008-12-04 22:39:35 -050011276 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011277 }
11278 }
James Smart5b75da22008-12-04 22:39:35 -050011279 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011280 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011281 if (lpfc_sli_read_hs(phba))
11282 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011283 /*
11284 * Check if there is a deferred error condition
11285 * is active
11286 */
11287 if ((HS_FFER1 & phba->work_hs) &&
11288 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011289 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11290 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011291 phba->hba_flag |= DEFER_ERATT;
11292 /* Clear all interrupt enable conditions */
11293 writel(0, phba->HCregaddr);
11294 readl(phba->HCregaddr);
11295 }
11296 }
11297
James Smart93996272008-08-24 21:50:30 -040011298 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011299 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011300 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011301 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011302 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011303
11304 /* First check out the status word */
11305 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11306 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011307 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011308 /*
11309 * Stray Mailbox Interrupt, mbxCommand <cmd>
11310 * mbxStatus <status>
11311 */
James Smart09372822008-01-11 01:52:54 -050011312 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011313 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011314 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011315 "Interrupt mbxCommand x%x "
11316 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011317 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011318 pmbox->mbxCommand,
11319 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011320 /* clear mailbox attention bit */
11321 work_ha_copy &= ~HA_MBATT;
11322 } else {
James Smart97eab632008-04-07 10:16:05 -040011323 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011324 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011325 phba->last_completion_time = jiffies;
11326 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011327 if (pmb->mbox_cmpl) {
11328 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11329 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011330 if (pmb->out_ext_byte_len &&
11331 pmb->context2)
11332 lpfc_sli_pcimem_bcopy(
11333 phba->mbox_ext,
11334 pmb->context2,
11335 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011336 }
James Smart09372822008-01-11 01:52:54 -050011337 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11338 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11339
11340 lpfc_debugfs_disc_trc(vport,
11341 LPFC_DISC_TRC_MBOX_VPORT,
11342 "MBOX dflt rpi: : "
11343 "status:x%x rpi:x%x",
11344 (uint32_t)pmbox->mbxStatus,
11345 pmbox->un.varWords[0], 0);
11346
11347 if (!pmbox->mbxStatus) {
11348 mp = (struct lpfc_dmabuf *)
11349 (pmb->context1);
11350 ndlp = (struct lpfc_nodelist *)
11351 pmb->context2;
11352
11353 /* Reg_LOGIN of dflt RPI was
11354 * successful. new lets get
11355 * rid of the RPI using the
11356 * same mbox buffer.
11357 */
11358 lpfc_unreg_login(phba,
11359 vport->vpi,
11360 pmbox->un.varWords[0],
11361 pmb);
11362 pmb->mbox_cmpl =
11363 lpfc_mbx_cmpl_dflt_rpi;
11364 pmb->context1 = mp;
11365 pmb->context2 = ndlp;
11366 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040011367 rc = lpfc_sli_issue_mbox(phba,
11368 pmb,
11369 MBX_NOWAIT);
11370 if (rc != MBX_BUSY)
11371 lpfc_printf_log(phba,
11372 KERN_ERR,
11373 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011374 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040011375 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040011376 if (rc != MBX_NOT_FINISHED)
11377 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050011378 }
11379 }
James Smart5b75da22008-12-04 22:39:35 -050011380 spin_lock_irqsave(
11381 &phba->pport->work_port_lock,
11382 iflag);
James Smart09372822008-01-11 01:52:54 -050011383 phba->pport->work_port_events &=
11384 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050011385 spin_unlock_irqrestore(
11386 &phba->pport->work_port_lock,
11387 iflag);
James Smart09372822008-01-11 01:52:54 -050011388 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050011389 }
James Smart97eab632008-04-07 10:16:05 -040011390 } else
James Smart5b75da22008-12-04 22:39:35 -050011391 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011392
James Smart92d7f7b2007-06-17 19:56:38 -050011393 if ((work_ha_copy & HA_MBATT) &&
11394 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050011395send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050011396 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040011397 do {
11398 rc = lpfc_sli_issue_mbox(phba, NULL,
11399 MBX_NOWAIT);
11400 } while (rc == MBX_NOT_FINISHED);
11401 if (rc != MBX_SUCCESS)
11402 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11403 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040011404 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050011405 }
11406
James Smart5b75da22008-12-04 22:39:35 -050011407 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011408 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050011409 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040011410 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050011411 }
James Smart93996272008-08-24 21:50:30 -040011412 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050011413unplug_error:
11414 spin_unlock_irqrestore(&phba->hbalock, iflag);
11415 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050011416
James Smart3772a992009-05-22 14:50:54 -040011417} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040011418
11419/**
James Smart3772a992009-05-22 14:50:54 -040011420 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040011421 * @irq: Interrupt number.
11422 * @dev_id: The device context pointer.
11423 *
11424 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011425 * service routine when device with SLI-3 interface spec is enabled with
11426 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11427 * ring event in the HBA. However, when the device is enabled with either
11428 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11429 * device-level interrupt handler. When the PCI slot is in error recovery
11430 * or the HBA is undergoing initialization, the interrupt handler will not
11431 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11432 * the intrrupt context. This function is called without any lock held.
11433 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011434 *
11435 * This function returns IRQ_HANDLED when interrupt is handled else it
11436 * returns IRQ_NONE.
11437 **/
11438irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011439lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011440{
11441 struct lpfc_hba *phba;
11442 uint32_t ha_copy;
11443 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011444 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040011445
11446 /* Get the driver's phba structure from the dev_id and
11447 * assume the HBA is not interrupting.
11448 */
11449 phba = (struct lpfc_hba *) dev_id;
11450
11451 if (unlikely(!phba))
11452 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050011453
11454 /*
James Smart93996272008-08-24 21:50:30 -040011455 * Stuff needs to be attented to when this function is invoked as an
11456 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011457 */
James Smart93996272008-08-24 21:50:30 -040011458 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011459 /* Check device state for handling interrupt */
11460 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011461 return IRQ_NONE;
11462 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050011463 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11464 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040011465 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050011466 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011467 /*
11468 * If there is deferred error attention, do not check for
11469 * any interrupt.
11470 */
11471 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011472 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011473 return IRQ_NONE;
11474 }
James Smart93996272008-08-24 21:50:30 -040011475 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11476 phba->HAregaddr);
11477 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011478 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011479 } else
11480 ha_copy = phba->ha_copy;
11481
11482 /*
11483 * Process all events on FCP ring. Take the optimized path for FCP IO.
11484 */
11485 ha_copy &= ~(phba->work_ha_mask);
11486
11487 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050011488 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050011489 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050011490 lpfc_sli_handle_fast_ring_event(phba,
11491 &phba->sli.ring[LPFC_FCP_RING],
11492 status);
James Smarta4bc3372006-12-02 13:34:16 -050011493
11494 if (phba->cfg_multi_ring_support == 2) {
11495 /*
James Smart93996272008-08-24 21:50:30 -040011496 * Process all events on extra ring. Take the optimized path
11497 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050011498 */
James Smart93996272008-08-24 21:50:30 -040011499 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050011500 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050011501 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050011502 lpfc_sli_handle_fast_ring_event(phba,
11503 &phba->sli.ring[LPFC_EXTRA_RING],
11504 status);
11505 }
11506 }
dea31012005-04-17 16:05:31 -050011507 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011508} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050011509
James Smart93996272008-08-24 21:50:30 -040011510/**
James Smart3772a992009-05-22 14:50:54 -040011511 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040011512 * @irq: Interrupt number.
11513 * @dev_id: The device context pointer.
11514 *
James Smart3772a992009-05-22 14:50:54 -040011515 * This function is the HBA device-level interrupt handler to device with
11516 * SLI-3 interface spec, called from the PCI layer when either MSI or
11517 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11518 * requires driver attention. This function invokes the slow-path interrupt
11519 * attention handling function and fast-path interrupt attention handling
11520 * function in turn to process the relevant HBA attention events. This
11521 * function is called without any lock held. It gets the hbalock to access
11522 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011523 *
11524 * This function returns IRQ_HANDLED when interrupt is handled, else it
11525 * returns IRQ_NONE.
11526 **/
11527irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011528lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011529{
11530 struct lpfc_hba *phba;
11531 irqreturn_t sp_irq_rc, fp_irq_rc;
11532 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050011533 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040011534
11535 /*
11536 * Get the driver's phba structure from the dev_id and
11537 * assume the HBA is not interrupting.
11538 */
11539 phba = (struct lpfc_hba *) dev_id;
11540
11541 if (unlikely(!phba))
11542 return IRQ_NONE;
11543
James Smart3772a992009-05-22 14:50:54 -040011544 /* Check device state for handling interrupt */
11545 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011546 return IRQ_NONE;
11547
11548 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050011549 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11550 spin_unlock(&phba->hbalock);
11551 return IRQ_HANDLED;
11552 }
11553
James Smart93996272008-08-24 21:50:30 -040011554 if (unlikely(!phba->ha_copy)) {
11555 spin_unlock(&phba->hbalock);
11556 return IRQ_NONE;
11557 } else if (phba->ha_copy & HA_ERATT) {
11558 if (phba->hba_flag & HBA_ERATT_HANDLED)
11559 /* ERATT polling has handled ERATT */
11560 phba->ha_copy &= ~HA_ERATT;
11561 else
11562 /* Indicate interrupt handler handles ERATT */
11563 phba->hba_flag |= HBA_ERATT_HANDLED;
11564 }
11565
James Smarta257bf92009-04-06 18:48:10 -040011566 /*
11567 * If there is deferred error attention, do not check for any interrupt.
11568 */
11569 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020011570 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040011571 return IRQ_NONE;
11572 }
11573
James Smart93996272008-08-24 21:50:30 -040011574 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050011575 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11576 spin_unlock(&phba->hbalock);
11577 return IRQ_HANDLED;
11578 }
James Smarta747c9c2009-11-18 15:41:10 -050011579 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11580 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11581 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011582 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011583 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011584 readl(phba->HAregaddr); /* flush */
11585 spin_unlock(&phba->hbalock);
11586
11587 /*
11588 * Invokes slow-path host attention interrupt handling as appropriate.
11589 */
11590
11591 /* status of events with mailbox and link attention */
11592 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11593
11594 /* status of events with ELS ring */
11595 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11596 status2 >>= (4*LPFC_ELS_RING);
11597
11598 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011599 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011600 else
11601 sp_irq_rc = IRQ_NONE;
11602
11603 /*
11604 * Invoke fast-path host attention interrupt handling as appropriate.
11605 */
11606
11607 /* status of events with FCP ring */
11608 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11609 status1 >>= (4*LPFC_FCP_RING);
11610
11611 /* status of events with extra ring */
11612 if (phba->cfg_multi_ring_support == 2) {
11613 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11614 status2 >>= (4*LPFC_EXTRA_RING);
11615 } else
11616 status2 = 0;
11617
11618 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011619 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011620 else
11621 fp_irq_rc = IRQ_NONE;
11622
11623 /* Return device-level interrupt handling status */
11624 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040011625} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040011626
11627/**
11628 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11629 * @phba: pointer to lpfc hba data structure.
11630 *
11631 * This routine is invoked by the worker thread to process all the pending
11632 * SLI4 FCP abort XRI events.
11633 **/
11634void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11635{
11636 struct lpfc_cq_event *cq_event;
11637
11638 /* First, declare the fcp xri abort event has been handled */
11639 spin_lock_irq(&phba->hbalock);
11640 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11641 spin_unlock_irq(&phba->hbalock);
11642 /* Now, handle all the fcp xri abort events */
11643 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11644 /* Get the first event from the head of the event queue */
11645 spin_lock_irq(&phba->hbalock);
11646 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11647 cq_event, struct lpfc_cq_event, list);
11648 spin_unlock_irq(&phba->hbalock);
11649 /* Notify aborted XRI for FCP work queue */
11650 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11651 /* Free the event processed back to the free pool */
11652 lpfc_sli4_cq_event_release(phba, cq_event);
11653 }
11654}
11655
11656/**
11657 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11658 * @phba: pointer to lpfc hba data structure.
11659 *
11660 * This routine is invoked by the worker thread to process all the pending
11661 * SLI4 els abort xri events.
11662 **/
11663void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11664{
11665 struct lpfc_cq_event *cq_event;
11666
11667 /* First, declare the els xri abort event has been handled */
11668 spin_lock_irq(&phba->hbalock);
11669 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11670 spin_unlock_irq(&phba->hbalock);
11671 /* Now, handle all the els xri abort events */
11672 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11673 /* Get the first event from the head of the event queue */
11674 spin_lock_irq(&phba->hbalock);
11675 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11676 cq_event, struct lpfc_cq_event, list);
11677 spin_unlock_irq(&phba->hbalock);
11678 /* Notify aborted XRI for ELS work queue */
11679 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11680 /* Free the event processed back to the free pool */
11681 lpfc_sli4_cq_event_release(phba, cq_event);
11682 }
11683}
11684
James Smart341af102010-01-26 23:07:37 -050011685/**
11686 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11687 * @phba: pointer to lpfc hba data structure
11688 * @pIocbIn: pointer to the rspiocbq
11689 * @pIocbOut: pointer to the cmdiocbq
11690 * @wcqe: pointer to the complete wcqe
11691 *
11692 * This routine transfers the fields of a command iocbq to a response iocbq
11693 * by copying all the IOCB fields from command iocbq and transferring the
11694 * completion status information from the complete wcqe.
11695 **/
James Smart4f774512009-05-22 14:52:35 -040011696static void
James Smart341af102010-01-26 23:07:37 -050011697lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11698 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040011699 struct lpfc_iocbq *pIocbOut,
11700 struct lpfc_wcqe_complete *wcqe)
11701{
James Smartaf227412013-10-10 12:23:10 -040011702 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050011703 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040011704 uint32_t status, max_response;
11705 struct lpfc_dmabuf *dmabuf;
11706 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040011707 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11708
11709 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11710 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040011711 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050011712 status = bf_get(lpfc_wcqe_c_status, wcqe);
11713 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040011714 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11715 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11716 pIocbIn->iocb.un.fcpi.fcpi_parm =
11717 pIocbOut->iocb.un.fcpi.fcpi_parm -
11718 wcqe->total_data_placed;
11719 else
11720 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011721 else {
James Smart4f774512009-05-22 14:52:35 -040011722 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040011723 switch (pIocbOut->iocb.ulpCommand) {
11724 case CMD_ELS_REQUEST64_CR:
11725 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11726 bpl = (struct ulp_bde64 *)dmabuf->virt;
11727 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11728 max_response = bde.tus.f.bdeSize;
11729 break;
11730 case CMD_GEN_REQUEST64_CR:
11731 max_response = 0;
11732 if (!pIocbOut->context3)
11733 break;
11734 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11735 sizeof(struct ulp_bde64);
11736 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11737 bpl = (struct ulp_bde64 *)dmabuf->virt;
11738 for (i = 0; i < numBdes; i++) {
11739 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11740 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11741 max_response += bde.tus.f.bdeSize;
11742 }
11743 break;
11744 default:
11745 max_response = wcqe->total_data_placed;
11746 break;
11747 }
11748 if (max_response < wcqe->total_data_placed)
11749 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11750 else
11751 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11752 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050011753 }
James Smart341af102010-01-26 23:07:37 -050011754
James Smartacd68592012-01-18 16:25:09 -050011755 /* Convert BG errors for completion status */
11756 if (status == CQE_STATUS_DI_ERROR) {
11757 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11758
11759 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11760 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11761 else
11762 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11763
11764 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11765 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11766 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11767 BGS_GUARD_ERR_MASK;
11768 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11769 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11770 BGS_APPTAG_ERR_MASK;
11771 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11772 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11773 BGS_REFTAG_ERR_MASK;
11774
11775 /* Check to see if there was any good data before the error */
11776 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11777 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11778 BGS_HI_WATER_MARK_PRESENT_MASK;
11779 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11780 wcqe->total_data_placed;
11781 }
11782
11783 /*
11784 * Set ALL the error bits to indicate we don't know what
11785 * type of error it is.
11786 */
11787 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11788 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11789 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11790 BGS_GUARD_ERR_MASK);
11791 }
11792
James Smart341af102010-01-26 23:07:37 -050011793 /* Pick up HBA exchange busy condition */
11794 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11795 spin_lock_irqsave(&phba->hbalock, iflags);
11796 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11797 spin_unlock_irqrestore(&phba->hbalock, iflags);
11798 }
James Smart4f774512009-05-22 14:52:35 -040011799}
11800
11801/**
James Smart45ed1192009-10-02 15:17:02 -040011802 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11803 * @phba: Pointer to HBA context object.
11804 * @wcqe: Pointer to work-queue completion queue entry.
11805 *
11806 * This routine handles an ELS work-queue completion event and construct
11807 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11808 * discovery engine to handle.
11809 *
11810 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11811 **/
11812static struct lpfc_iocbq *
11813lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11814 struct lpfc_iocbq *irspiocbq)
11815{
11816 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11817 struct lpfc_iocbq *cmdiocbq;
11818 struct lpfc_wcqe_complete *wcqe;
11819 unsigned long iflags;
11820
11821 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011822 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011823 pring->stats.iocb_event++;
11824 /* Look up the ELS command IOCB and create pseudo response IOCB */
11825 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11826 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart89533e92016-10-13 15:06:15 -070011827 /* Put the iocb back on the txcmplq */
11828 lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
James Smart7e56aa22012-08-03 12:35:34 -040011829 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011830
11831 if (unlikely(!cmdiocbq)) {
11832 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11833 "0386 ELS complete with no corresponding "
11834 "cmdiocb: iotag (%d)\n",
11835 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11836 lpfc_sli_release_iocbq(phba, irspiocbq);
11837 return NULL;
11838 }
11839
11840 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011841 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011842
11843 return irspiocbq;
11844}
11845
11846/**
James Smart04c68492009-05-22 14:52:52 -040011847 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11848 * @phba: Pointer to HBA context object.
11849 * @cqe: Pointer to mailbox completion queue entry.
11850 *
11851 * This routine process a mailbox completion queue entry with asynchrous
11852 * event.
11853 *
11854 * Return: true if work posted to worker thread, otherwise false.
11855 **/
11856static bool
11857lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11858{
11859 struct lpfc_cq_event *cq_event;
11860 unsigned long iflags;
11861
11862 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11863 "0392 Async Event: word0:x%x, word1:x%x, "
11864 "word2:x%x, word3:x%x\n", mcqe->word0,
11865 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11866
11867 /* Allocate a new internal CQ_EVENT entry */
11868 cq_event = lpfc_sli4_cq_event_alloc(phba);
11869 if (!cq_event) {
11870 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11871 "0394 Failed to allocate CQ_EVENT entry\n");
11872 return false;
11873 }
11874
11875 /* Move the CQE into an asynchronous event entry */
11876 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11877 spin_lock_irqsave(&phba->hbalock, iflags);
11878 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11879 /* Set the async event flag */
11880 phba->hba_flag |= ASYNC_EVENT;
11881 spin_unlock_irqrestore(&phba->hbalock, iflags);
11882
11883 return true;
11884}
11885
11886/**
11887 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11888 * @phba: Pointer to HBA context object.
11889 * @cqe: Pointer to mailbox completion queue entry.
11890 *
11891 * This routine process a mailbox completion queue entry with mailbox
11892 * completion event.
11893 *
11894 * Return: true if work posted to worker thread, otherwise false.
11895 **/
11896static bool
11897lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11898{
11899 uint32_t mcqe_status;
11900 MAILBOX_t *mbox, *pmbox;
11901 struct lpfc_mqe *mqe;
11902 struct lpfc_vport *vport;
11903 struct lpfc_nodelist *ndlp;
11904 struct lpfc_dmabuf *mp;
11905 unsigned long iflags;
11906 LPFC_MBOXQ_t *pmb;
11907 bool workposted = false;
11908 int rc;
11909
11910 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11911 if (!bf_get(lpfc_trailer_completed, mcqe))
11912 goto out_no_mqe_complete;
11913
11914 /* Get the reference to the active mbox command */
11915 spin_lock_irqsave(&phba->hbalock, iflags);
11916 pmb = phba->sli.mbox_active;
11917 if (unlikely(!pmb)) {
11918 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11919 "1832 No pending MBOX command to handle\n");
11920 spin_unlock_irqrestore(&phba->hbalock, iflags);
11921 goto out_no_mqe_complete;
11922 }
11923 spin_unlock_irqrestore(&phba->hbalock, iflags);
11924 mqe = &pmb->u.mqe;
11925 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11926 mbox = phba->mbox;
11927 vport = pmb->vport;
11928
11929 /* Reset heartbeat timer */
11930 phba->last_completion_time = jiffies;
11931 del_timer(&phba->sli.mbox_tmo);
11932
11933 /* Move mbox data to caller's mailbox region, do endian swapping */
11934 if (pmb->mbox_cmpl && mbox)
11935 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011936
James Smart73d91e52011-10-10 21:32:10 -040011937 /*
11938 * For mcqe errors, conditionally move a modified error code to
11939 * the mbox so that the error will not be missed.
11940 */
11941 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11942 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11943 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11944 bf_set(lpfc_mqe_status, mqe,
11945 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11946 }
James Smart04c68492009-05-22 14:52:52 -040011947 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11948 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11949 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11950 "MBOX dflt rpi: status:x%x rpi:x%x",
11951 mcqe_status,
11952 pmbox->un.varWords[0], 0);
11953 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11954 mp = (struct lpfc_dmabuf *)(pmb->context1);
11955 ndlp = (struct lpfc_nodelist *)pmb->context2;
11956 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11957 * RID of the PPI using the same mbox buffer.
11958 */
11959 lpfc_unreg_login(phba, vport->vpi,
11960 pmbox->un.varWords[0], pmb);
11961 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11962 pmb->context1 = mp;
11963 pmb->context2 = ndlp;
11964 pmb->vport = vport;
11965 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11966 if (rc != MBX_BUSY)
11967 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11968 LOG_SLI, "0385 rc should "
11969 "have been MBX_BUSY\n");
11970 if (rc != MBX_NOT_FINISHED)
11971 goto send_current_mbox;
11972 }
11973 }
11974 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11975 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11976 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11977
11978 /* There is mailbox completion work to do */
11979 spin_lock_irqsave(&phba->hbalock, iflags);
11980 __lpfc_mbox_cmpl_put(phba, pmb);
11981 phba->work_ha |= HA_MBATT;
11982 spin_unlock_irqrestore(&phba->hbalock, iflags);
11983 workposted = true;
11984
11985send_current_mbox:
11986 spin_lock_irqsave(&phba->hbalock, iflags);
11987 /* Release the mailbox command posting token */
11988 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11989 /* Setting active mailbox pointer need to be in sync to flag clear */
11990 phba->sli.mbox_active = NULL;
11991 spin_unlock_irqrestore(&phba->hbalock, iflags);
11992 /* Wake up worker thread to post the next pending mailbox command */
11993 lpfc_worker_wake_up(phba);
11994out_no_mqe_complete:
11995 if (bf_get(lpfc_trailer_consumed, mcqe))
11996 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11997 return workposted;
11998}
11999
12000/**
12001 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
12002 * @phba: Pointer to HBA context object.
12003 * @cqe: Pointer to mailbox completion queue entry.
12004 *
12005 * This routine process a mailbox completion queue entry, it invokes the
12006 * proper mailbox complete handling or asynchrous event handling routine
12007 * according to the MCQE's async bit.
12008 *
12009 * Return: true if work posted to worker thread, otherwise false.
12010 **/
12011static bool
12012lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
12013{
12014 struct lpfc_mcqe mcqe;
12015 bool workposted;
12016
12017 /* Copy the mailbox MCQE and convert endian order as needed */
12018 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12019
12020 /* Invoke the proper event handling routine */
12021 if (!bf_get(lpfc_trailer_async, &mcqe))
12022 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12023 else
12024 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12025 return workposted;
12026}
12027
12028/**
James Smart4f774512009-05-22 14:52:35 -040012029 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12030 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012031 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012032 * @wcqe: Pointer to work-queue completion queue entry.
12033 *
12034 * This routine handles an ELS work-queue completion event.
12035 *
12036 * Return: true if work posted to worker thread, otherwise false.
12037 **/
12038static bool
James Smart2a76a282012-08-03 12:35:54 -040012039lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012040 struct lpfc_wcqe_complete *wcqe)
12041{
James Smart4f774512009-05-22 14:52:35 -040012042 struct lpfc_iocbq *irspiocbq;
12043 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012044 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012045 int txq_cnt = 0;
12046 int txcmplq_cnt = 0;
12047 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012048
James Smart45ed1192009-10-02 15:17:02 -040012049 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012050 irspiocbq = lpfc_sli_get_iocbq(phba);
12051 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012052 if (!list_empty(&pring->txq))
12053 txq_cnt++;
12054 if (!list_empty(&pring->txcmplq))
12055 txcmplq_cnt++;
12056 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
12057 fcp_txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012058 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012059 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12060 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012061 txq_cnt, phba->iocb_cnt,
12062 fcp_txcmplq_cnt,
12063 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012064 return false;
James Smart4f774512009-05-22 14:52:35 -040012065 }
James Smart4f774512009-05-22 14:52:35 -040012066
James Smart45ed1192009-10-02 15:17:02 -040012067 /* Save off the slow-path queue event for work thread to process */
12068 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012069 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012070 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012071 &phba->sli4_hba.sp_queue_event);
12072 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012073 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012074
James Smart45ed1192009-10-02 15:17:02 -040012075 return true;
James Smart4f774512009-05-22 14:52:35 -040012076}
12077
12078/**
12079 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12080 * @phba: Pointer to HBA context object.
12081 * @wcqe: Pointer to work-queue completion queue entry.
12082 *
12083 * This routine handles slow-path WQ entry comsumed event by invoking the
12084 * proper WQ release routine to the slow-path WQ.
12085 **/
12086static void
12087lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12088 struct lpfc_wcqe_release *wcqe)
12089{
James Smart2e90f4b2011-12-13 13:22:37 -050012090 /* sanity check on queue memory */
12091 if (unlikely(!phba->sli4_hba.els_wq))
12092 return;
James Smart4f774512009-05-22 14:52:35 -040012093 /* Check for the slow-path ELS work queue */
12094 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12095 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12096 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12097 else
12098 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12099 "2579 Slow-path wqe consume event carries "
12100 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12101 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12102 phba->sli4_hba.els_wq->queue_id);
12103}
12104
12105/**
12106 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12107 * @phba: Pointer to HBA context object.
12108 * @cq: Pointer to a WQ completion queue.
12109 * @wcqe: Pointer to work-queue completion queue entry.
12110 *
12111 * This routine handles an XRI abort event.
12112 *
12113 * Return: true if work posted to worker thread, otherwise false.
12114 **/
12115static bool
12116lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12117 struct lpfc_queue *cq,
12118 struct sli4_wcqe_xri_aborted *wcqe)
12119{
12120 bool workposted = false;
12121 struct lpfc_cq_event *cq_event;
12122 unsigned long iflags;
12123
12124 /* Allocate a new internal CQ_EVENT entry */
12125 cq_event = lpfc_sli4_cq_event_alloc(phba);
12126 if (!cq_event) {
12127 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12128 "0602 Failed to allocate CQ_EVENT entry\n");
12129 return false;
12130 }
12131
12132 /* Move the CQE into the proper xri abort event list */
12133 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12134 switch (cq->subtype) {
12135 case LPFC_FCP:
12136 spin_lock_irqsave(&phba->hbalock, iflags);
12137 list_add_tail(&cq_event->list,
12138 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12139 /* Set the fcp xri abort event flag */
12140 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12141 spin_unlock_irqrestore(&phba->hbalock, iflags);
12142 workposted = true;
12143 break;
12144 case LPFC_ELS:
12145 spin_lock_irqsave(&phba->hbalock, iflags);
12146 list_add_tail(&cq_event->list,
12147 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12148 /* Set the els xri abort event flag */
12149 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12150 spin_unlock_irqrestore(&phba->hbalock, iflags);
12151 workposted = true;
12152 break;
12153 default:
12154 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12155 "0603 Invalid work queue CQE subtype (x%x)\n",
12156 cq->subtype);
12157 workposted = false;
12158 break;
12159 }
12160 return workposted;
12161}
12162
12163/**
James Smart4d9ab992009-10-02 15:16:39 -040012164 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012165 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012166 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012167 *
James Smart4d9ab992009-10-02 15:16:39 -040012168 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012169 *
12170 * Return: true if work posted to worker thread, otherwise false.
12171 **/
12172static bool
James Smart4d9ab992009-10-02 15:16:39 -040012173lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12174{
12175 bool workposted = false;
12176 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12177 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12178 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012179 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012180 unsigned long iflags;
12181
James Smart2e90f4b2011-12-13 13:22:37 -050012182 /* sanity check on queue memory */
12183 if (unlikely(!hrq) || unlikely(!drq))
12184 return workposted;
12185
James Smart7851fe22011-07-22 18:36:52 -040012186 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12187 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12188 else
12189 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12190 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012191 goto out;
12192
12193 status = bf_get(lpfc_rcqe_status, rcqe);
12194 switch (status) {
12195 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12196 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12197 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040012198 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040012199 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012200 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012201 spin_lock_irqsave(&phba->hbalock, iflags);
12202 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12203 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012204 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012205 spin_unlock_irqrestore(&phba->hbalock, iflags);
12206 goto out;
12207 }
James Smartb84daac2012-08-03 12:35:13 -040012208 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012209 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
12210 /* save off the frame for the word thread to process */
12211 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012212 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012213 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012214 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012215 spin_unlock_irqrestore(&phba->hbalock, iflags);
12216 workposted = true;
12217 break;
12218 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12219 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040012220 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012221 /* Post more buffers if possible */
12222 spin_lock_irqsave(&phba->hbalock, iflags);
12223 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12224 spin_unlock_irqrestore(&phba->hbalock, iflags);
12225 workposted = true;
12226 break;
12227 }
12228out:
12229 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012230}
12231
12232/**
12233 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12234 * @phba: Pointer to HBA context object.
12235 * @cq: Pointer to the completion queue.
12236 * @wcqe: Pointer to a completion queue entry.
12237 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012238 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012239 * entry.
12240 *
12241 * Return: true if work posted to worker thread, otherwise false.
12242 **/
12243static bool
12244lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012245 struct lpfc_cqe *cqe)
12246{
James Smart45ed1192009-10-02 15:17:02 -040012247 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012248 bool workposted = false;
12249
12250 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012251 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012252
12253 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012254 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012255 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012256 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012257 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012258 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012259 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012260 break;
12261 case CQE_CODE_RELEASE_WQE:
12262 /* Process the WQ release event */
12263 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012264 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012265 break;
12266 case CQE_CODE_XRI_ABORTED:
12267 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012268 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012269 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012270 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012271 break;
James Smart4d9ab992009-10-02 15:16:39 -040012272 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012273 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012274 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012275 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012276 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012277 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012278 break;
James Smart4f774512009-05-22 14:52:35 -040012279 default:
12280 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12281 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012282 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012283 break;
12284 }
12285 return workposted;
12286}
12287
12288/**
James Smart4f774512009-05-22 14:52:35 -040012289 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12290 * @phba: Pointer to HBA context object.
12291 * @eqe: Pointer to fast-path event queue entry.
12292 *
12293 * This routine process a event queue entry from the slow-path event queue.
12294 * It will check the MajorCode and MinorCode to determine this is for a
12295 * completion event on a completion queue, if not, an error shall be logged
12296 * and just return. Otherwise, it will get to the corresponding completion
12297 * queue and process all the entries on that completion queue, rearm the
12298 * completion queue, and then return.
12299 *
12300 **/
12301static void
James Smart67d12732012-08-03 12:36:13 -040012302lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12303 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040012304{
James Smart67d12732012-08-03 12:36:13 -040012305 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040012306 struct lpfc_cqe *cqe;
12307 bool workposted = false;
12308 int ecount = 0;
12309 uint16_t cqid;
12310
James Smart4f774512009-05-22 14:52:35 -040012311 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040012312 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040012313
James Smart4f774512009-05-22 14:52:35 -040012314 list_for_each_entry(childq, &speq->child_list, list) {
12315 if (childq->queue_id == cqid) {
12316 cq = childq;
12317 break;
12318 }
12319 }
12320 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012321 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12322 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12323 "0365 Slow-path CQ identifier "
12324 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040012325 return;
12326 }
12327
12328 /* Process all the entries to the CQ */
12329 switch (cq->type) {
12330 case LPFC_MCQ:
12331 while ((cqe = lpfc_sli4_cq_get(cq))) {
12332 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012333 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012334 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012335 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040012336 }
12337 break;
12338 case LPFC_WCQ:
12339 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040012340 if (cq->subtype == LPFC_FCP)
12341 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
12342 cqe);
12343 else
12344 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12345 cqe);
James Smart73d91e52011-10-10 21:32:10 -040012346 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012347 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12348 }
James Smartb84daac2012-08-03 12:35:13 -040012349
12350 /* Track the max number of CQEs processed in 1 EQ */
12351 if (ecount > cq->CQ_max_cqe)
12352 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040012353 break;
12354 default:
12355 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12356 "0370 Invalid completion queue type (%d)\n",
12357 cq->type);
12358 return;
12359 }
12360
12361 /* Catch the no cq entry condition, log an error */
12362 if (unlikely(ecount == 0))
12363 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12364 "0371 No entry from the CQ: identifier "
12365 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12366
12367 /* In any case, flash and re-arm the RCQ */
12368 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12369
12370 /* wake up worker thread if there are works to be done */
12371 if (workposted)
12372 lpfc_worker_wake_up(phba);
12373}
12374
12375/**
12376 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040012377 * @phba: Pointer to HBA context object.
12378 * @cq: Pointer to associated CQ
12379 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012380 *
12381 * This routine process a fast-path work queue completion entry from fast-path
12382 * event queue for FCP command response completion.
12383 **/
12384static void
James Smart2a76a282012-08-03 12:35:54 -040012385lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012386 struct lpfc_wcqe_complete *wcqe)
12387{
James Smart2a76a282012-08-03 12:35:54 -040012388 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040012389 struct lpfc_iocbq *cmdiocbq;
12390 struct lpfc_iocbq irspiocbq;
12391 unsigned long iflags;
12392
James Smart4f774512009-05-22 14:52:35 -040012393 /* Check for response status */
12394 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12395 /* If resource errors reported from HBA, reduce queue
12396 * depth of the SCSI device.
12397 */
James Smarte3d2b802012-08-14 14:25:43 -040012398 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12399 IOSTAT_LOCAL_REJECT)) &&
12400 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12401 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040012402 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040012403
James Smart4f774512009-05-22 14:52:35 -040012404 /* Log the error status */
12405 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12406 "0373 FCP complete error: status=x%x, "
12407 "hw_status=x%x, total_data_specified=%d, "
12408 "parameter=x%x, word3=x%x\n",
12409 bf_get(lpfc_wcqe_c_status, wcqe),
12410 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12411 wcqe->total_data_placed, wcqe->parameter,
12412 wcqe->word3);
12413 }
12414
12415 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040012416 spin_lock_irqsave(&pring->ring_lock, iflags);
12417 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040012418 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12419 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040012420 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012421 if (unlikely(!cmdiocbq)) {
12422 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12423 "0374 FCP complete with no corresponding "
12424 "cmdiocb: iotag (%d)\n",
12425 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12426 return;
12427 }
12428 if (unlikely(!cmdiocbq->iocb_cmpl)) {
12429 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12430 "0375 FCP cmdiocb not callback function "
12431 "iotag: (%d)\n",
12432 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12433 return;
12434 }
12435
12436 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012437 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040012438
James Smart0f65ff62010-02-26 14:14:23 -050012439 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12440 spin_lock_irqsave(&phba->hbalock, iflags);
12441 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12442 spin_unlock_irqrestore(&phba->hbalock, iflags);
12443 }
12444
James Smart4f774512009-05-22 14:52:35 -040012445 /* Pass the cmd_iocb and the rsp state to the upper layer */
12446 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12447}
12448
12449/**
12450 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12451 * @phba: Pointer to HBA context object.
12452 * @cq: Pointer to completion queue.
12453 * @wcqe: Pointer to work-queue completion queue entry.
12454 *
12455 * This routine handles an fast-path WQ entry comsumed event by invoking the
12456 * proper WQ release routine to the slow-path WQ.
12457 **/
12458static void
12459lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12460 struct lpfc_wcqe_release *wcqe)
12461{
12462 struct lpfc_queue *childwq;
12463 bool wqid_matched = false;
12464 uint16_t fcp_wqid;
12465
12466 /* Check for fast-path FCP work queue release */
12467 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
12468 list_for_each_entry(childwq, &cq->child_list, list) {
12469 if (childwq->queue_id == fcp_wqid) {
12470 lpfc_sli4_wq_release(childwq,
12471 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12472 wqid_matched = true;
12473 break;
12474 }
12475 }
12476 /* Report warning log message if no match found */
12477 if (wqid_matched != true)
12478 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12479 "2580 Fast-path wqe consume event carries "
12480 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
12481}
12482
12483/**
12484 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
12485 * @cq: Pointer to the completion queue.
12486 * @eqe: Pointer to fast-path completion queue entry.
12487 *
12488 * This routine process a fast-path work queue completion entry from fast-path
12489 * event queue for FCP command response completion.
12490 **/
12491static int
12492lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12493 struct lpfc_cqe *cqe)
12494{
12495 struct lpfc_wcqe_release wcqe;
12496 bool workposted = false;
12497
12498 /* Copy the work queue CQE and convert endian order if needed */
12499 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12500
12501 /* Check and process for different type of WCQE and dispatch */
12502 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12503 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012504 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040012505 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040012506 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012507 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040012508 (struct lpfc_wcqe_complete *)&wcqe);
12509 break;
12510 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012511 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040012512 /* Process the WQ release event */
12513 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12514 (struct lpfc_wcqe_release *)&wcqe);
12515 break;
12516 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040012517 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040012518 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012519 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012520 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12521 (struct sli4_wcqe_xri_aborted *)&wcqe);
12522 break;
12523 default:
12524 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12525 "0144 Not a valid WCQE code: x%x\n",
12526 bf_get(lpfc_wcqe_c_code, &wcqe));
12527 break;
12528 }
12529 return workposted;
12530}
12531
12532/**
James Smart67d12732012-08-03 12:36:13 -040012533 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040012534 * @phba: Pointer to HBA context object.
12535 * @eqe: Pointer to fast-path event queue entry.
12536 *
12537 * This routine process a event queue entry from the fast-path event queue.
12538 * It will check the MajorCode and MinorCode to determine this is for a
12539 * completion event on a completion queue, if not, an error shall be logged
12540 * and just return. Otherwise, it will get to the corresponding completion
12541 * queue and process all the entries on the completion queue, rearm the
12542 * completion queue, and then return.
12543 **/
12544static void
James Smart67d12732012-08-03 12:36:13 -040012545lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12546 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040012547{
12548 struct lpfc_queue *cq;
12549 struct lpfc_cqe *cqe;
12550 bool workposted = false;
12551 uint16_t cqid;
12552 int ecount = 0;
12553
James Smartcb5172e2010-03-15 11:25:07 -040012554 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040012555 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040012556 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040012557 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040012558 bf_get_le32(lpfc_eqe_major_code, eqe),
12559 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040012560 return;
12561 }
12562
James Smart67d12732012-08-03 12:36:13 -040012563 /* Get the reference to the corresponding CQ */
12564 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12565
12566 /* Check if this is a Slow path event */
12567 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12568 lpfc_sli4_sp_handle_eqe(phba, eqe,
12569 phba->sli4_hba.hba_eq[qidx]);
12570 return;
12571 }
12572
James Smart2e90f4b2011-12-13 13:22:37 -050012573 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12574 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12575 "3146 Fast-path completion queues "
12576 "does not exist\n");
12577 return;
12578 }
James Smart67d12732012-08-03 12:36:13 -040012579 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040012580 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012581 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12582 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12583 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040012584 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040012585 return;
12586 }
12587
James Smart4f774512009-05-22 14:52:35 -040012588 if (unlikely(cqid != cq->queue_id)) {
12589 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12590 "0368 Miss-matched fast-path completion "
12591 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12592 cqid, cq->queue_id);
12593 return;
12594 }
12595
12596 /* Process all the entries to the CQ */
12597 while ((cqe = lpfc_sli4_cq_get(cq))) {
12598 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012599 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012600 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12601 }
12602
James Smartb84daac2012-08-03 12:35:13 -040012603 /* Track the max number of CQEs processed in 1 EQ */
12604 if (ecount > cq->CQ_max_cqe)
12605 cq->CQ_max_cqe = ecount;
12606
James Smart4f774512009-05-22 14:52:35 -040012607 /* Catch the no cq entry condition */
12608 if (unlikely(ecount == 0))
12609 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12610 "0369 No entry from fast-path completion "
12611 "queue fcpcqid=%d\n", cq->queue_id);
12612
12613 /* In any case, flash and re-arm the CQ */
12614 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12615
12616 /* wake up worker thread if there are works to be done */
12617 if (workposted)
12618 lpfc_worker_wake_up(phba);
12619}
12620
12621static void
12622lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12623{
12624 struct lpfc_eqe *eqe;
12625
12626 /* walk all the EQ entries and drop on the floor */
12627 while ((eqe = lpfc_sli4_eq_get(eq)))
12628 ;
12629
12630 /* Clear and re-arm the EQ */
12631 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12632}
12633
James Smart1ba981f2014-02-20 09:56:45 -050012634
12635/**
12636 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
12637 * entry
12638 * @phba: Pointer to HBA context object.
12639 * @eqe: Pointer to fast-path event queue entry.
12640 *
12641 * This routine process a event queue entry from the Flash Optimized Fabric
12642 * event queue. It will check the MajorCode and MinorCode to determine this
12643 * is for a completion event on a completion queue, if not, an error shall be
12644 * logged and just return. Otherwise, it will get to the corresponding
12645 * completion queue and process all the entries on the completion queue, rearm
12646 * the completion queue, and then return.
12647 **/
12648static void
12649lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
12650{
12651 struct lpfc_queue *cq;
12652 struct lpfc_cqe *cqe;
12653 bool workposted = false;
12654 uint16_t cqid;
12655 int ecount = 0;
12656
12657 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12658 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12659 "9147 Not a valid completion "
12660 "event: majorcode=x%x, minorcode=x%x\n",
12661 bf_get_le32(lpfc_eqe_major_code, eqe),
12662 bf_get_le32(lpfc_eqe_minor_code, eqe));
12663 return;
12664 }
12665
12666 /* Get the reference to the corresponding CQ */
12667 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12668
12669 /* Next check for OAS */
12670 cq = phba->sli4_hba.oas_cq;
12671 if (unlikely(!cq)) {
12672 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12673 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12674 "9148 OAS completion queue "
12675 "does not exist\n");
12676 return;
12677 }
12678
12679 if (unlikely(cqid != cq->queue_id)) {
12680 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12681 "9149 Miss-matched fast-path compl "
12682 "queue id: eqcqid=%d, fcpcqid=%d\n",
12683 cqid, cq->queue_id);
12684 return;
12685 }
12686
12687 /* Process all the entries to the OAS CQ */
12688 while ((cqe = lpfc_sli4_cq_get(cq))) {
12689 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12690 if (!(++ecount % cq->entry_repost))
12691 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12692 }
12693
12694 /* Track the max number of CQEs processed in 1 EQ */
12695 if (ecount > cq->CQ_max_cqe)
12696 cq->CQ_max_cqe = ecount;
12697
12698 /* Catch the no cq entry condition */
12699 if (unlikely(ecount == 0))
12700 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12701 "9153 No entry from fast-path completion "
12702 "queue fcpcqid=%d\n", cq->queue_id);
12703
12704 /* In any case, flash and re-arm the CQ */
12705 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12706
12707 /* wake up worker thread if there are works to be done */
12708 if (workposted)
12709 lpfc_worker_wake_up(phba);
12710}
12711
12712/**
12713 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
12714 * @irq: Interrupt number.
12715 * @dev_id: The device context pointer.
12716 *
12717 * This function is directly called from the PCI layer as an interrupt
12718 * service routine when device with SLI-4 interface spec is enabled with
12719 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
12720 * IOCB ring event in the HBA. However, when the device is enabled with either
12721 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12722 * device-level interrupt handler. When the PCI slot is in error recovery
12723 * or the HBA is undergoing initialization, the interrupt handler will not
12724 * process the interrupt. The Flash Optimized Fabric ring event are handled in
12725 * the intrrupt context. This function is called without any lock held.
12726 * It gets the hbalock to access and update SLI data structures. Note that,
12727 * the EQ to CQ are one-to-one map such that the EQ index is
12728 * equal to that of CQ index.
12729 *
12730 * This function returns IRQ_HANDLED when interrupt is handled else it
12731 * returns IRQ_NONE.
12732 **/
12733irqreturn_t
12734lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
12735{
12736 struct lpfc_hba *phba;
12737 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12738 struct lpfc_queue *eq;
12739 struct lpfc_eqe *eqe;
12740 unsigned long iflag;
12741 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050012742
12743 /* Get the driver's phba structure from the dev_id */
12744 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12745 phba = fcp_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050012746
12747 if (unlikely(!phba))
12748 return IRQ_NONE;
12749
12750 /* Get to the EQ struct associated with this vector */
12751 eq = phba->sli4_hba.fof_eq;
12752 if (unlikely(!eq))
12753 return IRQ_NONE;
12754
12755 /* Check device state for handling interrupt */
12756 if (unlikely(lpfc_intr_state_check(phba))) {
12757 eq->EQ_badstate++;
12758 /* Check again for link_state with lock held */
12759 spin_lock_irqsave(&phba->hbalock, iflag);
12760 if (phba->link_state < LPFC_LINK_DOWN)
12761 /* Flush, clear interrupt, and rearm the EQ */
12762 lpfc_sli4_eq_flush(phba, eq);
12763 spin_unlock_irqrestore(&phba->hbalock, iflag);
12764 return IRQ_NONE;
12765 }
12766
12767 /*
12768 * Process all the event on FCP fast-path EQ
12769 */
12770 while ((eqe = lpfc_sli4_eq_get(eq))) {
12771 lpfc_sli4_fof_handle_eqe(phba, eqe);
12772 if (!(++ecount % eq->entry_repost))
12773 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
12774 eq->EQ_processed++;
12775 }
12776
12777 /* Track the max number of EQEs processed in 1 intr */
12778 if (ecount > eq->EQ_max_eqe)
12779 eq->EQ_max_eqe = ecount;
12780
12781
12782 if (unlikely(ecount == 0)) {
12783 eq->EQ_no_entry++;
12784
12785 if (phba->intr_type == MSIX)
12786 /* MSI-X treated interrupt served as no EQ share INT */
12787 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12788 "9145 MSI-X interrupt with no EQE\n");
12789 else {
12790 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12791 "9146 ISR interrupt with no EQE\n");
12792 /* Non MSI-X treated on interrupt as EQ share INT */
12793 return IRQ_NONE;
12794 }
12795 }
12796 /* Always clear and re-arm the fast-path EQ */
12797 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12798 return IRQ_HANDLED;
12799}
12800
James Smart4f774512009-05-22 14:52:35 -040012801/**
James Smart67d12732012-08-03 12:36:13 -040012802 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040012803 * @irq: Interrupt number.
12804 * @dev_id: The device context pointer.
12805 *
12806 * This function is directly called from the PCI layer as an interrupt
12807 * service routine when device with SLI-4 interface spec is enabled with
12808 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12809 * ring event in the HBA. However, when the device is enabled with either
12810 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12811 * device-level interrupt handler. When the PCI slot is in error recovery
12812 * or the HBA is undergoing initialization, the interrupt handler will not
12813 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12814 * the intrrupt context. This function is called without any lock held.
12815 * It gets the hbalock to access and update SLI data structures. Note that,
12816 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12817 * equal to that of FCP CQ index.
12818 *
James Smart67d12732012-08-03 12:36:13 -040012819 * The link attention and ELS ring attention events are handled
12820 * by the worker thread. The interrupt handler signals the worker thread
12821 * and returns for these events. This function is called without any lock
12822 * held. It gets the hbalock to access and update SLI data structures.
12823 *
James Smart4f774512009-05-22 14:52:35 -040012824 * This function returns IRQ_HANDLED when interrupt is handled else it
12825 * returns IRQ_NONE.
12826 **/
12827irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040012828lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040012829{
12830 struct lpfc_hba *phba;
12831 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12832 struct lpfc_queue *fpeq;
12833 struct lpfc_eqe *eqe;
12834 unsigned long iflag;
12835 int ecount = 0;
James Smart962bc512013-01-03 15:44:00 -050012836 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012837
12838 /* Get the driver's phba structure from the dev_id */
12839 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12840 phba = fcp_eq_hdl->phba;
12841 fcp_eqidx = fcp_eq_hdl->idx;
12842
12843 if (unlikely(!phba))
12844 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040012845 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040012846 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012847
12848 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040012849 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050012850 if (unlikely(!fpeq))
12851 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012852
James Smartba20c852012-08-03 12:36:52 -040012853 if (lpfc_fcp_look_ahead) {
12854 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12855 lpfc_sli4_eq_clr_intr(fpeq);
12856 else {
12857 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12858 return IRQ_NONE;
12859 }
12860 }
12861
James Smart4f774512009-05-22 14:52:35 -040012862 /* Check device state for handling interrupt */
12863 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040012864 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040012865 /* Check again for link_state with lock held */
12866 spin_lock_irqsave(&phba->hbalock, iflag);
12867 if (phba->link_state < LPFC_LINK_DOWN)
12868 /* Flush, clear interrupt, and rearm the EQ */
12869 lpfc_sli4_eq_flush(phba, fpeq);
12870 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040012871 if (lpfc_fcp_look_ahead)
12872 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012873 return IRQ_NONE;
12874 }
12875
12876 /*
12877 * Process all the event on FCP fast-path EQ
12878 */
12879 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040012880 if (eqe == NULL)
12881 break;
12882
James Smart67d12732012-08-03 12:36:13 -040012883 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040012884 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012885 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012886 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040012887 }
12888
James Smartb84daac2012-08-03 12:35:13 -040012889 /* Track the max number of EQEs processed in 1 intr */
12890 if (ecount > fpeq->EQ_max_eqe)
12891 fpeq->EQ_max_eqe = ecount;
12892
James Smart4f774512009-05-22 14:52:35 -040012893 /* Always clear and re-arm the fast-path EQ */
12894 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12895
12896 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040012897 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040012898
12899 if (lpfc_fcp_look_ahead) {
12900 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12901 return IRQ_NONE;
12902 }
12903
James Smart4f774512009-05-22 14:52:35 -040012904 if (phba->intr_type == MSIX)
12905 /* MSI-X treated interrupt served as no EQ share INT */
12906 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12907 "0358 MSI-X interrupt with no EQE\n");
12908 else
12909 /* Non MSI-X treated on interrupt as EQ share INT */
12910 return IRQ_NONE;
12911 }
12912
James Smartba20c852012-08-03 12:36:52 -040012913 if (lpfc_fcp_look_ahead)
12914 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012915 return IRQ_HANDLED;
12916} /* lpfc_sli4_fp_intr_handler */
12917
12918/**
12919 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12920 * @irq: Interrupt number.
12921 * @dev_id: The device context pointer.
12922 *
12923 * This function is the device-level interrupt handler to device with SLI-4
12924 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12925 * interrupt mode is enabled and there is an event in the HBA which requires
12926 * driver attention. This function invokes the slow-path interrupt attention
12927 * handling function and fast-path interrupt attention handling function in
12928 * turn to process the relevant HBA attention events. This function is called
12929 * without any lock held. It gets the hbalock to access and update SLI data
12930 * structures.
12931 *
12932 * This function returns IRQ_HANDLED when interrupt is handled, else it
12933 * returns IRQ_NONE.
12934 **/
12935irqreturn_t
12936lpfc_sli4_intr_handler(int irq, void *dev_id)
12937{
12938 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012939 irqreturn_t hba_irq_rc;
12940 bool hba_handled = false;
James Smart962bc512013-01-03 15:44:00 -050012941 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012942
12943 /* Get the driver's phba structure from the dev_id */
12944 phba = (struct lpfc_hba *)dev_id;
12945
12946 if (unlikely(!phba))
12947 return IRQ_NONE;
12948
12949 /*
James Smart4f774512009-05-22 14:52:35 -040012950 * Invoke fast-path host attention interrupt handling as appropriate.
12951 */
James Smart67d12732012-08-03 12:36:13 -040012952 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12953 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012954 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012955 if (hba_irq_rc == IRQ_HANDLED)
12956 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012957 }
12958
James Smart1ba981f2014-02-20 09:56:45 -050012959 if (phba->cfg_fof) {
12960 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
12961 &phba->sli4_hba.fcp_eq_hdl[0]);
12962 if (hba_irq_rc == IRQ_HANDLED)
12963 hba_handled |= true;
12964 }
12965
James Smart67d12732012-08-03 12:36:13 -040012966 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012967} /* lpfc_sli4_intr_handler */
12968
12969/**
12970 * lpfc_sli4_queue_free - free a queue structure and associated memory
12971 * @queue: The queue structure to free.
12972 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012973 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012974 * the host resident queue. This function must be called after destroying the
12975 * queue on the HBA.
12976 **/
12977void
12978lpfc_sli4_queue_free(struct lpfc_queue *queue)
12979{
12980 struct lpfc_dmabuf *dmabuf;
12981
12982 if (!queue)
12983 return;
12984
12985 while (!list_empty(&queue->page_list)) {
12986 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12987 list);
James Smart49198b32010-04-06 15:04:33 -040012988 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012989 dmabuf->virt, dmabuf->phys);
12990 kfree(dmabuf);
12991 }
12992 kfree(queue);
12993 return;
12994}
12995
12996/**
12997 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12998 * @phba: The HBA that this queue is being created on.
12999 * @entry_size: The size of each queue entry for this queue.
13000 * @entry count: The number of entries that this queue will handle.
13001 *
13002 * This function allocates a queue structure and the DMAable memory used for
13003 * the host resident queue. This function must be called before creating the
13004 * queue on the HBA.
13005 **/
13006struct lpfc_queue *
13007lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
13008 uint32_t entry_count)
13009{
13010 struct lpfc_queue *queue;
13011 struct lpfc_dmabuf *dmabuf;
13012 int x, total_qe_count;
13013 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040013014 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040013015
James Smartcb5172e2010-03-15 11:25:07 -040013016 if (!phba->sli4_hba.pc_sli4_params.supported)
13017 hw_page_size = SLI4_PAGE_SIZE;
13018
James Smart4f774512009-05-22 14:52:35 -040013019 queue = kzalloc(sizeof(struct lpfc_queue) +
13020 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13021 if (!queue)
13022 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040013023 queue->page_count = (ALIGN(entry_size * entry_count,
13024 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040013025 INIT_LIST_HEAD(&queue->list);
13026 INIT_LIST_HEAD(&queue->page_list);
13027 INIT_LIST_HEAD(&queue->child_list);
13028 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13029 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13030 if (!dmabuf)
13031 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040013032 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13033 hw_page_size, &dmabuf->phys,
13034 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013035 if (!dmabuf->virt) {
13036 kfree(dmabuf);
13037 goto out_fail;
13038 }
13039 dmabuf->buffer_tag = x;
13040 list_add_tail(&dmabuf->list, &queue->page_list);
13041 /* initialize queue's entry array */
13042 dma_pointer = dmabuf->virt;
13043 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040013044 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040013045 total_qe_count++, dma_pointer += entry_size) {
13046 queue->qe[total_qe_count].address = dma_pointer;
13047 }
13048 }
13049 queue->entry_size = entry_size;
13050 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040013051
13052 /*
13053 * entry_repost is calculated based on the number of entries in the
13054 * queue. This works out except for RQs. If buffers are NOT initially
13055 * posted for every RQE, entry_repost should be adjusted accordingly.
13056 */
13057 queue->entry_repost = (entry_count >> 3);
13058 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13059 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040013060 queue->phba = phba;
13061
13062 return queue;
13063out_fail:
13064 lpfc_sli4_queue_free(queue);
13065 return NULL;
13066}
13067
13068/**
James Smart962bc512013-01-03 15:44:00 -050013069 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13070 * @phba: HBA structure that indicates port to create a queue on.
13071 * @pci_barset: PCI BAR set flag.
13072 *
13073 * This function shall perform iomap of the specified PCI BAR address to host
13074 * memory address if not already done so and return it. The returned host
13075 * memory address can be NULL.
13076 */
13077static void __iomem *
13078lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13079{
James Smart962bc512013-01-03 15:44:00 -050013080 if (!phba->pcidev)
13081 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013082
13083 switch (pci_barset) {
13084 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013085 return phba->pci_bar0_memmap_p;
13086 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013087 return phba->pci_bar2_memmap_p;
13088 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013089 return phba->pci_bar4_memmap_p;
13090 default:
13091 break;
13092 }
13093 return NULL;
13094}
13095
13096/**
James Smart173edbb2012-06-12 13:54:50 -040013097 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
13098 * @phba: HBA structure that indicates port to create a queue on.
13099 * @startq: The starting FCP EQ to modify
13100 *
13101 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13102 *
13103 * The @phba struct is used to send mailbox command to HBA. The @startq
13104 * is used to get the starting FCP EQ to change.
13105 * This function is asynchronous and will wait for the mailbox
13106 * command to finish before continuing.
13107 *
13108 * On success this function will return a zero. If unable to allocate enough
13109 * memory this function will return -ENOMEM. If the queue create mailbox command
13110 * fails this function will return -ENXIO.
13111 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013112int
James Smart2c9c5a02015-04-07 15:07:15 -040013113lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013114{
13115 struct lpfc_mbx_modify_eq_delay *eq_delay;
13116 LPFC_MBOXQ_t *mbox;
13117 struct lpfc_queue *eq;
13118 int cnt, rc, length, status = 0;
13119 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013120 uint32_t result;
James Smart173edbb2012-06-12 13:54:50 -040013121 int fcp_eqidx;
13122 union lpfc_sli4_cfg_shdr *shdr;
13123 uint16_t dmult;
13124
James Smart67d12732012-08-03 12:36:13 -040013125 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040013126 return 0;
13127
13128 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13129 if (!mbox)
13130 return -ENOMEM;
13131 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13132 sizeof(struct lpfc_sli4_cfg_mhdr));
13133 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13134 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13135 length, LPFC_SLI4_MBX_EMBED);
13136 eq_delay = &mbox->u.mqe.un.eq_delay;
13137
13138 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040013139 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
13140 if (result > LPFC_DMULT_CONST)
13141 dmult = 0;
13142 else
13143 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040013144
13145 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040013146 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040013147 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040013148 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040013149 if (!eq)
13150 continue;
13151 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13152 eq_delay->u.request.eq[cnt].phase = 0;
13153 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13154 cnt++;
13155 if (cnt >= LPFC_MAX_EQ_DELAY)
13156 break;
13157 }
13158 eq_delay->u.request.num_eq = cnt;
13159
13160 mbox->vport = phba->pport;
13161 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13162 mbox->context1 = NULL;
13163 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13164 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13165 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13166 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13167 if (shdr_status || shdr_add_status || rc) {
13168 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13169 "2512 MODIFY_EQ_DELAY mailbox failed with "
13170 "status x%x add_status x%x, mbx status x%x\n",
13171 shdr_status, shdr_add_status, rc);
13172 status = -ENXIO;
13173 }
13174 mempool_free(mbox, phba->mbox_mem_pool);
13175 return status;
13176}
13177
13178/**
James Smart4f774512009-05-22 14:52:35 -040013179 * lpfc_eq_create - Create an Event Queue on the HBA
13180 * @phba: HBA structure that indicates port to create a queue on.
13181 * @eq: The queue structure to use to create the event queue.
13182 * @imax: The maximum interrupt per second limit.
13183 *
13184 * This function creates an event queue, as detailed in @eq, on a port,
13185 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13186 *
13187 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13188 * is used to get the entry count and entry size that are necessary to
13189 * determine the number of pages to allocate and use for this queue. This
13190 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13191 * event queue. This function is asynchronous and will wait for the mailbox
13192 * command to finish before continuing.
13193 *
13194 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013195 * memory this function will return -ENOMEM. If the queue create mailbox command
13196 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013197 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013198int
James Smartee020062012-09-29 11:28:52 -040013199lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040013200{
13201 struct lpfc_mbx_eq_create *eq_create;
13202 LPFC_MBOXQ_t *mbox;
13203 int rc, length, status = 0;
13204 struct lpfc_dmabuf *dmabuf;
13205 uint32_t shdr_status, shdr_add_status;
13206 union lpfc_sli4_cfg_shdr *shdr;
13207 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040013208 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13209
James Smart2e90f4b2011-12-13 13:22:37 -050013210 /* sanity check on queue memory */
13211 if (!eq)
13212 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013213 if (!phba->sli4_hba.pc_sli4_params.supported)
13214 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013215
13216 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13217 if (!mbox)
13218 return -ENOMEM;
13219 length = (sizeof(struct lpfc_mbx_eq_create) -
13220 sizeof(struct lpfc_sli4_cfg_mhdr));
13221 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13222 LPFC_MBOX_OPCODE_EQ_CREATE,
13223 length, LPFC_SLI4_MBX_EMBED);
13224 eq_create = &mbox->u.mqe.un.eq_create;
13225 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13226 eq->page_count);
13227 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13228 LPFC_EQE_SIZE);
13229 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040013230 /* don't setup delay multiplier using EQ_CREATE */
13231 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040013232 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13233 dmult);
13234 switch (eq->entry_count) {
13235 default:
13236 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13237 "0360 Unsupported EQ count. (%d)\n",
13238 eq->entry_count);
13239 if (eq->entry_count < 256)
13240 return -EINVAL;
13241 /* otherwise default to smallest count (drop through) */
13242 case 256:
13243 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13244 LPFC_EQ_CNT_256);
13245 break;
13246 case 512:
13247 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13248 LPFC_EQ_CNT_512);
13249 break;
13250 case 1024:
13251 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13252 LPFC_EQ_CNT_1024);
13253 break;
13254 case 2048:
13255 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13256 LPFC_EQ_CNT_2048);
13257 break;
13258 case 4096:
13259 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13260 LPFC_EQ_CNT_4096);
13261 break;
13262 }
13263 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013264 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013265 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13266 putPaddrLow(dmabuf->phys);
13267 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13268 putPaddrHigh(dmabuf->phys);
13269 }
13270 mbox->vport = phba->pport;
13271 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13272 mbox->context1 = NULL;
13273 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13274 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13275 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13276 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13277 if (shdr_status || shdr_add_status || rc) {
13278 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13279 "2500 EQ_CREATE mailbox failed with "
13280 "status x%x add_status x%x, mbx status x%x\n",
13281 shdr_status, shdr_add_status, rc);
13282 status = -ENXIO;
13283 }
13284 eq->type = LPFC_EQ;
13285 eq->subtype = LPFC_NONE;
13286 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13287 if (eq->queue_id == 0xFFFF)
13288 status = -ENXIO;
13289 eq->host_index = 0;
13290 eq->hba_index = 0;
13291
James Smart8fa38512009-07-19 10:01:03 -040013292 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013293 return status;
13294}
13295
13296/**
13297 * lpfc_cq_create - Create a Completion Queue on the HBA
13298 * @phba: HBA structure that indicates port to create a queue on.
13299 * @cq: The queue structure to use to create the completion queue.
13300 * @eq: The event queue to bind this completion queue to.
13301 *
13302 * This function creates a completion queue, as detailed in @wq, on a port,
13303 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13304 *
13305 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13306 * is used to get the entry count and entry size that are necessary to
13307 * determine the number of pages to allocate and use for this queue. The @eq
13308 * is used to indicate which event queue to bind this completion queue to. This
13309 * function will send the CQ_CREATE mailbox command to the HBA to setup the
13310 * completion queue. This function is asynchronous and will wait for the mailbox
13311 * command to finish before continuing.
13312 *
13313 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013314 * memory this function will return -ENOMEM. If the queue create mailbox command
13315 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013316 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013317int
James Smart4f774512009-05-22 14:52:35 -040013318lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13319 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13320{
13321 struct lpfc_mbx_cq_create *cq_create;
13322 struct lpfc_dmabuf *dmabuf;
13323 LPFC_MBOXQ_t *mbox;
13324 int rc, length, status = 0;
13325 uint32_t shdr_status, shdr_add_status;
13326 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013327 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13328
James Smart2e90f4b2011-12-13 13:22:37 -050013329 /* sanity check on queue memory */
13330 if (!cq || !eq)
13331 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013332 if (!phba->sli4_hba.pc_sli4_params.supported)
13333 hw_page_size = SLI4_PAGE_SIZE;
13334
James Smart4f774512009-05-22 14:52:35 -040013335 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13336 if (!mbox)
13337 return -ENOMEM;
13338 length = (sizeof(struct lpfc_mbx_cq_create) -
13339 sizeof(struct lpfc_sli4_cfg_mhdr));
13340 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13341 LPFC_MBOX_OPCODE_CQ_CREATE,
13342 length, LPFC_SLI4_MBX_EMBED);
13343 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013344 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013345 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13346 cq->page_count);
13347 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13348 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013349 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13350 phba->sli4_hba.pc_sli4_params.cqv);
13351 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040013352 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13353 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013354 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13355 eq->queue_id);
13356 } else {
13357 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13358 eq->queue_id);
13359 }
James Smart4f774512009-05-22 14:52:35 -040013360 switch (cq->entry_count) {
13361 default:
13362 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2ea259e2017-02-12 13:52:27 -080013363 "0361 Unsupported CQ count: "
13364 "entry cnt %d sz %d pg cnt %d repost %d\n",
13365 cq->entry_count, cq->entry_size,
13366 cq->page_count, cq->entry_repost);
James Smart4f4c1862012-06-12 13:54:02 -040013367 if (cq->entry_count < 256) {
13368 status = -EINVAL;
13369 goto out;
13370 }
James Smart4f774512009-05-22 14:52:35 -040013371 /* otherwise default to smallest count (drop through) */
13372 case 256:
13373 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13374 LPFC_CQ_CNT_256);
13375 break;
13376 case 512:
13377 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13378 LPFC_CQ_CNT_512);
13379 break;
13380 case 1024:
13381 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13382 LPFC_CQ_CNT_1024);
13383 break;
13384 }
13385 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013386 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013387 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13388 putPaddrLow(dmabuf->phys);
13389 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13390 putPaddrHigh(dmabuf->phys);
13391 }
13392 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13393
13394 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013395 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13396 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13397 if (shdr_status || shdr_add_status || rc) {
13398 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13399 "2501 CQ_CREATE mailbox failed with "
13400 "status x%x add_status x%x, mbx status x%x\n",
13401 shdr_status, shdr_add_status, rc);
13402 status = -ENXIO;
13403 goto out;
13404 }
13405 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13406 if (cq->queue_id == 0xFFFF) {
13407 status = -ENXIO;
13408 goto out;
13409 }
13410 /* link the cq onto the parent eq child list */
13411 list_add_tail(&cq->list, &eq->child_list);
13412 /* Set up completion queue's type and subtype */
13413 cq->type = type;
13414 cq->subtype = subtype;
13415 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050013416 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013417 cq->host_index = 0;
13418 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040013419
James Smart8fa38512009-07-19 10:01:03 -040013420out:
13421 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013422 return status;
13423}
13424
13425/**
James Smartb19a0612010-04-06 14:48:51 -040013426 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040013427 * @phba: HBA structure that indicates port to create a queue on.
13428 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040013429 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13430 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040013431 *
James Smartb19a0612010-04-06 14:48:51 -040013432 * This function provides failback (fb) functionality when the
13433 * mq_create_ext fails on older FW generations. It's purpose is identical
13434 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040013435 *
James Smartb19a0612010-04-06 14:48:51 -040013436 * This routine cannot fail as all attributes were previously accessed and
13437 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040013438 **/
James Smartb19a0612010-04-06 14:48:51 -040013439static void
13440lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13441 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040013442{
13443 struct lpfc_mbx_mq_create *mq_create;
13444 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040013445 int length;
James Smart04c68492009-05-22 14:52:52 -040013446
James Smart04c68492009-05-22 14:52:52 -040013447 length = (sizeof(struct lpfc_mbx_mq_create) -
13448 sizeof(struct lpfc_sli4_cfg_mhdr));
13449 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13450 LPFC_MBOX_OPCODE_MQ_CREATE,
13451 length, LPFC_SLI4_MBX_EMBED);
13452 mq_create = &mbox->u.mqe.un.mq_create;
13453 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040013454 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040013455 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040013456 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040013457 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13458 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040013459 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013460 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13461 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040013462 break;
13463 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013464 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13465 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040013466 break;
13467 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013468 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13469 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040013470 break;
13471 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013472 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13473 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040013474 break;
13475 }
13476 list_for_each_entry(dmabuf, &mq->page_list, list) {
13477 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040013478 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040013479 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040013480 putPaddrHigh(dmabuf->phys);
13481 }
13482}
13483
13484/**
13485 * lpfc_mq_create - Create a mailbox Queue on the HBA
13486 * @phba: HBA structure that indicates port to create a queue on.
13487 * @mq: The queue structure to use to create the mailbox queue.
13488 * @cq: The completion queue to associate with this cq.
13489 * @subtype: The queue's subtype.
13490 *
13491 * This function creates a mailbox queue, as detailed in @mq, on a port,
13492 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13493 *
13494 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13495 * is used to get the entry count and entry size that are necessary to
13496 * determine the number of pages to allocate and use for this queue. This
13497 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13498 * mailbox queue. This function is asynchronous and will wait for the mailbox
13499 * command to finish before continuing.
13500 *
13501 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013502 * memory this function will return -ENOMEM. If the queue create mailbox command
13503 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040013504 **/
13505int32_t
13506lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13507 struct lpfc_queue *cq, uint32_t subtype)
13508{
13509 struct lpfc_mbx_mq_create *mq_create;
13510 struct lpfc_mbx_mq_create_ext *mq_create_ext;
13511 struct lpfc_dmabuf *dmabuf;
13512 LPFC_MBOXQ_t *mbox;
13513 int rc, length, status = 0;
13514 uint32_t shdr_status, shdr_add_status;
13515 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013516 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040013517
James Smart2e90f4b2011-12-13 13:22:37 -050013518 /* sanity check on queue memory */
13519 if (!mq || !cq)
13520 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013521 if (!phba->sli4_hba.pc_sli4_params.supported)
13522 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040013523
13524 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13525 if (!mbox)
13526 return -ENOMEM;
13527 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13528 sizeof(struct lpfc_sli4_cfg_mhdr));
13529 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13530 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13531 length, LPFC_SLI4_MBX_EMBED);
13532
13533 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050013534 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050013535 bf_set(lpfc_mbx_mq_create_ext_num_pages,
13536 &mq_create_ext->u.request, mq->page_count);
13537 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13538 &mq_create_ext->u.request, 1);
13539 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040013540 &mq_create_ext->u.request, 1);
13541 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13542 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050013543 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13544 &mq_create_ext->u.request, 1);
13545 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13546 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040013547 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013548 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13549 phba->sli4_hba.pc_sli4_params.mqv);
13550 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13551 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13552 cq->queue_id);
13553 else
13554 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
13555 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040013556 switch (mq->entry_count) {
13557 default:
13558 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13559 "0362 Unsupported MQ count. (%d)\n",
13560 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013561 if (mq->entry_count < 16) {
13562 status = -EINVAL;
13563 goto out;
13564 }
James Smartb19a0612010-04-06 14:48:51 -040013565 /* otherwise default to smallest count (drop through) */
13566 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013567 bf_set(lpfc_mq_context_ring_size,
13568 &mq_create_ext->u.request.context,
13569 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040013570 break;
13571 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013572 bf_set(lpfc_mq_context_ring_size,
13573 &mq_create_ext->u.request.context,
13574 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040013575 break;
13576 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013577 bf_set(lpfc_mq_context_ring_size,
13578 &mq_create_ext->u.request.context,
13579 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040013580 break;
13581 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013582 bf_set(lpfc_mq_context_ring_size,
13583 &mq_create_ext->u.request.context,
13584 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040013585 break;
13586 }
13587 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013588 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040013589 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
13590 putPaddrLow(dmabuf->phys);
13591 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040013592 putPaddrHigh(dmabuf->phys);
13593 }
13594 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040013595 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13596 &mq_create_ext->u.response);
13597 if (rc != MBX_SUCCESS) {
13598 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13599 "2795 MQ_CREATE_EXT failed with "
13600 "status x%x. Failback to MQ_CREATE.\n",
13601 rc);
13602 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
13603 mq_create = &mbox->u.mqe.un.mq_create;
13604 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13605 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
13606 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13607 &mq_create->u.response);
13608 }
13609
James Smart04c68492009-05-22 14:52:52 -040013610 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040013611 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13612 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13613 if (shdr_status || shdr_add_status || rc) {
13614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13615 "2502 MQ_CREATE mailbox failed with "
13616 "status x%x add_status x%x, mbx status x%x\n",
13617 shdr_status, shdr_add_status, rc);
13618 status = -ENXIO;
13619 goto out;
13620 }
James Smart04c68492009-05-22 14:52:52 -040013621 if (mq->queue_id == 0xFFFF) {
13622 status = -ENXIO;
13623 goto out;
13624 }
13625 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050013626 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040013627 mq->subtype = subtype;
13628 mq->host_index = 0;
13629 mq->hba_index = 0;
13630
13631 /* link the mq onto the parent cq child list */
13632 list_add_tail(&mq->list, &cq->child_list);
13633out:
James Smart8fa38512009-07-19 10:01:03 -040013634 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013635 return status;
13636}
13637
13638/**
James Smart4f774512009-05-22 14:52:35 -040013639 * lpfc_wq_create - Create a Work Queue on the HBA
13640 * @phba: HBA structure that indicates port to create a queue on.
13641 * @wq: The queue structure to use to create the work queue.
13642 * @cq: The completion queue to bind this work queue to.
13643 * @subtype: The subtype of the work queue indicating its functionality.
13644 *
13645 * This function creates a work queue, as detailed in @wq, on a port, described
13646 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
13647 *
13648 * The @phba struct is used to send mailbox command to HBA. The @wq struct
13649 * is used to get the entry count and entry size that are necessary to
13650 * determine the number of pages to allocate and use for this queue. The @cq
13651 * is used to indicate which completion queue to bind this work queue to. This
13652 * function will send the WQ_CREATE mailbox command to the HBA to setup the
13653 * work queue. This function is asynchronous and will wait for the mailbox
13654 * command to finish before continuing.
13655 *
13656 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013657 * memory this function will return -ENOMEM. If the queue create mailbox command
13658 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013659 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013660int
James Smart4f774512009-05-22 14:52:35 -040013661lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
13662 struct lpfc_queue *cq, uint32_t subtype)
13663{
13664 struct lpfc_mbx_wq_create *wq_create;
13665 struct lpfc_dmabuf *dmabuf;
13666 LPFC_MBOXQ_t *mbox;
13667 int rc, length, status = 0;
13668 uint32_t shdr_status, shdr_add_status;
13669 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013670 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050013671 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050013672 void __iomem *bar_memmap_p;
13673 uint32_t db_offset;
13674 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013675
James Smart2e90f4b2011-12-13 13:22:37 -050013676 /* sanity check on queue memory */
13677 if (!wq || !cq)
13678 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013679 if (!phba->sli4_hba.pc_sli4_params.supported)
13680 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013681
13682 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13683 if (!mbox)
13684 return -ENOMEM;
13685 length = (sizeof(struct lpfc_mbx_wq_create) -
13686 sizeof(struct lpfc_sli4_cfg_mhdr));
13687 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13688 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13689 length, LPFC_SLI4_MBX_EMBED);
13690 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013691 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013692 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13693 wq->page_count);
13694 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13695 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040013696
13697 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050013698 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13699 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050013700
James Smart0c651872013-07-15 18:33:23 -040013701 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13702 case LPFC_Q_CREATE_VERSION_0:
13703 switch (wq->entry_size) {
13704 default:
13705 case 64:
13706 /* Nothing to do, version 0 ONLY supports 64 byte */
13707 page = wq_create->u.request.page;
13708 break;
13709 case 128:
13710 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13711 LPFC_WQ_SZ128_SUPPORT)) {
13712 status = -ERANGE;
13713 goto out;
13714 }
13715 /* If we get here the HBA MUST also support V1 and
13716 * we MUST use it
13717 */
13718 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13719 LPFC_Q_CREATE_VERSION_1);
13720
13721 bf_set(lpfc_mbx_wq_create_wqe_count,
13722 &wq_create->u.request_1, wq->entry_count);
13723 bf_set(lpfc_mbx_wq_create_wqe_size,
13724 &wq_create->u.request_1,
13725 LPFC_WQ_WQE_SIZE_128);
13726 bf_set(lpfc_mbx_wq_create_page_size,
13727 &wq_create->u.request_1,
James Smart8ea73db2017-02-12 13:52:25 -080013728 LPFC_WQ_PAGE_SIZE_4096);
James Smart0c651872013-07-15 18:33:23 -040013729 page = wq_create->u.request_1.page;
13730 break;
13731 }
13732 break;
13733 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050013734 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13735 wq->entry_count);
13736 switch (wq->entry_size) {
13737 default:
13738 case 64:
13739 bf_set(lpfc_mbx_wq_create_wqe_size,
13740 &wq_create->u.request_1,
13741 LPFC_WQ_WQE_SIZE_64);
13742 break;
13743 case 128:
James Smart0c651872013-07-15 18:33:23 -040013744 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13745 LPFC_WQ_SZ128_SUPPORT)) {
13746 status = -ERANGE;
13747 goto out;
13748 }
James Smart5a6f1332011-03-11 16:05:35 -050013749 bf_set(lpfc_mbx_wq_create_wqe_size,
13750 &wq_create->u.request_1,
13751 LPFC_WQ_WQE_SIZE_128);
13752 break;
13753 }
James Smart8ea73db2017-02-12 13:52:25 -080013754 bf_set(lpfc_mbx_wq_create_page_size,
13755 &wq_create->u.request_1,
13756 LPFC_WQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050013757 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040013758 break;
13759 default:
13760 status = -ERANGE;
13761 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050013762 }
James Smart0c651872013-07-15 18:33:23 -040013763
James Smart4f774512009-05-22 14:52:35 -040013764 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013765 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050013766 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13767 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040013768 }
James Smart962bc512013-01-03 15:44:00 -050013769
13770 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13771 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13772
James Smart4f774512009-05-22 14:52:35 -040013773 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13774 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013775 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13776 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13777 if (shdr_status || shdr_add_status || rc) {
13778 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13779 "2503 WQ_CREATE mailbox failed with "
13780 "status x%x add_status x%x, mbx status x%x\n",
13781 shdr_status, shdr_add_status, rc);
13782 status = -ENXIO;
13783 goto out;
13784 }
13785 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13786 if (wq->queue_id == 0xFFFF) {
13787 status = -ENXIO;
13788 goto out;
13789 }
James Smart962bc512013-01-03 15:44:00 -050013790 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13791 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13792 &wq_create->u.response);
13793 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13794 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13795 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13796 "3265 WQ[%d] doorbell format not "
13797 "supported: x%x\n", wq->queue_id,
13798 wq->db_format);
13799 status = -EINVAL;
13800 goto out;
13801 }
13802 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13803 &wq_create->u.response);
13804 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13805 if (!bar_memmap_p) {
13806 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13807 "3263 WQ[%d] failed to memmap pci "
13808 "barset:x%x\n", wq->queue_id,
13809 pci_barset);
13810 status = -ENOMEM;
13811 goto out;
13812 }
13813 db_offset = wq_create->u.response.doorbell_offset;
13814 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13815 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13816 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13817 "3252 WQ[%d] doorbell offset not "
13818 "supported: x%x\n", wq->queue_id,
13819 db_offset);
13820 status = -EINVAL;
13821 goto out;
13822 }
13823 wq->db_regaddr = bar_memmap_p + db_offset;
13824 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013825 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13826 "format:x%x\n", wq->queue_id, pci_barset,
13827 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013828 } else {
13829 wq->db_format = LPFC_DB_LIST_FORMAT;
13830 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13831 }
James Smart4f774512009-05-22 14:52:35 -040013832 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050013833 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013834 wq->subtype = subtype;
13835 wq->host_index = 0;
13836 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050013837 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040013838
13839 /* link the wq onto the parent cq child list */
13840 list_add_tail(&wq->list, &cq->child_list);
13841out:
James Smart8fa38512009-07-19 10:01:03 -040013842 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013843 return status;
13844}
13845
13846/**
James Smart73d91e52011-10-10 21:32:10 -040013847 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13848 * @phba: HBA structure that indicates port to create a queue on.
13849 * @rq: The queue structure to use for the receive queue.
13850 * @qno: The associated HBQ number
13851 *
13852 *
13853 * For SLI4 we need to adjust the RQ repost value based on
13854 * the number of buffers that are initially posted to the RQ.
13855 */
13856void
13857lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13858{
13859 uint32_t cnt;
13860
James Smart2e90f4b2011-12-13 13:22:37 -050013861 /* sanity check on queue memory */
13862 if (!rq)
13863 return;
James Smart73d91e52011-10-10 21:32:10 -040013864 cnt = lpfc_hbq_defs[qno]->entry_count;
13865
13866 /* Recalc repost for RQs based on buffers initially posted */
13867 cnt = (cnt >> 3);
13868 if (cnt < LPFC_QUEUE_MIN_REPOST)
13869 cnt = LPFC_QUEUE_MIN_REPOST;
13870
13871 rq->entry_repost = cnt;
13872}
13873
13874/**
James Smart4f774512009-05-22 14:52:35 -040013875 * lpfc_rq_create - Create a Receive Queue on the HBA
13876 * @phba: HBA structure that indicates port to create a queue on.
13877 * @hrq: The queue structure to use to create the header receive queue.
13878 * @drq: The queue structure to use to create the data receive queue.
13879 * @cq: The completion queue to bind this work queue to.
13880 *
13881 * This function creates a receive buffer queue pair , as detailed in @hrq and
13882 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13883 * to the HBA.
13884 *
13885 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13886 * struct is used to get the entry count that is necessary to determine the
13887 * number of pages to use for this queue. The @cq is used to indicate which
13888 * completion queue to bind received buffers that are posted to these queues to.
13889 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13890 * receive queue pair. This function is asynchronous and will wait for the
13891 * mailbox command to finish before continuing.
13892 *
13893 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013894 * memory this function will return -ENOMEM. If the queue create mailbox command
13895 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013896 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013897int
James Smart4f774512009-05-22 14:52:35 -040013898lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13899 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13900{
13901 struct lpfc_mbx_rq_create *rq_create;
13902 struct lpfc_dmabuf *dmabuf;
13903 LPFC_MBOXQ_t *mbox;
13904 int rc, length, status = 0;
13905 uint32_t shdr_status, shdr_add_status;
13906 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013907 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050013908 void __iomem *bar_memmap_p;
13909 uint32_t db_offset;
13910 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013911
James Smart2e90f4b2011-12-13 13:22:37 -050013912 /* sanity check on queue memory */
13913 if (!hrq || !drq || !cq)
13914 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013915 if (!phba->sli4_hba.pc_sli4_params.supported)
13916 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013917
13918 if (hrq->entry_count != drq->entry_count)
13919 return -EINVAL;
13920 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13921 if (!mbox)
13922 return -ENOMEM;
13923 length = (sizeof(struct lpfc_mbx_rq_create) -
13924 sizeof(struct lpfc_sli4_cfg_mhdr));
13925 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13926 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13927 length, LPFC_SLI4_MBX_EMBED);
13928 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013929 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13930 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13931 phba->sli4_hba.pc_sli4_params.rqv);
13932 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13933 bf_set(lpfc_rq_context_rqe_count_1,
13934 &rq_create->u.request.context,
13935 hrq->entry_count);
13936 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013937 bf_set(lpfc_rq_context_rqe_size,
13938 &rq_create->u.request.context,
13939 LPFC_RQE_SIZE_8);
13940 bf_set(lpfc_rq_context_page_size,
13941 &rq_create->u.request.context,
James Smart8ea73db2017-02-12 13:52:25 -080013942 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050013943 } else {
13944 switch (hrq->entry_count) {
13945 default:
13946 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13947 "2535 Unsupported RQ count. (%d)\n",
13948 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013949 if (hrq->entry_count < 512) {
13950 status = -EINVAL;
13951 goto out;
13952 }
James Smart5a6f1332011-03-11 16:05:35 -050013953 /* otherwise default to smallest count (drop through) */
13954 case 512:
13955 bf_set(lpfc_rq_context_rqe_count,
13956 &rq_create->u.request.context,
13957 LPFC_RQ_RING_SIZE_512);
13958 break;
13959 case 1024:
13960 bf_set(lpfc_rq_context_rqe_count,
13961 &rq_create->u.request.context,
13962 LPFC_RQ_RING_SIZE_1024);
13963 break;
13964 case 2048:
13965 bf_set(lpfc_rq_context_rqe_count,
13966 &rq_create->u.request.context,
13967 LPFC_RQ_RING_SIZE_2048);
13968 break;
13969 case 4096:
13970 bf_set(lpfc_rq_context_rqe_count,
13971 &rq_create->u.request.context,
13972 LPFC_RQ_RING_SIZE_4096);
13973 break;
13974 }
13975 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13976 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013977 }
13978 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13979 cq->queue_id);
13980 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13981 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013982 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013983 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013984 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13985 putPaddrLow(dmabuf->phys);
13986 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13987 putPaddrHigh(dmabuf->phys);
13988 }
James Smart962bc512013-01-03 15:44:00 -050013989 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13990 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13991
James Smart4f774512009-05-22 14:52:35 -040013992 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13993 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013994 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13995 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13996 if (shdr_status || shdr_add_status || rc) {
13997 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13998 "2504 RQ_CREATE mailbox failed with "
13999 "status x%x add_status x%x, mbx status x%x\n",
14000 shdr_status, shdr_add_status, rc);
14001 status = -ENXIO;
14002 goto out;
14003 }
14004 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14005 if (hrq->queue_id == 0xFFFF) {
14006 status = -ENXIO;
14007 goto out;
14008 }
James Smart962bc512013-01-03 15:44:00 -050014009
14010 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
14011 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
14012 &rq_create->u.response);
14013 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
14014 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
14015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14016 "3262 RQ [%d] doorbell format not "
14017 "supported: x%x\n", hrq->queue_id,
14018 hrq->db_format);
14019 status = -EINVAL;
14020 goto out;
14021 }
14022
14023 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
14024 &rq_create->u.response);
14025 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14026 if (!bar_memmap_p) {
14027 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14028 "3269 RQ[%d] failed to memmap pci "
14029 "barset:x%x\n", hrq->queue_id,
14030 pci_barset);
14031 status = -ENOMEM;
14032 goto out;
14033 }
14034
14035 db_offset = rq_create->u.response.doorbell_offset;
14036 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
14037 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
14038 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14039 "3270 RQ[%d] doorbell offset not "
14040 "supported: x%x\n", hrq->queue_id,
14041 db_offset);
14042 status = -EINVAL;
14043 goto out;
14044 }
14045 hrq->db_regaddr = bar_memmap_p + db_offset;
14046 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040014047 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
14048 "format:x%x\n", hrq->queue_id, pci_barset,
14049 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014050 } else {
14051 hrq->db_format = LPFC_DB_RING_FORMAT;
14052 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
14053 }
James Smart4f774512009-05-22 14:52:35 -040014054 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014055 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014056 hrq->subtype = subtype;
14057 hrq->host_index = 0;
14058 hrq->hba_index = 0;
14059
14060 /* now create the data queue */
14061 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14062 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14063 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050014064 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14065 phba->sli4_hba.pc_sli4_params.rqv);
14066 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14067 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040014068 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050014069 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040014070 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
14071 LPFC_RQE_SIZE_8);
14072 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
14073 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014074 } else {
14075 switch (drq->entry_count) {
14076 default:
14077 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14078 "2536 Unsupported RQ count. (%d)\n",
14079 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014080 if (drq->entry_count < 512) {
14081 status = -EINVAL;
14082 goto out;
14083 }
James Smart5a6f1332011-03-11 16:05:35 -050014084 /* otherwise default to smallest count (drop through) */
14085 case 512:
14086 bf_set(lpfc_rq_context_rqe_count,
14087 &rq_create->u.request.context,
14088 LPFC_RQ_RING_SIZE_512);
14089 break;
14090 case 1024:
14091 bf_set(lpfc_rq_context_rqe_count,
14092 &rq_create->u.request.context,
14093 LPFC_RQ_RING_SIZE_1024);
14094 break;
14095 case 2048:
14096 bf_set(lpfc_rq_context_rqe_count,
14097 &rq_create->u.request.context,
14098 LPFC_RQ_RING_SIZE_2048);
14099 break;
14100 case 4096:
14101 bf_set(lpfc_rq_context_rqe_count,
14102 &rq_create->u.request.context,
14103 LPFC_RQ_RING_SIZE_4096);
14104 break;
14105 }
14106 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14107 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040014108 }
14109 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14110 cq->queue_id);
14111 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14112 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040014113 list_for_each_entry(dmabuf, &drq->page_list, list) {
14114 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14115 putPaddrLow(dmabuf->phys);
14116 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14117 putPaddrHigh(dmabuf->phys);
14118 }
James Smart962bc512013-01-03 15:44:00 -050014119 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14120 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040014121 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14122 /* The IOCTL status is embedded in the mailbox subheader. */
14123 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14124 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14125 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14126 if (shdr_status || shdr_add_status || rc) {
14127 status = -ENXIO;
14128 goto out;
14129 }
14130 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14131 if (drq->queue_id == 0xFFFF) {
14132 status = -ENXIO;
14133 goto out;
14134 }
14135 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014136 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014137 drq->subtype = subtype;
14138 drq->host_index = 0;
14139 drq->hba_index = 0;
14140
14141 /* link the header and data RQs onto the parent cq child list */
14142 list_add_tail(&hrq->list, &cq->child_list);
14143 list_add_tail(&drq->list, &cq->child_list);
14144
14145out:
James Smart8fa38512009-07-19 10:01:03 -040014146 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014147 return status;
14148}
14149
14150/**
14151 * lpfc_eq_destroy - Destroy an event Queue on the HBA
14152 * @eq: The queue structure associated with the queue to destroy.
14153 *
14154 * This function destroys a queue, as detailed in @eq by sending an mailbox
14155 * command, specific to the type of queue, to the HBA.
14156 *
14157 * The @eq struct is used to get the queue ID of the queue to destroy.
14158 *
14159 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014160 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014161 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014162int
James Smart4f774512009-05-22 14:52:35 -040014163lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
14164{
14165 LPFC_MBOXQ_t *mbox;
14166 int rc, length, status = 0;
14167 uint32_t shdr_status, shdr_add_status;
14168 union lpfc_sli4_cfg_shdr *shdr;
14169
James Smart2e90f4b2011-12-13 13:22:37 -050014170 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014171 if (!eq)
14172 return -ENODEV;
14173 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
14174 if (!mbox)
14175 return -ENOMEM;
14176 length = (sizeof(struct lpfc_mbx_eq_destroy) -
14177 sizeof(struct lpfc_sli4_cfg_mhdr));
14178 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14179 LPFC_MBOX_OPCODE_EQ_DESTROY,
14180 length, LPFC_SLI4_MBX_EMBED);
14181 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
14182 eq->queue_id);
14183 mbox->vport = eq->phba->pport;
14184 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14185
14186 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
14187 /* The IOCTL status is embedded in the mailbox subheader. */
14188 shdr = (union lpfc_sli4_cfg_shdr *)
14189 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
14190 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14191 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14192 if (shdr_status || shdr_add_status || rc) {
14193 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14194 "2505 EQ_DESTROY mailbox failed with "
14195 "status x%x add_status x%x, mbx status x%x\n",
14196 shdr_status, shdr_add_status, rc);
14197 status = -ENXIO;
14198 }
14199
14200 /* Remove eq from any list */
14201 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040014202 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014203 return status;
14204}
14205
14206/**
14207 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
14208 * @cq: The queue structure associated with the queue to destroy.
14209 *
14210 * This function destroys a queue, as detailed in @cq by sending an mailbox
14211 * command, specific to the type of queue, to the HBA.
14212 *
14213 * The @cq struct is used to get the queue ID of the queue to destroy.
14214 *
14215 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014216 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014217 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014218int
James Smart4f774512009-05-22 14:52:35 -040014219lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14220{
14221 LPFC_MBOXQ_t *mbox;
14222 int rc, length, status = 0;
14223 uint32_t shdr_status, shdr_add_status;
14224 union lpfc_sli4_cfg_shdr *shdr;
14225
James Smart2e90f4b2011-12-13 13:22:37 -050014226 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014227 if (!cq)
14228 return -ENODEV;
14229 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14230 if (!mbox)
14231 return -ENOMEM;
14232 length = (sizeof(struct lpfc_mbx_cq_destroy) -
14233 sizeof(struct lpfc_sli4_cfg_mhdr));
14234 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14235 LPFC_MBOX_OPCODE_CQ_DESTROY,
14236 length, LPFC_SLI4_MBX_EMBED);
14237 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14238 cq->queue_id);
14239 mbox->vport = cq->phba->pport;
14240 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14241 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14242 /* The IOCTL status is embedded in the mailbox subheader. */
14243 shdr = (union lpfc_sli4_cfg_shdr *)
14244 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14245 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14246 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14247 if (shdr_status || shdr_add_status || rc) {
14248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14249 "2506 CQ_DESTROY mailbox failed with "
14250 "status x%x add_status x%x, mbx status x%x\n",
14251 shdr_status, shdr_add_status, rc);
14252 status = -ENXIO;
14253 }
14254 /* Remove cq from any list */
14255 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040014256 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014257 return status;
14258}
14259
14260/**
James Smart04c68492009-05-22 14:52:52 -040014261 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14262 * @qm: The queue structure associated with the queue to destroy.
14263 *
14264 * This function destroys a queue, as detailed in @mq by sending an mailbox
14265 * command, specific to the type of queue, to the HBA.
14266 *
14267 * The @mq struct is used to get the queue ID of the queue to destroy.
14268 *
14269 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014270 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040014271 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014272int
James Smart04c68492009-05-22 14:52:52 -040014273lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14274{
14275 LPFC_MBOXQ_t *mbox;
14276 int rc, length, status = 0;
14277 uint32_t shdr_status, shdr_add_status;
14278 union lpfc_sli4_cfg_shdr *shdr;
14279
James Smart2e90f4b2011-12-13 13:22:37 -050014280 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040014281 if (!mq)
14282 return -ENODEV;
14283 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14284 if (!mbox)
14285 return -ENOMEM;
14286 length = (sizeof(struct lpfc_mbx_mq_destroy) -
14287 sizeof(struct lpfc_sli4_cfg_mhdr));
14288 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14289 LPFC_MBOX_OPCODE_MQ_DESTROY,
14290 length, LPFC_SLI4_MBX_EMBED);
14291 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14292 mq->queue_id);
14293 mbox->vport = mq->phba->pport;
14294 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14295 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14296 /* The IOCTL status is embedded in the mailbox subheader. */
14297 shdr = (union lpfc_sli4_cfg_shdr *)
14298 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14299 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14300 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14301 if (shdr_status || shdr_add_status || rc) {
14302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14303 "2507 MQ_DESTROY mailbox failed with "
14304 "status x%x add_status x%x, mbx status x%x\n",
14305 shdr_status, shdr_add_status, rc);
14306 status = -ENXIO;
14307 }
14308 /* Remove mq from any list */
14309 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040014310 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014311 return status;
14312}
14313
14314/**
James Smart4f774512009-05-22 14:52:35 -040014315 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14316 * @wq: The queue structure associated with the queue to destroy.
14317 *
14318 * This function destroys a queue, as detailed in @wq by sending an mailbox
14319 * command, specific to the type of queue, to the HBA.
14320 *
14321 * The @wq struct is used to get the queue ID of the queue to destroy.
14322 *
14323 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014324 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014325 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014326int
James Smart4f774512009-05-22 14:52:35 -040014327lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14328{
14329 LPFC_MBOXQ_t *mbox;
14330 int rc, length, status = 0;
14331 uint32_t shdr_status, shdr_add_status;
14332 union lpfc_sli4_cfg_shdr *shdr;
14333
James Smart2e90f4b2011-12-13 13:22:37 -050014334 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014335 if (!wq)
14336 return -ENODEV;
14337 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14338 if (!mbox)
14339 return -ENOMEM;
14340 length = (sizeof(struct lpfc_mbx_wq_destroy) -
14341 sizeof(struct lpfc_sli4_cfg_mhdr));
14342 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14343 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14344 length, LPFC_SLI4_MBX_EMBED);
14345 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14346 wq->queue_id);
14347 mbox->vport = wq->phba->pport;
14348 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14349 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14350 shdr = (union lpfc_sli4_cfg_shdr *)
14351 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14352 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14353 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14354 if (shdr_status || shdr_add_status || rc) {
14355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14356 "2508 WQ_DESTROY mailbox failed with "
14357 "status x%x add_status x%x, mbx status x%x\n",
14358 shdr_status, shdr_add_status, rc);
14359 status = -ENXIO;
14360 }
14361 /* Remove wq from any list */
14362 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040014363 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014364 return status;
14365}
14366
14367/**
14368 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14369 * @rq: The queue structure associated with the queue to destroy.
14370 *
14371 * This function destroys a queue, as detailed in @rq by sending an mailbox
14372 * command, specific to the type of queue, to the HBA.
14373 *
14374 * The @rq struct is used to get the queue ID of the queue to destroy.
14375 *
14376 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014377 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014378 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014379int
James Smart4f774512009-05-22 14:52:35 -040014380lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14381 struct lpfc_queue *drq)
14382{
14383 LPFC_MBOXQ_t *mbox;
14384 int rc, length, status = 0;
14385 uint32_t shdr_status, shdr_add_status;
14386 union lpfc_sli4_cfg_shdr *shdr;
14387
James Smart2e90f4b2011-12-13 13:22:37 -050014388 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014389 if (!hrq || !drq)
14390 return -ENODEV;
14391 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14392 if (!mbox)
14393 return -ENOMEM;
14394 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050014395 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040014396 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14397 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14398 length, LPFC_SLI4_MBX_EMBED);
14399 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14400 hrq->queue_id);
14401 mbox->vport = hrq->phba->pport;
14402 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14403 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14404 /* The IOCTL status is embedded in the mailbox subheader. */
14405 shdr = (union lpfc_sli4_cfg_shdr *)
14406 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14407 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14408 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14409 if (shdr_status || shdr_add_status || rc) {
14410 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14411 "2509 RQ_DESTROY mailbox failed with "
14412 "status x%x add_status x%x, mbx status x%x\n",
14413 shdr_status, shdr_add_status, rc);
14414 if (rc != MBX_TIMEOUT)
14415 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14416 return -ENXIO;
14417 }
14418 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14419 drq->queue_id);
14420 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14421 shdr = (union lpfc_sli4_cfg_shdr *)
14422 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14423 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14424 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14425 if (shdr_status || shdr_add_status || rc) {
14426 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14427 "2510 RQ_DESTROY mailbox failed with "
14428 "status x%x add_status x%x, mbx status x%x\n",
14429 shdr_status, shdr_add_status, rc);
14430 status = -ENXIO;
14431 }
14432 list_del_init(&hrq->list);
14433 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040014434 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014435 return status;
14436}
14437
14438/**
14439 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14440 * @phba: The virtual port for which this call being executed.
14441 * @pdma_phys_addr0: Physical address of the 1st SGL page.
14442 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14443 * @xritag: the xritag that ties this io to the SGL pages.
14444 *
14445 * This routine will post the sgl pages for the IO that has the xritag
14446 * that is in the iocbq structure. The xritag is assigned during iocbq
14447 * creation and persists for as long as the driver is loaded.
14448 * if the caller has fewer than 256 scatter gather segments to map then
14449 * pdma_phys_addr1 should be 0.
14450 * If the caller needs to map more than 256 scatter gather segment then
14451 * pdma_phys_addr1 should be a valid physical address.
14452 * physical address for SGLs must be 64 byte aligned.
14453 * If you are going to map 2 SGL's then the first one must have 256 entries
14454 * the second sgl can have between 1 and 256 entries.
14455 *
14456 * Return codes:
14457 * 0 - Success
14458 * -ENXIO, -ENOMEM - Failure
14459 **/
14460int
14461lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14462 dma_addr_t pdma_phys_addr0,
14463 dma_addr_t pdma_phys_addr1,
14464 uint16_t xritag)
14465{
14466 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14467 LPFC_MBOXQ_t *mbox;
14468 int rc;
14469 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040014470 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040014471 union lpfc_sli4_cfg_shdr *shdr;
14472
14473 if (xritag == NO_XRI) {
14474 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14475 "0364 Invalid param:\n");
14476 return -EINVAL;
14477 }
14478
14479 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14480 if (!mbox)
14481 return -ENOMEM;
14482
14483 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14484 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14485 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050014486 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040014487
14488 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14489 &mbox->u.mqe.un.post_sgl_pages;
14490 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14491 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14492
14493 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14494 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14495 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14496 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14497
14498 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14499 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14500 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14501 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14502 if (!phba->sli4_hba.intr_enable)
14503 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040014504 else {
James Smarta183a152011-10-10 21:32:43 -040014505 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040014506 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14507 }
James Smart4f774512009-05-22 14:52:35 -040014508 /* The IOCTL status is embedded in the mailbox subheader. */
14509 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14510 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14511 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14512 if (rc != MBX_TIMEOUT)
14513 mempool_free(mbox, phba->mbox_mem_pool);
14514 if (shdr_status || shdr_add_status || rc) {
14515 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14516 "2511 POST_SGL mailbox failed with "
14517 "status x%x add_status x%x, mbx status x%x\n",
14518 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040014519 }
14520 return 0;
14521}
James Smart4f774512009-05-22 14:52:35 -040014522
14523/**
James Smart88a2cfb2011-07-22 18:36:33 -040014524 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040014525 * @phba: pointer to lpfc hba data structure.
14526 *
14527 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040014528 * HBA consistent with the SLI-4 interface spec. This routine
14529 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14530 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040014531 *
James Smart88a2cfb2011-07-22 18:36:33 -040014532 * Returns
14533 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14534 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14535 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014536static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014537lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14538{
14539 unsigned long xri;
14540
14541 /*
14542 * Fetch the next logical xri. Because this index is logical,
14543 * the driver starts at 0 each time.
14544 */
14545 spin_lock_irq(&phba->hbalock);
14546 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14547 phba->sli4_hba.max_cfg_param.max_xri, 0);
14548 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
14549 spin_unlock_irq(&phba->hbalock);
14550 return NO_XRI;
14551 } else {
14552 set_bit(xri, phba->sli4_hba.xri_bmask);
14553 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040014554 }
James Smart6d368e52011-05-24 11:44:12 -040014555 spin_unlock_irq(&phba->hbalock);
14556 return xri;
14557}
14558
14559/**
14560 * lpfc_sli4_free_xri - Release an xri for reuse.
14561 * @phba: pointer to lpfc hba data structure.
14562 *
14563 * This routine is invoked to release an xri to the pool of
14564 * available rpis maintained by the driver.
14565 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014566static void
James Smart6d368e52011-05-24 11:44:12 -040014567__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14568{
14569 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040014570 phba->sli4_hba.max_cfg_param.xri_used--;
14571 }
14572}
14573
14574/**
14575 * lpfc_sli4_free_xri - Release an xri for reuse.
14576 * @phba: pointer to lpfc hba data structure.
14577 *
14578 * This routine is invoked to release an xri to the pool of
14579 * available rpis maintained by the driver.
14580 **/
14581void
14582lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14583{
14584 spin_lock_irq(&phba->hbalock);
14585 __lpfc_sli4_free_xri(phba, xri);
14586 spin_unlock_irq(&phba->hbalock);
14587}
14588
14589/**
James Smart4f774512009-05-22 14:52:35 -040014590 * lpfc_sli4_next_xritag - Get an xritag for the io
14591 * @phba: Pointer to HBA context object.
14592 *
14593 * This function gets an xritag for the iocb. If there is no unused xritag
14594 * it will return 0xffff.
14595 * The function returns the allocated xritag if successful, else returns zero.
14596 * Zero is not a valid xritag.
14597 * The caller is not required to hold any lock.
14598 **/
14599uint16_t
14600lpfc_sli4_next_xritag(struct lpfc_hba *phba)
14601{
James Smart6d368e52011-05-24 11:44:12 -040014602 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040014603
James Smart6d368e52011-05-24 11:44:12 -040014604 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040014605 if (xri_index == NO_XRI)
14606 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14607 "2004 Failed to allocate XRI.last XRITAG is %d"
14608 " Max XRI is %d, Used XRI is %d\n",
14609 xri_index,
14610 phba->sli4_hba.max_cfg_param.max_xri,
14611 phba->sli4_hba.max_cfg_param.xri_used);
14612 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040014613}
14614
14615/**
James Smart6d368e52011-05-24 11:44:12 -040014616 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040014617 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040014618 * @post_sgl_list: pointer to els sgl entry list.
14619 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040014620 *
14621 * This routine is invoked to post a block of driver's sgl pages to the
14622 * HBA using non-embedded mailbox command. No Lock is held. This routine
14623 * is only called when the driver is loading and after all IO has been
14624 * stopped.
14625 **/
James Smart8a9d2e82012-05-09 21:16:12 -040014626static int
14627lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
14628 struct list_head *post_sgl_list,
14629 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040014630{
James Smart8a9d2e82012-05-09 21:16:12 -040014631 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040014632 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14633 struct sgl_page_pairs *sgl_pg_pairs;
14634 void *viraddr;
14635 LPFC_MBOXQ_t *mbox;
14636 uint32_t reqlen, alloclen, pg_pairs;
14637 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040014638 uint16_t xritag_start = 0;
14639 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040014640 uint32_t shdr_status, shdr_add_status;
14641 union lpfc_sli4_cfg_shdr *shdr;
14642
James Smart8a9d2e82012-05-09 21:16:12 -040014643 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014644 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014645 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014646 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14647 "2559 Block sgl registration required DMA "
14648 "size (%d) great than a page\n", reqlen);
14649 return -ENOMEM;
14650 }
14651 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040014652 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040014653 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040014654
14655 /* Allocate DMA memory and set up the non-embedded mailbox command */
14656 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14657 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14658 LPFC_SLI4_MBX_NEMBED);
14659
14660 if (alloclen < reqlen) {
14661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14662 "0285 Allocated DMA memory size (%d) is "
14663 "less than the requested DMA memory "
14664 "size (%d)\n", alloclen, reqlen);
14665 lpfc_sli4_mbox_cmd_free(phba, mbox);
14666 return -ENOMEM;
14667 }
James Smart4f774512009-05-22 14:52:35 -040014668 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040014669 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040014670 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14671 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14672
James Smart8a9d2e82012-05-09 21:16:12 -040014673 pg_pairs = 0;
14674 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040014675 /* Set up the sge entry */
14676 sgl_pg_pairs->sgl_pg0_addr_lo =
14677 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14678 sgl_pg_pairs->sgl_pg0_addr_hi =
14679 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14680 sgl_pg_pairs->sgl_pg1_addr_lo =
14681 cpu_to_le32(putPaddrLow(0));
14682 sgl_pg_pairs->sgl_pg1_addr_hi =
14683 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040014684
James Smart4f774512009-05-22 14:52:35 -040014685 /* Keep the first xritag on the list */
14686 if (pg_pairs == 0)
14687 xritag_start = sglq_entry->sli4_xritag;
14688 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040014689 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040014690 }
James Smart6d368e52011-05-24 11:44:12 -040014691
14692 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040014693 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040014694 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040014695 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040014696 if (!phba->sli4_hba.intr_enable)
14697 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14698 else {
James Smarta183a152011-10-10 21:32:43 -040014699 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014700 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14701 }
14702 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14703 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14704 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14705 if (rc != MBX_TIMEOUT)
14706 lpfc_sli4_mbox_cmd_free(phba, mbox);
14707 if (shdr_status || shdr_add_status || rc) {
14708 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14709 "2513 POST_SGL_BLOCK mailbox command failed "
14710 "status x%x add_status x%x mbx status x%x\n",
14711 shdr_status, shdr_add_status, rc);
14712 rc = -ENXIO;
14713 }
14714 return rc;
14715}
14716
14717/**
14718 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14719 * @phba: pointer to lpfc hba data structure.
14720 * @sblist: pointer to scsi buffer list.
14721 * @count: number of scsi buffers on the list.
14722 *
14723 * This routine is invoked to post a block of @count scsi sgl pages from a
14724 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14725 * No Lock is held.
14726 *
14727 **/
14728int
James Smart8a9d2e82012-05-09 21:16:12 -040014729lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14730 struct list_head *sblist,
14731 int count)
James Smart4f774512009-05-22 14:52:35 -040014732{
14733 struct lpfc_scsi_buf *psb;
14734 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14735 struct sgl_page_pairs *sgl_pg_pairs;
14736 void *viraddr;
14737 LPFC_MBOXQ_t *mbox;
14738 uint32_t reqlen, alloclen, pg_pairs;
14739 uint32_t mbox_tmo;
14740 uint16_t xritag_start = 0;
14741 int rc = 0;
14742 uint32_t shdr_status, shdr_add_status;
14743 dma_addr_t pdma_phys_bpl1;
14744 union lpfc_sli4_cfg_shdr *shdr;
14745
14746 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040014747 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014748 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014749 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014750 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14751 "0217 Block sgl registration required DMA "
14752 "size (%d) great than a page\n", reqlen);
14753 return -ENOMEM;
14754 }
14755 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14756 if (!mbox) {
14757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14758 "0283 Failed to allocate mbox cmd memory\n");
14759 return -ENOMEM;
14760 }
14761
14762 /* Allocate DMA memory and set up the non-embedded mailbox command */
14763 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14764 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14765 LPFC_SLI4_MBX_NEMBED);
14766
14767 if (alloclen < reqlen) {
14768 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14769 "2561 Allocated DMA memory size (%d) is "
14770 "less than the requested DMA memory "
14771 "size (%d)\n", alloclen, reqlen);
14772 lpfc_sli4_mbox_cmd_free(phba, mbox);
14773 return -ENOMEM;
14774 }
James Smart6d368e52011-05-24 11:44:12 -040014775
James Smart4f774512009-05-22 14:52:35 -040014776 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040014777 viraddr = mbox->sge_array->addr[0];
14778
14779 /* Set up the SGL pages in the non-embedded DMA pages */
14780 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14781 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14782
14783 pg_pairs = 0;
14784 list_for_each_entry(psb, sblist, list) {
14785 /* Set up the sge entry */
14786 sgl_pg_pairs->sgl_pg0_addr_lo =
14787 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14788 sgl_pg_pairs->sgl_pg0_addr_hi =
14789 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14790 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14791 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14792 else
14793 pdma_phys_bpl1 = 0;
14794 sgl_pg_pairs->sgl_pg1_addr_lo =
14795 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14796 sgl_pg_pairs->sgl_pg1_addr_hi =
14797 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14798 /* Keep the first xritag on the list */
14799 if (pg_pairs == 0)
14800 xritag_start = psb->cur_iocbq.sli4_xritag;
14801 sgl_pg_pairs++;
14802 pg_pairs++;
14803 }
14804 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14805 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14806 /* Perform endian conversion if necessary */
14807 sgl->word0 = cpu_to_le32(sgl->word0);
14808
14809 if (!phba->sli4_hba.intr_enable)
14810 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14811 else {
James Smarta183a152011-10-10 21:32:43 -040014812 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014813 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14814 }
14815 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14816 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14817 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14818 if (rc != MBX_TIMEOUT)
14819 lpfc_sli4_mbox_cmd_free(phba, mbox);
14820 if (shdr_status || shdr_add_status || rc) {
14821 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14822 "2564 POST_SGL_BLOCK mailbox command failed "
14823 "status x%x add_status x%x mbx status x%x\n",
14824 shdr_status, shdr_add_status, rc);
14825 rc = -ENXIO;
14826 }
14827 return rc;
14828}
14829
James Smart2ea259e2017-02-12 13:52:27 -080014830static char *lpfc_rctl_names[] = FC_RCTL_NAMES_INIT;
14831static char *lpfc_type_names[] = FC_TYPE_NAMES_INIT;
14832
James Smart4f774512009-05-22 14:52:35 -040014833/**
14834 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14835 * @phba: pointer to lpfc_hba struct that the frame was received on
14836 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14837 *
14838 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14839 * valid type of frame that the LPFC driver will handle. This function will
14840 * return a zero if the frame is a valid frame or a non zero value when the
14841 * frame does not pass the check.
14842 **/
14843static int
14844lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14845{
Tomas Henzl474ffb72010-12-22 16:52:40 +010014846 /* make rctl_names static to save stack space */
James Smart4f774512009-05-22 14:52:35 -040014847 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050014848 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040014849
14850 switch (fc_hdr->fh_r_ctl) {
14851 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14852 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14853 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14854 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14855 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14856 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14857 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14858 case FC_RCTL_DD_CMD_STATUS: /* command status */
14859 case FC_RCTL_ELS_REQ: /* extended link services request */
14860 case FC_RCTL_ELS_REP: /* extended link services reply */
14861 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14862 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14863 case FC_RCTL_BA_NOP: /* basic link service NOP */
14864 case FC_RCTL_BA_ABTS: /* basic link service abort */
14865 case FC_RCTL_BA_RMC: /* remove connection */
14866 case FC_RCTL_BA_ACC: /* basic accept */
14867 case FC_RCTL_BA_RJT: /* basic reject */
14868 case FC_RCTL_BA_PRMT:
14869 case FC_RCTL_ACK_1: /* acknowledge_1 */
14870 case FC_RCTL_ACK_0: /* acknowledge_0 */
14871 case FC_RCTL_P_RJT: /* port reject */
14872 case FC_RCTL_F_RJT: /* fabric reject */
14873 case FC_RCTL_P_BSY: /* port busy */
14874 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14875 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14876 case FC_RCTL_LCR: /* link credit reset */
14877 case FC_RCTL_END: /* end */
14878 break;
14879 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14880 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14881 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14882 return lpfc_fc_frame_check(phba, fc_hdr);
14883 default:
14884 goto drop;
14885 }
14886 switch (fc_hdr->fh_type) {
14887 case FC_TYPE_BLS:
14888 case FC_TYPE_ELS:
14889 case FC_TYPE_FCP:
14890 case FC_TYPE_CT:
14891 break;
14892 case FC_TYPE_IP:
14893 case FC_TYPE_ILS:
14894 default:
14895 goto drop;
14896 }
James Smart546fc852011-03-11 16:06:29 -050014897
James Smart4f774512009-05-22 14:52:35 -040014898 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040014899 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14900 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
James Smart2ea259e2017-02-12 13:52:27 -080014901 lpfc_rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14902 lpfc_type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050014903 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14904 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040014905 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14906 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040014907 return 0;
14908drop:
14909 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14910 "2539 Dropped frame rctl:%s type:%s\n",
James Smart2ea259e2017-02-12 13:52:27 -080014911 lpfc_rctl_names[fc_hdr->fh_r_ctl],
14912 lpfc_type_names[fc_hdr->fh_type]);
James Smart4f774512009-05-22 14:52:35 -040014913 return 1;
14914}
14915
14916/**
14917 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14918 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14919 *
14920 * This function processes the FC header to retrieve the VFI from the VF
14921 * header, if one exists. This function will return the VFI if one exists
14922 * or 0 if no VSAN Header exists.
14923 **/
14924static uint32_t
14925lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14926{
14927 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14928
14929 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14930 return 0;
14931 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14932}
14933
14934/**
14935 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14936 * @phba: Pointer to the HBA structure to search for the vport on
14937 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14938 * @fcfi: The FC Fabric ID that the frame came from
14939 *
14940 * This function searches the @phba for a vport that matches the content of the
14941 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14942 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14943 * returns the matching vport pointer or NULL if unable to match frame to a
14944 * vport.
14945 **/
14946static struct lpfc_vport *
14947lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14948 uint16_t fcfi)
14949{
14950 struct lpfc_vport **vports;
14951 struct lpfc_vport *vport = NULL;
14952 int i;
14953 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14954 fc_hdr->fh_d_id[1] << 8 |
14955 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040014956
James Smartbf086112011-08-21 21:48:13 -040014957 if (did == Fabric_DID)
14958 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040014959 if ((phba->pport->fc_flag & FC_PT2PT) &&
14960 !(phba->link_state == LPFC_HBA_READY))
14961 return phba->pport;
14962
James Smart4f774512009-05-22 14:52:35 -040014963 vports = lpfc_create_vport_work_array(phba);
14964 if (vports != NULL)
14965 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14966 if (phba->fcf.fcfi == fcfi &&
14967 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14968 vports[i]->fc_myDID == did) {
14969 vport = vports[i];
14970 break;
14971 }
14972 }
14973 lpfc_destroy_vport_work_array(phba, vports);
14974 return vport;
14975}
14976
14977/**
James Smart45ed1192009-10-02 15:17:02 -040014978 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14979 * @vport: The vport to work on.
14980 *
14981 * This function updates the receive sequence time stamp for this vport. The
14982 * receive sequence time stamp indicates the time that the last frame of the
14983 * the sequence that has been idle for the longest amount of time was received.
14984 * the driver uses this time stamp to indicate if any received sequences have
14985 * timed out.
14986 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014987static void
James Smart45ed1192009-10-02 15:17:02 -040014988lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14989{
14990 struct lpfc_dmabuf *h_buf;
14991 struct hbq_dmabuf *dmabuf = NULL;
14992
14993 /* get the oldest sequence on the rcv list */
14994 h_buf = list_get_first(&vport->rcv_buffer_list,
14995 struct lpfc_dmabuf, list);
14996 if (!h_buf)
14997 return;
14998 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14999 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
15000}
15001
15002/**
15003 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
15004 * @vport: The vport that the received sequences were sent to.
15005 *
15006 * This function cleans up all outstanding received sequences. This is called
15007 * by the driver when a link event or user action invalidates all the received
15008 * sequences.
15009 **/
15010void
15011lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
15012{
15013 struct lpfc_dmabuf *h_buf, *hnext;
15014 struct lpfc_dmabuf *d_buf, *dnext;
15015 struct hbq_dmabuf *dmabuf = NULL;
15016
15017 /* start with the oldest sequence on the rcv list */
15018 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15019 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15020 list_del_init(&dmabuf->hbuf.list);
15021 list_for_each_entry_safe(d_buf, dnext,
15022 &dmabuf->dbuf.list, list) {
15023 list_del_init(&d_buf->list);
15024 lpfc_in_buf_free(vport->phba, d_buf);
15025 }
15026 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15027 }
15028}
15029
15030/**
15031 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
15032 * @vport: The vport that the received sequences were sent to.
15033 *
15034 * This function determines whether any received sequences have timed out by
15035 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
15036 * indicates that there is at least one timed out sequence this routine will
15037 * go through the received sequences one at a time from most inactive to most
15038 * active to determine which ones need to be cleaned up. Once it has determined
15039 * that a sequence needs to be cleaned up it will simply free up the resources
15040 * without sending an abort.
15041 **/
15042void
15043lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
15044{
15045 struct lpfc_dmabuf *h_buf, *hnext;
15046 struct lpfc_dmabuf *d_buf, *dnext;
15047 struct hbq_dmabuf *dmabuf = NULL;
15048 unsigned long timeout;
15049 int abort_count = 0;
15050
15051 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15052 vport->rcv_buffer_time_stamp);
15053 if (list_empty(&vport->rcv_buffer_list) ||
15054 time_before(jiffies, timeout))
15055 return;
15056 /* start with the oldest sequence on the rcv list */
15057 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15058 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15059 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15060 dmabuf->time_stamp);
15061 if (time_before(jiffies, timeout))
15062 break;
15063 abort_count++;
15064 list_del_init(&dmabuf->hbuf.list);
15065 list_for_each_entry_safe(d_buf, dnext,
15066 &dmabuf->dbuf.list, list) {
15067 list_del_init(&d_buf->list);
15068 lpfc_in_buf_free(vport->phba, d_buf);
15069 }
15070 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15071 }
15072 if (abort_count)
15073 lpfc_update_rcv_time_stamp(vport);
15074}
15075
15076/**
James Smart4f774512009-05-22 14:52:35 -040015077 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
15078 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
15079 *
15080 * This function searches through the existing incomplete sequences that have
15081 * been sent to this @vport. If the frame matches one of the incomplete
15082 * sequences then the dbuf in the @dmabuf is added to the list of frames that
15083 * make up that sequence. If no sequence is found that matches this frame then
15084 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
15085 * This function returns a pointer to the first dmabuf in the sequence list that
15086 * the frame was linked to.
15087 **/
15088static struct hbq_dmabuf *
15089lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15090{
15091 struct fc_frame_header *new_hdr;
15092 struct fc_frame_header *temp_hdr;
15093 struct lpfc_dmabuf *d_buf;
15094 struct lpfc_dmabuf *h_buf;
15095 struct hbq_dmabuf *seq_dmabuf = NULL;
15096 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050015097 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040015098
James Smart4d9ab992009-10-02 15:16:39 -040015099 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015100 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040015101 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050015102
James Smart4f774512009-05-22 14:52:35 -040015103 /* Use the hdr_buf to find the sequence that this frame belongs to */
15104 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15105 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15106 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15107 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15108 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15109 continue;
15110 /* found a pending sequence that matches this frame */
15111 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15112 break;
15113 }
15114 if (!seq_dmabuf) {
15115 /*
15116 * This indicates first frame received for this sequence.
15117 * Queue the buffer on the vport's rcv_buffer_list.
15118 */
15119 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040015120 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015121 return dmabuf;
15122 }
15123 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050015124 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
15125 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040015126 list_del_init(&seq_dmabuf->hbuf.list);
15127 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15128 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015129 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015130 return dmabuf;
15131 }
James Smart45ed1192009-10-02 15:17:02 -040015132 /* move this sequence to the tail to indicate a young sequence */
15133 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
15134 seq_dmabuf->time_stamp = jiffies;
15135 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050015136 if (list_empty(&seq_dmabuf->dbuf.list)) {
15137 temp_hdr = dmabuf->hbuf.virt;
15138 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15139 return seq_dmabuf;
15140 }
James Smart4f774512009-05-22 14:52:35 -040015141 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050015142 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
15143 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040015144 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15145 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
15146 /*
15147 * If the frame's sequence count is greater than the frame on
15148 * the list then insert the frame right after this frame
15149 */
James Smarteeead812009-12-21 17:01:23 -050015150 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
15151 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040015152 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050015153 found = 1;
15154 break;
James Smart4f774512009-05-22 14:52:35 -040015155 }
James Smart4360ca92015-12-16 18:12:04 -050015156
15157 if (&d_buf->list == &seq_dmabuf->dbuf.list)
15158 break;
15159 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040015160 }
James Smart4360ca92015-12-16 18:12:04 -050015161
15162 if (found)
15163 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015164 return NULL;
15165}
15166
15167/**
James Smart6669f9b2009-10-02 15:16:45 -040015168 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
15169 * @vport: pointer to a vitural port
15170 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15171 *
15172 * This function tries to abort from the partially assembed sequence, described
15173 * by the information from basic abbort @dmabuf. It checks to see whether such
15174 * partially assembled sequence held by the driver. If so, it shall free up all
15175 * the frames from the partially assembled sequence.
15176 *
15177 * Return
15178 * true -- if there is matching partially assembled sequence present and all
15179 * the frames freed with the sequence;
15180 * false -- if there is no matching partially assembled sequence present so
15181 * nothing got aborted in the lower layer driver
15182 **/
15183static bool
15184lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
15185 struct hbq_dmabuf *dmabuf)
15186{
15187 struct fc_frame_header *new_hdr;
15188 struct fc_frame_header *temp_hdr;
15189 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
15190 struct hbq_dmabuf *seq_dmabuf = NULL;
15191
15192 /* Use the hdr_buf to find the sequence that matches this frame */
15193 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15194 INIT_LIST_HEAD(&dmabuf->hbuf.list);
15195 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15196 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15197 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15198 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15199 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15200 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15201 continue;
15202 /* found a pending sequence that matches this frame */
15203 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15204 break;
15205 }
15206
15207 /* Free up all the frames from the partially assembled sequence */
15208 if (seq_dmabuf) {
15209 list_for_each_entry_safe(d_buf, n_buf,
15210 &seq_dmabuf->dbuf.list, list) {
15211 list_del_init(&d_buf->list);
15212 lpfc_in_buf_free(vport->phba, d_buf);
15213 }
15214 return true;
15215 }
15216 return false;
15217}
15218
15219/**
James Smart6dd9e312013-01-03 15:43:37 -050015220 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15221 * @vport: pointer to a vitural port
15222 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15223 *
15224 * This function tries to abort from the assembed sequence from upper level
15225 * protocol, described by the information from basic abbort @dmabuf. It
15226 * checks to see whether such pending context exists at upper level protocol.
15227 * If so, it shall clean up the pending context.
15228 *
15229 * Return
15230 * true -- if there is matching pending context of the sequence cleaned
15231 * at ulp;
15232 * false -- if there is no matching pending context of the sequence present
15233 * at ulp.
15234 **/
15235static bool
15236lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15237{
15238 struct lpfc_hba *phba = vport->phba;
15239 int handled;
15240
15241 /* Accepting abort at ulp with SLI4 only */
15242 if (phba->sli_rev < LPFC_SLI_REV4)
15243 return false;
15244
15245 /* Register all caring upper level protocols to attend abort */
15246 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15247 if (handled)
15248 return true;
15249
15250 return false;
15251}
15252
15253/**
James Smart546fc852011-03-11 16:06:29 -050015254 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040015255 * @phba: Pointer to HBA context object.
15256 * @cmd_iocbq: pointer to the command iocbq structure.
15257 * @rsp_iocbq: pointer to the response iocbq structure.
15258 *
James Smart546fc852011-03-11 16:06:29 -050015259 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040015260 * event. It properly releases the memory allocated to the sequence abort
15261 * accept iocb.
15262 **/
15263static void
James Smart546fc852011-03-11 16:06:29 -050015264lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040015265 struct lpfc_iocbq *cmd_iocbq,
15266 struct lpfc_iocbq *rsp_iocbq)
15267{
James Smart6dd9e312013-01-03 15:43:37 -050015268 struct lpfc_nodelist *ndlp;
15269
15270 if (cmd_iocbq) {
15271 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15272 lpfc_nlp_put(ndlp);
15273 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015274 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050015275 }
James Smart6b5151f2012-01-18 16:24:06 -050015276
15277 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15278 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15279 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15280 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
15281 rsp_iocbq->iocb.ulpStatus,
15282 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040015283}
15284
15285/**
James Smart6d368e52011-05-24 11:44:12 -040015286 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15287 * @phba: Pointer to HBA context object.
15288 * @xri: xri id in transaction.
15289 *
15290 * This function validates the xri maps to the known range of XRIs allocated an
15291 * used by the driver.
15292 **/
James Smart7851fe22011-07-22 18:36:52 -040015293uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015294lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15295 uint16_t xri)
15296{
James Smarta2fc4aef2014-09-03 12:57:55 -040015297 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040015298
15299 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15300 if (xri == phba->sli4_hba.xri_ids[i])
15301 return i;
15302 }
15303 return NO_XRI;
15304}
15305
James Smart6d368e52011-05-24 11:44:12 -040015306/**
James Smart546fc852011-03-11 16:06:29 -050015307 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015308 * @phba: Pointer to HBA context object.
15309 * @fc_hdr: pointer to a FC frame header.
15310 *
James Smart546fc852011-03-11 16:06:29 -050015311 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015312 * event after aborting the sequence handling.
15313 **/
15314static void
James Smart6dd9e312013-01-03 15:43:37 -050015315lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15316 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040015317{
James Smart6dd9e312013-01-03 15:43:37 -050015318 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040015319 struct lpfc_iocbq *ctiocb = NULL;
15320 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040015321 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050015322 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040015323 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050015324 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040015325
15326 if (!lpfc_is_link_up(phba))
15327 return;
15328
15329 sid = sli4_sid_from_fc_hdr(fc_hdr);
15330 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050015331 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040015332
James Smart6dd9e312013-01-03 15:43:37 -050015333 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040015334 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050015335 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15336 if (!ndlp) {
15337 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15338 "1268 Failed to allocate ndlp for "
15339 "oxid:x%x SID:x%x\n", oxid, sid);
15340 return;
15341 }
15342 lpfc_nlp_init(vport, ndlp, sid);
15343 /* Put ndlp onto pport node list */
15344 lpfc_enqueue_node(vport, ndlp);
15345 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15346 /* re-setup ndlp without removing from node list */
15347 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15348 if (!ndlp) {
15349 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15350 "3275 Failed to active ndlp found "
15351 "for oxid:x%x SID:x%x\n", oxid, sid);
15352 return;
15353 }
James Smart6669f9b2009-10-02 15:16:45 -040015354 }
15355
James Smart546fc852011-03-11 16:06:29 -050015356 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040015357 ctiocb = lpfc_sli_get_iocbq(phba);
15358 if (!ctiocb)
15359 return;
15360
James Smart5ffc2662009-11-18 15:39:44 -050015361 /* Extract the F_CTL field from FC_HDR */
15362 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15363
James Smart6669f9b2009-10-02 15:16:45 -040015364 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040015365 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050015366 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040015367 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15368 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15369 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15370
15371 /* Fill in the rest of iocb fields */
15372 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15373 icmd->ulpBdeCount = 0;
15374 icmd->ulpLe = 1;
15375 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040015376 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050015377 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015378
James Smart6669f9b2009-10-02 15:16:45 -040015379 ctiocb->iocb_cmpl = NULL;
15380 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050015381 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040015382 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050015383 ctiocb->sli4_xritag = NO_XRI;
15384
James Smartee0f4fe2012-05-09 21:19:14 -040015385 if (fctl & FC_FC_EX_CTX)
15386 /* Exchange responder sent the abort so we
15387 * own the oxid.
15388 */
15389 xri = oxid;
15390 else
15391 xri = rxid;
15392 lxri = lpfc_sli4_xri_inrange(phba, xri);
15393 if (lxri != NO_XRI)
15394 lpfc_set_rrq_active(phba, ndlp, lxri,
15395 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050015396 /* For BA_ABTS from exchange responder, if the logical xri with
15397 * the oxid maps to the FCP XRI range, the port no longer has
15398 * that exchange context, send a BLS_RJT. Override the IOCB for
15399 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050015400 */
James Smart6dd9e312013-01-03 15:43:37 -050015401 if ((fctl & FC_FC_EX_CTX) &&
15402 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
15403 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15404 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15405 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15406 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15407 }
15408
15409 /* If BA_ABTS failed to abort a partially assembled receive sequence,
15410 * the driver no longer has that exchange, send a BLS_RJT. Override
15411 * the IOCB for a BA_RJT.
15412 */
15413 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050015414 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15415 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15416 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15417 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15418 }
James Smart6669f9b2009-10-02 15:16:45 -040015419
James Smart5ffc2662009-11-18 15:39:44 -050015420 if (fctl & FC_FC_EX_CTX) {
15421 /* ABTS sent by responder to CT exchange, construction
15422 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15423 * field and RX_ID from ABTS for RX_ID field.
15424 */
James Smart546fc852011-03-11 16:06:29 -050015425 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050015426 } else {
15427 /* ABTS sent by initiator to CT exchange, construction
15428 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050015429 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050015430 */
James Smart546fc852011-03-11 16:06:29 -050015431 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050015432 }
James Smartf09c3ac2012-03-01 22:33:29 -050015433 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050015434 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050015435
James Smart546fc852011-03-11 16:06:29 -050015436 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050015437 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15438 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15439 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050015440
15441 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15442 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050015443 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15444 "2925 Failed to issue CT ABTS RSP x%x on "
15445 "xri x%x, Data x%x\n",
15446 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15447 phba->link_state);
15448 lpfc_nlp_put(ndlp);
15449 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050015450 lpfc_sli_release_iocbq(phba, ctiocb);
15451 }
James Smart6669f9b2009-10-02 15:16:45 -040015452}
15453
15454/**
15455 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15456 * @vport: Pointer to the vport on which this sequence was received
15457 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15458 *
15459 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15460 * receive sequence is only partially assembed by the driver, it shall abort
15461 * the partially assembled frames for the sequence. Otherwise, if the
15462 * unsolicited receive sequence has been completely assembled and passed to
15463 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15464 * unsolicited sequence has been aborted. After that, it will issue a basic
15465 * accept to accept the abort.
15466 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015467static void
James Smart6669f9b2009-10-02 15:16:45 -040015468lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15469 struct hbq_dmabuf *dmabuf)
15470{
15471 struct lpfc_hba *phba = vport->phba;
15472 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050015473 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050015474 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040015475
James Smart6669f9b2009-10-02 15:16:45 -040015476 /* Make a copy of fc_hdr before the dmabuf being released */
15477 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050015478 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040015479
James Smart5ffc2662009-11-18 15:39:44 -050015480 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050015481 /* ABTS by responder to exchange, no cleanup needed */
15482 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050015483 } else {
James Smart6dd9e312013-01-03 15:43:37 -050015484 /* ABTS by initiator to exchange, need to do cleanup */
15485 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15486 if (aborted == false)
15487 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050015488 }
James Smart6dd9e312013-01-03 15:43:37 -050015489 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15490
15491 /* Respond with BA_ACC or BA_RJT accordingly */
15492 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040015493}
15494
15495/**
James Smart4f774512009-05-22 14:52:35 -040015496 * lpfc_seq_complete - Indicates if a sequence is complete
15497 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15498 *
15499 * This function checks the sequence, starting with the frame described by
15500 * @dmabuf, to see if all the frames associated with this sequence are present.
15501 * the frames associated with this sequence are linked to the @dmabuf using the
15502 * dbuf list. This function looks for two major things. 1) That the first frame
15503 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15504 * set. 3) That there are no holes in the sequence count. The function will
15505 * return 1 when the sequence is complete, otherwise it will return 0.
15506 **/
15507static int
15508lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15509{
15510 struct fc_frame_header *hdr;
15511 struct lpfc_dmabuf *d_buf;
15512 struct hbq_dmabuf *seq_dmabuf;
15513 uint32_t fctl;
15514 int seq_count = 0;
15515
15516 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15517 /* make sure first fame of sequence has a sequence count of zero */
15518 if (hdr->fh_seq_cnt != seq_count)
15519 return 0;
15520 fctl = (hdr->fh_f_ctl[0] << 16 |
15521 hdr->fh_f_ctl[1] << 8 |
15522 hdr->fh_f_ctl[2]);
15523 /* If last frame of sequence we can return success. */
15524 if (fctl & FC_FC_END_SEQ)
15525 return 1;
15526 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15527 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15528 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15529 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050015530 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040015531 return 0;
15532 fctl = (hdr->fh_f_ctl[0] << 16 |
15533 hdr->fh_f_ctl[1] << 8 |
15534 hdr->fh_f_ctl[2]);
15535 /* If last frame of sequence we can return success. */
15536 if (fctl & FC_FC_END_SEQ)
15537 return 1;
15538 }
15539 return 0;
15540}
15541
15542/**
15543 * lpfc_prep_seq - Prep sequence for ULP processing
15544 * @vport: Pointer to the vport on which this sequence was received
15545 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15546 *
15547 * This function takes a sequence, described by a list of frames, and creates
15548 * a list of iocbq structures to describe the sequence. This iocbq list will be
15549 * used to issue to the generic unsolicited sequence handler. This routine
15550 * returns a pointer to the first iocbq in the list. If the function is unable
15551 * to allocate an iocbq then it throw out the received frames that were not
15552 * able to be described and return a pointer to the first iocbq. If unable to
15553 * allocate any iocbqs (including the first) this function will return NULL.
15554 **/
15555static struct lpfc_iocbq *
15556lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
15557{
James Smart7851fe22011-07-22 18:36:52 -040015558 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040015559 struct lpfc_dmabuf *d_buf, *n_buf;
15560 struct lpfc_iocbq *first_iocbq, *iocbq;
15561 struct fc_frame_header *fc_hdr;
15562 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040015563 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050015564 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040015565
15566 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15567 /* remove from receive buffer list */
15568 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015569 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015570 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040015571 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040015572 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015573 /* Get an iocbq struct to fill in. */
15574 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
15575 if (first_iocbq) {
15576 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040015577 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015578 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040015579
15580 /* Check FC Header to see what TYPE of frame we are rcv'ing */
15581 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
15582 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
15583 first_iocbq->iocb.un.rcvels.parmRo =
15584 sli4_did_from_fc_hdr(fc_hdr);
15585 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
15586 } else
15587 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040015588 first_iocbq->iocb.ulpContext = NO_XRI;
15589 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
15590 be16_to_cpu(fc_hdr->fh_ox_id);
15591 /* iocbq is prepped for internal consumption. Physical vpi. */
15592 first_iocbq->iocb.unsli3.rcvsli3.vpi =
15593 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040015594 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040015595 tot_len = bf_get(lpfc_rcqe_length,
15596 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
15597
James Smart4f774512009-05-22 14:52:35 -040015598 first_iocbq->context2 = &seq_dmabuf->dbuf;
15599 first_iocbq->context3 = NULL;
15600 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040015601 if (tot_len > LPFC_DATA_BUF_SIZE)
15602 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040015603 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040015604 else
15605 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
15606
James Smart4f774512009-05-22 14:52:35 -040015607 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040015608
James Smart7851fe22011-07-22 18:36:52 -040015609 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040015610 }
15611 iocbq = first_iocbq;
15612 /*
15613 * Each IOCBq can have two Buffers assigned, so go through the list
15614 * of buffers for this sequence and save two buffers in each IOCBq
15615 */
15616 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
15617 if (!iocbq) {
15618 lpfc_in_buf_free(vport->phba, d_buf);
15619 continue;
15620 }
15621 if (!iocbq->context3) {
15622 iocbq->context3 = d_buf;
15623 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040015624 /* We need to get the size out of the right CQE */
15625 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15626 len = bf_get(lpfc_rcqe_length,
15627 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015628 pbde = (struct ulp_bde64 *)
15629 &iocbq->iocb.unsli3.sli3Words[4];
15630 if (len > LPFC_DATA_BUF_SIZE)
15631 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
15632 else
15633 pbde->tus.f.bdeSize = len;
15634
James Smart7851fe22011-07-22 18:36:52 -040015635 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
15636 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040015637 } else {
15638 iocbq = lpfc_sli_get_iocbq(vport->phba);
15639 if (!iocbq) {
15640 if (first_iocbq) {
15641 first_iocbq->iocb.ulpStatus =
15642 IOSTAT_FCP_RSP_ERROR;
15643 first_iocbq->iocb.un.ulpWord[4] =
15644 IOERR_NO_RESOURCES;
15645 }
15646 lpfc_in_buf_free(vport->phba, d_buf);
15647 continue;
15648 }
James Smart7851fe22011-07-22 18:36:52 -040015649 /* We need to get the size out of the right CQE */
15650 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15651 len = bf_get(lpfc_rcqe_length,
15652 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015653 iocbq->context2 = d_buf;
15654 iocbq->context3 = NULL;
15655 iocbq->iocb.ulpBdeCount = 1;
15656 if (len > LPFC_DATA_BUF_SIZE)
15657 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15658 LPFC_DATA_BUF_SIZE;
15659 else
15660 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
15661
James Smart7851fe22011-07-22 18:36:52 -040015662 tot_len += len;
15663 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15664
James Smart4f774512009-05-22 14:52:35 -040015665 iocbq->iocb.un.rcvels.remoteID = sid;
15666 list_add_tail(&iocbq->list, &first_iocbq->list);
15667 }
15668 }
15669 return first_iocbq;
15670}
15671
James Smart6669f9b2009-10-02 15:16:45 -040015672static void
15673lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
15674 struct hbq_dmabuf *seq_dmabuf)
15675{
15676 struct fc_frame_header *fc_hdr;
15677 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15678 struct lpfc_hba *phba = vport->phba;
15679
15680 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15681 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15682 if (!iocbq) {
15683 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15684 "2707 Ring %d handler: Failed to allocate "
15685 "iocb Rctl x%x Type x%x received\n",
15686 LPFC_ELS_RING,
15687 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15688 return;
15689 }
15690 if (!lpfc_complete_unsol_iocb(phba,
15691 &phba->sli.ring[LPFC_ELS_RING],
15692 iocbq, fc_hdr->fh_r_ctl,
15693 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040015694 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040015695 "2540 Ring %d handler: unexpected Rctl "
15696 "x%x Type x%x received\n",
15697 LPFC_ELS_RING,
15698 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15699
15700 /* Free iocb created in lpfc_prep_seq */
15701 list_for_each_entry_safe(curr_iocb, next_iocb,
15702 &iocbq->list, list) {
15703 list_del_init(&curr_iocb->list);
15704 lpfc_sli_release_iocbq(phba, curr_iocb);
15705 }
15706 lpfc_sli_release_iocbq(phba, iocbq);
15707}
15708
James Smart4f774512009-05-22 14:52:35 -040015709/**
15710 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15711 * @phba: Pointer to HBA context object.
15712 *
15713 * This function is called with no lock held. This function processes all
15714 * the received buffers and gives it to upper layers when a received buffer
15715 * indicates that it is the final frame in the sequence. The interrupt
15716 * service routine processes received buffers at interrupt contexts and adds
15717 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15718 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15719 * appropriate receive function when the final frame in a sequence is received.
15720 **/
James Smart4d9ab992009-10-02 15:16:39 -040015721void
15722lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15723 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040015724{
James Smart4d9ab992009-10-02 15:16:39 -040015725 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015726 struct fc_frame_header *fc_hdr;
15727 struct lpfc_vport *vport;
15728 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040015729 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040015730
James Smart4f774512009-05-22 14:52:35 -040015731 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040015732 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart2ea259e2017-02-12 13:52:27 -080015733
James Smart4d9ab992009-10-02 15:16:39 -040015734 /* check to see if this a valid type of frame */
15735 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15736 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15737 return;
15738 }
James Smart2ea259e2017-02-12 13:52:27 -080015739
James Smart7851fe22011-07-22 18:36:52 -040015740 if ((bf_get(lpfc_cqe_code,
15741 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15742 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15743 &dmabuf->cq_event.cqe.rcqe_cmpl);
15744 else
15745 fcfi = bf_get(lpfc_rcqe_fcf_id,
15746 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040015747
James Smart4d9ab992009-10-02 15:16:39 -040015748 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040015749 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040015750 /* throw out the frame */
15751 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15752 return;
15753 }
James Smart939723a2012-05-09 21:19:03 -040015754
15755 /* d_id this frame is directed to */
15756 did = sli4_did_from_fc_hdr(fc_hdr);
15757
15758 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15759 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15760 (did != Fabric_DID)) {
15761 /*
15762 * Throw out the frame if we are not pt2pt.
15763 * The pt2pt protocol allows for discovery frames
15764 * to be received without a registered VPI.
15765 */
15766 if (!(vport->fc_flag & FC_PT2PT) ||
15767 (phba->link_state == LPFC_HBA_READY)) {
15768 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15769 return;
15770 }
15771 }
15772
James Smart6669f9b2009-10-02 15:16:45 -040015773 /* Handle the basic abort sequence (BA_ABTS) event */
15774 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15775 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15776 return;
15777 }
15778
James Smart4d9ab992009-10-02 15:16:39 -040015779 /* Link this frame */
15780 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15781 if (!seq_dmabuf) {
15782 /* unable to add frame to vport - throw it out */
15783 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15784 return;
15785 }
15786 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050015787 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040015788 return;
James Smartdef9c7a2009-12-21 17:02:28 -050015789
James Smart6669f9b2009-10-02 15:16:45 -040015790 /* Send the complete sequence to the upper layer protocol */
15791 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040015792}
James Smart6fb120a2009-05-22 14:52:59 -040015793
15794/**
15795 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15796 * @phba: pointer to lpfc hba data structure.
15797 *
15798 * This routine is invoked to post rpi header templates to the
15799 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015800 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15801 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015802 *
15803 * This routine does not require any locks. It's usage is expected
15804 * to be driver load or reset recovery when the driver is
15805 * sequential.
15806 *
15807 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015808 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015809 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015810 * When this error occurs, the driver is not guaranteed
15811 * to have any rpi regions posted to the device and
15812 * must either attempt to repost the regions or take a
15813 * fatal error.
15814 **/
15815int
15816lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15817{
15818 struct lpfc_rpi_hdr *rpi_page;
15819 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040015820 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015821
James Smart6d368e52011-05-24 11:44:12 -040015822 /* SLI4 ports that support extents do not require RPI headers. */
15823 if (!phba->sli4_hba.rpi_hdrs_in_use)
15824 goto exit;
15825 if (phba->sli4_hba.extents_in_use)
15826 return -EIO;
15827
James Smart6fb120a2009-05-22 14:52:59 -040015828 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040015829 /*
15830 * Assign the rpi headers a physical rpi only if the driver
15831 * has not initialized those resources. A port reset only
15832 * needs the headers posted.
15833 */
15834 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15835 LPFC_RPI_RSRC_RDY)
15836 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15837
James Smart6fb120a2009-05-22 14:52:59 -040015838 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15839 if (rc != MBX_SUCCESS) {
15840 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15841 "2008 Error %d posting all rpi "
15842 "headers\n", rc);
15843 rc = -EIO;
15844 break;
15845 }
15846 }
15847
James Smart6d368e52011-05-24 11:44:12 -040015848 exit:
15849 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15850 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040015851 return rc;
15852}
15853
15854/**
15855 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15856 * @phba: pointer to lpfc hba data structure.
15857 * @rpi_page: pointer to the rpi memory region.
15858 *
15859 * This routine is invoked to post a single rpi header to the
15860 * HBA consistent with the SLI-4 interface spec. This memory region
15861 * maps up to 64 rpi context regions.
15862 *
15863 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015864 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015865 * -ENOMEM - No available memory
15866 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015867 **/
15868int
15869lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15870{
15871 LPFC_MBOXQ_t *mboxq;
15872 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15873 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015874 uint32_t shdr_status, shdr_add_status;
15875 union lpfc_sli4_cfg_shdr *shdr;
15876
James Smart6d368e52011-05-24 11:44:12 -040015877 /* SLI4 ports that support extents do not require RPI headers. */
15878 if (!phba->sli4_hba.rpi_hdrs_in_use)
15879 return rc;
15880 if (phba->sli4_hba.extents_in_use)
15881 return -EIO;
15882
James Smart6fb120a2009-05-22 14:52:59 -040015883 /* The port is notified of the header region via a mailbox command. */
15884 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15885 if (!mboxq) {
15886 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15887 "2001 Unable to allocate memory for issuing "
15888 "SLI_CONFIG_SPECIAL mailbox command\n");
15889 return -ENOMEM;
15890 }
15891
15892 /* Post all rpi memory regions to the port. */
15893 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040015894 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15895 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15896 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050015897 sizeof(struct lpfc_sli4_cfg_mhdr),
15898 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040015899
15900
15901 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040015902 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15903 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040015904 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15905 hdr_tmpl, rpi_page->page_count);
15906
James Smart6fb120a2009-05-22 14:52:59 -040015907 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15908 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040015909 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040015910 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15911 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15912 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15913 if (rc != MBX_TIMEOUT)
15914 mempool_free(mboxq, phba->mbox_mem_pool);
15915 if (shdr_status || shdr_add_status || rc) {
15916 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15917 "2514 POST_RPI_HDR mailbox failed with "
15918 "status x%x add_status x%x, mbx status x%x\n",
15919 shdr_status, shdr_add_status, rc);
15920 rc = -ENXIO;
15921 }
15922 return rc;
15923}
15924
15925/**
15926 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15927 * @phba: pointer to lpfc hba data structure.
15928 *
15929 * This routine is invoked to post rpi header templates to the
15930 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015931 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15932 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015933 *
15934 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015935 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040015936 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15937 **/
15938int
15939lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15940{
James Smart6d368e52011-05-24 11:44:12 -040015941 unsigned long rpi;
15942 uint16_t max_rpi, rpi_limit;
15943 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015944 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040015945 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040015946
James Smart6fb120a2009-05-22 14:52:59 -040015947 /*
James Smart6d368e52011-05-24 11:44:12 -040015948 * Fetch the next logical rpi. Because this index is logical,
15949 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040015950 */
James Smart4902b382013-10-10 12:20:35 -040015951 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040015952 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
15953 rpi_limit = phba->sli4_hba.next_rpi;
15954
James Smart6d368e52011-05-24 11:44:12 -040015955 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15956 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040015957 rpi = LPFC_RPI_ALLOC_ERROR;
15958 else {
15959 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15960 phba->sli4_hba.max_cfg_param.rpi_used++;
15961 phba->sli4_hba.rpi_count++;
15962 }
James Smartbe6bb942015-04-07 15:07:22 -040015963 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15964 "0001 rpi:%x max:%x lim:%x\n",
15965 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040015966
15967 /*
15968 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040015969 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040015970 */
15971 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15972 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040015973 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015974 return rpi;
15975 }
15976
15977 /*
James Smart6d368e52011-05-24 11:44:12 -040015978 * RPI header postings are not required for SLI4 ports capable of
15979 * extents.
15980 */
15981 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040015982 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015983 return rpi;
15984 }
15985
15986 /*
James Smart6fb120a2009-05-22 14:52:59 -040015987 * If the driver is running low on rpi resources, allocate another
15988 * page now. Note that the next_rpi value is used because
15989 * it represents how many are actually in use whereas max_rpi notes
15990 * how many are supported max by the device.
15991 */
James Smart6d368e52011-05-24 11:44:12 -040015992 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040015993 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015994 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15995 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15996 if (!rpi_hdr) {
15997 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15998 "2002 Error Could not grow rpi "
15999 "count\n");
16000 } else {
James Smart6d368e52011-05-24 11:44:12 -040016001 lrpi = rpi_hdr->start_rpi;
16002 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040016003 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
16004 }
16005 }
16006
16007 return rpi;
16008}
16009
16010/**
16011 * lpfc_sli4_free_rpi - Release an rpi for reuse.
16012 * @phba: pointer to lpfc hba data structure.
16013 *
16014 * This routine is invoked to release an rpi to the pool of
16015 * available rpis maintained by the driver.
16016 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016017static void
James Smartd7c47992010-06-08 18:31:54 -040016018__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16019{
16020 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
16021 phba->sli4_hba.rpi_count--;
16022 phba->sli4_hba.max_cfg_param.rpi_used--;
16023 }
16024}
16025
16026/**
16027 * lpfc_sli4_free_rpi - Release an rpi for reuse.
16028 * @phba: pointer to lpfc hba data structure.
16029 *
16030 * This routine is invoked to release an rpi to the pool of
16031 * available rpis maintained by the driver.
16032 **/
16033void
James Smart6fb120a2009-05-22 14:52:59 -040016034lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16035{
16036 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040016037 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040016038 spin_unlock_irq(&phba->hbalock);
16039}
16040
16041/**
16042 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
16043 * @phba: pointer to lpfc hba data structure.
16044 *
16045 * This routine is invoked to remove the memory region that
16046 * provided rpi via a bitmask.
16047 **/
16048void
16049lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
16050{
16051 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040016052 kfree(phba->sli4_hba.rpi_ids);
16053 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040016054}
16055
16056/**
16057 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
16058 * @phba: pointer to lpfc hba data structure.
16059 *
16060 * This routine is invoked to remove the memory region that
16061 * provided rpi via a bitmask.
16062 **/
16063int
James Smart6b5151f2012-01-18 16:24:06 -050016064lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
16065 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040016066{
16067 LPFC_MBOXQ_t *mboxq;
16068 struct lpfc_hba *phba = ndlp->phba;
16069 int rc;
16070
16071 /* The port is notified of the header region via a mailbox command. */
16072 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16073 if (!mboxq)
16074 return -ENOMEM;
16075
16076 /* Post all rpi memory regions to the port. */
16077 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050016078 if (cmpl) {
16079 mboxq->mbox_cmpl = cmpl;
16080 mboxq->context1 = arg;
16081 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050016082 } else
16083 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050016084 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040016085 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16086 if (rc == MBX_NOT_FINISHED) {
16087 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16088 "2010 Resume RPI Mailbox failed "
16089 "status %d, mbxStatus x%x\n", rc,
16090 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16091 mempool_free(mboxq, phba->mbox_mem_pool);
16092 return -EIO;
16093 }
16094 return 0;
16095}
16096
16097/**
16098 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050016099 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040016100 *
James Smart76a95d72010-11-20 23:11:48 -050016101 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040016102 *
16103 * Returns:
16104 * 0 success
16105 * -Evalue otherwise
16106 **/
16107int
James Smart76a95d72010-11-20 23:11:48 -050016108lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040016109{
16110 LPFC_MBOXQ_t *mboxq;
16111 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040016112 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040016113 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050016114 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040016115 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16116 if (!mboxq)
16117 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050016118 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040016119 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040016120 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040016121 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050016122 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040016123 "2022 INIT VPI Mailbox failed "
16124 "status %d, mbxStatus x%x\n", rc,
16125 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040016126 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040016127 }
James Smart6a9c52c2009-10-02 15:16:51 -040016128 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050016129 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040016130
16131 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040016132}
16133
16134/**
16135 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
16136 * @phba: pointer to lpfc hba data structure.
16137 * @mboxq: Pointer to mailbox object.
16138 *
16139 * This routine is invoked to manually add a single FCF record. The caller
16140 * must pass a completely initialized FCF_Record. This routine takes
16141 * care of the nonembedded mailbox operations.
16142 **/
16143static void
16144lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
16145{
16146 void *virt_addr;
16147 union lpfc_sli4_cfg_shdr *shdr;
16148 uint32_t shdr_status, shdr_add_status;
16149
16150 virt_addr = mboxq->sge_array->addr[0];
16151 /* The IOCTL status is embedded in the mailbox subheader. */
16152 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
16153 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16154 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16155
16156 if ((shdr_status || shdr_add_status) &&
16157 (shdr_status != STATUS_FCF_IN_USE))
16158 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16159 "2558 ADD_FCF_RECORD mailbox failed with "
16160 "status x%x add_status x%x\n",
16161 shdr_status, shdr_add_status);
16162
16163 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16164}
16165
16166/**
16167 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
16168 * @phba: pointer to lpfc hba data structure.
16169 * @fcf_record: pointer to the initialized fcf record to add.
16170 *
16171 * This routine is invoked to manually add a single FCF record. The caller
16172 * must pass a completely initialized FCF_Record. This routine takes
16173 * care of the nonembedded mailbox operations.
16174 **/
16175int
16176lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
16177{
16178 int rc = 0;
16179 LPFC_MBOXQ_t *mboxq;
16180 uint8_t *bytep;
16181 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040016182 struct lpfc_mbx_sge sge;
16183 uint32_t alloc_len, req_len;
16184 uint32_t fcfindex;
16185
16186 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16187 if (!mboxq) {
16188 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16189 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
16190 return -ENOMEM;
16191 }
16192
16193 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
16194 sizeof(uint32_t);
16195
16196 /* Allocate DMA memory and set up the non-embedded mailbox command */
16197 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16198 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
16199 req_len, LPFC_SLI4_MBX_NEMBED);
16200 if (alloc_len < req_len) {
16201 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16202 "2523 Allocated DMA memory size (x%x) is "
16203 "less than the requested DMA memory "
16204 "size (x%x)\n", alloc_len, req_len);
16205 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16206 return -ENOMEM;
16207 }
16208
16209 /*
16210 * Get the first SGE entry from the non-embedded DMA memory. This
16211 * routine only uses a single SGE.
16212 */
16213 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040016214 virt_addr = mboxq->sge_array->addr[0];
16215 /*
16216 * Configure the FCF record for FCFI 0. This is the driver's
16217 * hardcoded default and gets used in nonFIP mode.
16218 */
16219 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16220 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16221 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16222
16223 /*
16224 * Copy the fcf_index and the FCF Record Data. The data starts after
16225 * the FCoE header plus word10. The data copy needs to be endian
16226 * correct.
16227 */
16228 bytep += sizeof(uint32_t);
16229 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16230 mboxq->vport = phba->pport;
16231 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16232 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16233 if (rc == MBX_NOT_FINISHED) {
16234 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16235 "2515 ADD_FCF_RECORD mailbox failed with "
16236 "status 0x%x\n", rc);
16237 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16238 rc = -EIO;
16239 } else
16240 rc = 0;
16241
16242 return rc;
16243}
16244
16245/**
16246 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16247 * @phba: pointer to lpfc hba data structure.
16248 * @fcf_record: pointer to the fcf record to write the default data.
16249 * @fcf_index: FCF table entry index.
16250 *
16251 * This routine is invoked to build the driver's default FCF record. The
16252 * values used are hardcoded. This routine handles memory initialization.
16253 *
16254 **/
16255void
16256lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16257 struct fcf_record *fcf_record,
16258 uint16_t fcf_index)
16259{
16260 memset(fcf_record, 0, sizeof(struct fcf_record));
16261 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16262 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16263 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16264 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16265 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16266 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16267 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16268 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16269 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16270 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16271 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16272 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16273 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040016274 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040016275 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16276 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16277 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16278 /* Set the VLAN bit map */
16279 if (phba->valid_vlan) {
16280 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16281 = 1 << (phba->vlan_id % 8);
16282 }
16283}
16284
16285/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016286 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040016287 * @phba: pointer to lpfc hba data structure.
16288 * @fcf_index: FCF table entry offset.
16289 *
James Smart0c9ab6f2010-02-26 14:15:57 -050016290 * This routine is invoked to scan the entire FCF table by reading FCF
16291 * record and processing it one at a time starting from the @fcf_index
16292 * for initial FCF discovery or fast FCF failover rediscovery.
16293 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016294 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016295 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040016296 **/
16297int
James Smart0c9ab6f2010-02-26 14:15:57 -050016298lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040016299{
16300 int rc = 0, error;
16301 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040016302
James Smart32b97932009-07-19 10:01:21 -040016303 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050016304 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040016305 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16306 if (!mboxq) {
16307 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16308 "2000 Failed to allocate mbox for "
16309 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040016310 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050016311 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016312 }
James Smartecfd03c2010-02-12 14:41:27 -050016313 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050016314 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050016315 if (rc) {
16316 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050016317 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016318 }
James Smartecfd03c2010-02-12 14:41:27 -050016319 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040016320 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050016321 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040016322
16323 spin_lock_irq(&phba->hbalock);
16324 phba->hba_flag |= FCF_TS_INPROG;
16325 spin_unlock_irq(&phba->hbalock);
16326
James Smart6fb120a2009-05-22 14:52:59 -040016327 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050016328 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040016329 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050016330 else {
James Smart38b92ef2010-08-04 16:11:39 -040016331 /* Reset eligible FCF count for new scan */
16332 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040016333 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016334 error = 0;
James Smart32b97932009-07-19 10:01:21 -040016335 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016336fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040016337 if (error) {
16338 if (mboxq)
16339 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040016340 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040016341 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040016342 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040016343 spin_unlock_irq(&phba->hbalock);
16344 }
James Smart6fb120a2009-05-22 14:52:59 -040016345 return error;
16346}
James Smarta0c87cb2009-07-19 10:01:10 -040016347
16348/**
James Smarta93ff372010-10-22 11:06:08 -040016349 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050016350 * @phba: pointer to lpfc hba data structure.
16351 * @fcf_index: FCF table entry offset.
16352 *
16353 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040016354 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050016355 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016356 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016357 * otherwise.
16358 **/
16359int
16360lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16361{
16362 int rc = 0, error;
16363 LPFC_MBOXQ_t *mboxq;
16364
16365 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16366 if (!mboxq) {
16367 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16368 "2763 Failed to allocate mbox for "
16369 "READ_FCF cmd\n");
16370 error = -ENOMEM;
16371 goto fail_fcf_read;
16372 }
16373 /* Construct the read FCF record mailbox command */
16374 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16375 if (rc) {
16376 error = -EINVAL;
16377 goto fail_fcf_read;
16378 }
16379 /* Issue the mailbox command asynchronously */
16380 mboxq->vport = phba->pport;
16381 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16382 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16383 if (rc == MBX_NOT_FINISHED)
16384 error = -EIO;
16385 else
16386 error = 0;
16387
16388fail_fcf_read:
16389 if (error && mboxq)
16390 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16391 return error;
16392}
16393
16394/**
16395 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16396 * @phba: pointer to lpfc hba data structure.
16397 * @fcf_index: FCF table entry offset.
16398 *
16399 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040016400 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050016401 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016402 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016403 * otherwise.
16404 **/
16405int
16406lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16407{
16408 int rc = 0, error;
16409 LPFC_MBOXQ_t *mboxq;
16410
16411 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16412 if (!mboxq) {
16413 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16414 "2758 Failed to allocate mbox for "
16415 "READ_FCF cmd\n");
16416 error = -ENOMEM;
16417 goto fail_fcf_read;
16418 }
16419 /* Construct the read FCF record mailbox command */
16420 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16421 if (rc) {
16422 error = -EINVAL;
16423 goto fail_fcf_read;
16424 }
16425 /* Issue the mailbox command asynchronously */
16426 mboxq->vport = phba->pport;
16427 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16428 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16429 if (rc == MBX_NOT_FINISHED)
16430 error = -EIO;
16431 else
16432 error = 0;
16433
16434fail_fcf_read:
16435 if (error && mboxq)
16436 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16437 return error;
16438}
16439
16440/**
James Smartf5cb5302015-12-16 18:11:52 -050016441 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040016442 * phba pointer to the lpfc_hba struct for this port.
16443 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16444 * routine when the rr_bmask is empty. The FCF indecies are put into the
16445 * rr_bmask based on their priority level. Starting from the highest priority
16446 * to the lowest. The most likely FCF candidate will be in the highest
16447 * priority group. When this routine is called it searches the fcf_pri list for
16448 * next lowest priority group and repopulates the rr_bmask with only those
16449 * fcf_indexes.
16450 * returns:
16451 * 1=success 0=failure
16452 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016453static int
James Smart7d791df2011-07-22 18:37:52 -040016454lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16455{
16456 uint16_t next_fcf_pri;
16457 uint16_t last_index;
16458 struct lpfc_fcf_pri *fcf_pri;
16459 int rc;
16460 int ret = 0;
16461
16462 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16463 LPFC_SLI4_FCF_TBL_INDX_MAX);
16464 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16465 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050016466
16467 /* Verify the priority list has 2 or more entries */
16468 spin_lock_irq(&phba->hbalock);
16469 if (list_empty(&phba->fcf.fcf_pri_list) ||
16470 list_is_singular(&phba->fcf.fcf_pri_list)) {
16471 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040016472 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16473 "3061 Last IDX %d\n", last_index);
16474 return 0; /* Empty rr list */
16475 }
James Smart25626692013-03-01 16:36:54 -050016476 spin_unlock_irq(&phba->hbalock);
16477
James Smart7d791df2011-07-22 18:37:52 -040016478 next_fcf_pri = 0;
16479 /*
16480 * Clear the rr_bmask and set all of the bits that are at this
16481 * priority.
16482 */
16483 memset(phba->fcf.fcf_rr_bmask, 0,
16484 sizeof(*phba->fcf.fcf_rr_bmask));
16485 spin_lock_irq(&phba->hbalock);
16486 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16487 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16488 continue;
16489 /*
16490 * the 1st priority that has not FLOGI failed
16491 * will be the highest.
16492 */
16493 if (!next_fcf_pri)
16494 next_fcf_pri = fcf_pri->fcf_rec.priority;
16495 spin_unlock_irq(&phba->hbalock);
16496 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16497 rc = lpfc_sli4_fcf_rr_index_set(phba,
16498 fcf_pri->fcf_rec.fcf_index);
16499 if (rc)
16500 return 0;
16501 }
16502 spin_lock_irq(&phba->hbalock);
16503 }
16504 /*
16505 * if next_fcf_pri was not set above and the list is not empty then
16506 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070016507 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040016508 */
16509 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16510 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16511 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16512 /*
16513 * the 1st priority that has not FLOGI failed
16514 * will be the highest.
16515 */
16516 if (!next_fcf_pri)
16517 next_fcf_pri = fcf_pri->fcf_rec.priority;
16518 spin_unlock_irq(&phba->hbalock);
16519 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16520 rc = lpfc_sli4_fcf_rr_index_set(phba,
16521 fcf_pri->fcf_rec.fcf_index);
16522 if (rc)
16523 return 0;
16524 }
16525 spin_lock_irq(&phba->hbalock);
16526 }
16527 } else
16528 ret = 1;
16529 spin_unlock_irq(&phba->hbalock);
16530
16531 return ret;
16532}
16533/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016534 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16535 * @phba: pointer to lpfc hba data structure.
16536 *
16537 * This routine is to get the next eligible FCF record index in a round
16538 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040016539 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050016540 * shall be returned, otherwise, the next eligible FCF record's index
16541 * shall be returned.
16542 **/
16543uint16_t
16544lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16545{
16546 uint16_t next_fcf_index;
16547
James Smart421c6622013-01-03 15:44:16 -050016548initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040016549 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050016550 next_fcf_index = phba->fcf.current_rec.fcf_indx;
16551
James Smart7d791df2011-07-22 18:37:52 -040016552next_priority:
James Smart421c6622013-01-03 15:44:16 -050016553 /* Determine the next fcf index to check */
16554 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050016555 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16556 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040016557 next_fcf_index);
16558
James Smart0c9ab6f2010-02-26 14:15:57 -050016559 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040016560 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16561 /*
16562 * If we have wrapped then we need to clear the bits that
16563 * have been tested so that we can detect when we should
16564 * change the priority level.
16565 */
James Smart0c9ab6f2010-02-26 14:15:57 -050016566 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16567 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040016568 }
16569
James Smart0c9ab6f2010-02-26 14:15:57 -050016570
James Smart3804dc82010-07-14 15:31:37 -040016571 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040016572 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
16573 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
16574 /*
16575 * If next fcf index is not found check if there are lower
16576 * Priority level fcf's in the fcf_priority list.
16577 * Set up the rr_bmask with all of the avaiable fcf bits
16578 * at that level and continue the selection process.
16579 */
16580 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050016581 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040016582 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16583 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040016584 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
16585 return LPFC_FCOE_FCF_NEXT_NONE;
16586 else {
16587 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16588 "3063 Only FCF available idx %d, flag %x\n",
16589 next_fcf_index,
16590 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
16591 return next_fcf_index;
16592 }
James Smart3804dc82010-07-14 15:31:37 -040016593 }
16594
James Smart7d791df2011-07-22 18:37:52 -040016595 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
16596 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050016597 LPFC_FCF_FLOGI_FAILED) {
16598 if (list_is_singular(&phba->fcf.fcf_pri_list))
16599 return LPFC_FCOE_FCF_NEXT_NONE;
16600
James Smart7d791df2011-07-22 18:37:52 -040016601 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050016602 }
James Smart7d791df2011-07-22 18:37:52 -040016603
James Smart3804dc82010-07-14 15:31:37 -040016604 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016605 "2845 Get next roundrobin failover FCF (x%x)\n",
16606 next_fcf_index);
16607
James Smart0c9ab6f2010-02-26 14:15:57 -050016608 return next_fcf_index;
16609}
16610
16611/**
16612 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
16613 * @phba: pointer to lpfc hba data structure.
16614 *
16615 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016616 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016617 * does not go beyond the range of the driver allocated bmask dimension
16618 * before setting the bit.
16619 *
16620 * Returns 0 if the index bit successfully set, otherwise, it returns
16621 * -EINVAL.
16622 **/
16623int
16624lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
16625{
16626 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16627 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016628 "2610 FCF (x%x) reached driver's book "
16629 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016630 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16631 return -EINVAL;
16632 }
16633 /* Set the eligible FCF record index bmask */
16634 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16635
James Smart3804dc82010-07-14 15:31:37 -040016636 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016637 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040016638 "bmask\n", fcf_index);
16639
James Smart0c9ab6f2010-02-26 14:15:57 -050016640 return 0;
16641}
16642
16643/**
James Smart3804dc82010-07-14 15:31:37 -040016644 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050016645 * @phba: pointer to lpfc hba data structure.
16646 *
16647 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016648 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016649 * does not go beyond the range of the driver allocated bmask dimension
16650 * before clearing the bit.
16651 **/
16652void
16653lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
16654{
James Smart9a803a72013-09-06 12:17:56 -040016655 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050016656 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16657 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016658 "2762 FCF (x%x) reached driver's book "
16659 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016660 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16661 return;
16662 }
16663 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040016664 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040016665 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
16666 list) {
James Smart7d791df2011-07-22 18:37:52 -040016667 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
16668 list_del_init(&fcf_pri->list);
16669 break;
16670 }
16671 }
16672 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016673 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040016674
16675 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016676 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040016677 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050016678}
16679
16680/**
James Smartecfd03c2010-02-12 14:41:27 -050016681 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
16682 * @phba: pointer to lpfc hba data structure.
16683 *
16684 * This routine is the completion routine for the rediscover FCF table mailbox
16685 * command. If the mailbox command returned failure, it will try to stop the
16686 * FCF rediscover wait timer.
16687 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016688static void
James Smartecfd03c2010-02-12 14:41:27 -050016689lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16690{
16691 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16692 uint32_t shdr_status, shdr_add_status;
16693
16694 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16695
16696 shdr_status = bf_get(lpfc_mbox_hdr_status,
16697 &redisc_fcf->header.cfg_shdr.response);
16698 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16699 &redisc_fcf->header.cfg_shdr.response);
16700 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050016701 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050016702 "2746 Requesting for FCF rediscovery failed "
16703 "status x%x add_status x%x\n",
16704 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050016705 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050016706 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016707 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016708 spin_unlock_irq(&phba->hbalock);
16709 /*
16710 * CVL event triggered FCF rediscover request failed,
16711 * last resort to re-try current registered FCF entry.
16712 */
16713 lpfc_retry_pport_discovery(phba);
16714 } else {
16715 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016716 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016717 spin_unlock_irq(&phba->hbalock);
16718 /*
16719 * DEAD FCF event triggered FCF rediscover request
16720 * failed, last resort to fail over as a link down
16721 * to FCF registration.
16722 */
16723 lpfc_sli4_fcf_dead_failthrough(phba);
16724 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016725 } else {
16726 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016727 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050016728 /*
16729 * Start FCF rediscovery wait timer for pending FCF
16730 * before rescan FCF record table.
16731 */
16732 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050016733 }
James Smartecfd03c2010-02-12 14:41:27 -050016734
16735 mempool_free(mbox, phba->mbox_mem_pool);
16736}
16737
16738/**
James Smart3804dc82010-07-14 15:31:37 -040016739 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050016740 * @phba: pointer to lpfc hba data structure.
16741 *
16742 * This routine is invoked to request for rediscovery of the entire FCF table
16743 * by the port.
16744 **/
16745int
16746lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16747{
16748 LPFC_MBOXQ_t *mbox;
16749 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16750 int rc, length;
16751
James Smart0c9ab6f2010-02-26 14:15:57 -050016752 /* Cancel retry delay timers to all vports before FCF rediscover */
16753 lpfc_cancel_all_vport_retry_delay_timer(phba);
16754
James Smartecfd03c2010-02-12 14:41:27 -050016755 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16756 if (!mbox) {
16757 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16758 "2745 Failed to allocate mbox for "
16759 "requesting FCF rediscover.\n");
16760 return -ENOMEM;
16761 }
16762
16763 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16764 sizeof(struct lpfc_sli4_cfg_mhdr));
16765 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16766 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16767 length, LPFC_SLI4_MBX_EMBED);
16768
16769 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16770 /* Set count to 0 for invalidating the entire FCF database */
16771 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16772
16773 /* Issue the mailbox command asynchronously */
16774 mbox->vport = phba->pport;
16775 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16776 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16777
16778 if (rc == MBX_NOT_FINISHED) {
16779 mempool_free(mbox, phba->mbox_mem_pool);
16780 return -EIO;
16781 }
16782 return 0;
16783}
16784
16785/**
James Smartfc2b9892010-02-26 14:15:29 -050016786 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16787 * @phba: pointer to lpfc hba data structure.
16788 *
16789 * This function is the failover routine as a last resort to the FCF DEAD
16790 * event when driver failed to perform fast FCF failover.
16791 **/
16792void
16793lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16794{
16795 uint32_t link_state;
16796
16797 /*
16798 * Last resort as FCF DEAD event failover will treat this as
16799 * a link down, but save the link state because we don't want
16800 * it to be changed to Link Down unless it is already down.
16801 */
16802 link_state = phba->link_state;
16803 lpfc_linkdown(phba);
16804 phba->link_state = link_state;
16805
16806 /* Unregister FCF if no devices connected to it */
16807 lpfc_unregister_unused_fcf(phba);
16808}
16809
16810/**
James Smart026abb82011-12-13 13:20:45 -050016811 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016812 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050016813 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016814 *
James Smart026abb82011-12-13 13:20:45 -050016815 * This function gets SLI3 port configure region 23 data through memory dump
16816 * mailbox command. When it successfully retrieves data, the size of the data
16817 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040016818 **/
James Smart026abb82011-12-13 13:20:45 -050016819static uint32_t
16820lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040016821{
16822 LPFC_MBOXQ_t *pmb = NULL;
16823 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050016824 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016825 int rc;
16826
James Smart026abb82011-12-13 13:20:45 -050016827 if (!rgn23_data)
16828 return 0;
16829
James Smarta0c87cb2009-07-19 10:01:10 -040016830 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16831 if (!pmb) {
16832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016833 "2600 failed to allocate mailbox memory\n");
16834 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016835 }
16836 mb = &pmb->u.mb;
16837
James Smarta0c87cb2009-07-19 10:01:10 -040016838 do {
16839 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16840 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16841
16842 if (rc != MBX_SUCCESS) {
16843 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016844 "2601 failed to read config "
16845 "region 23, rc 0x%x Status 0x%x\n",
16846 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040016847 mb->un.varDmp.word_cnt = 0;
16848 }
16849 /*
16850 * dump mem may return a zero when finished or we got a
16851 * mailbox error, either way we are done.
16852 */
16853 if (mb->un.varDmp.word_cnt == 0)
16854 break;
16855 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16856 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16857
16858 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050016859 rgn23_data + offset,
16860 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040016861 offset += mb->un.varDmp.word_cnt;
16862 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16863
James Smart026abb82011-12-13 13:20:45 -050016864 mempool_free(pmb, phba->mbox_mem_pool);
16865 return offset;
16866}
16867
16868/**
16869 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16870 * @phba: pointer to lpfc hba data structure.
16871 * @rgn23_data: pointer to configure region 23 data.
16872 *
16873 * This function gets SLI4 port configure region 23 data through memory dump
16874 * mailbox command. When it successfully retrieves data, the size of the data
16875 * will be returned, otherwise, 0 will be returned.
16876 **/
16877static uint32_t
16878lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16879{
16880 LPFC_MBOXQ_t *mboxq = NULL;
16881 struct lpfc_dmabuf *mp = NULL;
16882 struct lpfc_mqe *mqe;
16883 uint32_t data_length = 0;
16884 int rc;
16885
16886 if (!rgn23_data)
16887 return 0;
16888
16889 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16890 if (!mboxq) {
16891 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16892 "3105 failed to allocate mailbox memory\n");
16893 return 0;
16894 }
16895
16896 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16897 goto out;
16898 mqe = &mboxq->u.mqe;
16899 mp = (struct lpfc_dmabuf *) mboxq->context1;
16900 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16901 if (rc)
16902 goto out;
16903 data_length = mqe->un.mb_words[5];
16904 if (data_length == 0)
16905 goto out;
16906 if (data_length > DMP_RGN23_SIZE) {
16907 data_length = 0;
16908 goto out;
16909 }
16910 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16911out:
16912 mempool_free(mboxq, phba->mbox_mem_pool);
16913 if (mp) {
16914 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16915 kfree(mp);
16916 }
16917 return data_length;
16918}
16919
16920/**
16921 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16922 * @phba: pointer to lpfc hba data structure.
16923 *
16924 * This function read region 23 and parse TLV for port status to
16925 * decide if the user disaled the port. If the TLV indicates the
16926 * port is disabled, the hba_flag is set accordingly.
16927 **/
16928void
16929lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16930{
16931 uint8_t *rgn23_data = NULL;
16932 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16933 uint32_t offset = 0;
16934
16935 /* Get adapter Region 23 data */
16936 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16937 if (!rgn23_data)
16938 goto out;
16939
16940 if (phba->sli_rev < LPFC_SLI_REV4)
16941 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16942 else {
16943 if_type = bf_get(lpfc_sli_intf_if_type,
16944 &phba->sli4_hba.sli_intf);
16945 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16946 goto out;
16947 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16948 }
James Smarta0c87cb2009-07-19 10:01:10 -040016949
16950 if (!data_size)
16951 goto out;
16952
16953 /* Check the region signature first */
16954 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16955 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16956 "2619 Config region 23 has bad signature\n");
16957 goto out;
16958 }
16959 offset += 4;
16960
16961 /* Check the data structure version */
16962 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16963 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16964 "2620 Config region 23 has bad version\n");
16965 goto out;
16966 }
16967 offset += 4;
16968
16969 /* Parse TLV entries in the region */
16970 while (offset < data_size) {
16971 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16972 break;
16973 /*
16974 * If the TLV is not driver specific TLV or driver id is
16975 * not linux driver id, skip the record.
16976 */
16977 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16978 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16979 (rgn23_data[offset + 3] != 0)) {
16980 offset += rgn23_data[offset + 1] * 4 + 4;
16981 continue;
16982 }
16983
16984 /* Driver found a driver specific TLV in the config region */
16985 sub_tlv_len = rgn23_data[offset + 1] * 4;
16986 offset += 4;
16987 tlv_offset = 0;
16988
16989 /*
16990 * Search for configured port state sub-TLV.
16991 */
16992 while ((offset < data_size) &&
16993 (tlv_offset < sub_tlv_len)) {
16994 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16995 offset += 4;
16996 tlv_offset += 4;
16997 break;
16998 }
16999 if (rgn23_data[offset] != PORT_STE_TYPE) {
17000 offset += rgn23_data[offset + 1] * 4 + 4;
17001 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
17002 continue;
17003 }
17004
17005 /* This HBA contains PORT_STE configured */
17006 if (!rgn23_data[offset + 2])
17007 phba->hba_flag |= LINK_DISABLED;
17008
17009 goto out;
17010 }
17011 }
James Smart026abb82011-12-13 13:20:45 -050017012
James Smarta0c87cb2009-07-19 10:01:10 -040017013out:
James Smarta0c87cb2009-07-19 10:01:10 -040017014 kfree(rgn23_data);
17015 return;
17016}
James Smart695a8142010-01-26 23:08:03 -050017017
17018/**
James Smart52d52442011-05-24 11:42:45 -040017019 * lpfc_wr_object - write an object to the firmware
17020 * @phba: HBA structure that indicates port to create a queue on.
17021 * @dmabuf_list: list of dmabufs to write to the port.
17022 * @size: the total byte value of the objects to write to the port.
17023 * @offset: the current offset to be used to start the transfer.
17024 *
17025 * This routine will create a wr_object mailbox command to send to the port.
17026 * the mailbox command will be constructed using the dma buffers described in
17027 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
17028 * BDEs that the imbedded mailbox can support. The @offset variable will be
17029 * used to indicate the starting offset of the transfer and will also return
17030 * the offset after the write object mailbox has completed. @size is used to
17031 * determine the end of the object and whether the eof bit should be set.
17032 *
17033 * Return 0 is successful and offset will contain the the new offset to use
17034 * for the next write.
17035 * Return negative value for error cases.
17036 **/
17037int
17038lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
17039 uint32_t size, uint32_t *offset)
17040{
17041 struct lpfc_mbx_wr_object *wr_object;
17042 LPFC_MBOXQ_t *mbox;
17043 int rc = 0, i = 0;
17044 uint32_t shdr_status, shdr_add_status;
17045 uint32_t mbox_tmo;
17046 union lpfc_sli4_cfg_shdr *shdr;
17047 struct lpfc_dmabuf *dmabuf;
17048 uint32_t written = 0;
17049
17050 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17051 if (!mbox)
17052 return -ENOMEM;
17053
17054 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17055 LPFC_MBOX_OPCODE_WRITE_OBJECT,
17056 sizeof(struct lpfc_mbx_wr_object) -
17057 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17058
17059 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
17060 wr_object->u.request.write_offset = *offset;
17061 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
17062 wr_object->u.request.object_name[0] =
17063 cpu_to_le32(wr_object->u.request.object_name[0]);
17064 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
17065 list_for_each_entry(dmabuf, dmabuf_list, list) {
17066 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
17067 break;
17068 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
17069 wr_object->u.request.bde[i].addrHigh =
17070 putPaddrHigh(dmabuf->phys);
17071 if (written + SLI4_PAGE_SIZE >= size) {
17072 wr_object->u.request.bde[i].tus.f.bdeSize =
17073 (size - written);
17074 written += (size - written);
17075 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
17076 } else {
17077 wr_object->u.request.bde[i].tus.f.bdeSize =
17078 SLI4_PAGE_SIZE;
17079 written += SLI4_PAGE_SIZE;
17080 }
17081 i++;
17082 }
17083 wr_object->u.request.bde_count = i;
17084 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
17085 if (!phba->sli4_hba.intr_enable)
17086 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17087 else {
James Smarta183a152011-10-10 21:32:43 -040017088 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040017089 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17090 }
17091 /* The IOCTL status is embedded in the mailbox subheader. */
17092 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
17093 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17094 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17095 if (rc != MBX_TIMEOUT)
17096 mempool_free(mbox, phba->mbox_mem_pool);
17097 if (shdr_status || shdr_add_status || rc) {
17098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17099 "3025 Write Object mailbox failed with "
17100 "status x%x add_status x%x, mbx status x%x\n",
17101 shdr_status, shdr_add_status, rc);
17102 rc = -ENXIO;
17103 } else
17104 *offset += wr_object->u.response.actual_write_length;
17105 return rc;
17106}
17107
17108/**
James Smart695a8142010-01-26 23:08:03 -050017109 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
17110 * @vport: pointer to vport data structure.
17111 *
17112 * This function iterate through the mailboxq and clean up all REG_LOGIN
17113 * and REG_VPI mailbox commands associated with the vport. This function
17114 * is called when driver want to restart discovery of the vport due to
17115 * a Clear Virtual Link event.
17116 **/
17117void
17118lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
17119{
17120 struct lpfc_hba *phba = vport->phba;
17121 LPFC_MBOXQ_t *mb, *nextmb;
17122 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040017123 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040017124 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040017125 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040017126 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050017127 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050017128
James Smartd439d282010-09-29 11:18:45 -040017129 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050017130 spin_lock_irq(&phba->hbalock);
17131 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
17132 if (mb->vport != vport)
17133 continue;
17134
17135 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17136 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17137 continue;
17138
James Smartd439d282010-09-29 11:18:45 -040017139 list_del(&mb->list);
17140 list_add_tail(&mb->list, &mbox_cmd_list);
17141 }
17142 /* Clean up active mailbox command with the vport */
17143 mb = phba->sli.mbox_active;
17144 if (mb && (mb->vport == vport)) {
17145 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
17146 (mb->u.mb.mbxCommand == MBX_REG_VPI))
17147 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17148 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17149 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
17150 /* Put reference count for delayed processing */
17151 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
17152 /* Unregister the RPI when mailbox complete */
17153 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17154 }
17155 }
James Smart63e801c2010-11-20 23:14:19 -050017156 /* Cleanup any mailbox completions which are not yet processed */
17157 do {
17158 restart_loop = 0;
17159 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
17160 /*
17161 * If this mailox is already processed or it is
17162 * for another vport ignore it.
17163 */
17164 if ((mb->vport != vport) ||
17165 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
17166 continue;
17167
17168 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17169 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17170 continue;
17171
17172 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17173 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17174 ndlp = (struct lpfc_nodelist *)mb->context2;
17175 /* Unregister the RPI when mailbox complete */
17176 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17177 restart_loop = 1;
17178 spin_unlock_irq(&phba->hbalock);
17179 spin_lock(shost->host_lock);
17180 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17181 spin_unlock(shost->host_lock);
17182 spin_lock_irq(&phba->hbalock);
17183 break;
17184 }
17185 }
17186 } while (restart_loop);
17187
James Smartd439d282010-09-29 11:18:45 -040017188 spin_unlock_irq(&phba->hbalock);
17189
17190 /* Release the cleaned-up mailbox commands */
17191 while (!list_empty(&mbox_cmd_list)) {
17192 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050017193 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17194 mp = (struct lpfc_dmabuf *) (mb->context1);
17195 if (mp) {
17196 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
17197 kfree(mp);
17198 }
James Smart78730cf2010-04-06 15:06:30 -040017199 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040017200 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040017201 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017202 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040017203 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017204 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040017205 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040017206 }
James Smart695a8142010-01-26 23:08:03 -050017207 }
James Smart695a8142010-01-26 23:08:03 -050017208 mempool_free(mb, phba->mbox_mem_pool);
17209 }
James Smartd439d282010-09-29 11:18:45 -040017210
17211 /* Release the ndlp with the cleaned-up active mailbox command */
17212 if (act_mbx_ndlp) {
17213 spin_lock(shost->host_lock);
17214 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17215 spin_unlock(shost->host_lock);
17216 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050017217 }
James Smart695a8142010-01-26 23:08:03 -050017218}
17219
James Smart2a9bf3d2010-06-07 15:24:45 -040017220/**
17221 * lpfc_drain_txq - Drain the txq
17222 * @phba: Pointer to HBA context object.
17223 *
17224 * This function attempt to submit IOCBs on the txq
17225 * to the adapter. For SLI4 adapters, the txq contains
17226 * ELS IOCBs that have been deferred because the there
17227 * are no SGLs. This congestion can occur with large
17228 * vport counts during node discovery.
17229 **/
17230
17231uint32_t
17232lpfc_drain_txq(struct lpfc_hba *phba)
17233{
17234 LIST_HEAD(completions);
17235 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
Daeseok Youn2e706372014-02-21 09:03:32 +090017236 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040017237 unsigned long iflags = 0;
17238 char *fail_msg = NULL;
17239 struct lpfc_sglq *sglq;
James Smart2f077842016-12-19 15:07:29 -080017240 union lpfc_wqe128 wqe128;
17241 union lpfc_wqe *wqe = (union lpfc_wqe *) &wqe128;
James Smarta2fc4aef2014-09-03 12:57:55 -040017242 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040017243
James Smart398d81c2013-05-31 17:04:19 -040017244 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050017245 list_for_each_entry(piocbq, &pring->txq, list) {
17246 txq_cnt++;
17247 }
17248
17249 if (txq_cnt > pring->txq_max)
17250 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017251
James Smart398d81c2013-05-31 17:04:19 -040017252 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017253
James Smart0e9bb8d2013-03-01 16:35:12 -050017254 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040017255 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017256
James Smart19ca7602010-11-20 23:11:55 -050017257 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040017258 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040017259 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040017260 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17261 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050017262 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040017263 break;
17264 }
James Smart19ca7602010-11-20 23:11:55 -050017265 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040017266 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050017267 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040017268 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017269 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040017270 }
James Smart0e9bb8d2013-03-01 16:35:12 -050017271 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040017272
17273 /* The xri and iocb resources secured,
17274 * attempt to issue request
17275 */
James Smart6d368e52011-05-24 11:44:12 -040017276 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040017277 piocbq->sli4_xritag = sglq->sli4_xritag;
17278 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17279 fail_msg = "to convert bpl to sgl";
James Smart2f077842016-12-19 15:07:29 -080017280 else if (lpfc_sli4_iocb2wqe(phba, piocbq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040017281 fail_msg = "to convert iocb to wqe";
James Smart2f077842016-12-19 15:07:29 -080017282 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart2a9bf3d2010-06-07 15:24:45 -040017283 fail_msg = " - Wq is full";
17284 else
17285 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17286
17287 if (fail_msg) {
17288 /* Failed means we can't issue and need to cancel */
17289 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17290 "2822 IOCB failed %s iotag 0x%x "
17291 "xri 0x%x\n",
17292 fail_msg,
17293 piocbq->iotag, piocbq->sli4_xritag);
17294 list_add_tail(&piocbq->list, &completions);
17295 }
James Smart398d81c2013-05-31 17:04:19 -040017296 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017297 }
17298
James Smart2a9bf3d2010-06-07 15:24:45 -040017299 /* Cancel all the IOCBs that cannot be issued */
17300 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17301 IOERR_SLI_ABORTED);
17302
James Smart0e9bb8d2013-03-01 16:35:12 -050017303 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017304}