blob: 6df06e716da1274b5cfc7397c1c6af573e40e61e [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"
dea31012005-04-17 16:05:31 -050050
51/* There are only four IOCB completion types. */
52typedef enum _lpfc_iocb_type {
53 LPFC_UNKNOWN_IOCB,
54 LPFC_UNSOL_IOCB,
55 LPFC_SOL_IOCB,
56 LPFC_ABORT_IOCB
57} lpfc_iocb_type;
58
James Smart4f774512009-05-22 14:52:35 -040059
60/* Provide function prototypes local to this module. */
61static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
62 uint32_t);
63static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040064 uint8_t *, uint32_t *);
65static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
66 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040067static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
68 struct hbq_dmabuf *);
James Smart05580562011-05-24 11:40:48 -040069static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
70 struct lpfc_cqe *);
James Smart8a9d2e82012-05-09 21:16:12 -040071static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
72 int);
James Smartba20c852012-08-03 12:36:52 -040073static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
74 uint32_t);
James Smarte8d3c3b2013-10-10 12:21:30 -040075static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
76static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart05580562011-05-24 11:40:48 -040077
James Smart4f774512009-05-22 14:52:35 -040078static IOCB_t *
79lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
80{
81 return &iocbq->iocb;
82}
83
84/**
85 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
86 * @q: The Work Queue to operate on.
87 * @wqe: The work Queue Entry to put on the Work queue.
88 *
89 * This routine will copy the contents of @wqe to the next available entry on
90 * the @q. This function will then ring the Work Queue Doorbell to signal the
91 * HBA to start processing the Work Queue Entry. This function returns 0 if
92 * successful. If no entries are available on @q then this function will return
93 * -ENOMEM.
94 * The caller is expected to hold the hbalock when calling this routine.
95 **/
96static uint32_t
97lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
98{
James Smart2e90f4b2011-12-13 13:22:37 -050099 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400100 struct lpfc_register doorbell;
101 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400102 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400103
James Smart2e90f4b2011-12-13 13:22:37 -0500104 /* sanity check on queue memory */
105 if (unlikely(!q))
106 return -ENOMEM;
107 temp_wqe = q->qe[q->host_index].wqe;
108
James Smart4f774512009-05-22 14:52:35 -0400109 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400110 idx = ((q->host_index + 1) % q->entry_count);
111 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400112 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400113 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400114 }
115 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400116 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500117 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400118 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smarte0d35e32018-01-30 15:58:45 -0800119 else
120 bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
James Smartfedd3b72011-02-16 12:39:24 -0500121 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
122 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400123 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
James Smart01347f52016-12-19 15:07:30 -0800124 /* ensure WQE bcopy flushed before doorbell write */
125 wmb();
James Smart4f774512009-05-22 14:52:35 -0400126
127 /* Update the host index before invoking device */
128 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400129
130 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400131
132 /* Ring Doorbell */
133 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500134 if (q->db_format == LPFC_DB_LIST_FORMAT) {
135 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
136 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
137 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
138 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
139 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
140 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
141 } else {
142 return -EINVAL;
143 }
144 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400145
146 return 0;
147}
148
149/**
150 * lpfc_sli4_wq_release - Updates internal hba index for WQ
151 * @q: The Work Queue to operate on.
152 * @index: The index to advance the hba index to.
153 *
154 * This routine will update the HBA index of a queue to reflect consumption of
155 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
156 * an entry the host calls this function to update the queue's internal
157 * pointers. This routine returns the number of entries that were consumed by
158 * the HBA.
159 **/
160static uint32_t
161lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
162{
163 uint32_t released = 0;
164
James Smart2e90f4b2011-12-13 13:22:37 -0500165 /* sanity check on queue memory */
166 if (unlikely(!q))
167 return 0;
168
James Smart4f774512009-05-22 14:52:35 -0400169 if (q->hba_index == index)
170 return 0;
171 do {
172 q->hba_index = ((q->hba_index + 1) % q->entry_count);
173 released++;
174 } while (q->hba_index != index);
175 return released;
176}
177
178/**
179 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
180 * @q: The Mailbox Queue to operate on.
181 * @wqe: The Mailbox Queue Entry to put on the Work queue.
182 *
183 * This routine will copy the contents of @mqe to the next available entry on
184 * the @q. This function will then ring the Work Queue Doorbell to signal the
185 * HBA to start processing the Work Queue Entry. This function returns 0 if
186 * successful. If no entries are available on @q then this function will return
187 * -ENOMEM.
188 * The caller is expected to hold the hbalock when calling this routine.
189 **/
190static uint32_t
191lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
192{
James Smart2e90f4b2011-12-13 13:22:37 -0500193 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400194 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400195
James Smart2e90f4b2011-12-13 13:22:37 -0500196 /* sanity check on queue memory */
197 if (unlikely(!q))
198 return -ENOMEM;
199 temp_mqe = q->qe[q->host_index].mqe;
200
James Smart4f774512009-05-22 14:52:35 -0400201 /* If the host has not yet processed the next entry then we are done */
202 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
203 return -ENOMEM;
204 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
205 /* Save off the mailbox pointer for completion */
206 q->phba->mbox = (MAILBOX_t *)temp_mqe;
207
208 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400209 q->host_index = ((q->host_index + 1) % q->entry_count);
210
211 /* Ring Doorbell */
212 doorbell.word0 = 0;
213 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
214 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
215 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400216 return 0;
217}
218
219/**
220 * lpfc_sli4_mq_release - Updates internal hba index for MQ
221 * @q: The Mailbox Queue to operate on.
222 *
223 * This routine will update the HBA index of a queue to reflect consumption of
224 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
225 * an entry the host calls this function to update the queue's internal
226 * pointers. This routine returns the number of entries that were consumed by
227 * the HBA.
228 **/
229static uint32_t
230lpfc_sli4_mq_release(struct lpfc_queue *q)
231{
James Smart2e90f4b2011-12-13 13:22:37 -0500232 /* sanity check on queue memory */
233 if (unlikely(!q))
234 return 0;
235
James Smart4f774512009-05-22 14:52:35 -0400236 /* Clear the mailbox pointer for completion */
237 q->phba->mbox = NULL;
238 q->hba_index = ((q->hba_index + 1) % q->entry_count);
239 return 1;
240}
241
242/**
243 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
244 * @q: The Event Queue to get the first valid EQE from
245 *
246 * This routine will get the first valid Event Queue Entry from @q, update
247 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
248 * the Queue (no more work to do), or the Queue is full of EQEs that have been
249 * processed, but not popped back to the HBA then this routine will return NULL.
250 **/
251static struct lpfc_eqe *
252lpfc_sli4_eq_get(struct lpfc_queue *q)
253{
James Smart2e90f4b2011-12-13 13:22:37 -0500254 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400255 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500256
257 /* sanity check on queue memory */
258 if (unlikely(!q))
259 return NULL;
260 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400261
262 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400263 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400264 return NULL;
265 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400266 idx = ((q->hba_index + 1) % q->entry_count);
267 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400268 return NULL;
269
James Smart027140e2012-08-03 12:35:44 -0400270 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400271
272 /*
273 * insert barrier for instruction interlock : data from the hardware
274 * must have the valid bit checked before it can be copied and acted
275 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
276 * instructions allowing action on content before valid bit checked,
277 * add barrier here as well. May not be needed as "content" is a
278 * single 32-bit entity here (vs multi word structure for cq's).
279 */
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
390 * upon. Speculative instructions were allowing a bcopy at the start
391 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
392 * after our return, to copy data before the valid bit check above
393 * was done. As such, some of the copied data was stale. The barrier
394 * ensures the check is before any data is copied.
395 */
396 mb();
James Smart4f774512009-05-22 14:52:35 -0400397 return cqe;
398}
399
400/**
401 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
402 * @q: The Completion Queue that the host has completed processing for.
403 * @arm: Indicates whether the host wants to arms this CQ.
404 *
405 * This routine will mark all Completion queue entries on @q, from the last
406 * known completed entry to the last entry that was processed, as completed
407 * by clearing the valid bit for each completion queue entry. Then it will
408 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
409 * The internal host index in the @q will be updated by this routine to indicate
410 * that the host has finished processing the entries. The @arm parameter
411 * indicates that the queue should be rearmed when ringing the doorbell.
412 *
413 * This function will return the number of CQEs that were released.
414 **/
415uint32_t
416lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
417{
418 uint32_t released = 0;
419 struct lpfc_cqe *temp_qe;
420 struct lpfc_register doorbell;
421
James Smart2e90f4b2011-12-13 13:22:37 -0500422 /* sanity check on queue memory */
423 if (unlikely(!q))
424 return 0;
James Smart4f774512009-05-22 14:52:35 -0400425 /* while there are valid entries */
426 while (q->hba_index != q->host_index) {
427 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400428 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400429 released++;
430 q->host_index = ((q->host_index + 1) % q->entry_count);
431 }
432 if (unlikely(released == 0 && !arm))
433 return 0;
434
435 /* ring doorbell for number popped */
436 doorbell.word0 = 0;
437 if (arm)
438 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
439 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
440 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500441 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
442 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
443 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400444 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
445 return released;
446}
447
448/**
449 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
450 * @q: The Header Receive Queue to operate on.
451 * @wqe: The Receive Queue Entry to put on the Receive queue.
452 *
453 * This routine will copy the contents of @wqe to the next available entry on
454 * the @q. This function will then ring the Receive Queue Doorbell to signal the
455 * HBA to start processing the Receive Queue Entry. This function returns the
456 * index that the rqe was copied to if successful. If no entries are available
457 * on @q then this function will return -ENOMEM.
458 * The caller is expected to hold the hbalock when calling this routine.
459 **/
460static int
461lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
462 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
463{
James Smart2e90f4b2011-12-13 13:22:37 -0500464 struct lpfc_rqe *temp_hrqe;
465 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400466 struct lpfc_register doorbell;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500467 int put_index;
James Smart4f774512009-05-22 14:52:35 -0400468
James Smart2e90f4b2011-12-13 13:22:37 -0500469 /* sanity check on queue memory */
470 if (unlikely(!hq) || unlikely(!dq))
471 return -ENOMEM;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500472 put_index = hq->host_index;
James Smart2e90f4b2011-12-13 13:22:37 -0500473 temp_hrqe = hq->qe[hq->host_index].rqe;
474 temp_drqe = dq->qe[dq->host_index].rqe;
475
James Smart4f774512009-05-22 14:52:35 -0400476 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
477 return -EINVAL;
478 if (hq->host_index != dq->host_index)
479 return -EINVAL;
480 /* If the host has not yet processed the next entry then we are done */
481 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
482 return -EBUSY;
483 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
484 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
485
486 /* Update the host index to point to the next slot */
487 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
488 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
489
490 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400491 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400492 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500493 if (hq->db_format == LPFC_DB_RING_FORMAT) {
494 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
495 hq->entry_repost);
496 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
497 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
498 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
499 hq->entry_repost);
500 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
501 hq->host_index);
502 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
503 } else {
504 return -EINVAL;
505 }
506 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400507 }
508 return put_index;
509}
510
511/**
512 * lpfc_sli4_rq_release - Updates internal hba index for RQ
513 * @q: The Header Receive Queue to operate on.
514 *
515 * This routine will update the HBA index of a queue to reflect consumption of
516 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
517 * consumed an entry the host calls this function to update the queue's
518 * internal pointers. This routine returns the number of entries that were
519 * consumed by the HBA.
520 **/
521static uint32_t
522lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
523{
James Smart2e90f4b2011-12-13 13:22:37 -0500524 /* sanity check on queue memory */
525 if (unlikely(!hq) || unlikely(!dq))
526 return 0;
527
James Smart4f774512009-05-22 14:52:35 -0400528 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
529 return 0;
530 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
531 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
532 return 1;
533}
534
James Smarte59058c2008-08-24 21:49:00 -0400535/**
James Smart3621a712009-04-06 18:47:14 -0400536 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400537 * @phba: Pointer to HBA context object.
538 * @pring: Pointer to driver SLI ring object.
539 *
540 * This function returns pointer to next command iocb entry
541 * in the command ring. The caller must hold hbalock to prevent
542 * other threads consume the next command iocb.
543 * SLI-2/SLI-3 provide different sized iocbs.
544 **/
James Smarted957682007-06-17 19:56:37 -0500545static inline IOCB_t *
546lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
547{
James Smart7e56aa22012-08-03 12:35:34 -0400548 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
549 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500550}
551
James Smarte59058c2008-08-24 21:49:00 -0400552/**
James Smart3621a712009-04-06 18:47:14 -0400553 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400554 * @phba: Pointer to HBA context object.
555 * @pring: Pointer to driver SLI ring object.
556 *
557 * This function returns pointer to next response iocb entry
558 * in the response ring. The caller must hold hbalock to make sure
559 * that no other thread consume the next response iocb.
560 * SLI-2/SLI-3 provide different sized iocbs.
561 **/
James Smarted957682007-06-17 19:56:37 -0500562static inline IOCB_t *
563lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
564{
James Smart7e56aa22012-08-03 12:35:34 -0400565 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
566 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500567}
568
James Smarte59058c2008-08-24 21:49:00 -0400569/**
James Smart3621a712009-04-06 18:47:14 -0400570 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400571 * @phba: Pointer to HBA context object.
572 *
573 * This function is called with hbalock held. This function
574 * allocates a new driver iocb object from the iocb pool. If the
575 * allocation is successful, it returns pointer to the newly
576 * allocated iocb object else it returns NULL.
577 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400578struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500579__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400580{
581 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
582 struct lpfc_iocbq * iocbq = NULL;
583
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100584 lockdep_assert_held(&phba->hbalock);
585
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400586 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400587 if (iocbq)
588 phba->iocb_cnt++;
589 if (phba->iocb_cnt > phba->iocb_max)
590 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400591 return iocbq;
592}
593
James Smarte59058c2008-08-24 21:49:00 -0400594/**
James Smartda0436e2009-05-22 14:51:39 -0400595 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
596 * @phba: Pointer to HBA context object.
597 * @xritag: XRI value.
598 *
599 * This function clears the sglq pointer from the array of acive
600 * sglq's. The xritag that is passed in is used to index into the
601 * array. Before the xritag can be used it needs to be adjusted
602 * by subtracting the xribase.
603 *
604 * Returns sglq ponter = success, NULL = Failure.
605 **/
606static struct lpfc_sglq *
607__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
608{
James Smartda0436e2009-05-22 14:51:39 -0400609 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400610
611 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
612 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400613 return sglq;
614}
615
616/**
617 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
618 * @phba: Pointer to HBA context object.
619 * @xritag: XRI value.
620 *
621 * This function returns the sglq pointer from the array of acive
622 * sglq's. The xritag that is passed in is used to index into the
623 * array. Before the xritag can be used it needs to be adjusted
624 * by subtracting the xribase.
625 *
626 * Returns sglq ponter = success, NULL = Failure.
627 **/
James Smart0f65ff62010-02-26 14:14:23 -0500628struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400629__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
630{
James Smartda0436e2009-05-22 14:51:39 -0400631 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400632
633 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400634 return sglq;
635}
636
637/**
James Smart1151e3e2011-02-16 12:39:35 -0500638 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500639 * @phba: Pointer to HBA context object.
640 * @xritag: xri used in this exchange.
641 * @rrq: The RRQ to be cleared.
642 *
James Smart19ca7602010-11-20 23:11:55 -0500643 **/
James Smart1151e3e2011-02-16 12:39:35 -0500644void
645lpfc_clr_rrq_active(struct lpfc_hba *phba,
646 uint16_t xritag,
647 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500648{
James Smart1151e3e2011-02-16 12:39:35 -0500649 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500650
James Smart1151e3e2011-02-16 12:39:35 -0500651 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
652 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500653
654 /* The target DID could have been swapped (cable swap)
655 * we should use the ndlp from the findnode if it is
656 * available.
657 */
James Smart1151e3e2011-02-16 12:39:35 -0500658 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500659 ndlp = rrq->ndlp;
660
James Smart1151e3e2011-02-16 12:39:35 -0500661 if (!ndlp)
662 goto out;
663
James Smartcff261f2013-12-17 20:29:47 -0500664 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500665 rrq->send_rrq = 0;
666 rrq->xritag = 0;
667 rrq->rrq_stop_time = 0;
668 }
James Smart1151e3e2011-02-16 12:39:35 -0500669out:
James Smart19ca7602010-11-20 23:11:55 -0500670 mempool_free(rrq, phba->rrq_pool);
671}
672
673/**
674 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
675 * @phba: Pointer to HBA context object.
676 *
677 * This function is called with hbalock held. This function
678 * Checks if stop_time (ratov from setting rrq active) has
679 * been reached, if it has and the send_rrq flag is set then
680 * it will call lpfc_send_rrq. If the send_rrq flag is not set
681 * then it will just call the routine to clear the rrq and
682 * free the rrq resource.
683 * The timer is set to the next rrq that is going to expire before
684 * leaving the routine.
685 *
686 **/
687void
688lpfc_handle_rrq_active(struct lpfc_hba *phba)
689{
690 struct lpfc_node_rrq *rrq;
691 struct lpfc_node_rrq *nextrrq;
692 unsigned long next_time;
693 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500694 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500695
696 spin_lock_irqsave(&phba->hbalock, iflags);
697 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400698 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500699 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500700 &phba->active_rrq_list, list) {
701 if (time_after(jiffies, rrq->rrq_stop_time))
702 list_move(&rrq->list, &send_rrq);
703 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500704 next_time = rrq->rrq_stop_time;
705 }
706 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500707 if ((!list_empty(&phba->active_rrq_list)) &&
708 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500709 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500710 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
711 list_del(&rrq->list);
712 if (!rrq->send_rrq)
713 /* this call will free the rrq */
714 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
715 else if (lpfc_send_rrq(phba, rrq)) {
716 /* if we send the rrq then the completion handler
717 * will clear the bit in the xribitmap.
718 */
719 lpfc_clr_rrq_active(phba, rrq->xritag,
720 rrq);
721 }
722 }
James Smart19ca7602010-11-20 23:11:55 -0500723}
724
725/**
726 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
727 * @vport: Pointer to vport context object.
728 * @xri: The xri used in the exchange.
729 * @did: The targets DID for this exchange.
730 *
731 * returns NULL = rrq not found in the phba->active_rrq_list.
732 * rrq = rrq for this xri and target.
733 **/
734struct lpfc_node_rrq *
735lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
736{
737 struct lpfc_hba *phba = vport->phba;
738 struct lpfc_node_rrq *rrq;
739 struct lpfc_node_rrq *nextrrq;
740 unsigned long iflags;
741
742 if (phba->sli_rev != LPFC_SLI_REV4)
743 return NULL;
744 spin_lock_irqsave(&phba->hbalock, iflags);
745 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
746 if (rrq->vport == vport && rrq->xritag == xri &&
747 rrq->nlp_DID == did){
748 list_del(&rrq->list);
749 spin_unlock_irqrestore(&phba->hbalock, iflags);
750 return rrq;
751 }
752 }
753 spin_unlock_irqrestore(&phba->hbalock, iflags);
754 return NULL;
755}
756
757/**
758 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
759 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500760 * @ndlp: Pointer to the lpfc_node_list structure.
761 * If ndlp is NULL Remove all active RRQs for this vport from the
762 * phba->active_rrq_list and clear the rrq.
763 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500764 **/
765void
James Smart1151e3e2011-02-16 12:39:35 -0500766lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500767
768{
769 struct lpfc_hba *phba = vport->phba;
770 struct lpfc_node_rrq *rrq;
771 struct lpfc_node_rrq *nextrrq;
772 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500773 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500774
775 if (phba->sli_rev != LPFC_SLI_REV4)
776 return;
James Smart1151e3e2011-02-16 12:39:35 -0500777 if (!ndlp) {
778 lpfc_sli4_vport_delete_els_xri_aborted(vport);
779 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500780 }
James Smart1151e3e2011-02-16 12:39:35 -0500781 spin_lock_irqsave(&phba->hbalock, iflags);
782 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
783 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
784 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500785 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500786
787 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
788 list_del(&rrq->list);
789 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
790 }
James Smart19ca7602010-11-20 23:11:55 -0500791}
792
793/**
James Smart1151e3e2011-02-16 12:39:35 -0500794 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500795 * @phba: Pointer to HBA context object.
796 * @ndlp: Targets nodelist pointer for this exchange.
797 * @xritag the xri in the bitmap to test.
798 *
799 * This function is called with hbalock held. This function
800 * returns 0 = rrq not active for this xri
801 * 1 = rrq is valid for this xri.
802 **/
James Smart1151e3e2011-02-16 12:39:35 -0500803int
804lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500805 uint16_t xritag)
806{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100807 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -0500808 if (!ndlp)
809 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500810 if (!ndlp->active_rrqs_xri_bitmap)
811 return 0;
812 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500813 return 1;
814 else
815 return 0;
816}
817
818/**
819 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
820 * @phba: Pointer to HBA context object.
821 * @ndlp: nodelist pointer for this target.
822 * @xritag: xri used in this exchange.
823 * @rxid: Remote Exchange ID.
824 * @send_rrq: Flag used to determine if we should send rrq els cmd.
825 *
826 * This function takes the hbalock.
827 * The active bit is always set in the active rrq xri_bitmap even
828 * if there is no slot avaiable for the other rrq information.
829 *
830 * returns 0 rrq actived for this xri
831 * < 0 No memory or invalid ndlp.
832 **/
833int
834lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500835 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500836{
James Smart19ca7602010-11-20 23:11:55 -0500837 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500838 struct lpfc_node_rrq *rrq;
839 int empty;
840
841 if (!ndlp)
842 return -EINVAL;
843
844 if (!phba->cfg_enable_rrq)
845 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500846
847 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500848 if (phba->pport->load_flag & FC_UNLOADING) {
849 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
850 goto out;
851 }
852
853 /*
854 * set the active bit even if there is no mem available.
855 */
856 if (NLP_CHK_FREE_REQ(ndlp))
857 goto out;
858
859 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
860 goto out;
861
James Smartcff261f2013-12-17 20:29:47 -0500862 if (!ndlp->active_rrqs_xri_bitmap)
863 goto out;
864
865 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -0500866 goto out;
867
James Smart19ca7602010-11-20 23:11:55 -0500868 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500869 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
870 if (!rrq) {
871 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
872 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
873 " DID:0x%x Send:%d\n",
874 xritag, rxid, ndlp->nlp_DID, send_rrq);
875 return -EINVAL;
876 }
James Smarte5771b42013-03-01 16:37:14 -0500877 if (phba->cfg_enable_rrq == 1)
878 rrq->send_rrq = send_rrq;
879 else
880 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500881 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -0400882 rrq->rrq_stop_time = jiffies +
883 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -0500884 rrq->ndlp = ndlp;
885 rrq->nlp_DID = ndlp->nlp_DID;
886 rrq->vport = ndlp->vport;
887 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500888 spin_lock_irqsave(&phba->hbalock, iflags);
889 empty = list_empty(&phba->active_rrq_list);
890 list_add_tail(&rrq->list, &phba->active_rrq_list);
891 phba->hba_flag |= HBA_RRQ_ACTIVE;
892 if (empty)
893 lpfc_worker_wake_up(phba);
894 spin_unlock_irqrestore(&phba->hbalock, iflags);
895 return 0;
896out:
897 spin_unlock_irqrestore(&phba->hbalock, iflags);
898 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
899 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
900 " DID:0x%x Send:%d\n",
901 xritag, rxid, ndlp->nlp_DID, send_rrq);
902 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500903}
904
905/**
James Smartda0436e2009-05-22 14:51:39 -0400906 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
907 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500908 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400909 *
James Smartdafe8ce2014-09-03 12:56:40 -0400910 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400911 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400912 * list is not empty then it is successful, it returns pointer to the newly
913 * allocated sglq object else it returns NULL.
914 **/
915static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500916__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400917{
918 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
919 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500920 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500921 struct lpfc_scsi_buf *lpfc_cmd;
922 struct lpfc_nodelist *ndlp;
923 int found = 0;
924
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100925 lockdep_assert_held(&phba->hbalock);
926
James Smart19ca7602010-11-20 23:11:55 -0500927 if (piocbq->iocb_flag & LPFC_IO_FCP) {
928 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
929 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500930 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -0400931 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -0500932 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -0400933 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
934 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
935 ndlp = NULL;
936 else
937 ndlp = piocbq->context_un.ndlp;
938 } else {
James Smart19ca7602010-11-20 23:11:55 -0500939 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -0400940 }
James Smart19ca7602010-11-20 23:11:55 -0500941
James Smartda0436e2009-05-22 14:51:39 -0400942 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500943 start_sglq = sglq;
944 while (!found) {
945 if (!sglq)
946 return NULL;
James Smartee0f4fe2012-05-09 21:19:14 -0400947 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500948 /* This xri has an rrq outstanding for this DID.
949 * put it back in the list and get another xri.
950 */
951 list_add_tail(&sglq->list, lpfc_sgl_list);
952 sglq = NULL;
953 list_remove_head(lpfc_sgl_list, sglq,
954 struct lpfc_sglq, list);
955 if (sglq == start_sglq) {
956 sglq = NULL;
957 break;
958 } else
959 continue;
960 }
961 sglq->ndlp = ndlp;
962 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400963 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500964 sglq->state = SGL_ALLOCATED;
965 }
James Smartda0436e2009-05-22 14:51:39 -0400966 return sglq;
967}
968
969/**
James Smart3621a712009-04-06 18:47:14 -0400970 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400971 * @phba: Pointer to HBA context object.
972 *
973 * This function is called with no lock held. This function
974 * allocates a new driver iocb object from the iocb pool. If the
975 * allocation is successful, it returns pointer to the newly
976 * allocated iocb object else it returns NULL.
977 **/
James Smart2e0fef82007-06-17 19:56:36 -0500978struct lpfc_iocbq *
979lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500980{
James Smart2e0fef82007-06-17 19:56:36 -0500981 struct lpfc_iocbq * iocbq = NULL;
982 unsigned long iflags;
983
984 spin_lock_irqsave(&phba->hbalock, iflags);
985 iocbq = __lpfc_sli_get_iocbq(phba);
986 spin_unlock_irqrestore(&phba->hbalock, iflags);
987 return iocbq;
988}
989
James Smarte59058c2008-08-24 21:49:00 -0400990/**
James Smart4f774512009-05-22 14:52:35 -0400991 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
992 * @phba: Pointer to HBA context object.
993 * @iocbq: Pointer to driver iocb object.
994 *
995 * This function is called with hbalock held to release driver
996 * iocb object to the iocb pool. The iotag in the iocb object
997 * does not change for each use of the iocb object. This function
998 * clears all other fields of the iocb object when it is freed.
999 * The sqlq structure that holds the xritag and phys and virtual
1000 * mappings for the scatter gather list is retrieved from the
1001 * active array of sglq. The get of the sglq pointer also clears
1002 * the entry in the array. If the status of the IO indiactes that
1003 * this IO was aborted then the sglq entry it put on the
1004 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1005 * IO has good status or fails for any other reason then the sglq
1006 * entry is added to the free list (lpfc_sgl_list).
1007 **/
1008static void
1009__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1010{
1011 struct lpfc_sglq *sglq;
1012 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001013 unsigned long iflag = 0;
1014 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -04001015
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001016 lockdep_assert_held(&phba->hbalock);
1017
James Smart4f774512009-05-22 14:52:35 -04001018 if (iocbq->sli4_xritag == NO_XRI)
1019 sglq = NULL;
1020 else
James Smart6d368e52011-05-24 11:44:12 -04001021 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1022
James Smart0e9bb8d2013-03-01 16:35:12 -05001023
James Smart4f774512009-05-22 14:52:35 -04001024 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -05001025 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1026 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -04001027 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1028 iflag);
1029 list_add(&sglq->list,
1030 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1031 spin_unlock_irqrestore(
1032 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001033 } else {
James Smartdafe8ce2014-09-03 12:56:40 -04001034 spin_lock_irqsave(&pring->ring_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001035 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001036 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001037 list_add_tail(&sglq->list,
1038 &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04001039 spin_unlock_irqrestore(&pring->ring_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001040
1041 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001042 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001043 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001044 }
James Smart4f774512009-05-22 14:52:35 -04001045 }
1046
1047
1048 /*
1049 * Clean all volatile data fields, preserve iotag and node struct.
1050 */
1051 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001052 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001053 iocbq->sli4_xritag = NO_XRI;
1054 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1055}
1056
James Smart2a9bf3d2010-06-07 15:24:45 -04001057
James Smart4f774512009-05-22 14:52:35 -04001058/**
James Smart3772a992009-05-22 14:50:54 -04001059 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1060 * @phba: Pointer to HBA context object.
1061 * @iocbq: Pointer to driver iocb object.
1062 *
1063 * This function is called with hbalock held to release driver
1064 * iocb object to the iocb pool. The iotag in the iocb object
1065 * does not change for each use of the iocb object. This function
1066 * clears all other fields of the iocb object when it is freed.
1067 **/
1068static void
1069__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1070{
1071 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1072
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001073 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001074
1075 /*
James Smart3772a992009-05-22 14:50:54 -04001076 * Clean all volatile data fields, preserve iotag and node struct.
1077 */
1078 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1079 iocbq->sli4_xritag = NO_XRI;
1080 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1081}
1082
1083/**
James Smart3621a712009-04-06 18:47:14 -04001084 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001085 * @phba: Pointer to HBA context object.
1086 * @iocbq: Pointer to driver iocb object.
1087 *
1088 * This function is called with hbalock held to release driver
1089 * iocb object to the iocb pool. The iotag in the iocb object
1090 * does not change for each use of the iocb object. This function
1091 * clears all other fields of the iocb object when it is freed.
1092 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001093static void
James Smart2e0fef82007-06-17 19:56:36 -05001094__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1095{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001096 lockdep_assert_held(&phba->hbalock);
1097
James Smart3772a992009-05-22 14:50:54 -04001098 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001099 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001100}
1101
James Smarte59058c2008-08-24 21:49:00 -04001102/**
James Smart3621a712009-04-06 18:47:14 -04001103 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001104 * @phba: Pointer to HBA context object.
1105 * @iocbq: Pointer to driver iocb object.
1106 *
1107 * This function is called with no lock held to release the iocb to
1108 * iocb pool.
1109 **/
James Smart2e0fef82007-06-17 19:56:36 -05001110void
1111lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1112{
1113 unsigned long iflags;
1114
1115 /*
1116 * Clean all volatile data fields, preserve iotag and node struct.
1117 */
1118 spin_lock_irqsave(&phba->hbalock, iflags);
1119 __lpfc_sli_release_iocbq(phba, iocbq);
1120 spin_unlock_irqrestore(&phba->hbalock, iflags);
1121}
1122
James Smarte59058c2008-08-24 21:49:00 -04001123/**
James Smarta257bf92009-04-06 18:48:10 -04001124 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1125 * @phba: Pointer to HBA context object.
1126 * @iocblist: List of IOCBs.
1127 * @ulpstatus: ULP status in IOCB command field.
1128 * @ulpWord4: ULP word-4 in IOCB command field.
1129 *
1130 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1131 * on the list by invoking the complete callback function associated with the
1132 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1133 * fields.
1134 **/
1135void
1136lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1137 uint32_t ulpstatus, uint32_t ulpWord4)
1138{
1139 struct lpfc_iocbq *piocb;
1140
1141 while (!list_empty(iocblist)) {
1142 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001143 if (!piocb->iocb_cmpl)
1144 lpfc_sli_release_iocbq(phba, piocb);
1145 else {
1146 piocb->iocb.ulpStatus = ulpstatus;
1147 piocb->iocb.un.ulpWord[4] = ulpWord4;
1148 (piocb->iocb_cmpl) (phba, piocb, piocb);
1149 }
1150 }
1151 return;
1152}
1153
1154/**
James Smart3621a712009-04-06 18:47:14 -04001155 * lpfc_sli_iocb_cmd_type - Get the iocb type
1156 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001157 *
1158 * This function is called by ring event handler function to get the iocb type.
1159 * This function translates the iocb command to an iocb command type used to
1160 * decide the final disposition of each completed IOCB.
1161 * The function returns
1162 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1163 * LPFC_SOL_IOCB if it is a solicited iocb completion
1164 * LPFC_ABORT_IOCB if it is an abort iocb
1165 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1166 *
1167 * The caller is not required to hold any lock.
1168 **/
dea31012005-04-17 16:05:31 -05001169static lpfc_iocb_type
1170lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1171{
1172 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1173
1174 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1175 return 0;
1176
1177 switch (iocb_cmnd) {
1178 case CMD_XMIT_SEQUENCE_CR:
1179 case CMD_XMIT_SEQUENCE_CX:
1180 case CMD_XMIT_BCAST_CN:
1181 case CMD_XMIT_BCAST_CX:
1182 case CMD_ELS_REQUEST_CR:
1183 case CMD_ELS_REQUEST_CX:
1184 case CMD_CREATE_XRI_CR:
1185 case CMD_CREATE_XRI_CX:
1186 case CMD_GET_RPI_CN:
1187 case CMD_XMIT_ELS_RSP_CX:
1188 case CMD_GET_RPI_CR:
1189 case CMD_FCP_IWRITE_CR:
1190 case CMD_FCP_IWRITE_CX:
1191 case CMD_FCP_IREAD_CR:
1192 case CMD_FCP_IREAD_CX:
1193 case CMD_FCP_ICMND_CR:
1194 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001195 case CMD_FCP_TSEND_CX:
1196 case CMD_FCP_TRSP_CX:
1197 case CMD_FCP_TRECEIVE_CX:
1198 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001199 case CMD_ADAPTER_MSG:
1200 case CMD_ADAPTER_DUMP:
1201 case CMD_XMIT_SEQUENCE64_CR:
1202 case CMD_XMIT_SEQUENCE64_CX:
1203 case CMD_XMIT_BCAST64_CN:
1204 case CMD_XMIT_BCAST64_CX:
1205 case CMD_ELS_REQUEST64_CR:
1206 case CMD_ELS_REQUEST64_CX:
1207 case CMD_FCP_IWRITE64_CR:
1208 case CMD_FCP_IWRITE64_CX:
1209 case CMD_FCP_IREAD64_CR:
1210 case CMD_FCP_IREAD64_CX:
1211 case CMD_FCP_ICMND64_CR:
1212 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001213 case CMD_FCP_TSEND64_CX:
1214 case CMD_FCP_TRSP64_CX:
1215 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001216 case CMD_GEN_REQUEST64_CR:
1217 case CMD_GEN_REQUEST64_CX:
1218 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001219 case DSSCMD_IWRITE64_CR:
1220 case DSSCMD_IWRITE64_CX:
1221 case DSSCMD_IREAD64_CR:
1222 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001223 type = LPFC_SOL_IOCB;
1224 break;
1225 case CMD_ABORT_XRI_CN:
1226 case CMD_ABORT_XRI_CX:
1227 case CMD_CLOSE_XRI_CN:
1228 case CMD_CLOSE_XRI_CX:
1229 case CMD_XRI_ABORTED_CX:
1230 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001231 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001232 type = LPFC_ABORT_IOCB;
1233 break;
1234 case CMD_RCV_SEQUENCE_CX:
1235 case CMD_RCV_ELS_REQ_CX:
1236 case CMD_RCV_SEQUENCE64_CX:
1237 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001238 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001239 case CMD_IOCB_RCV_SEQ64_CX:
1240 case CMD_IOCB_RCV_ELS64_CX:
1241 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001242 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001243 type = LPFC_UNSOL_IOCB;
1244 break;
James Smart3163f722008-02-08 18:50:25 -05001245 case CMD_IOCB_XMIT_MSEQ64_CR:
1246 case CMD_IOCB_XMIT_MSEQ64_CX:
1247 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1248 case CMD_IOCB_RCV_ELS_LIST64_CX:
1249 case CMD_IOCB_CLOSE_EXTENDED_CN:
1250 case CMD_IOCB_ABORT_EXTENDED_CN:
1251 case CMD_IOCB_RET_HBQE64_CN:
1252 case CMD_IOCB_FCP_IBIDIR64_CR:
1253 case CMD_IOCB_FCP_IBIDIR64_CX:
1254 case CMD_IOCB_FCP_ITASKMGT64_CX:
1255 case CMD_IOCB_LOGENTRY_CN:
1256 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1257 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001258 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001259 type = LPFC_UNKNOWN_IOCB;
1260 break;
dea31012005-04-17 16:05:31 -05001261 default:
1262 type = LPFC_UNKNOWN_IOCB;
1263 break;
1264 }
1265
1266 return type;
1267}
1268
James Smarte59058c2008-08-24 21:49:00 -04001269/**
James Smart3621a712009-04-06 18:47:14 -04001270 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001271 * @phba: Pointer to HBA context object.
1272 *
1273 * This function is called from SLI initialization code
1274 * to configure every ring of the HBA's SLI interface. The
1275 * caller is not required to hold any lock. This function issues
1276 * a config_ring mailbox command for each ring.
1277 * This function returns zero if successful else returns a negative
1278 * error code.
1279 **/
dea31012005-04-17 16:05:31 -05001280static int
James Smarted957682007-06-17 19:56:37 -05001281lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001282{
1283 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001284 LPFC_MBOXQ_t *pmb;
1285 MAILBOX_t *pmbox;
1286 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001287
James Smarted957682007-06-17 19:56:37 -05001288 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1289 if (!pmb)
1290 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001291 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001292 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001293 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001294 lpfc_config_ring(phba, i, pmb);
1295 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1296 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001297 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001298 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001299 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1300 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001301 rc, pmbox->mbxCommand,
1302 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001303 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001304 ret = -ENXIO;
1305 break;
dea31012005-04-17 16:05:31 -05001306 }
1307 }
James Smarted957682007-06-17 19:56:37 -05001308 mempool_free(pmb, phba->mbox_mem_pool);
1309 return ret;
dea31012005-04-17 16:05:31 -05001310}
1311
James Smarte59058c2008-08-24 21:49:00 -04001312/**
James Smart3621a712009-04-06 18:47:14 -04001313 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001314 * @phba: Pointer to HBA context object.
1315 * @pring: Pointer to driver SLI ring object.
1316 * @piocb: Pointer to the driver iocb object.
1317 *
1318 * This function is called with hbalock held. The function adds the
1319 * new iocb to txcmplq of the given ring. This function always returns
1320 * 0. If this function is called for ELS ring, this function checks if
1321 * there is a vport associated with the ELS command. This function also
1322 * starts els_tmofunc timer if this is an ELS command.
1323 **/
dea31012005-04-17 16:05:31 -05001324static int
James Smart2e0fef82007-06-17 19:56:36 -05001325lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1326 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001327{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001328 lockdep_assert_held(&phba->hbalock);
1329
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001330 BUG_ON(!piocb);
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001331
dea31012005-04-17 16:05:31 -05001332 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001333 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001334
James Smart92d7f7b2007-06-17 19:56:38 -05001335 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1336 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
Mauricio Faria de Oliveira2319f842016-11-23 10:33:19 -02001337 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1338 BUG_ON(!piocb->vport);
1339 if (!(piocb->vport->load_flag & FC_UNLOADING))
1340 mod_timer(&piocb->vport->els_tmofunc,
1341 jiffies +
1342 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1343 }
dea31012005-04-17 16:05:31 -05001344
James Smart2e0fef82007-06-17 19:56:36 -05001345 return 0;
dea31012005-04-17 16:05:31 -05001346}
1347
James Smarte59058c2008-08-24 21:49:00 -04001348/**
James Smart3621a712009-04-06 18:47:14 -04001349 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001350 * @phba: Pointer to HBA context object.
1351 * @pring: Pointer to driver SLI ring object.
1352 *
1353 * This function is called with hbalock held to get next
1354 * iocb in txq of the given ring. If there is any iocb in
1355 * the txq, the function returns first iocb in the list after
1356 * removing the iocb from the list, else it returns NULL.
1357 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001358struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001359lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001360{
dea31012005-04-17 16:05:31 -05001361 struct lpfc_iocbq *cmd_iocb;
1362
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001363 lockdep_assert_held(&phba->hbalock);
1364
James Smart858c9f62007-06-17 19:56:39 -05001365 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001366 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001367}
1368
James Smarte59058c2008-08-24 21:49:00 -04001369/**
James Smart3621a712009-04-06 18:47:14 -04001370 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001371 * @phba: Pointer to HBA context object.
1372 * @pring: Pointer to driver SLI ring object.
1373 *
1374 * This function is called with hbalock held and the caller must post the
1375 * iocb without releasing the lock. If the caller releases the lock,
1376 * iocb slot returned by the function is not guaranteed to be available.
1377 * The function returns pointer to the next available iocb slot if there
1378 * is available slot in the ring, else it returns NULL.
1379 * If the get index of the ring is ahead of the put index, the function
1380 * will post an error attention event to the worker thread to take the
1381 * HBA to offline state.
1382 **/
dea31012005-04-17 16:05:31 -05001383static IOCB_t *
1384lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1385{
James Smart34b02dc2008-08-24 21:49:55 -04001386 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001387 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001388
1389 lockdep_assert_held(&phba->hbalock);
1390
James Smart7e56aa22012-08-03 12:35:34 -04001391 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1392 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1393 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001394
James Smart7e56aa22012-08-03 12:35:34 -04001395 if (unlikely(pring->sli.sli3.local_getidx ==
1396 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001397
James Smart7e56aa22012-08-03 12:35:34 -04001398 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001399
James Smart7e56aa22012-08-03 12:35:34 -04001400 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001401 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001402 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001403 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001404 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001405 pring->sli.sli3.local_getidx,
1406 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001407
James Smart2e0fef82007-06-17 19:56:36 -05001408 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001409 /*
1410 * All error attention handlers are posted to
1411 * worker thread
1412 */
1413 phba->work_ha |= HA_ERATT;
1414 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001415
James Smart5e9d9b82008-06-14 22:52:53 -04001416 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001417
1418 return NULL;
1419 }
1420
James Smart7e56aa22012-08-03 12:35:34 -04001421 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001422 return NULL;
1423 }
1424
James Smarted957682007-06-17 19:56:37 -05001425 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001426}
1427
James Smarte59058c2008-08-24 21:49:00 -04001428/**
James Smart3621a712009-04-06 18:47:14 -04001429 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001430 * @phba: Pointer to HBA context object.
1431 * @iocbq: Pointer to driver iocb object.
1432 *
1433 * This function gets an iotag for the iocb. If there is no unused iotag and
1434 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1435 * array and assigns a new iotag.
1436 * The function returns the allocated iotag if successful, else returns zero.
1437 * Zero is not a valid iotag.
1438 * The caller is not required to hold any lock.
1439 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001440uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001441lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001442{
James Smart2e0fef82007-06-17 19:56:36 -05001443 struct lpfc_iocbq **new_arr;
1444 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001445 size_t new_len;
1446 struct lpfc_sli *psli = &phba->sli;
1447 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001448
James Smart2e0fef82007-06-17 19:56:36 -05001449 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001450 iotag = psli->last_iotag;
1451 if(++iotag < psli->iocbq_lookup_len) {
1452 psli->last_iotag = iotag;
1453 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001454 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001455 iocbq->iotag = iotag;
1456 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001457 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001458 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1459 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001460 spin_unlock_irq(&phba->hbalock);
1461 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001462 GFP_KERNEL);
1463 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001464 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001465 old_arr = psli->iocbq_lookup;
1466 if (new_len <= psli->iocbq_lookup_len) {
1467 /* highly unprobable case */
1468 kfree(new_arr);
1469 iotag = psli->last_iotag;
1470 if(++iotag < psli->iocbq_lookup_len) {
1471 psli->last_iotag = iotag;
1472 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001473 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001474 iocbq->iotag = iotag;
1475 return iotag;
1476 }
James Smart2e0fef82007-06-17 19:56:36 -05001477 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001478 return 0;
1479 }
1480 if (psli->iocbq_lookup)
1481 memcpy(new_arr, old_arr,
1482 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001483 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001484 psli->iocbq_lookup = new_arr;
1485 psli->iocbq_lookup_len = new_len;
1486 psli->last_iotag = iotag;
1487 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001488 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001489 iocbq->iotag = iotag;
1490 kfree(old_arr);
1491 return iotag;
1492 }
James Smart8f6d98d2006-08-01 07:34:00 -04001493 } else
James Smart2e0fef82007-06-17 19:56:36 -05001494 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001495
James Smartbc739052010-08-04 16:11:18 -04001496 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001497 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1498 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001499
James Bottomley604a3e32005-10-29 10:28:33 -05001500 return 0;
dea31012005-04-17 16:05:31 -05001501}
1502
James Smarte59058c2008-08-24 21:49:00 -04001503/**
James Smart3621a712009-04-06 18:47:14 -04001504 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001505 * @phba: Pointer to HBA context object.
1506 * @pring: Pointer to driver SLI ring object.
1507 * @iocb: Pointer to iocb slot in the ring.
1508 * @nextiocb: Pointer to driver iocb object which need to be
1509 * posted to firmware.
1510 *
1511 * This function is called with hbalock held to post a new iocb to
1512 * the firmware. This function copies the new iocb to ring iocb slot and
1513 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1514 * a completion call back for this iocb else the function will free the
1515 * iocb object.
1516 **/
dea31012005-04-17 16:05:31 -05001517static void
1518lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1519 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1520{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001521 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001522 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001523 * Set up an iotag
dea31012005-04-17 16:05:31 -05001524 */
James Bottomley604a3e32005-10-29 10:28:33 -05001525 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001526
James Smarte2a0a9d2008-12-04 22:40:02 -05001527
James Smarta58cbd52007-08-02 11:09:43 -04001528 if (pring->ringno == LPFC_ELS_RING) {
1529 lpfc_debugfs_slow_ring_trc(phba,
1530 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1531 *(((uint32_t *) &nextiocb->iocb) + 4),
1532 *(((uint32_t *) &nextiocb->iocb) + 6),
1533 *(((uint32_t *) &nextiocb->iocb) + 7));
1534 }
1535
dea31012005-04-17 16:05:31 -05001536 /*
1537 * Issue iocb command to adapter
1538 */
James Smart92d7f7b2007-06-17 19:56:38 -05001539 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001540 wmb();
1541 pring->stats.iocb_cmd++;
1542
1543 /*
1544 * If there is no completion routine to call, we can release the
1545 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1546 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1547 */
1548 if (nextiocb->iocb_cmpl)
1549 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001550 else
James Smart2e0fef82007-06-17 19:56:36 -05001551 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001552
1553 /*
1554 * Let the HBA know what IOCB slot will be the next one the
1555 * driver will put a command into.
1556 */
James Smart7e56aa22012-08-03 12:35:34 -04001557 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1558 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001559}
1560
James Smarte59058c2008-08-24 21:49:00 -04001561/**
James Smart3621a712009-04-06 18:47:14 -04001562 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001563 * @phba: Pointer to HBA context object.
1564 * @pring: Pointer to driver SLI ring object.
1565 *
1566 * The caller is not required to hold any lock for calling this function.
1567 * This function updates the chip attention bits for the ring to inform firmware
1568 * that there are pending work to be done for this ring and requests an
1569 * interrupt when there is space available in the ring. This function is
1570 * called when the driver is unable to post more iocbs to the ring due
1571 * to unavailability of space in the ring.
1572 **/
dea31012005-04-17 16:05:31 -05001573static void
James Smart2e0fef82007-06-17 19:56:36 -05001574lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001575{
1576 int ringno = pring->ringno;
1577
1578 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1579
1580 wmb();
1581
1582 /*
1583 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1584 * The HBA will tell us when an IOCB entry is available.
1585 */
1586 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1587 readl(phba->CAregaddr); /* flush */
1588
1589 pring->stats.iocb_cmd_full++;
1590}
1591
James Smarte59058c2008-08-24 21:49:00 -04001592/**
James Smart3621a712009-04-06 18:47:14 -04001593 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001594 * @phba: Pointer to HBA context object.
1595 * @pring: Pointer to driver SLI ring object.
1596 *
1597 * This function updates the chip attention register bit for the
1598 * given ring to inform HBA that there is more work to be done
1599 * in this ring. The caller is not required to hold any lock.
1600 **/
dea31012005-04-17 16:05:31 -05001601static void
James Smart2e0fef82007-06-17 19:56:36 -05001602lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001603{
1604 int ringno = pring->ringno;
1605
1606 /*
1607 * Tell the HBA that there is work to do in this ring.
1608 */
James Smart34b02dc2008-08-24 21:49:55 -04001609 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1610 wmb();
1611 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1612 readl(phba->CAregaddr); /* flush */
1613 }
dea31012005-04-17 16:05:31 -05001614}
1615
James Smarte59058c2008-08-24 21:49:00 -04001616/**
James Smart3621a712009-04-06 18:47:14 -04001617 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001618 * @phba: Pointer to HBA context object.
1619 * @pring: Pointer to driver SLI ring object.
1620 *
1621 * This function is called with hbalock held to post pending iocbs
1622 * in the txq to the firmware. This function is called when driver
1623 * detects space available in the ring.
1624 **/
dea31012005-04-17 16:05:31 -05001625static void
James Smart2e0fef82007-06-17 19:56:36 -05001626lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001627{
1628 IOCB_t *iocb;
1629 struct lpfc_iocbq *nextiocb;
1630
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001631 lockdep_assert_held(&phba->hbalock);
1632
dea31012005-04-17 16:05:31 -05001633 /*
1634 * Check to see if:
1635 * (a) there is anything on the txq to send
1636 * (b) link is up
1637 * (c) link attention events can be processed (fcp ring only)
1638 * (d) IOCB processing is not blocked by the outstanding mbox command.
1639 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001640
1641 if (lpfc_is_link_up(phba) &&
1642 (!list_empty(&pring->txq)) &&
dea31012005-04-17 16:05:31 -05001643 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001644 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001645
1646 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1647 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1648 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1649
1650 if (iocb)
1651 lpfc_sli_update_ring(phba, pring);
1652 else
1653 lpfc_sli_update_full_ring(phba, pring);
1654 }
1655
1656 return;
1657}
1658
James Smarte59058c2008-08-24 21:49:00 -04001659/**
James Smart3621a712009-04-06 18:47:14 -04001660 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001661 * @phba: Pointer to HBA context object.
1662 * @hbqno: HBQ number.
1663 *
1664 * This function is called with hbalock held to get the next
1665 * available slot for the given HBQ. If there is free slot
1666 * available for the HBQ it will return pointer to the next available
1667 * HBQ entry else it will return NULL.
1668 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001669static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001670lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1671{
1672 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1673
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001674 lockdep_assert_held(&phba->hbalock);
1675
James Smarted957682007-06-17 19:56:37 -05001676 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1677 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1678 hbqp->next_hbqPutIdx = 0;
1679
1680 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001681 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001682 uint32_t getidx = le32_to_cpu(raw_index);
1683
1684 hbqp->local_hbqGetIdx = getidx;
1685
1686 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1687 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001688 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001689 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001690 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001691 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001692 hbqp->entry_count);
1693
1694 phba->link_state = LPFC_HBA_ERROR;
1695 return NULL;
1696 }
1697
1698 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1699 return NULL;
1700 }
1701
James Smart51ef4c22007-08-02 11:10:31 -04001702 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1703 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001704}
1705
James Smarte59058c2008-08-24 21:49:00 -04001706/**
James Smart3621a712009-04-06 18:47:14 -04001707 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001708 * @phba: Pointer to HBA context object.
1709 *
1710 * This function is called with no lock held to free all the
1711 * hbq buffers while uninitializing the SLI interface. It also
1712 * frees the HBQ buffers returned by the firmware but not yet
1713 * processed by the upper layers.
1714 **/
James Smarted957682007-06-17 19:56:37 -05001715void
1716lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1717{
James Smart92d7f7b2007-06-17 19:56:38 -05001718 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1719 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001720 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001721 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001722 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001723
James Smart51ef4c22007-08-02 11:10:31 -04001724 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001725 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001726 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001727 for (i = 0; i < hbq_count; ++i) {
1728 list_for_each_entry_safe(dmabuf, next_dmabuf,
1729 &phba->hbqs[i].hbq_buffer_list, list) {
1730 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1731 list_del(&hbq_buf->dbuf.list);
1732 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1733 }
James Smarta8adb832007-10-27 13:37:53 -04001734 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001735 }
James Smart3163f722008-02-08 18:50:25 -05001736 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001737 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1738 list) {
James Smart3163f722008-02-08 18:50:25 -05001739 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1740 list_del(&hbq_buf->dbuf.list);
1741 if (hbq_buf->tag == -1) {
1742 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1743 (phba, hbq_buf);
1744 } else {
1745 hbqno = hbq_buf->tag >> 16;
1746 if (hbqno >= LPFC_MAX_HBQS)
1747 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1748 (phba, hbq_buf);
1749 else
1750 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1751 hbq_buf);
1752 }
1753 }
1754
1755 /* Mark the HBQs not in use */
1756 phba->hbq_in_use = 0;
1757 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001758}
1759
James Smarte59058c2008-08-24 21:49:00 -04001760/**
James Smart3621a712009-04-06 18:47:14 -04001761 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001762 * @phba: Pointer to HBA context object.
1763 * @hbqno: HBQ number.
1764 * @hbq_buf: Pointer to HBQ buffer.
1765 *
1766 * This function is called with the hbalock held to post a
1767 * hbq buffer to the firmware. If the function finds an empty
1768 * slot in the HBQ, it will post the buffer. The function will return
1769 * pointer to the hbq entry if it successfully post the buffer
1770 * else it will return NULL.
1771 **/
James Smart3772a992009-05-22 14:50:54 -04001772static int
James Smarted957682007-06-17 19:56:37 -05001773lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001774 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001775{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001776 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001777 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1778}
1779
1780/**
1781 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1782 * @phba: Pointer to HBA context object.
1783 * @hbqno: HBQ number.
1784 * @hbq_buf: Pointer to HBQ buffer.
1785 *
1786 * This function is called with the hbalock held to post a hbq buffer to the
1787 * firmware. If the function finds an empty slot in the HBQ, it will post the
1788 * buffer and place it on the hbq_buffer_list. The function will return zero if
1789 * it successfully post the buffer else it will return an error.
1790 **/
1791static int
1792lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1793 struct hbq_dmabuf *hbq_buf)
1794{
James Smarted957682007-06-17 19:56:37 -05001795 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001796 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001797
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001798 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05001799 /* Get next HBQ entry slot to use */
1800 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1801 if (hbqe) {
1802 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1803
James Smart92d7f7b2007-06-17 19:56:38 -05001804 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1805 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001806 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001807 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001808 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1809 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1810 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001811 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1812 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001813 /* flush */
James Smarted957682007-06-17 19:56:37 -05001814 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001815 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001816 return 0;
1817 } else
1818 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001819}
1820
James Smart4f774512009-05-22 14:52:35 -04001821/**
1822 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1823 * @phba: Pointer to HBA context object.
1824 * @hbqno: HBQ number.
1825 * @hbq_buf: Pointer to HBQ buffer.
1826 *
1827 * This function is called with the hbalock held to post an RQE to the SLI4
1828 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1829 * the hbq_buffer_list and return zero, otherwise it will return an error.
1830 **/
1831static int
1832lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1833 struct hbq_dmabuf *hbq_buf)
1834{
1835 int rc;
1836 struct lpfc_rqe hrqe;
1837 struct lpfc_rqe drqe;
1838
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001839 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04001840 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1841 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1842 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1843 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1844 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1845 &hrqe, &drqe);
1846 if (rc < 0)
1847 return rc;
1848 hbq_buf->tag = rc;
1849 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1850 return 0;
1851}
1852
James Smarte59058c2008-08-24 21:49:00 -04001853/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001854static struct lpfc_hbq_init lpfc_els_hbq = {
1855 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001856 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001857 .mask_count = 0,
1858 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001859 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001860 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001861 .init_count = 40,
1862 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001863};
James Smarted957682007-06-17 19:56:37 -05001864
James Smarte59058c2008-08-24 21:49:00 -04001865/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001866static struct lpfc_hbq_init lpfc_extra_hbq = {
1867 .rn = 1,
1868 .entry_count = 200,
1869 .mask_count = 0,
1870 .profile = 0,
1871 .ring_mask = (1 << LPFC_EXTRA_RING),
1872 .buffer_count = 0,
1873 .init_count = 0,
1874 .add_count = 5,
1875};
1876
James Smarte59058c2008-08-24 21:49:00 -04001877/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001878struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001879 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001880 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001881};
1882
James Smarte59058c2008-08-24 21:49:00 -04001883/**
James Smart3621a712009-04-06 18:47:14 -04001884 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001885 * @phba: Pointer to HBA context object.
1886 * @hbqno: HBQ number.
1887 * @count: Number of HBQ buffers to be posted.
1888 *
James Smartd7c255b2008-08-24 21:50:00 -04001889 * This function is called with no lock held to post more hbq buffers to the
1890 * given HBQ. The function returns the number of HBQ buffers successfully
1891 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001892 **/
James Smart311464e2007-08-02 11:10:37 -04001893static int
James Smart92d7f7b2007-06-17 19:56:38 -05001894lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1895{
James Smartd7c255b2008-08-24 21:50:00 -04001896 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001897 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001898 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001899 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001900 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001901 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001902
James Smartd7c255b2008-08-24 21:50:00 -04001903 if ((phba->hbqs[hbqno].buffer_count + count) >
1904 lpfc_hbq_defs[hbqno]->entry_count)
1905 count = lpfc_hbq_defs[hbqno]->entry_count -
1906 phba->hbqs[hbqno].buffer_count;
1907 if (!count)
1908 return 0;
1909 /* Allocate HBQ entries */
1910 for (i = 0; i < count; i++) {
1911 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1912 if (!hbq_buffer)
1913 break;
1914 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1915 }
James Smart3163f722008-02-08 18:50:25 -05001916 /* Check whether HBQ is still in use */
1917 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001918 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001919 goto err;
1920 while (!list_empty(&hbq_buf_list)) {
1921 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1922 dbuf.list);
1923 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1924 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001925 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001926 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001927 posted++;
1928 } else
James Smart51ef4c22007-08-02 11:10:31 -04001929 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001930 }
James Smart3163f722008-02-08 18:50:25 -05001931 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001932 return posted;
1933err:
1934 spin_unlock_irqrestore(&phba->hbalock, flags);
1935 while (!list_empty(&hbq_buf_list)) {
1936 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1937 dbuf.list);
1938 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1939 }
James Smart92d7f7b2007-06-17 19:56:38 -05001940 return 0;
James Smarted957682007-06-17 19:56:37 -05001941}
1942
James Smarte59058c2008-08-24 21:49:00 -04001943/**
James Smart3621a712009-04-06 18:47:14 -04001944 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001945 * @phba: Pointer to HBA context object.
1946 * @qno: HBQ number.
1947 *
1948 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001949 * is called with no lock held. The function returns the number of HBQ entries
1950 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001951 **/
James Smarted957682007-06-17 19:56:37 -05001952int
James Smart92d7f7b2007-06-17 19:56:38 -05001953lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001954{
James Smartdef9c7a2009-12-21 17:02:28 -05001955 if (phba->sli_rev == LPFC_SLI_REV4)
1956 return 0;
1957 else
1958 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1959 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001960}
1961
James Smarte59058c2008-08-24 21:49:00 -04001962/**
James Smart3621a712009-04-06 18:47:14 -04001963 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001964 * @phba: Pointer to HBA context object.
1965 * @qno: HBQ queue number.
1966 *
1967 * This function is called from SLI initialization code path with
1968 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001969 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001970 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001971static int
James Smart92d7f7b2007-06-17 19:56:38 -05001972lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001973{
James Smartdef9c7a2009-12-21 17:02:28 -05001974 if (phba->sli_rev == LPFC_SLI_REV4)
1975 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001976 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001977 else
1978 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1979 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001980}
1981
James Smarte59058c2008-08-24 21:49:00 -04001982/**
James Smart3772a992009-05-22 14:50:54 -04001983 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1984 * @phba: Pointer to HBA context object.
1985 * @hbqno: HBQ number.
1986 *
1987 * This function removes the first hbq buffer on an hbq list and returns a
1988 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1989 **/
1990static struct hbq_dmabuf *
1991lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1992{
1993 struct lpfc_dmabuf *d_buf;
1994
1995 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1996 if (!d_buf)
1997 return NULL;
1998 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1999}
2000
2001/**
James Smart3621a712009-04-06 18:47:14 -04002002 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04002003 * @phba: Pointer to HBA context object.
2004 * @tag: Tag of the hbq buffer.
2005 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002006 * This function searches for the hbq buffer associated with the given tag in
2007 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2008 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002009 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002010static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002011lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2012{
James Smart92d7f7b2007-06-17 19:56:38 -05002013 struct lpfc_dmabuf *d_buf;
2014 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002015 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002016
James Smart51ef4c22007-08-02 11:10:31 -04002017 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002018 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002019 return NULL;
2020
James Smart3772a992009-05-22 14:50:54 -04002021 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002022 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002023 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002024 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002025 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002026 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002027 }
2028 }
James Smart3772a992009-05-22 14:50:54 -04002029 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002030 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002031 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002032 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002033 return NULL;
James Smarted957682007-06-17 19:56:37 -05002034}
2035
James Smarte59058c2008-08-24 21:49:00 -04002036/**
James Smart3621a712009-04-06 18:47:14 -04002037 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002038 * @phba: Pointer to HBA context object.
2039 * @hbq_buffer: Pointer to HBQ buffer.
2040 *
2041 * This function is called with hbalock. This function gives back
2042 * the hbq buffer to firmware. If the HBQ does not have space to
2043 * post the buffer, it will free the buffer.
2044 **/
James Smarted957682007-06-17 19:56:37 -05002045void
James Smart51ef4c22007-08-02 11:10:31 -04002046lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002047{
2048 uint32_t hbqno;
2049
James Smart51ef4c22007-08-02 11:10:31 -04002050 if (hbq_buffer) {
2051 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002052 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002053 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002054 }
2055}
2056
James Smarte59058c2008-08-24 21:49:00 -04002057/**
James Smart3621a712009-04-06 18:47:14 -04002058 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002059 * @mbxCommand: mailbox command code.
2060 *
2061 * This function is called by the mailbox event handler function to verify
2062 * that the completed mailbox command is a legitimate mailbox command. If the
2063 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2064 * and the mailbox event handler will take the HBA offline.
2065 **/
dea31012005-04-17 16:05:31 -05002066static int
2067lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2068{
2069 uint8_t ret;
2070
2071 switch (mbxCommand) {
2072 case MBX_LOAD_SM:
2073 case MBX_READ_NV:
2074 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002075 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002076 case MBX_RUN_BIU_DIAG:
2077 case MBX_INIT_LINK:
2078 case MBX_DOWN_LINK:
2079 case MBX_CONFIG_LINK:
2080 case MBX_CONFIG_RING:
2081 case MBX_RESET_RING:
2082 case MBX_READ_CONFIG:
2083 case MBX_READ_RCONFIG:
2084 case MBX_READ_SPARM:
2085 case MBX_READ_STATUS:
2086 case MBX_READ_RPI:
2087 case MBX_READ_XRI:
2088 case MBX_READ_REV:
2089 case MBX_READ_LNK_STAT:
2090 case MBX_REG_LOGIN:
2091 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002092 case MBX_CLEAR_LA:
2093 case MBX_DUMP_MEMORY:
2094 case MBX_DUMP_CONTEXT:
2095 case MBX_RUN_DIAGS:
2096 case MBX_RESTART:
2097 case MBX_UPDATE_CFG:
2098 case MBX_DOWN_LOAD:
2099 case MBX_DEL_LD_ENTRY:
2100 case MBX_RUN_PROGRAM:
2101 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002102 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002103 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002104 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002105 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002106 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002107 case MBX_LOAD_AREA:
2108 case MBX_RUN_BIU_DIAG64:
2109 case MBX_CONFIG_PORT:
2110 case MBX_READ_SPARM64:
2111 case MBX_READ_RPI64:
2112 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002113 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002114 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002115 case MBX_SET_DEBUG:
2116 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002117 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002118 case MBX_REG_VPI:
2119 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002120 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002121 case MBX_PORT_CAPABILITIES:
2122 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002123 case MBX_SLI4_CONFIG:
2124 case MBX_SLI4_REQ_FTRS:
2125 case MBX_REG_FCFI:
2126 case MBX_UNREG_FCFI:
2127 case MBX_REG_VFI:
2128 case MBX_UNREG_VFI:
2129 case MBX_INIT_VPI:
2130 case MBX_INIT_VFI:
2131 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002132 case MBX_READ_EVENT_LOG_STATUS:
2133 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002134 case MBX_SECURITY_MGMT:
2135 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002136 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002137 ret = mbxCommand;
2138 break;
2139 default:
2140 ret = MBX_SHUTDOWN;
2141 break;
2142 }
James Smart2e0fef82007-06-17 19:56:36 -05002143 return ret;
dea31012005-04-17 16:05:31 -05002144}
James Smarte59058c2008-08-24 21:49:00 -04002145
2146/**
James Smart3621a712009-04-06 18:47:14 -04002147 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002148 * @phba: Pointer to HBA context object.
2149 * @pmboxq: Pointer to mailbox command.
2150 *
2151 * This is completion handler function for mailbox commands issued from
2152 * lpfc_sli_issue_mbox_wait function. This function is called by the
2153 * mailbox event handler function with no lock held. This function
2154 * will wake up thread waiting on the wait queue pointed by context1
2155 * of the mailbox.
2156 **/
James Smart04c68492009-05-22 14:52:52 -04002157void
James Smart2e0fef82007-06-17 19:56:36 -05002158lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002159{
2160 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002161 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002162
2163 /*
2164 * If pdone_q is empty, the driver thread gave up waiting and
2165 * continued running.
2166 */
James Smart7054a602007-04-25 09:52:34 -04002167 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002168 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002169 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2170 if (pdone_q)
2171 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002172 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002173 return;
2174}
2175
James Smarte59058c2008-08-24 21:49:00 -04002176
2177/**
James Smart3621a712009-04-06 18:47:14 -04002178 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002179 * @phba: Pointer to HBA context object.
2180 * @pmb: Pointer to mailbox object.
2181 *
2182 * This function is the default mailbox completion handler. It
2183 * frees the memory resources associated with the completed mailbox
2184 * command. If the completed command is a REG_LOGIN mailbox command,
2185 * this function will issue a UREG_LOGIN to re-claim the RPI.
2186 **/
dea31012005-04-17 16:05:31 -05002187void
James Smart2e0fef82007-06-17 19:56:36 -05002188lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002189{
James Smartd439d282010-09-29 11:18:45 -04002190 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002191 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002192 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002193 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002194 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002195 int rc;
2196
dea31012005-04-17 16:05:31 -05002197 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002198
dea31012005-04-17 16:05:31 -05002199 if (mp) {
2200 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2201 kfree(mp);
2202 }
James Smart7054a602007-04-25 09:52:34 -04002203
2204 /*
2205 * If a REG_LOGIN succeeded after node is destroyed or node
2206 * is in re-discovery driver need to cleanup the RPI.
2207 */
James Smart2e0fef82007-06-17 19:56:36 -05002208 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002209 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2210 !pmb->u.mb.mbxStatus) {
2211 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002212 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002213 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002214 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002215 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002216 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2217 if (rc != MBX_NOT_FINISHED)
2218 return;
2219 }
2220
James Smart695a8142010-01-26 23:08:03 -05002221 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2222 !(phba->pport->load_flag & FC_UNLOADING) &&
2223 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002224 shost = lpfc_shost_from_vport(vport);
2225 spin_lock_irq(shost->host_lock);
2226 vport->vpi_state |= LPFC_VPI_REGISTERED;
2227 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2228 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002229 }
2230
James Smartd439d282010-09-29 11:18:45 -04002231 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2232 ndlp = (struct lpfc_nodelist *)pmb->context2;
2233 lpfc_nlp_put(ndlp);
2234 pmb->context2 = NULL;
2235 }
2236
James Smartdcf2a4e2010-09-29 11:18:53 -04002237 /* Check security permission status on INIT_LINK mailbox command */
2238 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2239 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2240 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2241 "2860 SLI authentication is required "
2242 "for INIT_LINK but has not done yet\n");
2243
James Smart04c68492009-05-22 14:52:52 -04002244 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2245 lpfc_sli4_mbox_cmd_free(phba, pmb);
2246 else
2247 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002248}
James Smartbe6bb942015-04-07 15:07:22 -04002249 /**
2250 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2251 * @phba: Pointer to HBA context object.
2252 * @pmb: Pointer to mailbox object.
2253 *
2254 * This function is the unreg rpi mailbox completion handler. It
2255 * frees the memory resources associated with the completed mailbox
2256 * command. An additional refrenece is put on the ndlp to prevent
2257 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2258 * the unreg mailbox command completes, this routine puts the
2259 * reference back.
2260 *
2261 **/
2262void
2263lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2264{
2265 struct lpfc_vport *vport = pmb->vport;
2266 struct lpfc_nodelist *ndlp;
2267
2268 ndlp = pmb->context1;
2269 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2270 if (phba->sli_rev == LPFC_SLI_REV4 &&
2271 (bf_get(lpfc_sli_intf_if_type,
2272 &phba->sli4_hba.sli_intf) ==
2273 LPFC_SLI_INTF_IF_TYPE_2)) {
2274 if (ndlp) {
2275 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2276 "0010 UNREG_LOGIN vpi:%x "
2277 "rpi:%x DID:%x map:%x %p\n",
2278 vport->vpi, ndlp->nlp_rpi,
2279 ndlp->nlp_DID,
2280 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002281 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002282 lpfc_nlp_put(ndlp);
2283 }
2284 }
2285 }
2286
2287 mempool_free(pmb, phba->mbox_mem_pool);
2288}
dea31012005-04-17 16:05:31 -05002289
James Smarte59058c2008-08-24 21:49:00 -04002290/**
James Smart3621a712009-04-06 18:47:14 -04002291 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002292 * @phba: Pointer to HBA context object.
2293 *
2294 * This function is called with no lock held. This function processes all
2295 * the completed mailbox commands and gives it to upper layers. The interrupt
2296 * service routine processes mailbox completion interrupt and adds completed
2297 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2298 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2299 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2300 * function returns the mailbox commands to the upper layer by calling the
2301 * completion handler function of each mailbox.
2302 **/
dea31012005-04-17 16:05:31 -05002303int
James Smart2e0fef82007-06-17 19:56:36 -05002304lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002305{
James Smart92d7f7b2007-06-17 19:56:38 -05002306 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002307 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002308 int rc;
2309 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002310
2311 phba->sli.slistat.mbox_event++;
2312
James Smart92d7f7b2007-06-17 19:56:38 -05002313 /* Get all completed mailboxe buffers into the cmplq */
2314 spin_lock_irq(&phba->hbalock);
2315 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2316 spin_unlock_irq(&phba->hbalock);
2317
dea31012005-04-17 16:05:31 -05002318 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002319 do {
2320 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2321 if (pmb == NULL)
2322 break;
2323
James Smart04c68492009-05-22 14:52:52 -04002324 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002325
James Smart858c9f62007-06-17 19:56:39 -05002326 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2327 if (pmb->vport) {
2328 lpfc_debugfs_disc_trc(pmb->vport,
2329 LPFC_DISC_TRC_MBOX_VPORT,
2330 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2331 (uint32_t)pmbox->mbxCommand,
2332 pmbox->un.varWords[0],
2333 pmbox->un.varWords[1]);
2334 }
2335 else {
2336 lpfc_debugfs_disc_trc(phba->pport,
2337 LPFC_DISC_TRC_MBOX,
2338 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2339 (uint32_t)pmbox->mbxCommand,
2340 pmbox->un.varWords[0],
2341 pmbox->un.varWords[1]);
2342 }
2343 }
2344
dea31012005-04-17 16:05:31 -05002345 /*
2346 * It is a fatal error if unknown mbox command completion.
2347 */
2348 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2349 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002350 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002351 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002352 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002353 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002354 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002355 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002356 lpfc_sli_config_mbox_subsys_get(phba,
2357 pmb),
2358 lpfc_sli_config_mbox_opcode_get(phba,
2359 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002360 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002361 phba->work_hs = HS_FFER3;
2362 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002363 continue;
dea31012005-04-17 16:05:31 -05002364 }
2365
dea31012005-04-17 16:05:31 -05002366 if (pmbox->mbxStatus) {
2367 phba->sli.slistat.mbox_stat_err++;
2368 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2369 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002370 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002371 LOG_MBOX | LOG_SLI,
2372 "(%d):0305 Mbox cmd cmpl "
2373 "error - RETRYing Data: x%x "
2374 "(x%x/x%x) x%x x%x x%x\n",
2375 pmb->vport ? pmb->vport->vpi : 0,
2376 pmbox->mbxCommand,
2377 lpfc_sli_config_mbox_subsys_get(phba,
2378 pmb),
2379 lpfc_sli_config_mbox_opcode_get(phba,
2380 pmb),
2381 pmbox->mbxStatus,
2382 pmbox->un.varWords[0],
2383 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002384 pmbox->mbxStatus = 0;
2385 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002386 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002387 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002388 continue;
dea31012005-04-17 16:05:31 -05002389 }
2390 }
2391
2392 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002393 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002394 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002395 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2396 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002397 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002398 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002399 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2400 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002401 pmb->mbox_cmpl,
2402 *((uint32_t *) pmbox),
2403 pmbox->un.varWords[0],
2404 pmbox->un.varWords[1],
2405 pmbox->un.varWords[2],
2406 pmbox->un.varWords[3],
2407 pmbox->un.varWords[4],
2408 pmbox->un.varWords[5],
2409 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002410 pmbox->un.varWords[7],
2411 pmbox->un.varWords[8],
2412 pmbox->un.varWords[9],
2413 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002414
James Smart92d7f7b2007-06-17 19:56:38 -05002415 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002416 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002417 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002418 return 0;
dea31012005-04-17 16:05:31 -05002419}
James Smart92d7f7b2007-06-17 19:56:38 -05002420
James Smarte59058c2008-08-24 21:49:00 -04002421/**
James Smart3621a712009-04-06 18:47:14 -04002422 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002423 * @phba: Pointer to HBA context object.
2424 * @pring: Pointer to driver SLI ring object.
2425 * @tag: buffer tag.
2426 *
2427 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2428 * is set in the tag the buffer is posted for a particular exchange,
2429 * the function will return the buffer without replacing the buffer.
2430 * If the buffer is for unsolicited ELS or CT traffic, this function
2431 * returns the buffer and also posts another buffer to the firmware.
2432 **/
James Smart76bb24e2007-10-27 13:38:00 -04002433static struct lpfc_dmabuf *
2434lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002435 struct lpfc_sli_ring *pring,
2436 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002437{
James Smart9f1e1b52008-12-04 22:39:40 -05002438 struct hbq_dmabuf *hbq_entry;
2439
James Smart76bb24e2007-10-27 13:38:00 -04002440 if (tag & QUE_BUFTAG_BIT)
2441 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002442 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2443 if (!hbq_entry)
2444 return NULL;
2445 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002446}
James Smart57127f12007-10-27 13:37:05 -04002447
James Smart3772a992009-05-22 14:50:54 -04002448/**
2449 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2450 * @phba: Pointer to HBA context object.
2451 * @pring: Pointer to driver SLI ring object.
2452 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2453 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2454 * @fch_type: the type for the first frame of the sequence.
2455 *
2456 * This function is called with no lock held. This function uses the r_ctl and
2457 * type of the received sequence to find the correct callback function to call
2458 * to process the sequence.
2459 **/
2460static int
2461lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2462 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2463 uint32_t fch_type)
2464{
2465 int i;
2466
2467 /* unSolicited Responses */
2468 if (pring->prt[0].profile) {
2469 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2470 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2471 saveq);
2472 return 1;
2473 }
2474 /* We must search, based on rctl / type
2475 for the right routine */
2476 for (i = 0; i < pring->num_mask; i++) {
2477 if ((pring->prt[i].rctl == fch_r_ctl) &&
2478 (pring->prt[i].type == fch_type)) {
2479 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2480 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2481 (phba, pring, saveq);
2482 return 1;
2483 }
2484 }
2485 return 0;
2486}
James Smarte59058c2008-08-24 21:49:00 -04002487
2488/**
James Smart3621a712009-04-06 18:47:14 -04002489 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002490 * @phba: Pointer to HBA context object.
2491 * @pring: Pointer to driver SLI ring object.
2492 * @saveq: Pointer to the unsolicited iocb.
2493 *
2494 * This function is called with no lock held by the ring event handler
2495 * when there is an unsolicited iocb posted to the response ring by the
2496 * firmware. This function gets the buffer associated with the iocbs
2497 * and calls the event handler for the ring. This function handles both
2498 * qring buffers and hbq buffers.
2499 * When the function returns 1 the caller can free the iocb object otherwise
2500 * upper layer functions will free the iocb objects.
2501 **/
dea31012005-04-17 16:05:31 -05002502static int
2503lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2504 struct lpfc_iocbq *saveq)
2505{
2506 IOCB_t * irsp;
2507 WORD5 * w5p;
2508 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002509 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002510 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002511
dea31012005-04-17 16:05:31 -05002512 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002513
2514 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2515 if (pring->lpfc_sli_rcv_async_status)
2516 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2517 else
2518 lpfc_printf_log(phba,
2519 KERN_WARNING,
2520 LOG_SLI,
2521 "0316 Ring %d handler: unexpected "
2522 "ASYNC_STATUS iocb received evt_code "
2523 "0x%x\n",
2524 pring->ringno,
2525 irsp->un.asyncstat.evt_code);
2526 return 1;
2527 }
2528
James Smart3163f722008-02-08 18:50:25 -05002529 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2530 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2531 if (irsp->ulpBdeCount > 0) {
2532 dmzbuf = lpfc_sli_get_buff(phba, pring,
2533 irsp->un.ulpWord[3]);
2534 lpfc_in_buf_free(phba, dmzbuf);
2535 }
2536
2537 if (irsp->ulpBdeCount > 1) {
2538 dmzbuf = lpfc_sli_get_buff(phba, pring,
2539 irsp->unsli3.sli3Words[3]);
2540 lpfc_in_buf_free(phba, dmzbuf);
2541 }
2542
2543 if (irsp->ulpBdeCount > 2) {
2544 dmzbuf = lpfc_sli_get_buff(phba, pring,
2545 irsp->unsli3.sli3Words[7]);
2546 lpfc_in_buf_free(phba, dmzbuf);
2547 }
2548
2549 return 1;
2550 }
2551
James Smart92d7f7b2007-06-17 19:56:38 -05002552 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002553 if (irsp->ulpBdeCount != 0) {
2554 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002555 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002556 if (!saveq->context2)
2557 lpfc_printf_log(phba,
2558 KERN_ERR,
2559 LOG_SLI,
2560 "0341 Ring %d Cannot find buffer for "
2561 "an unsolicited iocb. tag 0x%x\n",
2562 pring->ringno,
2563 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002564 }
2565 if (irsp->ulpBdeCount == 2) {
2566 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002567 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002568 if (!saveq->context3)
2569 lpfc_printf_log(phba,
2570 KERN_ERR,
2571 LOG_SLI,
2572 "0342 Ring %d Cannot find buffer for an"
2573 " unsolicited iocb. tag 0x%x\n",
2574 pring->ringno,
2575 irsp->unsli3.sli3Words[7]);
2576 }
2577 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002578 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002579 if (irsp->ulpBdeCount != 0) {
2580 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2581 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002582 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002583 lpfc_printf_log(phba,
2584 KERN_ERR,
2585 LOG_SLI,
2586 "0343 Ring %d Cannot find "
2587 "buffer for an unsolicited iocb"
2588 ". tag 0x%x\n", pring->ringno,
2589 irsp->un.ulpWord[3]);
2590 }
2591 if (irsp->ulpBdeCount == 2) {
2592 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2593 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002594 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002595 lpfc_printf_log(phba,
2596 KERN_ERR,
2597 LOG_SLI,
2598 "0344 Ring %d Cannot find "
2599 "buffer for an unsolicited "
2600 "iocb. tag 0x%x\n",
2601 pring->ringno,
2602 irsp->unsli3.sli3Words[7]);
2603 }
2604 }
James Smart92d7f7b2007-06-17 19:56:38 -05002605 }
James Smart9c2face2008-01-11 01:53:18 -05002606 if (irsp->ulpBdeCount != 0 &&
2607 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2608 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2609 int found = 0;
2610
2611 /* search continue save q for same XRI */
2612 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002613 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2614 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002615 list_add_tail(&saveq->list, &iocbq->list);
2616 found = 1;
2617 break;
2618 }
2619 }
2620 if (!found)
2621 list_add_tail(&saveq->clist,
2622 &pring->iocb_continue_saveq);
2623 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2624 list_del_init(&iocbq->clist);
2625 saveq = iocbq;
2626 irsp = &(saveq->iocb);
2627 } else
2628 return 0;
2629 }
2630 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2631 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2632 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002633 Rctl = FC_RCTL_ELS_REQ;
2634 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002635 } else {
2636 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2637 Rctl = w5p->hcsw.Rctl;
2638 Type = w5p->hcsw.Type;
2639
2640 /* Firmware Workaround */
2641 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2642 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2643 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002644 Rctl = FC_RCTL_ELS_REQ;
2645 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002646 w5p->hcsw.Rctl = Rctl;
2647 w5p->hcsw.Type = Type;
2648 }
2649 }
James Smart92d7f7b2007-06-17 19:56:38 -05002650
James Smart3772a992009-05-22 14:50:54 -04002651 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002652 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002653 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002654 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002655 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002656
James Smart92d7f7b2007-06-17 19:56:38 -05002657 return 1;
dea31012005-04-17 16:05:31 -05002658}
2659
James Smarte59058c2008-08-24 21:49:00 -04002660/**
James Smart3621a712009-04-06 18:47:14 -04002661 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002662 * @phba: Pointer to HBA context object.
2663 * @pring: Pointer to driver SLI ring object.
2664 * @prspiocb: Pointer to response iocb object.
2665 *
2666 * This function looks up the iocb_lookup table to get the command iocb
2667 * corresponding to the given response iocb using the iotag of the
2668 * response iocb. This function is called with the hbalock held.
2669 * This function returns the command iocb object if it finds the command
2670 * iocb else returns NULL.
2671 **/
dea31012005-04-17 16:05:31 -05002672static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002673lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2674 struct lpfc_sli_ring *pring,
2675 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002676{
dea31012005-04-17 16:05:31 -05002677 struct lpfc_iocbq *cmd_iocb = NULL;
2678 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002679 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002680
James Bottomley604a3e32005-10-29 10:28:33 -05002681 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002682
James Bottomley604a3e32005-10-29 10:28:33 -05002683 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2684 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002685 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002686 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart4f2e66c2012-05-09 21:17:07 -04002687 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002688 }
James Bottomley604a3e32005-10-29 10:28:33 -05002689 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002690 }
2691
dea31012005-04-17 16:05:31 -05002692 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002693 "0317 iotag x%x is out off "
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 Smart3772a992009-05-22 14:50:54 -04002728 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2729 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2730 iotag, phba->sli.last_iotag);
2731 return NULL;
2732}
2733
2734/**
James Smart3621a712009-04-06 18:47:14 -04002735 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002736 * @phba: Pointer to HBA context object.
2737 * @pring: Pointer to driver SLI ring object.
2738 * @saveq: Pointer to the response iocb to be processed.
2739 *
2740 * This function is called by the ring event handler for non-fcp
2741 * rings when there is a new response iocb in the response ring.
2742 * The caller is not required to hold any locks. This function
2743 * gets the command iocb associated with the response iocb and
2744 * calls the completion handler for the command iocb. If there
2745 * is no completion handler, the function will free the resources
2746 * associated with command iocb. If the response iocb is for
2747 * an already aborted command iocb, the status of the completion
2748 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2749 * This function always returns 1.
2750 **/
dea31012005-04-17 16:05:31 -05002751static int
James Smart2e0fef82007-06-17 19:56:36 -05002752lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002753 struct lpfc_iocbq *saveq)
2754{
James Smart2e0fef82007-06-17 19:56:36 -05002755 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002756 int rc = 1;
2757 unsigned long iflag;
2758
2759 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002760 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002761 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002762 spin_unlock_irqrestore(&phba->hbalock, iflag);
2763
dea31012005-04-17 16:05:31 -05002764 if (cmdiocbp) {
2765 if (cmdiocbp->iocb_cmpl) {
2766 /*
James Smartea2151b2008-09-07 11:52:10 -04002767 * If an ELS command failed send an event to mgmt
2768 * application.
2769 */
2770 if (saveq->iocb.ulpStatus &&
2771 (pring->ringno == LPFC_ELS_RING) &&
2772 (cmdiocbp->iocb.ulpCommand ==
2773 CMD_ELS_REQUEST64_CR))
2774 lpfc_send_els_failure_event(phba,
2775 cmdiocbp, saveq);
2776
2777 /*
dea31012005-04-17 16:05:31 -05002778 * Post all ELS completions to the worker thread.
2779 * All other are passed to the completion callback.
2780 */
2781 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002782 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2783 (cmdiocbp->iocb_flag &
2784 LPFC_DRIVER_ABORTED)) {
2785 spin_lock_irqsave(&phba->hbalock,
2786 iflag);
James Smart07951072007-04-25 09:51:38 -04002787 cmdiocbp->iocb_flag &=
2788 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002789 spin_unlock_irqrestore(&phba->hbalock,
2790 iflag);
James Smart07951072007-04-25 09:51:38 -04002791 saveq->iocb.ulpStatus =
2792 IOSTAT_LOCAL_REJECT;
2793 saveq->iocb.un.ulpWord[4] =
2794 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002795
2796 /* Firmware could still be in progress
2797 * of DMAing payload, so don't free data
2798 * buffer till after a hbeat.
2799 */
James Smart341af102010-01-26 23:07:37 -05002800 spin_lock_irqsave(&phba->hbalock,
2801 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002802 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002803 spin_unlock_irqrestore(&phba->hbalock,
2804 iflag);
2805 }
James Smart0f65ff62010-02-26 14:14:23 -05002806 if (phba->sli_rev == LPFC_SLI_REV4) {
2807 if (saveq->iocb_flag &
2808 LPFC_EXCHANGE_BUSY) {
2809 /* Set cmdiocb flag for the
2810 * exchange busy so sgl (xri)
2811 * will not be released until
2812 * the abort xri is received
2813 * from hba.
2814 */
2815 spin_lock_irqsave(
2816 &phba->hbalock, iflag);
2817 cmdiocbp->iocb_flag |=
2818 LPFC_EXCHANGE_BUSY;
2819 spin_unlock_irqrestore(
2820 &phba->hbalock, iflag);
2821 }
2822 if (cmdiocbp->iocb_flag &
2823 LPFC_DRIVER_ABORTED) {
2824 /*
2825 * Clear LPFC_DRIVER_ABORTED
2826 * bit in case it was driver
2827 * initiated abort.
2828 */
2829 spin_lock_irqsave(
2830 &phba->hbalock, iflag);
2831 cmdiocbp->iocb_flag &=
2832 ~LPFC_DRIVER_ABORTED;
2833 spin_unlock_irqrestore(
2834 &phba->hbalock, iflag);
2835 cmdiocbp->iocb.ulpStatus =
2836 IOSTAT_LOCAL_REJECT;
2837 cmdiocbp->iocb.un.ulpWord[4] =
2838 IOERR_ABORT_REQUESTED;
2839 /*
2840 * For SLI4, irsiocb contains
2841 * NO_XRI in sli_xritag, it
2842 * shall not affect releasing
2843 * sgl (xri) process.
2844 */
2845 saveq->iocb.ulpStatus =
2846 IOSTAT_LOCAL_REJECT;
2847 saveq->iocb.un.ulpWord[4] =
2848 IOERR_SLI_ABORTED;
2849 spin_lock_irqsave(
2850 &phba->hbalock, iflag);
2851 saveq->iocb_flag |=
2852 LPFC_DELAY_MEM_FREE;
2853 spin_unlock_irqrestore(
2854 &phba->hbalock, iflag);
2855 }
James Smart07951072007-04-25 09:51:38 -04002856 }
dea31012005-04-17 16:05:31 -05002857 }
James Smart2e0fef82007-06-17 19:56:36 -05002858 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002859 } else
2860 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002861 } else {
2862 /*
2863 * Unknown initiating command based on the response iotag.
2864 * This could be the case on the ELS ring because of
2865 * lpfc_els_abort().
2866 */
2867 if (pring->ringno != LPFC_ELS_RING) {
2868 /*
2869 * Ring <ringno> handler: unexpected completion IoTag
2870 * <IoTag>
2871 */
James Smarta257bf92009-04-06 18:48:10 -04002872 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002873 "0322 Ring %d handler: "
2874 "unexpected completion IoTag x%x "
2875 "Data: x%x x%x x%x x%x\n",
2876 pring->ringno,
2877 saveq->iocb.ulpIoTag,
2878 saveq->iocb.ulpStatus,
2879 saveq->iocb.un.ulpWord[4],
2880 saveq->iocb.ulpCommand,
2881 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002882 }
2883 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002884
dea31012005-04-17 16:05:31 -05002885 return rc;
2886}
2887
James Smarte59058c2008-08-24 21:49:00 -04002888/**
James Smart3621a712009-04-06 18:47:14 -04002889 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002890 * @phba: Pointer to HBA context object.
2891 * @pring: Pointer to driver SLI ring object.
2892 *
2893 * This function is called from the iocb ring event handlers when
2894 * put pointer is ahead of the get pointer for a ring. This function signal
2895 * an error attention condition to the worker thread and the worker
2896 * thread will transition the HBA to offline state.
2897 **/
James Smart2e0fef82007-06-17 19:56:36 -05002898static void
2899lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002900{
James Smart34b02dc2008-08-24 21:49:55 -04002901 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002902 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002903 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002904 * rsp ring <portRspMax>
2905 */
2906 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002907 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002908 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002909 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002910 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002911
James Smart2e0fef82007-06-17 19:56:36 -05002912 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002913
2914 /*
2915 * All error attention handlers are posted to
2916 * worker thread
2917 */
2918 phba->work_ha |= HA_ERATT;
2919 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002920
James Smart5e9d9b82008-06-14 22:52:53 -04002921 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002922
2923 return;
2924}
2925
James Smarte59058c2008-08-24 21:49:00 -04002926/**
James Smart3621a712009-04-06 18:47:14 -04002927 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002928 * @ptr: Pointer to address of HBA context object.
2929 *
2930 * This function is invoked by the Error Attention polling timer when the
2931 * timer times out. It will check the SLI Error Attention register for
2932 * possible attention events. If so, it will post an Error Attention event
2933 * and wake up worker thread to process it. Otherwise, it will set up the
2934 * Error Attention polling timer for the next poll.
2935 **/
2936void lpfc_poll_eratt(unsigned long ptr)
2937{
2938 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04002939 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04002940 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002941
2942 phba = (struct lpfc_hba *)ptr;
2943
James Smartaa6fbb72012-08-03 12:36:03 -04002944 /* Here we will also keep track of interrupts per sec of the hba */
2945 sli_intr = phba->sli.slistat.sli_intr;
2946
2947 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2948 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2949 sli_intr);
2950 else
2951 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2952
James Smart65791f12016-07-06 12:35:56 -07002953 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
2954 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04002955 phba->sli.slistat.sli_ips = cnt;
2956
2957 phba->sli.slistat.sli_prev_intr = sli_intr;
2958
James Smart93996272008-08-24 21:50:30 -04002959 /* Check chip HA register for error event */
2960 eratt = lpfc_sli_check_eratt(phba);
2961
2962 if (eratt)
2963 /* Tell the worker thread there is work to do */
2964 lpfc_worker_wake_up(phba);
2965 else
2966 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04002967 mod_timer(&phba->eratt_poll,
2968 jiffies +
James Smart65791f12016-07-06 12:35:56 -07002969 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04002970 return;
2971}
2972
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002973
James Smarte59058c2008-08-24 21:49:00 -04002974/**
James Smart3621a712009-04-06 18:47:14 -04002975 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002976 * @phba: Pointer to HBA context object.
2977 * @pring: Pointer to driver SLI ring object.
2978 * @mask: Host attention register mask for this ring.
2979 *
2980 * This function is called from the interrupt context when there is a ring
2981 * event for the fcp ring. The caller does not hold any lock.
2982 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002983 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002984 * LE bit set. The function will call the completion handler of the command iocb
2985 * if the response iocb indicates a completion for a command iocb or it is
2986 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2987 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002988 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002989 * to check it explicitly.
2990 */
2991int
James Smart2e0fef82007-06-17 19:56:36 -05002992lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2993 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002994{
James Smart34b02dc2008-08-24 21:49:55 -04002995 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002996 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002997 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002998 struct lpfc_iocbq *cmdiocbq = NULL;
2999 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05003000 uint32_t status;
3001 uint32_t portRspPut, portRspMax;
3002 int rc = 1;
3003 lpfc_iocb_type type;
3004 unsigned long iflag;
3005 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003006
James Smart2e0fef82007-06-17 19:56:36 -05003007 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003008 pring->stats.iocb_event++;
3009
dea31012005-04-17 16:05:31 -05003010 /*
3011 * The next available response entry should never exceed the maximum
3012 * entries. If it does, treat it as an adapter hardware error.
3013 */
James Smart7e56aa22012-08-03 12:35:34 -04003014 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003015 portRspPut = le32_to_cpu(pgp->rspPutInx);
3016 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003017 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003018 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003019 return 1;
3020 }
James Smart45ed1192009-10-02 15:17:02 -04003021 if (phba->fcp_ring_in_use) {
3022 spin_unlock_irqrestore(&phba->hbalock, iflag);
3023 return 1;
3024 } else
3025 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003026
3027 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003028 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003029 /*
3030 * Fetch an entry off the ring and copy it into a local data
3031 * structure. The copy involves a byte-swap since the
3032 * network byte order and pci byte orders are different.
3033 */
James Smarted957682007-06-17 19:56:37 -05003034 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003035 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003036
James Smart7e56aa22012-08-03 12:35:34 -04003037 if (++pring->sli.sli3.rspidx >= portRspMax)
3038 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003039
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003040 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3041 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003042 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003043 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003044 irsp = &rspiocbq.iocb;
3045
dea31012005-04-17 16:05:31 -05003046 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3047 pring->stats.iocb_rsp++;
3048 rsp_cmpl++;
3049
3050 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003051 /*
3052 * If resource errors reported from HBA, reduce
3053 * queuedepths of the SCSI device.
3054 */
3055 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003056 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3057 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003058 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003059 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003060 spin_lock_irqsave(&phba->hbalock, iflag);
3061 }
3062
dea31012005-04-17 16:05:31 -05003063 /* Rsp ring <ringno> error: IOCB */
3064 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003065 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003066 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003067 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003068 irsp->un.ulpWord[0],
3069 irsp->un.ulpWord[1],
3070 irsp->un.ulpWord[2],
3071 irsp->un.ulpWord[3],
3072 irsp->un.ulpWord[4],
3073 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003074 *(uint32_t *)&irsp->un1,
3075 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003076 }
3077
3078 switch (type) {
3079 case LPFC_ABORT_IOCB:
3080 case LPFC_SOL_IOCB:
3081 /*
3082 * Idle exchange closed via ABTS from port. No iocb
3083 * resources need to be recovered.
3084 */
3085 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003086 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003087 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003088 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003089 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003090 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003091 break;
3092 }
3093
James Bottomley604a3e32005-10-29 10:28:33 -05003094 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3095 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003096 if (unlikely(!cmdiocbq))
3097 break;
3098 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3099 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3100 if (cmdiocbq->iocb_cmpl) {
3101 spin_unlock_irqrestore(&phba->hbalock, iflag);
3102 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3103 &rspiocbq);
3104 spin_lock_irqsave(&phba->hbalock, iflag);
3105 }
dea31012005-04-17 16:05:31 -05003106 break;
James Smarta4bc3372006-12-02 13:34:16 -05003107 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003108 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003109 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003110 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003111 break;
dea31012005-04-17 16:05:31 -05003112 default:
3113 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3114 char adaptermsg[LPFC_MAX_ADPTMSG];
3115 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3116 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3117 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003118 dev_warn(&((phba->pcidev)->dev),
3119 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003120 phba->brd_no, adaptermsg);
3121 } else {
3122 /* Unknown IOCB command */
3123 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003124 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003125 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003126 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003127 irsp->ulpStatus,
3128 irsp->ulpIoTag,
3129 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003130 }
3131 break;
3132 }
3133
3134 /*
3135 * The response IOCB has been processed. Update the ring
3136 * pointer in SLIM. If the port response put pointer has not
3137 * been updated, sync the pgp->rspPutInx and fetch the new port
3138 * response put pointer.
3139 */
James Smart7e56aa22012-08-03 12:35:34 -04003140 writel(pring->sli.sli3.rspidx,
3141 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003142
James Smart7e56aa22012-08-03 12:35:34 -04003143 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003144 portRspPut = le32_to_cpu(pgp->rspPutInx);
3145 }
3146
3147 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3148 pring->stats.iocb_rsp_full++;
3149 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3150 writel(status, phba->CAregaddr);
3151 readl(phba->CAregaddr);
3152 }
3153 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3154 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3155 pring->stats.iocb_cmd_empty++;
3156
3157 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003158 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003159 lpfc_sli_resume_iocb(phba, pring);
3160
3161 if ((pring->lpfc_sli_cmd_available))
3162 (pring->lpfc_sli_cmd_available) (phba, pring);
3163
3164 }
3165
James Smart45ed1192009-10-02 15:17:02 -04003166 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003167 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003168 return rc;
3169}
3170
James Smarte59058c2008-08-24 21:49:00 -04003171/**
James Smart3772a992009-05-22 14:50:54 -04003172 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3173 * @phba: Pointer to HBA context object.
3174 * @pring: Pointer to driver SLI ring object.
3175 * @rspiocbp: Pointer to driver response IOCB object.
3176 *
3177 * This function is called from the worker thread when there is a slow-path
3178 * response IOCB to process. This function chains all the response iocbs until
3179 * seeing the iocb with the LE bit set. The function will call
3180 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3181 * completion of a command iocb. The function will call the
3182 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3183 * The function frees the resources or calls the completion handler if this
3184 * iocb is an abort completion. The function returns NULL when the response
3185 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3186 * this function shall chain the iocb on to the iocb_continueq and return the
3187 * response iocb passed in.
3188 **/
3189static struct lpfc_iocbq *
3190lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3191 struct lpfc_iocbq *rspiocbp)
3192{
3193 struct lpfc_iocbq *saveq;
3194 struct lpfc_iocbq *cmdiocbp;
3195 struct lpfc_iocbq *next_iocb;
3196 IOCB_t *irsp = NULL;
3197 uint32_t free_saveq;
3198 uint8_t iocb_cmd_type;
3199 lpfc_iocb_type type;
3200 unsigned long iflag;
3201 int rc;
3202
3203 spin_lock_irqsave(&phba->hbalock, iflag);
3204 /* First add the response iocb to the countinueq list */
3205 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3206 pring->iocb_continueq_cnt++;
3207
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003208 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003209 irsp = &rspiocbp->iocb;
3210 if (irsp->ulpLe) {
3211 /*
3212 * By default, the driver expects to free all resources
3213 * associated with this iocb completion.
3214 */
3215 free_saveq = 1;
3216 saveq = list_get_first(&pring->iocb_continueq,
3217 struct lpfc_iocbq, list);
3218 irsp = &(saveq->iocb);
3219 list_del_init(&pring->iocb_continueq);
3220 pring->iocb_continueq_cnt = 0;
3221
3222 pring->stats.iocb_rsp++;
3223
3224 /*
3225 * If resource errors reported from HBA, reduce
3226 * queuedepths of the SCSI device.
3227 */
3228 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003229 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3230 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003231 spin_unlock_irqrestore(&phba->hbalock, iflag);
3232 phba->lpfc_rampdown_queue_depth(phba);
3233 spin_lock_irqsave(&phba->hbalock, iflag);
3234 }
3235
3236 if (irsp->ulpStatus) {
3237 /* Rsp ring <ringno> error: IOCB */
3238 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3239 "0328 Rsp Ring %d error: "
3240 "IOCB Data: "
3241 "x%x x%x x%x x%x "
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\n",
3245 pring->ringno,
3246 irsp->un.ulpWord[0],
3247 irsp->un.ulpWord[1],
3248 irsp->un.ulpWord[2],
3249 irsp->un.ulpWord[3],
3250 irsp->un.ulpWord[4],
3251 irsp->un.ulpWord[5],
3252 *(((uint32_t *) irsp) + 6),
3253 *(((uint32_t *) irsp) + 7),
3254 *(((uint32_t *) irsp) + 8),
3255 *(((uint32_t *) irsp) + 9),
3256 *(((uint32_t *) irsp) + 10),
3257 *(((uint32_t *) irsp) + 11),
3258 *(((uint32_t *) irsp) + 12),
3259 *(((uint32_t *) irsp) + 13),
3260 *(((uint32_t *) irsp) + 14),
3261 *(((uint32_t *) irsp) + 15));
3262 }
3263
3264 /*
3265 * Fetch the IOCB command type and call the correct completion
3266 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3267 * get freed back to the lpfc_iocb_list by the discovery
3268 * kernel thread.
3269 */
3270 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3271 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3272 switch (type) {
3273 case LPFC_SOL_IOCB:
3274 spin_unlock_irqrestore(&phba->hbalock, iflag);
3275 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3276 spin_lock_irqsave(&phba->hbalock, iflag);
3277 break;
3278
3279 case LPFC_UNSOL_IOCB:
3280 spin_unlock_irqrestore(&phba->hbalock, iflag);
3281 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3282 spin_lock_irqsave(&phba->hbalock, iflag);
3283 if (!rc)
3284 free_saveq = 0;
3285 break;
3286
3287 case LPFC_ABORT_IOCB:
3288 cmdiocbp = NULL;
3289 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3290 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3291 saveq);
3292 if (cmdiocbp) {
3293 /* Call the specified completion routine */
3294 if (cmdiocbp->iocb_cmpl) {
3295 spin_unlock_irqrestore(&phba->hbalock,
3296 iflag);
3297 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3298 saveq);
3299 spin_lock_irqsave(&phba->hbalock,
3300 iflag);
3301 } else
3302 __lpfc_sli_release_iocbq(phba,
3303 cmdiocbp);
3304 }
3305 break;
3306
3307 case LPFC_UNKNOWN_IOCB:
3308 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3309 char adaptermsg[LPFC_MAX_ADPTMSG];
3310 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3311 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3312 MAX_MSG_DATA);
3313 dev_warn(&((phba->pcidev)->dev),
3314 "lpfc%d: %s\n",
3315 phba->brd_no, adaptermsg);
3316 } else {
3317 /* Unknown IOCB command */
3318 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3319 "0335 Unknown IOCB "
3320 "command Data: x%x "
3321 "x%x x%x x%x\n",
3322 irsp->ulpCommand,
3323 irsp->ulpStatus,
3324 irsp->ulpIoTag,
3325 irsp->ulpContext);
3326 }
3327 break;
3328 }
3329
3330 if (free_saveq) {
3331 list_for_each_entry_safe(rspiocbp, next_iocb,
3332 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003333 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003334 __lpfc_sli_release_iocbq(phba, rspiocbp);
3335 }
3336 __lpfc_sli_release_iocbq(phba, saveq);
3337 }
3338 rspiocbp = NULL;
3339 }
3340 spin_unlock_irqrestore(&phba->hbalock, iflag);
3341 return rspiocbp;
3342}
3343
3344/**
3345 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003346 * @phba: Pointer to HBA context object.
3347 * @pring: Pointer to driver SLI ring object.
3348 * @mask: Host attention register mask for this ring.
3349 *
James Smart3772a992009-05-22 14:50:54 -04003350 * This routine wraps the actual slow_ring event process routine from the
3351 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003352 **/
James Smart3772a992009-05-22 14:50:54 -04003353void
James Smart2e0fef82007-06-17 19:56:36 -05003354lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3355 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003356{
James Smart3772a992009-05-22 14:50:54 -04003357 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3358}
3359
3360/**
3361 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3362 * @phba: Pointer to HBA context object.
3363 * @pring: Pointer to driver SLI ring object.
3364 * @mask: Host attention register mask for this ring.
3365 *
3366 * This function is called from the worker thread when there is a ring event
3367 * for non-fcp rings. The caller does not hold any lock. The function will
3368 * remove each response iocb in the response ring and calls the handle
3369 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3370 **/
3371static void
3372lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3373 struct lpfc_sli_ring *pring, uint32_t mask)
3374{
James Smart34b02dc2008-08-24 21:49:55 -04003375 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003376 IOCB_t *entry;
3377 IOCB_t *irsp = NULL;
3378 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003379 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003380 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003381 uint32_t status;
dea31012005-04-17 16:05:31 -05003382
James Smart34b02dc2008-08-24 21:49:55 -04003383 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003384 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003385 pring->stats.iocb_event++;
3386
dea31012005-04-17 16:05:31 -05003387 /*
3388 * The next available response entry should never exceed the maximum
3389 * entries. If it does, treat it as an adapter hardware error.
3390 */
James Smart7e56aa22012-08-03 12:35:34 -04003391 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003392 portRspPut = le32_to_cpu(pgp->rspPutInx);
3393 if (portRspPut >= portRspMax) {
3394 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003395 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003396 * rsp ring <portRspMax>
3397 */
James Smarted957682007-06-17 19:56:37 -05003398 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003399 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003400 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003401 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003402
James Smart2e0fef82007-06-17 19:56:36 -05003403 phba->link_state = LPFC_HBA_ERROR;
3404 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003405
3406 phba->work_hs = HS_FFER3;
3407 lpfc_handle_eratt(phba);
3408
James Smart3772a992009-05-22 14:50:54 -04003409 return;
dea31012005-04-17 16:05:31 -05003410 }
3411
3412 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003413 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003414 /*
3415 * Build a completion list and call the appropriate handler.
3416 * The process is to get the next available response iocb, get
3417 * a free iocb from the list, copy the response data into the
3418 * free iocb, insert to the continuation list, and update the
3419 * next response index to slim. This process makes response
3420 * iocb's in the ring available to DMA as fast as possible but
3421 * pays a penalty for a copy operation. Since the iocb is
3422 * only 32 bytes, this penalty is considered small relative to
3423 * the PCI reads for register values and a slim write. When
3424 * the ulpLe field is set, the entire Command has been
3425 * received.
3426 */
James Smarted957682007-06-17 19:56:37 -05003427 entry = lpfc_resp_iocb(phba, pring);
3428
James Smart858c9f62007-06-17 19:56:39 -05003429 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003430 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003431 if (rspiocbp == NULL) {
3432 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003433 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003434 break;
3435 }
3436
James Smarted957682007-06-17 19:56:37 -05003437 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3438 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003439 irsp = &rspiocbp->iocb;
3440
James Smart7e56aa22012-08-03 12:35:34 -04003441 if (++pring->sli.sli3.rspidx >= portRspMax)
3442 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003443
James Smarta58cbd52007-08-02 11:09:43 -04003444 if (pring->ringno == LPFC_ELS_RING) {
3445 lpfc_debugfs_slow_ring_trc(phba,
3446 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3447 *(((uint32_t *) irsp) + 4),
3448 *(((uint32_t *) irsp) + 6),
3449 *(((uint32_t *) irsp) + 7));
3450 }
3451
James Smart7e56aa22012-08-03 12:35:34 -04003452 writel(pring->sli.sli3.rspidx,
3453 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003454
James Smart3772a992009-05-22 14:50:54 -04003455 spin_unlock_irqrestore(&phba->hbalock, iflag);
3456 /* Handle the response IOCB */
3457 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3458 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003459
3460 /*
3461 * If the port response put pointer has not been updated, sync
3462 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3463 * response put pointer.
3464 */
James Smart7e56aa22012-08-03 12:35:34 -04003465 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003466 portRspPut = le32_to_cpu(pgp->rspPutInx);
3467 }
James Smart7e56aa22012-08-03 12:35:34 -04003468 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003469
James Smart92d7f7b2007-06-17 19:56:38 -05003470 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003471 /* At least one response entry has been freed */
3472 pring->stats.iocb_rsp_full++;
3473 /* SET RxRE_RSP in Chip Att register */
3474 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3475 writel(status, phba->CAregaddr);
3476 readl(phba->CAregaddr); /* flush */
3477 }
3478 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3479 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3480 pring->stats.iocb_cmd_empty++;
3481
3482 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003483 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003484 lpfc_sli_resume_iocb(phba, pring);
3485
3486 if ((pring->lpfc_sli_cmd_available))
3487 (pring->lpfc_sli_cmd_available) (phba, pring);
3488
3489 }
3490
James Smart2e0fef82007-06-17 19:56:36 -05003491 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003492 return;
dea31012005-04-17 16:05:31 -05003493}
3494
James Smarte59058c2008-08-24 21:49:00 -04003495/**
James Smart4f774512009-05-22 14:52:35 -04003496 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3497 * @phba: Pointer to HBA context object.
3498 * @pring: Pointer to driver SLI ring object.
3499 * @mask: Host attention register mask for this ring.
3500 *
3501 * This function is called from the worker thread when there is a pending
3502 * ELS response iocb on the driver internal slow-path response iocb worker
3503 * queue. The caller does not hold any lock. The function will remove each
3504 * response iocb from the response worker queue and calls the handle
3505 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3506 **/
3507static void
3508lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3509 struct lpfc_sli_ring *pring, uint32_t mask)
3510{
3511 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003512 struct hbq_dmabuf *dmabuf;
3513 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003514 unsigned long iflag;
3515
James Smart45ed1192009-10-02 15:17:02 -04003516 spin_lock_irqsave(&phba->hbalock, iflag);
3517 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3518 spin_unlock_irqrestore(&phba->hbalock, iflag);
3519 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003520 /* Get the response iocb from the head of work queue */
3521 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003522 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003523 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003524 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003525
3526 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3527 case CQE_CODE_COMPL_WQE:
3528 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3529 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003530 /* Translate ELS WCQE to response IOCBQ */
3531 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3532 irspiocbq);
3533 if (irspiocbq)
3534 lpfc_sli_sp_handle_rspiocb(phba, pring,
3535 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003536 break;
3537 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003538 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003539 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3540 cq_event);
3541 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3542 break;
3543 default:
3544 break;
3545 }
James Smart4f774512009-05-22 14:52:35 -04003546 }
3547}
3548
3549/**
James Smart3621a712009-04-06 18:47:14 -04003550 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003551 * @phba: Pointer to HBA context object.
3552 * @pring: Pointer to driver SLI ring object.
3553 *
3554 * This function aborts all iocbs in the given ring and frees all the iocb
3555 * objects in txq. This function issues an abort iocb for all the iocb commands
3556 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3557 * the return of this function. The caller is not required to hold any locks.
3558 **/
James Smart2e0fef82007-06-17 19:56:36 -05003559void
dea31012005-04-17 16:05:31 -05003560lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3561{
James Smart2534ba72007-04-25 09:52:20 -04003562 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003563 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003564
James Smart92d7f7b2007-06-17 19:56:38 -05003565 if (pring->ringno == LPFC_ELS_RING) {
3566 lpfc_fabric_abort_hba(phba);
3567 }
3568
dea31012005-04-17 16:05:31 -05003569 /* Error everything on txq and txcmplq
3570 * First do the txq.
3571 */
James Smartdb55fba2014-04-04 13:52:02 -04003572 if (phba->sli_rev >= LPFC_SLI_REV4) {
3573 spin_lock_irq(&pring->ring_lock);
3574 list_splice_init(&pring->txq, &completions);
3575 pring->txq_cnt = 0;
3576 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003577
James Smartdb55fba2014-04-04 13:52:02 -04003578 spin_lock_irq(&phba->hbalock);
3579 /* Next issue ABTS for everything on the txcmplq */
3580 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3581 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3582 spin_unlock_irq(&phba->hbalock);
3583 } else {
3584 spin_lock_irq(&phba->hbalock);
3585 list_splice_init(&pring->txq, &completions);
3586 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003587
James Smartdb55fba2014-04-04 13:52:02 -04003588 /* Next issue ABTS for everything on the txcmplq */
3589 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3590 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3591 spin_unlock_irq(&phba->hbalock);
3592 }
James Smart2534ba72007-04-25 09:52:20 -04003593
James Smarta257bf92009-04-06 18:48:10 -04003594 /* Cancel all the IOCBs from the completions list */
3595 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3596 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003597}
3598
James Smarte59058c2008-08-24 21:49:00 -04003599/**
James Smartdb55fba2014-04-04 13:52:02 -04003600 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3601 * @phba: Pointer to HBA context object.
3602 * @pring: Pointer to driver SLI ring object.
3603 *
3604 * This function aborts all iocbs in FCP rings and frees all the iocb
3605 * objects in txq. This function issues an abort iocb for all the iocb commands
3606 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3607 * the return of this function. The caller is not required to hold any locks.
3608 **/
3609void
3610lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3611{
3612 struct lpfc_sli *psli = &phba->sli;
3613 struct lpfc_sli_ring *pring;
3614 uint32_t i;
3615
3616 /* Look on all the FCP Rings for the iotag */
3617 if (phba->sli_rev >= LPFC_SLI_REV4) {
3618 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3619 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3620 lpfc_sli_abort_iocb_ring(phba, pring);
3621 }
3622 } else {
3623 pring = &psli->ring[psli->fcp_ring];
3624 lpfc_sli_abort_iocb_ring(phba, pring);
3625 }
3626}
3627
3628
3629/**
James Smart3621a712009-04-06 18:47:14 -04003630 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003631 * @phba: Pointer to HBA context object.
3632 *
3633 * This function flushes all iocbs in the fcp ring and frees all the iocb
3634 * objects in txq and txcmplq. This function will not issue abort iocbs
3635 * for all the iocb commands in txcmplq, they will just be returned with
3636 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3637 * slot has been permanently disabled.
3638 **/
3639void
3640lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3641{
3642 LIST_HEAD(txq);
3643 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003644 struct lpfc_sli *psli = &phba->sli;
3645 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003646 uint32_t i;
James Smarta8e497d2008-08-24 21:50:11 -04003647
3648 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003649 /* Indicate the I/O queues are flushed */
3650 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003651 spin_unlock_irq(&phba->hbalock);
3652
James Smartdb55fba2014-04-04 13:52:02 -04003653 /* Look on all the FCP Rings for the iotag */
3654 if (phba->sli_rev >= LPFC_SLI_REV4) {
3655 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3656 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
James Smarta8e497d2008-08-24 21:50:11 -04003657
James Smartdb55fba2014-04-04 13:52:02 -04003658 spin_lock_irq(&pring->ring_lock);
3659 /* Retrieve everything on txq */
3660 list_splice_init(&pring->txq, &txq);
3661 /* Retrieve everything on the txcmplq */
3662 list_splice_init(&pring->txcmplq, &txcmplq);
3663 pring->txq_cnt = 0;
3664 pring->txcmplq_cnt = 0;
3665 spin_unlock_irq(&pring->ring_lock);
3666
3667 /* Flush the txq */
3668 lpfc_sli_cancel_iocbs(phba, &txq,
3669 IOSTAT_LOCAL_REJECT,
3670 IOERR_SLI_DOWN);
3671 /* Flush the txcmpq */
3672 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3673 IOSTAT_LOCAL_REJECT,
3674 IOERR_SLI_DOWN);
3675 }
3676 } else {
3677 pring = &psli->ring[psli->fcp_ring];
3678
3679 spin_lock_irq(&phba->hbalock);
3680 /* Retrieve everything on txq */
3681 list_splice_init(&pring->txq, &txq);
3682 /* Retrieve everything on the txcmplq */
3683 list_splice_init(&pring->txcmplq, &txcmplq);
3684 pring->txq_cnt = 0;
3685 pring->txcmplq_cnt = 0;
3686 spin_unlock_irq(&phba->hbalock);
3687
3688 /* Flush the txq */
3689 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3690 IOERR_SLI_DOWN);
3691 /* Flush the txcmpq */
3692 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3693 IOERR_SLI_DOWN);
3694 }
James Smarta8e497d2008-08-24 21:50:11 -04003695}
3696
3697/**
James Smart3772a992009-05-22 14:50:54 -04003698 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003699 * @phba: Pointer to HBA context object.
3700 * @mask: Bit mask to be checked.
3701 *
3702 * This function reads the host status register and compares
3703 * with the provided bit mask to check if HBA completed
3704 * the restart. This function will wait in a loop for the
3705 * HBA to complete restart. If the HBA does not restart within
3706 * 15 iterations, the function will reset the HBA again. The
3707 * function returns 1 when HBA fail to restart otherwise returns
3708 * zero.
3709 **/
James Smart3772a992009-05-22 14:50:54 -04003710static int
3711lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003712{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003713 uint32_t status;
3714 int i = 0;
3715 int retval = 0;
dea31012005-04-17 16:05:31 -05003716
Jamie Wellnitz41415862006-02-28 19:25:27 -05003717 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003718 if (lpfc_readl(phba->HSregaddr, &status))
3719 return 1;
dea31012005-04-17 16:05:31 -05003720
Jamie Wellnitz41415862006-02-28 19:25:27 -05003721 /*
3722 * Check status register every 100ms for 5 retries, then every
3723 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3724 * every 2.5 sec for 4.
3725 * Break our of the loop if errors occurred during init.
3726 */
3727 while (((status & mask) != mask) &&
3728 !(status & HS_FFERM) &&
3729 i++ < 20) {
dea31012005-04-17 16:05:31 -05003730
Jamie Wellnitz41415862006-02-28 19:25:27 -05003731 if (i <= 5)
3732 msleep(10);
3733 else if (i <= 10)
3734 msleep(500);
3735 else
3736 msleep(2500);
dea31012005-04-17 16:05:31 -05003737
Jamie Wellnitz41415862006-02-28 19:25:27 -05003738 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003739 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003740 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003741 lpfc_sli_brdrestart(phba);
3742 }
3743 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003744 if (lpfc_readl(phba->HSregaddr, &status)) {
3745 retval = 1;
3746 break;
3747 }
dea31012005-04-17 16:05:31 -05003748 }
dea31012005-04-17 16:05:31 -05003749
Jamie Wellnitz41415862006-02-28 19:25:27 -05003750 /* Check to see if any errors occurred during init */
3751 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003752 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3753 "2751 Adapter failed to restart, "
3754 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3755 status,
3756 readl(phba->MBslimaddr + 0xa8),
3757 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003758 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003759 retval = 1;
3760 }
dea31012005-04-17 16:05:31 -05003761
Jamie Wellnitz41415862006-02-28 19:25:27 -05003762 return retval;
dea31012005-04-17 16:05:31 -05003763}
3764
James Smartda0436e2009-05-22 14:51:39 -04003765/**
3766 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3767 * @phba: Pointer to HBA context object.
3768 * @mask: Bit mask to be checked.
3769 *
3770 * This function checks the host status register to check if HBA is
3771 * ready. This function will wait in a loop for the HBA to be ready
3772 * If the HBA is not ready , the function will will reset the HBA PCI
3773 * function again. The function returns 1 when HBA fail to be ready
3774 * otherwise returns zero.
3775 **/
3776static int
3777lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3778{
3779 uint32_t status;
3780 int retval = 0;
3781
3782 /* Read the HBA Host Status Register */
3783 status = lpfc_sli4_post_status_check(phba);
3784
3785 if (status) {
3786 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3787 lpfc_sli_brdrestart(phba);
3788 status = lpfc_sli4_post_status_check(phba);
3789 }
3790
3791 /* Check to see if any errors occurred during init */
3792 if (status) {
3793 phba->link_state = LPFC_HBA_ERROR;
3794 retval = 1;
3795 } else
3796 phba->sli4_hba.intr_enable = 0;
3797
3798 return retval;
3799}
3800
3801/**
3802 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3803 * @phba: Pointer to HBA context object.
3804 * @mask: Bit mask to be checked.
3805 *
3806 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3807 * from the API jump table function pointer from the lpfc_hba struct.
3808 **/
3809int
3810lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3811{
3812 return phba->lpfc_sli_brdready(phba, mask);
3813}
3814
James Smart92908312006-03-07 15:04:13 -05003815#define BARRIER_TEST_PATTERN (0xdeadbeef)
3816
James Smarte59058c2008-08-24 21:49:00 -04003817/**
James Smart3621a712009-04-06 18:47:14 -04003818 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003819 * @phba: Pointer to HBA context object.
3820 *
James Smart1b511972011-12-13 13:23:09 -05003821 * This function is called before resetting an HBA. This function is called
3822 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003823 **/
James Smart2e0fef82007-06-17 19:56:36 -05003824void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003825{
James Smart65a29c12006-07-06 15:50:50 -04003826 uint32_t __iomem *resp_buf;
3827 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003828 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003829 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003830 int i;
3831 uint8_t hdrtype;
3832
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003833 lockdep_assert_held(&phba->hbalock);
3834
James Smart92908312006-03-07 15:04:13 -05003835 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3836 if (hdrtype != 0x80 ||
3837 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3838 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3839 return;
3840
3841 /*
3842 * Tell the other part of the chip to suspend temporarily all
3843 * its DMA activity.
3844 */
James Smart65a29c12006-07-06 15:50:50 -04003845 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003846
3847 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003848 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3849 return;
James Smart92908312006-03-07 15:04:13 -05003850 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3851 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003852 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003853
James Smart9940b972011-03-11 16:06:12 -05003854 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3855 return;
3856 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003857 /* Clear Chip error bit */
3858 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003859 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003860 }
3861
3862 mbox = 0;
3863 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3864 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3865
3866 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003867 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003868 writel(mbox, mbox_buf);
3869
James Smart9940b972011-03-11 16:06:12 -05003870 for (i = 0; i < 50; i++) {
3871 if (lpfc_readl((resp_buf + 1), &resp_data))
3872 return;
3873 if (resp_data != ~(BARRIER_TEST_PATTERN))
3874 mdelay(1);
3875 else
3876 break;
3877 }
3878 resp_data = 0;
3879 if (lpfc_readl((resp_buf + 1), &resp_data))
3880 return;
3881 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003882 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003883 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003884 goto restore_hc;
3885 else
3886 goto clear_errat;
3887 }
3888
3889 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003890 resp_data = 0;
3891 for (i = 0; i < 500; i++) {
3892 if (lpfc_readl(resp_buf, &resp_data))
3893 return;
3894 if (resp_data != mbox)
3895 mdelay(1);
3896 else
3897 break;
3898 }
James Smart92908312006-03-07 15:04:13 -05003899
3900clear_errat:
3901
James Smart9940b972011-03-11 16:06:12 -05003902 while (++i < 500) {
3903 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3904 return;
3905 if (!(ha_copy & HA_ERATT))
3906 mdelay(1);
3907 else
3908 break;
3909 }
James Smart92908312006-03-07 15:04:13 -05003910
3911 if (readl(phba->HAregaddr) & HA_ERATT) {
3912 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003913 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003914 }
3915
3916restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003917 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003918 writel(hc_copy, phba->HCregaddr);
3919 readl(phba->HCregaddr); /* flush */
3920}
3921
James Smarte59058c2008-08-24 21:49:00 -04003922/**
James Smart3621a712009-04-06 18:47:14 -04003923 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003924 * @phba: Pointer to HBA context object.
3925 *
3926 * This function issues a kill_board mailbox command and waits for
3927 * the error attention interrupt. This function is called for stopping
3928 * the firmware processing. The caller is not required to hold any
3929 * locks. This function calls lpfc_hba_down_post function to free
3930 * any pending commands after the kill. The function will return 1 when it
3931 * fails to kill the board else will return 0.
3932 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003933int
James Smart2e0fef82007-06-17 19:56:36 -05003934lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003935{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003936 struct lpfc_sli *psli;
3937 LPFC_MBOXQ_t *pmb;
3938 uint32_t status;
3939 uint32_t ha_copy;
3940 int retval;
3941 int i = 0;
3942
3943 psli = &phba->sli;
3944
3945 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003946 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003947 "0329 Kill HBA Data: x%x x%x\n",
3948 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003949
James Smart98c9ea52007-10-27 13:37:33 -04003950 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3951 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003952 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003953
3954 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003955 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003956 if (lpfc_readl(phba->HCregaddr, &status)) {
3957 spin_unlock_irq(&phba->hbalock);
3958 mempool_free(pmb, phba->mbox_mem_pool);
3959 return 1;
3960 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003961 status &= ~HC_ERINT_ENA;
3962 writel(status, phba->HCregaddr);
3963 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003964 phba->link_flag |= LS_IGNORE_ERATT;
3965 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003966
3967 lpfc_kill_board(phba, pmb);
3968 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3969 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3970
3971 if (retval != MBX_SUCCESS) {
3972 if (retval != MBX_BUSY)
3973 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003974 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3975 "2752 KILL_BOARD command failed retval %d\n",
3976 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003977 spin_lock_irq(&phba->hbalock);
3978 phba->link_flag &= ~LS_IGNORE_ERATT;
3979 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003980 return 1;
3981 }
3982
James Smartf4b4c682009-05-22 14:53:12 -04003983 spin_lock_irq(&phba->hbalock);
3984 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3985 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003986
Jamie Wellnitz41415862006-02-28 19:25:27 -05003987 mempool_free(pmb, phba->mbox_mem_pool);
3988
3989 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3990 * attention every 100ms for 3 seconds. If we don't get ERATT after
3991 * 3 seconds we still set HBA_ERROR state because the status of the
3992 * board is now undefined.
3993 */
James Smart9940b972011-03-11 16:06:12 -05003994 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3995 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003996 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3997 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003998 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3999 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004000 }
4001
4002 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05004003 if (ha_copy & HA_ERATT) {
4004 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004005 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004006 }
James Smart2e0fef82007-06-17 19:56:36 -05004007 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004008 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004009 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004010 phba->link_flag &= ~LS_IGNORE_ERATT;
4011 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004012
Jamie Wellnitz41415862006-02-28 19:25:27 -05004013 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004014 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004015
James Smart2e0fef82007-06-17 19:56:36 -05004016 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004017}
4018
James Smarte59058c2008-08-24 21:49:00 -04004019/**
James Smart3772a992009-05-22 14:50:54 -04004020 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004021 * @phba: Pointer to HBA context object.
4022 *
4023 * This function resets the HBA by writing HC_INITFF to the control
4024 * register. After the HBA resets, this function resets all the iocb ring
4025 * indices. This function disables PCI layer parity checking during
4026 * the reset.
4027 * This function returns 0 always.
4028 * The caller is not required to hold any locks.
4029 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004030int
James Smart2e0fef82007-06-17 19:56:36 -05004031lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004032{
4033 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004034 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004035 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004036 int i;
dea31012005-04-17 16:05:31 -05004037
Jamie Wellnitz41415862006-02-28 19:25:27 -05004038 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004039
Jamie Wellnitz41415862006-02-28 19:25:27 -05004040 /* Reset HBA */
4041 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004042 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004043 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05004044
4045 /* perform board reset */
4046 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004047 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004048 phba->pport->fc_myDID = 0;
4049 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05004050
Jamie Wellnitz41415862006-02-28 19:25:27 -05004051 /* Turn off parity checking and serr during the physical reset */
4052 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4053 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4054 (cfg_value &
4055 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4056
James Smart3772a992009-05-22 14:50:54 -04004057 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4058
Jamie Wellnitz41415862006-02-28 19:25:27 -05004059 /* Now toggle INITFF bit in the Host Control Register */
4060 writel(HC_INITFF, phba->HCregaddr);
4061 mdelay(1);
4062 readl(phba->HCregaddr); /* flush */
4063 writel(0, phba->HCregaddr);
4064 readl(phba->HCregaddr); /* flush */
4065
4066 /* Restore PCI cmd register */
4067 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004068
4069 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004070 for (i = 0; i < psli->num_rings; i++) {
4071 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05004072 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004073 pring->sli.sli3.rspidx = 0;
4074 pring->sli.sli3.next_cmdidx = 0;
4075 pring->sli.sli3.local_getidx = 0;
4076 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004077 pring->missbufcnt = 0;
4078 }
dea31012005-04-17 16:05:31 -05004079
James Smart2e0fef82007-06-17 19:56:36 -05004080 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004081 return 0;
4082}
4083
James Smarte59058c2008-08-24 21:49:00 -04004084/**
James Smartda0436e2009-05-22 14:51:39 -04004085 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4086 * @phba: Pointer to HBA context object.
4087 *
4088 * This function resets a SLI4 HBA. This function disables PCI layer parity
4089 * checking during resets the device. The caller is not required to hold
4090 * any locks.
4091 *
4092 * This function returns 0 always.
4093 **/
4094int
4095lpfc_sli4_brdreset(struct lpfc_hba *phba)
4096{
4097 struct lpfc_sli *psli = &phba->sli;
4098 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004099 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004100
4101 /* Reset HBA */
4102 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004103 "0295 Reset HBA Data: x%x x%x x%x\n",
4104 phba->pport->port_state, psli->sli_flag,
4105 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004106
4107 /* perform board reset */
4108 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004109 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004110 phba->pport->fc_myDID = 0;
4111 phba->pport->fc_prevDID = 0;
4112
James Smartda0436e2009-05-22 14:51:39 -04004113 spin_lock_irq(&phba->hbalock);
4114 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4115 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004116 spin_unlock_irq(&phba->hbalock);
4117
James Smart02936352014-04-04 13:52:12 -04004118 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4119 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4120 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4121 return rc;
4122 }
4123
James Smartda0436e2009-05-22 14:51:39 -04004124 /* Now physically reset the device */
4125 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4126 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004127
4128 /* Turn off parity checking and serr during the physical reset */
4129 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4130 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4131 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4132
James Smart88318812012-09-29 11:29:29 -04004133 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004134 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04004135 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004136
James Smartbe858b62010-12-15 17:57:20 -05004137 /* Restore PCI cmd register */
4138 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4139
James Smart27b01b82012-05-09 21:19:44 -04004140 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004141}
4142
4143/**
4144 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004145 * @phba: Pointer to HBA context object.
4146 *
4147 * This function is called in the SLI initialization code path to
4148 * restart the HBA. The caller is not required to hold any lock.
4149 * This function writes MBX_RESTART mailbox command to the SLIM and
4150 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4151 * function to free any pending commands. The function enables
4152 * POST only during the first initialization. The function returns zero.
4153 * The function does not guarantee completion of MBX_RESTART mailbox
4154 * command before the return of this function.
4155 **/
James Smartda0436e2009-05-22 14:51:39 -04004156static int
4157lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004158{
4159 MAILBOX_t *mb;
4160 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004161 volatile uint32_t word0;
4162 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004163 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004164
James Smart2e0fef82007-06-17 19:56:36 -05004165 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004166
James Smart0d878412009-10-02 15:16:56 -04004167 /* Take PCIe device Advanced Error Reporting (AER) state */
4168 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4169
Jamie Wellnitz41415862006-02-28 19:25:27 -05004170 psli = &phba->sli;
4171
4172 /* Restart HBA */
4173 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004174 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004175 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004176
4177 word0 = 0;
4178 mb = (MAILBOX_t *) &word0;
4179 mb->mbxCommand = MBX_RESTART;
4180 mb->mbxHc = 1;
4181
James Smart92908312006-03-07 15:04:13 -05004182 lpfc_reset_barrier(phba);
4183
Jamie Wellnitz41415862006-02-28 19:25:27 -05004184 to_slim = phba->MBslimaddr;
4185 writel(*(uint32_t *) mb, to_slim);
4186 readl(to_slim); /* flush */
4187
4188 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004189 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004190 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004191 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004192 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004193 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004194 writel(*(uint32_t *) mb, to_slim);
4195 readl(to_slim); /* flush */
4196
4197 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004198 phba->pport->stopped = 0;
4199 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004200 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004201 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004202
James Smart64ba8812006-08-02 15:24:34 -04004203 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4204 psli->stats_start = get_seconds();
4205
James Smarteaf15d52008-12-04 22:39:29 -05004206 /* Give the INITFF and Post time to settle. */
4207 mdelay(100);
dea31012005-04-17 16:05:31 -05004208
James Smart0d878412009-10-02 15:16:56 -04004209 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4210 if (hba_aer_enabled)
4211 pci_disable_pcie_error_reporting(phba->pcidev);
4212
Jamie Wellnitz41415862006-02-28 19:25:27 -05004213 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004214
4215 return 0;
4216}
4217
James Smarte59058c2008-08-24 21:49:00 -04004218/**
James Smartda0436e2009-05-22 14:51:39 -04004219 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4220 * @phba: Pointer to HBA context object.
4221 *
4222 * This function is called in the SLI initialization code path to restart
4223 * a SLI4 HBA. The caller is not required to hold any lock.
4224 * At the end of the function, it calls lpfc_hba_down_post function to
4225 * free any pending commands.
4226 **/
4227static int
4228lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4229{
4230 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004231 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004232 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004233
4234 /* Restart HBA */
4235 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4236 "0296 Restart HBA Data: x%x x%x\n",
4237 phba->pport->port_state, psli->sli_flag);
4238
James Smart75baf692010-06-08 18:31:21 -04004239 /* Take PCIe device Advanced Error Reporting (AER) state */
4240 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4241
James Smart27b01b82012-05-09 21:19:44 -04004242 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004243
4244 spin_lock_irq(&phba->hbalock);
4245 phba->pport->stopped = 0;
4246 phba->link_state = LPFC_INIT_START;
4247 phba->hba_flag = 0;
4248 spin_unlock_irq(&phba->hbalock);
4249
4250 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4251 psli->stats_start = get_seconds();
4252
James Smart75baf692010-06-08 18:31:21 -04004253 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4254 if (hba_aer_enabled)
4255 pci_disable_pcie_error_reporting(phba->pcidev);
4256
James Smartda0436e2009-05-22 14:51:39 -04004257 lpfc_hba_down_post(phba);
4258
James Smart27b01b82012-05-09 21:19:44 -04004259 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004260}
4261
4262/**
4263 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4264 * @phba: Pointer to HBA context object.
4265 *
4266 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4267 * API jump table function pointer from the lpfc_hba struct.
4268**/
4269int
4270lpfc_sli_brdrestart(struct lpfc_hba *phba)
4271{
4272 return phba->lpfc_sli_brdrestart(phba);
4273}
4274
4275/**
James Smart3621a712009-04-06 18:47:14 -04004276 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004277 * @phba: Pointer to HBA context object.
4278 *
4279 * This function is called after a HBA restart to wait for successful
4280 * restart of the HBA. Successful restart of the HBA is indicated by
4281 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4282 * iteration, the function will restart the HBA again. The function returns
4283 * zero if HBA successfully restarted else returns negative error code.
4284 **/
dea31012005-04-17 16:05:31 -05004285static int
4286lpfc_sli_chipset_init(struct lpfc_hba *phba)
4287{
4288 uint32_t status, i = 0;
4289
4290 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004291 if (lpfc_readl(phba->HSregaddr, &status))
4292 return -EIO;
dea31012005-04-17 16:05:31 -05004293
4294 /* Check status register to see what current state is */
4295 i = 0;
4296 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4297
James Smartdcf2a4e2010-09-29 11:18:53 -04004298 /* Check every 10ms for 10 retries, then every 100ms for 90
4299 * retries, then every 1 sec for 50 retires for a total of
4300 * ~60 seconds before reset the board again and check every
4301 * 1 sec for 50 retries. The up to 60 seconds before the
4302 * board ready is required by the Falcon FIPS zeroization
4303 * complete, and any reset the board in between shall cause
4304 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004305 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004306 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004307 /* Adapter failed to init, timeout, status reg
4308 <status> */
James Smarted957682007-06-17 19:56:37 -05004309 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004310 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004311 "timeout, status reg x%x, "
4312 "FW Data: A8 x%x AC x%x\n", status,
4313 readl(phba->MBslimaddr + 0xa8),
4314 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004315 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004316 return -ETIMEDOUT;
4317 }
4318
4319 /* Check to see if any errors occurred during init */
4320 if (status & HS_FFERM) {
4321 /* ERROR: During chipset initialization */
4322 /* Adapter failed to init, chipset, status reg
4323 <status> */
James Smarted957682007-06-17 19:56:37 -05004324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004325 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004326 "chipset, status reg x%x, "
4327 "FW Data: A8 x%x AC x%x\n", status,
4328 readl(phba->MBslimaddr + 0xa8),
4329 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004330 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004331 return -EIO;
4332 }
4333
James Smartdcf2a4e2010-09-29 11:18:53 -04004334 if (i <= 10)
dea31012005-04-17 16:05:31 -05004335 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004336 else if (i <= 100)
4337 msleep(100);
4338 else
4339 msleep(1000);
dea31012005-04-17 16:05:31 -05004340
James Smartdcf2a4e2010-09-29 11:18:53 -04004341 if (i == 150) {
4342 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004343 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004344 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004345 }
4346 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004347 if (lpfc_readl(phba->HSregaddr, &status))
4348 return -EIO;
dea31012005-04-17 16:05:31 -05004349 }
4350
4351 /* Check to see if any errors occurred during init */
4352 if (status & HS_FFERM) {
4353 /* ERROR: During chipset initialization */
4354 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004355 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004356 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004357 "status reg x%x, "
4358 "FW Data: A8 x%x AC x%x\n", status,
4359 readl(phba->MBslimaddr + 0xa8),
4360 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004361 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004362 return -EIO;
4363 }
4364
4365 /* Clear all interrupt enable conditions */
4366 writel(0, phba->HCregaddr);
4367 readl(phba->HCregaddr); /* flush */
4368
4369 /* setup host attn register */
4370 writel(0xffffffff, phba->HAregaddr);
4371 readl(phba->HAregaddr); /* flush */
4372 return 0;
4373}
4374
James Smarte59058c2008-08-24 21:49:00 -04004375/**
James Smart3621a712009-04-06 18:47:14 -04004376 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004377 *
4378 * This function calculates and returns the number of HBQs required to be
4379 * configured.
4380 **/
James Smart78b2d852007-08-02 11:10:21 -04004381int
James Smarted957682007-06-17 19:56:37 -05004382lpfc_sli_hbq_count(void)
4383{
James Smart92d7f7b2007-06-17 19:56:38 -05004384 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004385}
4386
James Smarte59058c2008-08-24 21:49:00 -04004387/**
James Smart3621a712009-04-06 18:47:14 -04004388 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004389 *
4390 * This function adds the number of hbq entries in every HBQ to get
4391 * the total number of hbq entries required for the HBA and returns
4392 * the total count.
4393 **/
James Smarted957682007-06-17 19:56:37 -05004394static int
4395lpfc_sli_hbq_entry_count(void)
4396{
4397 int hbq_count = lpfc_sli_hbq_count();
4398 int count = 0;
4399 int i;
4400
4401 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004402 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004403 return count;
4404}
4405
James Smarte59058c2008-08-24 21:49:00 -04004406/**
James Smart3621a712009-04-06 18:47:14 -04004407 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004408 *
4409 * This function calculates amount of memory required for all hbq entries
4410 * to be configured and returns the total memory required.
4411 **/
dea31012005-04-17 16:05:31 -05004412int
James Smarted957682007-06-17 19:56:37 -05004413lpfc_sli_hbq_size(void)
4414{
4415 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4416}
4417
James Smarte59058c2008-08-24 21:49:00 -04004418/**
James Smart3621a712009-04-06 18:47:14 -04004419 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004420 * @phba: Pointer to HBA context object.
4421 *
4422 * This function is called during the SLI initialization to configure
4423 * all the HBQs and post buffers to the HBQ. The caller is not
4424 * required to hold any locks. This function will return zero if successful
4425 * else it will return negative error code.
4426 **/
James Smarted957682007-06-17 19:56:37 -05004427static int
4428lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4429{
4430 int hbq_count = lpfc_sli_hbq_count();
4431 LPFC_MBOXQ_t *pmb;
4432 MAILBOX_t *pmbox;
4433 uint32_t hbqno;
4434 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004435
James Smart92d7f7b2007-06-17 19:56:38 -05004436 /* Get a Mailbox buffer to setup mailbox
4437 * commands for HBA initialization
4438 */
James Smarted957682007-06-17 19:56:37 -05004439 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4440
4441 if (!pmb)
4442 return -ENOMEM;
4443
James Smart04c68492009-05-22 14:52:52 -04004444 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004445
4446 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4447 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004448 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004449
4450 hbq_entry_index = 0;
4451 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4452 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4453 phba->hbqs[hbqno].hbqPutIdx = 0;
4454 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4455 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004456 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004457 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4458 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004459 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4460
4461 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4462 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4463 mbxStatus <status>, ring <num> */
4464
4465 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004466 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004467 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004468 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004469 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004470 pmbox->mbxStatus, hbqno);
4471
4472 phba->link_state = LPFC_HBA_ERROR;
4473 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004474 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004475 }
4476 }
4477 phba->hbq_count = hbq_count;
4478
James Smarted957682007-06-17 19:56:37 -05004479 mempool_free(pmb, phba->mbox_mem_pool);
4480
James Smart92d7f7b2007-06-17 19:56:38 -05004481 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004482 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4483 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004484 return 0;
4485}
4486
James Smarte59058c2008-08-24 21:49:00 -04004487/**
James Smart4f774512009-05-22 14:52:35 -04004488 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4489 * @phba: Pointer to HBA context object.
4490 *
4491 * This function is called during the SLI initialization to configure
4492 * all the HBQs and post buffers to the HBQ. The caller is not
4493 * required to hold any locks. This function will return zero if successful
4494 * else it will return negative error code.
4495 **/
4496static int
4497lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4498{
4499 phba->hbq_in_use = 1;
4500 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4501 phba->hbq_count = 1;
4502 /* Initially populate or replenish the HBQs */
4503 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4504 return 0;
4505}
4506
4507/**
James Smart3621a712009-04-06 18:47:14 -04004508 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004509 * @phba: Pointer to HBA context object.
4510 * @sli_mode: sli mode - 2/3
4511 *
4512 * This function is called by the sli intialization code path
4513 * to issue config_port mailbox command. This function restarts the
4514 * HBA firmware and issues a config_port mailbox command to configure
4515 * the SLI interface in the sli mode specified by sli_mode
4516 * variable. The caller is not required to hold any locks.
4517 * The function returns 0 if successful, else returns negative error
4518 * code.
4519 **/
James Smart93996272008-08-24 21:50:30 -04004520int
4521lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004522{
4523 LPFC_MBOXQ_t *pmb;
4524 uint32_t resetcount = 0, rc = 0, done = 0;
4525
4526 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4527 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004528 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004529 return -ENOMEM;
4530 }
4531
James Smarted957682007-06-17 19:56:37 -05004532 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004533 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004534 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004535 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004536 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004537 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004538 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004539 rc = lpfc_sli_chipset_init(phba);
4540 if (rc)
4541 break;
4542
James Smart2e0fef82007-06-17 19:56:36 -05004543 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004544 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004545 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004546 resetcount++;
4547
James Smarted957682007-06-17 19:56:37 -05004548 /* Call pre CONFIG_PORT mailbox command initialization. A
4549 * value of 0 means the call was successful. Any other
4550 * nonzero value is a failure, but if ERESTART is returned,
4551 * the driver may reset the HBA and try again.
4552 */
dea31012005-04-17 16:05:31 -05004553 rc = lpfc_config_port_prep(phba);
4554 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004555 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004556 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004557 } else if (rc)
dea31012005-04-17 16:05:31 -05004558 break;
James Smart6d368e52011-05-24 11:44:12 -04004559
James Smart2e0fef82007-06-17 19:56:36 -05004560 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004561 lpfc_config_port(phba, pmb);
4562 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004563 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4564 LPFC_SLI3_HBQ_ENABLED |
4565 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004566 LPFC_SLI3_BG_ENABLED |
4567 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004568 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004570 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004571 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004572 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004573 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004574 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004575 spin_unlock_irq(&phba->hbalock);
4576 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004577 } else {
4578 /* Allow asynchronous mailbox command to go through */
4579 spin_lock_irq(&phba->hbalock);
4580 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4581 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004582 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004583
4584 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4585 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4586 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4587 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004588 }
dea31012005-04-17 16:05:31 -05004589 }
James Smarted957682007-06-17 19:56:37 -05004590 if (!done) {
4591 rc = -EINVAL;
4592 goto do_prep_failed;
4593 }
James Smart04c68492009-05-22 14:52:52 -04004594 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4595 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004596 rc = -ENXIO;
4597 goto do_prep_failed;
4598 }
James Smart04c68492009-05-22 14:52:52 -04004599 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004600 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004601 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4602 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4603 phba->max_vpi : phba->max_vports;
4604
James Smart34b02dc2008-08-24 21:49:55 -04004605 } else
4606 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004607 phba->fips_level = 0;
4608 phba->fips_spec_rev = 0;
4609 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004610 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004611 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4612 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4613 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4614 "2850 Security Crypto Active. FIPS x%d "
4615 "(Spec Rev: x%d)",
4616 phba->fips_level, phba->fips_spec_rev);
4617 }
4618 if (pmb->u.mb.un.varCfgPort.sec_err) {
4619 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4620 "2856 Config Port Security Crypto "
4621 "Error: x%x ",
4622 pmb->u.mb.un.varCfgPort.sec_err);
4623 }
James Smart04c68492009-05-22 14:52:52 -04004624 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004625 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004626 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004627 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004628
4629 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4630 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004631
4632 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004633 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004634 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4635 else
4636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4637 "0443 Adapter did not grant "
4638 "BlockGuard\n");
4639 }
James Smart34b02dc2008-08-24 21:49:55 -04004640 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004641 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004642 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004643 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004644 }
James Smart92d7f7b2007-06-17 19:56:38 -05004645do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004646 mempool_free(pmb, phba->mbox_mem_pool);
4647 return rc;
4648}
4649
James Smarte59058c2008-08-24 21:49:00 -04004650
4651/**
James Smart3621a712009-04-06 18:47:14 -04004652 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004653 * @phba: Pointer to HBA context object.
4654 *
4655 * This function is the main SLI intialization function. This function
4656 * is called by the HBA intialization code, HBA reset code and HBA
4657 * error attention handler code. Caller is not required to hold any
4658 * locks. This function issues config_port mailbox command to configure
4659 * the SLI, setup iocb rings and HBQ rings. In the end the function
4660 * calls the config_port_post function to issue init_link mailbox
4661 * command and to start the discovery. The function will return zero
4662 * if successful, else it will return negative error code.
4663 **/
James Smarted957682007-06-17 19:56:37 -05004664int
4665lpfc_sli_hba_setup(struct lpfc_hba *phba)
4666{
4667 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004668 int mode = 3, i;
4669 int longs;
James Smarted957682007-06-17 19:56:37 -05004670
James Smart12247e82016-07-06 12:36:09 -07004671 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05004672 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004673 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004675 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004676 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07004677 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004678 break;
4679 }
James Smarted957682007-06-17 19:56:37 -05004680 mode = 2;
4681 break;
4682 case 0:
4683 case 3:
4684 break;
4685 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004687 "1819 Unrecognized sli_mode parameter: %d.\n",
4688 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004689
4690 break;
4691 }
James Smartb5c53952016-03-31 14:12:30 -07004692 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004693
James Smart93996272008-08-24 21:50:30 -04004694 rc = lpfc_sli_config_port(phba, mode);
4695
James Smart12247e82016-07-06 12:36:09 -07004696 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004697 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004698 "1820 Unable to select SLI-3. "
4699 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004700 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004701 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07004702 else if (rc && mode == 2)
4703 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05004704 if (rc)
dea31012005-04-17 16:05:31 -05004705 goto lpfc_sli_hba_setup_error;
4706
James Smart0d878412009-10-02 15:16:56 -04004707 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4708 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4709 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4710 if (!rc) {
4711 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4712 "2709 This device supports "
4713 "Advanced Error Reporting (AER)\n");
4714 spin_lock_irq(&phba->hbalock);
4715 phba->hba_flag |= HBA_AER_ENABLED;
4716 spin_unlock_irq(&phba->hbalock);
4717 } else {
4718 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4719 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004720 "Advanced Error Reporting (AER): %d\n",
4721 rc);
James Smart0d878412009-10-02 15:16:56 -04004722 phba->cfg_aer_support = 0;
4723 }
4724 }
4725
James Smarted957682007-06-17 19:56:37 -05004726 if (phba->sli_rev == 3) {
4727 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4728 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004729 } else {
4730 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4731 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004732 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004733 }
4734
4735 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004736 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4737 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004738 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004739
4740 if (rc)
4741 goto lpfc_sli_hba_setup_error;
4742
James Smart6d368e52011-05-24 11:44:12 -04004743 /* Initialize VPIs. */
4744 if (phba->sli_rev == LPFC_SLI_REV3) {
4745 /*
4746 * The VPI bitmask and physical ID array are allocated
4747 * and initialized once only - at driver load. A port
4748 * reset doesn't need to reinitialize this memory.
4749 */
4750 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4751 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4752 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4753 GFP_KERNEL);
4754 if (!phba->vpi_bmask) {
4755 rc = -ENOMEM;
4756 goto lpfc_sli_hba_setup_error;
4757 }
4758
4759 phba->vpi_ids = kzalloc(
4760 (phba->max_vpi+1) * sizeof(uint16_t),
4761 GFP_KERNEL);
4762 if (!phba->vpi_ids) {
4763 kfree(phba->vpi_bmask);
4764 rc = -ENOMEM;
4765 goto lpfc_sli_hba_setup_error;
4766 }
4767 for (i = 0; i < phba->max_vpi; i++)
4768 phba->vpi_ids[i] = i;
4769 }
4770 }
4771
James Smart93996272008-08-24 21:50:30 -04004772 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004773 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4774 rc = lpfc_sli_hbq_setup(phba);
4775 if (rc)
4776 goto lpfc_sli_hba_setup_error;
4777 }
James Smart04c68492009-05-22 14:52:52 -04004778 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004779 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004780 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004781
4782 rc = lpfc_config_port_post(phba);
4783 if (rc)
4784 goto lpfc_sli_hba_setup_error;
4785
James Smarted957682007-06-17 19:56:37 -05004786 return rc;
4787
James Smart92d7f7b2007-06-17 19:56:38 -05004788lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004789 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004791 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004792 return rc;
4793}
4794
James Smartda0436e2009-05-22 14:51:39 -04004795/**
4796 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4797 * @phba: Pointer to HBA context object.
4798 * @mboxq: mailbox pointer.
4799 * This function issue a dump mailbox command to read config region
4800 * 23 and parse the records in the region and populate driver
4801 * data structure.
4802 **/
4803static int
James Smartff78d8f2011-12-13 13:21:35 -05004804lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004805{
James Smartff78d8f2011-12-13 13:21:35 -05004806 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004807 struct lpfc_dmabuf *mp;
4808 struct lpfc_mqe *mqe;
4809 uint32_t data_length;
4810 int rc;
4811
4812 /* Program the default value of vlan_id and fc_map */
4813 phba->valid_vlan = 0;
4814 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4815 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4816 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4817
James Smartff78d8f2011-12-13 13:21:35 -05004818 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4819 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004820 return -ENOMEM;
4821
James Smartff78d8f2011-12-13 13:21:35 -05004822 mqe = &mboxq->u.mqe;
4823 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4824 rc = -ENOMEM;
4825 goto out_free_mboxq;
4826 }
4827
James Smartda0436e2009-05-22 14:51:39 -04004828 mp = (struct lpfc_dmabuf *) mboxq->context1;
4829 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4830
4831 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4832 "(%d):2571 Mailbox cmd x%x Status x%x "
4833 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4834 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4835 "CQ: x%x x%x x%x x%x\n",
4836 mboxq->vport ? mboxq->vport->vpi : 0,
4837 bf_get(lpfc_mqe_command, mqe),
4838 bf_get(lpfc_mqe_status, mqe),
4839 mqe->un.mb_words[0], mqe->un.mb_words[1],
4840 mqe->un.mb_words[2], mqe->un.mb_words[3],
4841 mqe->un.mb_words[4], mqe->un.mb_words[5],
4842 mqe->un.mb_words[6], mqe->un.mb_words[7],
4843 mqe->un.mb_words[8], mqe->un.mb_words[9],
4844 mqe->un.mb_words[10], mqe->un.mb_words[11],
4845 mqe->un.mb_words[12], mqe->un.mb_words[13],
4846 mqe->un.mb_words[14], mqe->un.mb_words[15],
4847 mqe->un.mb_words[16], mqe->un.mb_words[50],
4848 mboxq->mcqe.word0,
4849 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4850 mboxq->mcqe.trailer);
4851
4852 if (rc) {
4853 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4854 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004855 rc = -EIO;
4856 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004857 }
4858 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004859 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004860 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4861 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004862 rc = -EIO;
4863 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004864 }
James Smartda0436e2009-05-22 14:51:39 -04004865
4866 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4867 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4868 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004869 rc = 0;
4870
4871out_free_mboxq:
4872 mempool_free(mboxq, phba->mbox_mem_pool);
4873 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004874}
4875
4876/**
4877 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4878 * @phba: pointer to lpfc hba data structure.
4879 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4880 * @vpd: pointer to the memory to hold resulting port vpd data.
4881 * @vpd_size: On input, the number of bytes allocated to @vpd.
4882 * On output, the number of data bytes in @vpd.
4883 *
4884 * This routine executes a READ_REV SLI4 mailbox command. In
4885 * addition, this routine gets the port vpd data.
4886 *
4887 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004888 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004889 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004890 **/
4891static int
4892lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4893 uint8_t *vpd, uint32_t *vpd_size)
4894{
4895 int rc = 0;
4896 uint32_t dma_size;
4897 struct lpfc_dmabuf *dmabuf;
4898 struct lpfc_mqe *mqe;
4899
4900 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4901 if (!dmabuf)
4902 return -ENOMEM;
4903
4904 /*
4905 * Get a DMA buffer for the vpd data resulting from the READ_REV
4906 * mailbox command.
4907 */
4908 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04004909 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
4910 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04004911 if (!dmabuf->virt) {
4912 kfree(dmabuf);
4913 return -ENOMEM;
4914 }
James Smartda0436e2009-05-22 14:51:39 -04004915
4916 /*
4917 * The SLI4 implementation of READ_REV conflicts at word1,
4918 * bits 31:16 and SLI4 adds vpd functionality not present
4919 * in SLI3. This code corrects the conflicts.
4920 */
4921 lpfc_read_rev(phba, mboxq);
4922 mqe = &mboxq->u.mqe;
4923 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4924 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4925 mqe->un.read_rev.word1 &= 0x0000FFFF;
4926 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4927 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4928
4929 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4930 if (rc) {
4931 dma_free_coherent(&phba->pcidev->dev, dma_size,
4932 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004933 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004934 return -EIO;
4935 }
4936
James Smartda0436e2009-05-22 14:51:39 -04004937 /*
4938 * The available vpd length cannot be bigger than the
4939 * DMA buffer passed to the port. Catch the less than
4940 * case and update the caller's size.
4941 */
4942 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4943 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4944
James Smartd7c47992010-06-08 18:31:54 -04004945 memcpy(vpd, dmabuf->virt, *vpd_size);
4946
James Smartda0436e2009-05-22 14:51:39 -04004947 dma_free_coherent(&phba->pcidev->dev, dma_size,
4948 dmabuf->virt, dmabuf->phys);
4949 kfree(dmabuf);
4950 return 0;
4951}
4952
4953/**
James Smartcd1c8302011-10-10 21:33:25 -04004954 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4955 * @phba: pointer to lpfc hba data structure.
4956 *
4957 * This routine retrieves SLI4 device physical port name this PCI function
4958 * is attached to.
4959 *
4960 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004961 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04004962 * otherwise - failed to retrieve physical port name
4963 **/
4964static int
4965lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4966{
4967 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004968 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4969 struct lpfc_controller_attribute *cntl_attr;
4970 struct lpfc_mbx_get_port_name *get_port_name;
4971 void *virtaddr = NULL;
4972 uint32_t alloclen, reqlen;
4973 uint32_t shdr_status, shdr_add_status;
4974 union lpfc_sli4_cfg_shdr *shdr;
4975 char cport_name = 0;
4976 int rc;
4977
4978 /* We assume nothing at this point */
4979 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4980 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4981
4982 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4983 if (!mboxq)
4984 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004985 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004986 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4987 lpfc_sli4_read_config(phba);
4988 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4989 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004990
4991 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4992 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4993 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4994 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4995 LPFC_SLI4_MBX_NEMBED);
4996 if (alloclen < reqlen) {
4997 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4998 "3084 Allocated DMA memory size (%d) is "
4999 "less than the requested DMA memory size "
5000 "(%d)\n", alloclen, reqlen);
5001 rc = -ENOMEM;
5002 goto out_free_mboxq;
5003 }
5004 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5005 virtaddr = mboxq->sge_array->addr[0];
5006 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5007 shdr = &mbx_cntl_attr->cfg_shdr;
5008 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5009 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5010 if (shdr_status || shdr_add_status || rc) {
5011 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5012 "3085 Mailbox x%x (x%x/x%x) failed, "
5013 "rc:x%x, status:x%x, add_status:x%x\n",
5014 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5015 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5016 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5017 rc, shdr_status, shdr_add_status);
5018 rc = -ENXIO;
5019 goto out_free_mboxq;
5020 }
5021 cntl_attr = &mbx_cntl_attr->cntl_attr;
5022 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5023 phba->sli4_hba.lnk_info.lnk_tp =
5024 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5025 phba->sli4_hba.lnk_info.lnk_no =
5026 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5027 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5028 "3086 lnk_type:%d, lnk_numb:%d\n",
5029 phba->sli4_hba.lnk_info.lnk_tp,
5030 phba->sli4_hba.lnk_info.lnk_no);
5031
5032retrieve_ppname:
5033 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5034 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5035 sizeof(struct lpfc_mbx_get_port_name) -
5036 sizeof(struct lpfc_sli4_cfg_mhdr),
5037 LPFC_SLI4_MBX_EMBED);
5038 get_port_name = &mboxq->u.mqe.un.get_port_name;
5039 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5040 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5041 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5042 phba->sli4_hba.lnk_info.lnk_tp);
5043 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5044 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5045 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5046 if (shdr_status || shdr_add_status || rc) {
5047 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5048 "3087 Mailbox x%x (x%x/x%x) failed: "
5049 "rc:x%x, status:x%x, add_status:x%x\n",
5050 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5051 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5052 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5053 rc, shdr_status, shdr_add_status);
5054 rc = -ENXIO;
5055 goto out_free_mboxq;
5056 }
5057 switch (phba->sli4_hba.lnk_info.lnk_no) {
5058 case LPFC_LINK_NUMBER_0:
5059 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5060 &get_port_name->u.response);
5061 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5062 break;
5063 case LPFC_LINK_NUMBER_1:
5064 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5065 &get_port_name->u.response);
5066 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5067 break;
5068 case LPFC_LINK_NUMBER_2:
5069 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5070 &get_port_name->u.response);
5071 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5072 break;
5073 case LPFC_LINK_NUMBER_3:
5074 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5075 &get_port_name->u.response);
5076 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5077 break;
5078 default:
5079 break;
5080 }
5081
5082 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5083 phba->Port[0] = cport_name;
5084 phba->Port[1] = '\0';
5085 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5086 "3091 SLI get port name: %s\n", phba->Port);
5087 }
5088
5089out_free_mboxq:
5090 if (rc != MBX_TIMEOUT) {
5091 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5092 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5093 else
5094 mempool_free(mboxq, phba->mbox_mem_pool);
5095 }
5096 return rc;
5097}
5098
5099/**
James Smartda0436e2009-05-22 14:51:39 -04005100 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5101 * @phba: pointer to lpfc hba data structure.
5102 *
5103 * This routine is called to explicitly arm the SLI4 device's completion and
5104 * event queues
5105 **/
5106static void
5107lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5108{
James Smart962bc512013-01-03 15:44:00 -05005109 int fcp_eqidx;
James Smartda0436e2009-05-22 14:51:39 -04005110
5111 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5112 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04005113 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05005114 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04005115 do {
James Smart2e90f4b2011-12-13 13:22:37 -05005116 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
5117 LPFC_QUEUE_REARM);
James Smart67d12732012-08-03 12:36:13 -04005118 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
James Smart2e90f4b2011-12-13 13:22:37 -05005119 }
James Smart1ba981f2014-02-20 09:56:45 -05005120
James Smartf38fa0b2014-04-04 13:52:21 -04005121 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005122 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5123
James Smart67d12732012-08-03 12:36:13 -04005124 if (phba->sli4_hba.hba_eq) {
5125 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05005126 fcp_eqidx++)
James Smart67d12732012-08-03 12:36:13 -04005127 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
James Smart2e90f4b2011-12-13 13:22:37 -05005128 LPFC_QUEUE_REARM);
5129 }
James Smart1ba981f2014-02-20 09:56:45 -05005130
5131 if (phba->cfg_fof)
5132 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005133}
5134
5135/**
James Smart6d368e52011-05-24 11:44:12 -04005136 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5137 * @phba: Pointer to HBA context object.
5138 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005139 * @extnt_count: buffer to hold port available extent count.
5140 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005141 *
James Smartb76f2dc2011-07-22 18:37:42 -04005142 * This function calls the port and retrievs the number of available
5143 * extents and their size for a particular extent type.
5144 *
5145 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005146 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005147int
James Smart6d368e52011-05-24 11:44:12 -04005148lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5149 uint16_t *extnt_count, uint16_t *extnt_size)
5150{
5151 int rc = 0;
5152 uint32_t length;
5153 uint32_t mbox_tmo;
5154 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5155 LPFC_MBOXQ_t *mbox;
5156
5157 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5158 if (!mbox)
5159 return -ENOMEM;
5160
5161 /* Find out how many extents are available for this resource type */
5162 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5163 sizeof(struct lpfc_sli4_cfg_mhdr));
5164 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5165 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5166 length, LPFC_SLI4_MBX_EMBED);
5167
5168 /* Send an extents count of 0 - the GET doesn't use it. */
5169 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5170 LPFC_SLI4_MBX_EMBED);
5171 if (unlikely(rc)) {
5172 rc = -EIO;
5173 goto err_exit;
5174 }
5175
5176 if (!phba->sli4_hba.intr_enable)
5177 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5178 else {
James Smarta183a152011-10-10 21:32:43 -04005179 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005180 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5181 }
5182 if (unlikely(rc)) {
5183 rc = -EIO;
5184 goto err_exit;
5185 }
5186
5187 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5188 if (bf_get(lpfc_mbox_hdr_status,
5189 &rsrc_info->header.cfg_shdr.response)) {
5190 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5191 "2930 Failed to get resource extents "
5192 "Status 0x%x Add'l Status 0x%x\n",
5193 bf_get(lpfc_mbox_hdr_status,
5194 &rsrc_info->header.cfg_shdr.response),
5195 bf_get(lpfc_mbox_hdr_add_status,
5196 &rsrc_info->header.cfg_shdr.response));
5197 rc = -EIO;
5198 goto err_exit;
5199 }
5200
5201 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5202 &rsrc_info->u.rsp);
5203 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5204 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005205
5206 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5207 "3162 Retrieved extents type-%d from port: count:%d, "
5208 "size:%d\n", type, *extnt_count, *extnt_size);
5209
5210err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005211 mempool_free(mbox, phba->mbox_mem_pool);
5212 return rc;
5213}
5214
5215/**
5216 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5217 * @phba: Pointer to HBA context object.
5218 * @type: The extent type to check.
5219 *
5220 * This function reads the current available extents from the port and checks
5221 * if the extent count or extent size has changed since the last access.
5222 * Callers use this routine post port reset to understand if there is a
5223 * extent reprovisioning requirement.
5224 *
5225 * Returns:
5226 * -Error: error indicates problem.
5227 * 1: Extent count or size has changed.
5228 * 0: No changes.
5229 **/
5230static int
5231lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5232{
5233 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5234 uint16_t size_diff, rsrc_ext_size;
5235 int rc = 0;
5236 struct lpfc_rsrc_blks *rsrc_entry;
5237 struct list_head *rsrc_blk_list = NULL;
5238
5239 size_diff = 0;
5240 curr_ext_cnt = 0;
5241 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5242 &rsrc_ext_cnt,
5243 &rsrc_ext_size);
5244 if (unlikely(rc))
5245 return -EIO;
5246
5247 switch (type) {
5248 case LPFC_RSC_TYPE_FCOE_RPI:
5249 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5250 break;
5251 case LPFC_RSC_TYPE_FCOE_VPI:
5252 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5253 break;
5254 case LPFC_RSC_TYPE_FCOE_XRI:
5255 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5256 break;
5257 case LPFC_RSC_TYPE_FCOE_VFI:
5258 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5259 break;
5260 default:
5261 break;
5262 }
5263
5264 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5265 curr_ext_cnt++;
5266 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5267 size_diff++;
5268 }
5269
5270 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5271 rc = 1;
5272
5273 return rc;
5274}
5275
5276/**
5277 * lpfc_sli4_cfg_post_extnts -
5278 * @phba: Pointer to HBA context object.
5279 * @extnt_cnt - number of available extents.
5280 * @type - the extent type (rpi, xri, vfi, vpi).
5281 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5282 * @mbox - pointer to the caller's allocated mailbox structure.
5283 *
5284 * This function executes the extents allocation request. It also
5285 * takes care of the amount of memory needed to allocate or get the
5286 * allocated extents. It is the caller's responsibility to evaluate
5287 * the response.
5288 *
5289 * Returns:
5290 * -Error: Error value describes the condition found.
5291 * 0: if successful
5292 **/
5293static int
James Smart8a9d2e82012-05-09 21:16:12 -04005294lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005295 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5296{
5297 int rc = 0;
5298 uint32_t req_len;
5299 uint32_t emb_len;
5300 uint32_t alloc_len, mbox_tmo;
5301
5302 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005303 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005304
5305 /*
5306 * Calculate the size of an embedded mailbox. The uint32_t
5307 * accounts for extents-specific word.
5308 */
5309 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5310 sizeof(uint32_t);
5311
5312 /*
5313 * Presume the allocation and response will fit into an embedded
5314 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5315 */
5316 *emb = LPFC_SLI4_MBX_EMBED;
5317 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005318 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005319 sizeof(union lpfc_sli4_cfg_shdr) +
5320 sizeof(uint32_t);
5321 *emb = LPFC_SLI4_MBX_NEMBED;
5322 }
5323
5324 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5325 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5326 req_len, *emb);
5327 if (alloc_len < req_len) {
5328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005329 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005330 "less than the requested DMA memory "
5331 "size (x%x)\n", alloc_len, req_len);
5332 return -ENOMEM;
5333 }
James Smart8a9d2e82012-05-09 21:16:12 -04005334 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005335 if (unlikely(rc))
5336 return -EIO;
5337
5338 if (!phba->sli4_hba.intr_enable)
5339 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5340 else {
James Smarta183a152011-10-10 21:32:43 -04005341 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005342 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5343 }
5344
5345 if (unlikely(rc))
5346 rc = -EIO;
5347 return rc;
5348}
5349
5350/**
5351 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5352 * @phba: Pointer to HBA context object.
5353 * @type: The resource extent type to allocate.
5354 *
5355 * This function allocates the number of elements for the specified
5356 * resource type.
5357 **/
5358static int
5359lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5360{
5361 bool emb = false;
5362 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5363 uint16_t rsrc_id, rsrc_start, j, k;
5364 uint16_t *ids;
5365 int i, rc;
5366 unsigned long longs;
5367 unsigned long *bmask;
5368 struct lpfc_rsrc_blks *rsrc_blks;
5369 LPFC_MBOXQ_t *mbox;
5370 uint32_t length;
5371 struct lpfc_id_range *id_array = NULL;
5372 void *virtaddr = NULL;
5373 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5374 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5375 struct list_head *ext_blk_list;
5376
5377 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5378 &rsrc_cnt,
5379 &rsrc_size);
5380 if (unlikely(rc))
5381 return -EIO;
5382
5383 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5384 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5385 "3009 No available Resource Extents "
5386 "for resource type 0x%x: Count: 0x%x, "
5387 "Size 0x%x\n", type, rsrc_cnt,
5388 rsrc_size);
5389 return -ENOMEM;
5390 }
5391
James Smart8a9d2e82012-05-09 21:16:12 -04005392 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5393 "2903 Post resource extents type-0x%x: "
5394 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005395
5396 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5397 if (!mbox)
5398 return -ENOMEM;
5399
James Smart8a9d2e82012-05-09 21:16:12 -04005400 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005401 if (unlikely(rc)) {
5402 rc = -EIO;
5403 goto err_exit;
5404 }
5405
5406 /*
5407 * Figure out where the response is located. Then get local pointers
5408 * to the response data. The port does not guarantee to respond to
5409 * all extents counts request so update the local variable with the
5410 * allocated count from the port.
5411 */
5412 if (emb == LPFC_SLI4_MBX_EMBED) {
5413 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5414 id_array = &rsrc_ext->u.rsp.id[0];
5415 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5416 } else {
5417 virtaddr = mbox->sge_array->addr[0];
5418 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5419 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5420 id_array = &n_rsrc->id;
5421 }
5422
5423 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5424 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5425
5426 /*
5427 * Based on the resource size and count, correct the base and max
5428 * resource values.
5429 */
5430 length = sizeof(struct lpfc_rsrc_blks);
5431 switch (type) {
5432 case LPFC_RSC_TYPE_FCOE_RPI:
5433 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5434 sizeof(unsigned long),
5435 GFP_KERNEL);
5436 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5437 rc = -ENOMEM;
5438 goto err_exit;
5439 }
5440 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5441 sizeof(uint16_t),
5442 GFP_KERNEL);
5443 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5444 kfree(phba->sli4_hba.rpi_bmask);
5445 rc = -ENOMEM;
5446 goto err_exit;
5447 }
5448
5449 /*
5450 * The next_rpi was initialized with the maximum available
5451 * count but the port may allocate a smaller number. Catch
5452 * that case and update the next_rpi.
5453 */
5454 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5455
5456 /* Initialize local ptrs for common extent processing later. */
5457 bmask = phba->sli4_hba.rpi_bmask;
5458 ids = phba->sli4_hba.rpi_ids;
5459 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5460 break;
5461 case LPFC_RSC_TYPE_FCOE_VPI:
5462 phba->vpi_bmask = kzalloc(longs *
5463 sizeof(unsigned long),
5464 GFP_KERNEL);
5465 if (unlikely(!phba->vpi_bmask)) {
5466 rc = -ENOMEM;
5467 goto err_exit;
5468 }
5469 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5470 sizeof(uint16_t),
5471 GFP_KERNEL);
5472 if (unlikely(!phba->vpi_ids)) {
5473 kfree(phba->vpi_bmask);
5474 rc = -ENOMEM;
5475 goto err_exit;
5476 }
5477
5478 /* Initialize local ptrs for common extent processing later. */
5479 bmask = phba->vpi_bmask;
5480 ids = phba->vpi_ids;
5481 ext_blk_list = &phba->lpfc_vpi_blk_list;
5482 break;
5483 case LPFC_RSC_TYPE_FCOE_XRI:
5484 phba->sli4_hba.xri_bmask = kzalloc(longs *
5485 sizeof(unsigned long),
5486 GFP_KERNEL);
5487 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5488 rc = -ENOMEM;
5489 goto err_exit;
5490 }
James Smart8a9d2e82012-05-09 21:16:12 -04005491 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005492 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5493 sizeof(uint16_t),
5494 GFP_KERNEL);
5495 if (unlikely(!phba->sli4_hba.xri_ids)) {
5496 kfree(phba->sli4_hba.xri_bmask);
5497 rc = -ENOMEM;
5498 goto err_exit;
5499 }
5500
5501 /* Initialize local ptrs for common extent processing later. */
5502 bmask = phba->sli4_hba.xri_bmask;
5503 ids = phba->sli4_hba.xri_ids;
5504 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5505 break;
5506 case LPFC_RSC_TYPE_FCOE_VFI:
5507 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5508 sizeof(unsigned long),
5509 GFP_KERNEL);
5510 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5511 rc = -ENOMEM;
5512 goto err_exit;
5513 }
5514 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5515 sizeof(uint16_t),
5516 GFP_KERNEL);
5517 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5518 kfree(phba->sli4_hba.vfi_bmask);
5519 rc = -ENOMEM;
5520 goto err_exit;
5521 }
5522
5523 /* Initialize local ptrs for common extent processing later. */
5524 bmask = phba->sli4_hba.vfi_bmask;
5525 ids = phba->sli4_hba.vfi_ids;
5526 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5527 break;
5528 default:
5529 /* Unsupported Opcode. Fail call. */
5530 id_array = NULL;
5531 bmask = NULL;
5532 ids = NULL;
5533 ext_blk_list = NULL;
5534 goto err_exit;
5535 }
5536
5537 /*
5538 * Complete initializing the extent configuration with the
5539 * allocated ids assigned to this function. The bitmask serves
5540 * as an index into the array and manages the available ids. The
5541 * array just stores the ids communicated to the port via the wqes.
5542 */
5543 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5544 if ((i % 2) == 0)
5545 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5546 &id_array[k]);
5547 else
5548 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5549 &id_array[k]);
5550
5551 rsrc_blks = kzalloc(length, GFP_KERNEL);
5552 if (unlikely(!rsrc_blks)) {
5553 rc = -ENOMEM;
5554 kfree(bmask);
5555 kfree(ids);
5556 goto err_exit;
5557 }
5558 rsrc_blks->rsrc_start = rsrc_id;
5559 rsrc_blks->rsrc_size = rsrc_size;
5560 list_add_tail(&rsrc_blks->list, ext_blk_list);
5561 rsrc_start = rsrc_id;
5562 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5563 phba->sli4_hba.scsi_xri_start = rsrc_start +
5564 lpfc_sli4_get_els_iocb_cnt(phba);
5565
5566 while (rsrc_id < (rsrc_start + rsrc_size)) {
5567 ids[j] = rsrc_id;
5568 rsrc_id++;
5569 j++;
5570 }
5571 /* Entire word processed. Get next word.*/
5572 if ((i % 2) == 1)
5573 k++;
5574 }
5575 err_exit:
5576 lpfc_sli4_mbox_cmd_free(phba, mbox);
5577 return rc;
5578}
5579
5580/**
5581 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5582 * @phba: Pointer to HBA context object.
5583 * @type: the extent's type.
5584 *
5585 * This function deallocates all extents of a particular resource type.
5586 * SLI4 does not allow for deallocating a particular extent range. It
5587 * is the caller's responsibility to release all kernel memory resources.
5588 **/
5589static int
5590lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5591{
5592 int rc;
5593 uint32_t length, mbox_tmo = 0;
5594 LPFC_MBOXQ_t *mbox;
5595 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5596 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5597
5598 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5599 if (!mbox)
5600 return -ENOMEM;
5601
5602 /*
5603 * This function sends an embedded mailbox because it only sends the
5604 * the resource type. All extents of this type are released by the
5605 * port.
5606 */
5607 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5608 sizeof(struct lpfc_sli4_cfg_mhdr));
5609 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5610 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5611 length, LPFC_SLI4_MBX_EMBED);
5612
5613 /* Send an extents count of 0 - the dealloc doesn't use it. */
5614 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5615 LPFC_SLI4_MBX_EMBED);
5616 if (unlikely(rc)) {
5617 rc = -EIO;
5618 goto out_free_mbox;
5619 }
5620 if (!phba->sli4_hba.intr_enable)
5621 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5622 else {
James Smarta183a152011-10-10 21:32:43 -04005623 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005624 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5625 }
5626 if (unlikely(rc)) {
5627 rc = -EIO;
5628 goto out_free_mbox;
5629 }
5630
5631 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5632 if (bf_get(lpfc_mbox_hdr_status,
5633 &dealloc_rsrc->header.cfg_shdr.response)) {
5634 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5635 "2919 Failed to release resource extents "
5636 "for type %d - Status 0x%x Add'l Status 0x%x. "
5637 "Resource memory not released.\n",
5638 type,
5639 bf_get(lpfc_mbox_hdr_status,
5640 &dealloc_rsrc->header.cfg_shdr.response),
5641 bf_get(lpfc_mbox_hdr_add_status,
5642 &dealloc_rsrc->header.cfg_shdr.response));
5643 rc = -EIO;
5644 goto out_free_mbox;
5645 }
5646
5647 /* Release kernel memory resources for the specific type. */
5648 switch (type) {
5649 case LPFC_RSC_TYPE_FCOE_VPI:
5650 kfree(phba->vpi_bmask);
5651 kfree(phba->vpi_ids);
5652 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5653 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5654 &phba->lpfc_vpi_blk_list, list) {
5655 list_del_init(&rsrc_blk->list);
5656 kfree(rsrc_blk);
5657 }
James Smart16a3a202013-04-17 20:14:38 -04005658 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005659 break;
5660 case LPFC_RSC_TYPE_FCOE_XRI:
5661 kfree(phba->sli4_hba.xri_bmask);
5662 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005663 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5664 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5665 list_del_init(&rsrc_blk->list);
5666 kfree(rsrc_blk);
5667 }
5668 break;
5669 case LPFC_RSC_TYPE_FCOE_VFI:
5670 kfree(phba->sli4_hba.vfi_bmask);
5671 kfree(phba->sli4_hba.vfi_ids);
5672 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5673 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5674 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5675 list_del_init(&rsrc_blk->list);
5676 kfree(rsrc_blk);
5677 }
5678 break;
5679 case LPFC_RSC_TYPE_FCOE_RPI:
5680 /* RPI bitmask and physical id array are cleaned up earlier. */
5681 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5682 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5683 list_del_init(&rsrc_blk->list);
5684 kfree(rsrc_blk);
5685 }
5686 break;
5687 default:
5688 break;
5689 }
5690
5691 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5692
5693 out_free_mbox:
5694 mempool_free(mbox, phba->mbox_mem_pool);
5695 return rc;
5696}
5697
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08005698static void
James Smart7bdedb32016-07-06 12:36:00 -07005699lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5700 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07005701{
James Smart65791f12016-07-06 12:35:56 -07005702 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07005703
James Smart65791f12016-07-06 12:35:56 -07005704 len = sizeof(struct lpfc_mbx_set_feature) -
5705 sizeof(struct lpfc_sli4_cfg_mhdr);
5706 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5707 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5708 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07005709
James Smart7bdedb32016-07-06 12:36:00 -07005710 switch (feature) {
5711 case LPFC_SET_UE_RECOVERY:
5712 bf_set(lpfc_mbx_set_feature_UER,
5713 &mbox->u.mqe.un.set_feature, 1);
5714 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5715 mbox->u.mqe.un.set_feature.param_len = 8;
5716 break;
5717 case LPFC_SET_MDS_DIAGS:
5718 bf_set(lpfc_mbx_set_feature_mds,
5719 &mbox->u.mqe.un.set_feature, 1);
5720 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5721 &mbox->u.mqe.un.set_feature, 0);
5722 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5723 mbox->u.mqe.un.set_feature.param_len = 8;
5724 break;
James Smart65791f12016-07-06 12:35:56 -07005725 }
James Smart7bdedb32016-07-06 12:36:00 -07005726
5727 return;
James Smart65791f12016-07-06 12:35:56 -07005728}
5729
James Smart6d368e52011-05-24 11:44:12 -04005730/**
5731 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5732 * @phba: Pointer to HBA context object.
5733 *
5734 * This function allocates all SLI4 resource identifiers.
5735 **/
5736int
5737lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5738{
5739 int i, rc, error = 0;
5740 uint16_t count, base;
5741 unsigned long longs;
5742
James Smartff78d8f2011-12-13 13:21:35 -05005743 if (!phba->sli4_hba.rpi_hdrs_in_use)
5744 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005745 if (phba->sli4_hba.extents_in_use) {
5746 /*
5747 * The port supports resource extents. The XRI, VPI, VFI, RPI
5748 * resource extent count must be read and allocated before
5749 * provisioning the resource id arrays.
5750 */
5751 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5752 LPFC_IDX_RSRC_RDY) {
5753 /*
5754 * Extent-based resources are set - the driver could
5755 * be in a port reset. Figure out if any corrective
5756 * actions need to be taken.
5757 */
5758 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5759 LPFC_RSC_TYPE_FCOE_VFI);
5760 if (rc != 0)
5761 error++;
5762 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5763 LPFC_RSC_TYPE_FCOE_VPI);
5764 if (rc != 0)
5765 error++;
5766 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5767 LPFC_RSC_TYPE_FCOE_XRI);
5768 if (rc != 0)
5769 error++;
5770 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5771 LPFC_RSC_TYPE_FCOE_RPI);
5772 if (rc != 0)
5773 error++;
5774
5775 /*
5776 * It's possible that the number of resources
5777 * provided to this port instance changed between
5778 * resets. Detect this condition and reallocate
5779 * resources. Otherwise, there is no action.
5780 */
5781 if (error) {
5782 lpfc_printf_log(phba, KERN_INFO,
5783 LOG_MBOX | LOG_INIT,
5784 "2931 Detected extent resource "
5785 "change. Reallocating all "
5786 "extents.\n");
5787 rc = lpfc_sli4_dealloc_extent(phba,
5788 LPFC_RSC_TYPE_FCOE_VFI);
5789 rc = lpfc_sli4_dealloc_extent(phba,
5790 LPFC_RSC_TYPE_FCOE_VPI);
5791 rc = lpfc_sli4_dealloc_extent(phba,
5792 LPFC_RSC_TYPE_FCOE_XRI);
5793 rc = lpfc_sli4_dealloc_extent(phba,
5794 LPFC_RSC_TYPE_FCOE_RPI);
5795 } else
5796 return 0;
5797 }
5798
5799 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5800 if (unlikely(rc))
5801 goto err_exit;
5802
5803 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5804 if (unlikely(rc))
5805 goto err_exit;
5806
5807 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5808 if (unlikely(rc))
5809 goto err_exit;
5810
5811 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5812 if (unlikely(rc))
5813 goto err_exit;
5814 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5815 LPFC_IDX_RSRC_RDY);
5816 return rc;
5817 } else {
5818 /*
5819 * The port does not support resource extents. The XRI, VPI,
5820 * VFI, RPI resource ids were determined from READ_CONFIG.
5821 * Just allocate the bitmasks and provision the resource id
5822 * arrays. If a port reset is active, the resources don't
5823 * need any action - just exit.
5824 */
5825 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005826 LPFC_IDX_RSRC_RDY) {
5827 lpfc_sli4_dealloc_resource_identifiers(phba);
5828 lpfc_sli4_remove_rpis(phba);
5829 }
James Smart6d368e52011-05-24 11:44:12 -04005830 /* RPIs. */
5831 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05005832 if (count <= 0) {
5833 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5834 "3279 Invalid provisioning of "
5835 "rpi:%d\n", count);
5836 rc = -EINVAL;
5837 goto err_exit;
5838 }
James Smart6d368e52011-05-24 11:44:12 -04005839 base = phba->sli4_hba.max_cfg_param.rpi_base;
5840 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5841 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5842 sizeof(unsigned long),
5843 GFP_KERNEL);
5844 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5845 rc = -ENOMEM;
5846 goto err_exit;
5847 }
5848 phba->sli4_hba.rpi_ids = kzalloc(count *
5849 sizeof(uint16_t),
5850 GFP_KERNEL);
5851 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5852 rc = -ENOMEM;
5853 goto free_rpi_bmask;
5854 }
5855
5856 for (i = 0; i < count; i++)
5857 phba->sli4_hba.rpi_ids[i] = base + i;
5858
5859 /* VPIs. */
5860 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05005861 if (count <= 0) {
5862 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5863 "3280 Invalid provisioning of "
5864 "vpi:%d\n", count);
5865 rc = -EINVAL;
5866 goto free_rpi_ids;
5867 }
James Smart6d368e52011-05-24 11:44:12 -04005868 base = phba->sli4_hba.max_cfg_param.vpi_base;
5869 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5870 phba->vpi_bmask = kzalloc(longs *
5871 sizeof(unsigned long),
5872 GFP_KERNEL);
5873 if (unlikely(!phba->vpi_bmask)) {
5874 rc = -ENOMEM;
5875 goto free_rpi_ids;
5876 }
5877 phba->vpi_ids = kzalloc(count *
5878 sizeof(uint16_t),
5879 GFP_KERNEL);
5880 if (unlikely(!phba->vpi_ids)) {
5881 rc = -ENOMEM;
5882 goto free_vpi_bmask;
5883 }
5884
5885 for (i = 0; i < count; i++)
5886 phba->vpi_ids[i] = base + i;
5887
5888 /* XRIs. */
5889 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05005890 if (count <= 0) {
5891 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5892 "3281 Invalid provisioning of "
5893 "xri:%d\n", count);
5894 rc = -EINVAL;
5895 goto free_vpi_ids;
5896 }
James Smart6d368e52011-05-24 11:44:12 -04005897 base = phba->sli4_hba.max_cfg_param.xri_base;
5898 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5899 phba->sli4_hba.xri_bmask = kzalloc(longs *
5900 sizeof(unsigned long),
5901 GFP_KERNEL);
5902 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5903 rc = -ENOMEM;
5904 goto free_vpi_ids;
5905 }
James Smart41899be2012-03-01 22:34:19 -05005906 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005907 phba->sli4_hba.xri_ids = kzalloc(count *
5908 sizeof(uint16_t),
5909 GFP_KERNEL);
5910 if (unlikely(!phba->sli4_hba.xri_ids)) {
5911 rc = -ENOMEM;
5912 goto free_xri_bmask;
5913 }
5914
5915 for (i = 0; i < count; i++)
5916 phba->sli4_hba.xri_ids[i] = base + i;
5917
5918 /* VFIs. */
5919 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05005920 if (count <= 0) {
5921 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5922 "3282 Invalid provisioning of "
5923 "vfi:%d\n", count);
5924 rc = -EINVAL;
5925 goto free_xri_ids;
5926 }
James Smart6d368e52011-05-24 11:44:12 -04005927 base = phba->sli4_hba.max_cfg_param.vfi_base;
5928 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5929 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5930 sizeof(unsigned long),
5931 GFP_KERNEL);
5932 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5933 rc = -ENOMEM;
5934 goto free_xri_ids;
5935 }
5936 phba->sli4_hba.vfi_ids = kzalloc(count *
5937 sizeof(uint16_t),
5938 GFP_KERNEL);
5939 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5940 rc = -ENOMEM;
5941 goto free_vfi_bmask;
5942 }
5943
5944 for (i = 0; i < count; i++)
5945 phba->sli4_hba.vfi_ids[i] = base + i;
5946
5947 /*
5948 * Mark all resources ready. An HBA reset doesn't need
5949 * to reset the initialization.
5950 */
5951 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5952 LPFC_IDX_RSRC_RDY);
5953 return 0;
5954 }
5955
5956 free_vfi_bmask:
5957 kfree(phba->sli4_hba.vfi_bmask);
Roberto Sassuc32462d2017-01-11 11:06:42 +01005958 phba->sli4_hba.vfi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005959 free_xri_ids:
5960 kfree(phba->sli4_hba.xri_ids);
Roberto Sassuc32462d2017-01-11 11:06:42 +01005961 phba->sli4_hba.xri_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005962 free_xri_bmask:
5963 kfree(phba->sli4_hba.xri_bmask);
Roberto Sassuc32462d2017-01-11 11:06:42 +01005964 phba->sli4_hba.xri_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005965 free_vpi_ids:
5966 kfree(phba->vpi_ids);
Roberto Sassuc32462d2017-01-11 11:06:42 +01005967 phba->vpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005968 free_vpi_bmask:
5969 kfree(phba->vpi_bmask);
Roberto Sassuc32462d2017-01-11 11:06:42 +01005970 phba->vpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005971 free_rpi_ids:
5972 kfree(phba->sli4_hba.rpi_ids);
Roberto Sassuc32462d2017-01-11 11:06:42 +01005973 phba->sli4_hba.rpi_ids = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005974 free_rpi_bmask:
5975 kfree(phba->sli4_hba.rpi_bmask);
Roberto Sassuc32462d2017-01-11 11:06:42 +01005976 phba->sli4_hba.rpi_bmask = NULL;
James Smart6d368e52011-05-24 11:44:12 -04005977 err_exit:
5978 return rc;
5979}
5980
5981/**
5982 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5983 * @phba: Pointer to HBA context object.
5984 *
5985 * This function allocates the number of elements for the specified
5986 * resource type.
5987 **/
5988int
5989lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5990{
5991 if (phba->sli4_hba.extents_in_use) {
5992 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5993 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5994 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5995 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5996 } else {
5997 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04005998 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005999 kfree(phba->vpi_ids);
6000 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6001 kfree(phba->sli4_hba.xri_bmask);
6002 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04006003 kfree(phba->sli4_hba.vfi_bmask);
6004 kfree(phba->sli4_hba.vfi_ids);
6005 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6006 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6007 }
6008
6009 return 0;
6010}
6011
6012/**
James Smartb76f2dc2011-07-22 18:37:42 -04006013 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6014 * @phba: Pointer to HBA context object.
6015 * @type: The resource extent type.
6016 * @extnt_count: buffer to hold port extent count response
6017 * @extnt_size: buffer to hold port extent size response.
6018 *
6019 * This function calls the port to read the host allocated extents
6020 * for a particular type.
6021 **/
6022int
6023lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6024 uint16_t *extnt_cnt, uint16_t *extnt_size)
6025{
6026 bool emb;
6027 int rc = 0;
6028 uint16_t curr_blks = 0;
6029 uint32_t req_len, emb_len;
6030 uint32_t alloc_len, mbox_tmo;
6031 struct list_head *blk_list_head;
6032 struct lpfc_rsrc_blks *rsrc_blk;
6033 LPFC_MBOXQ_t *mbox;
6034 void *virtaddr = NULL;
6035 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6036 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6037 union lpfc_sli4_cfg_shdr *shdr;
6038
6039 switch (type) {
6040 case LPFC_RSC_TYPE_FCOE_VPI:
6041 blk_list_head = &phba->lpfc_vpi_blk_list;
6042 break;
6043 case LPFC_RSC_TYPE_FCOE_XRI:
6044 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6045 break;
6046 case LPFC_RSC_TYPE_FCOE_VFI:
6047 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6048 break;
6049 case LPFC_RSC_TYPE_FCOE_RPI:
6050 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6051 break;
6052 default:
6053 return -EIO;
6054 }
6055
6056 /* Count the number of extents currently allocatd for this type. */
6057 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6058 if (curr_blks == 0) {
6059 /*
6060 * The GET_ALLOCATED mailbox does not return the size,
6061 * just the count. The size should be just the size
6062 * stored in the current allocated block and all sizes
6063 * for an extent type are the same so set the return
6064 * value now.
6065 */
6066 *extnt_size = rsrc_blk->rsrc_size;
6067 }
6068 curr_blks++;
6069 }
6070
James Smartb76f2dc2011-07-22 18:37:42 -04006071 /*
6072 * Calculate the size of an embedded mailbox. The uint32_t
6073 * accounts for extents-specific word.
6074 */
6075 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6076 sizeof(uint32_t);
6077
6078 /*
6079 * Presume the allocation and response will fit into an embedded
6080 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6081 */
6082 emb = LPFC_SLI4_MBX_EMBED;
6083 req_len = emb_len;
6084 if (req_len > emb_len) {
6085 req_len = curr_blks * sizeof(uint16_t) +
6086 sizeof(union lpfc_sli4_cfg_shdr) +
6087 sizeof(uint32_t);
6088 emb = LPFC_SLI4_MBX_NEMBED;
6089 }
6090
6091 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6092 if (!mbox)
6093 return -ENOMEM;
6094 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6095
6096 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6097 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6098 req_len, emb);
6099 if (alloc_len < req_len) {
6100 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6101 "2983 Allocated DMA memory size (x%x) is "
6102 "less than the requested DMA memory "
6103 "size (x%x)\n", alloc_len, req_len);
6104 rc = -ENOMEM;
6105 goto err_exit;
6106 }
6107 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6108 if (unlikely(rc)) {
6109 rc = -EIO;
6110 goto err_exit;
6111 }
6112
6113 if (!phba->sli4_hba.intr_enable)
6114 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6115 else {
James Smarta183a152011-10-10 21:32:43 -04006116 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006117 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6118 }
6119
6120 if (unlikely(rc)) {
6121 rc = -EIO;
6122 goto err_exit;
6123 }
6124
6125 /*
6126 * Figure out where the response is located. Then get local pointers
6127 * to the response data. The port does not guarantee to respond to
6128 * all extents counts request so update the local variable with the
6129 * allocated count from the port.
6130 */
6131 if (emb == LPFC_SLI4_MBX_EMBED) {
6132 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6133 shdr = &rsrc_ext->header.cfg_shdr;
6134 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6135 } else {
6136 virtaddr = mbox->sge_array->addr[0];
6137 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6138 shdr = &n_rsrc->cfg_shdr;
6139 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6140 }
6141
6142 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6143 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6144 "2984 Failed to read allocated resources "
6145 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6146 type,
6147 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6148 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6149 rc = -EIO;
6150 goto err_exit;
6151 }
6152 err_exit:
6153 lpfc_sli4_mbox_cmd_free(phba, mbox);
6154 return rc;
6155}
6156
6157/**
James Smart8a9d2e82012-05-09 21:16:12 -04006158 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
6159 * @phba: pointer to lpfc hba data structure.
6160 *
6161 * This routine walks the list of els buffers that have been allocated and
6162 * repost them to the port by using SGL block post. This is needed after a
6163 * pci_function_reset/warm_start or start. It attempts to construct blocks
6164 * of els buffer sgls which contains contiguous xris and uses the non-embedded
6165 * SGL block post mailbox commands to post them to the port. For single els
6166 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6167 * mailbox command for posting.
6168 *
6169 * Returns: 0 = success, non-zero failure.
6170 **/
6171static int
6172lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
6173{
6174 struct lpfc_sglq *sglq_entry = NULL;
6175 struct lpfc_sglq *sglq_entry_next = NULL;
6176 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart711ea882013-04-17 20:18:29 -04006177 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006178 int last_xritag = NO_XRI;
James Smartdafe8ce2014-09-03 12:56:40 -04006179 struct lpfc_sli_ring *pring;
James Smart8a9d2e82012-05-09 21:16:12 -04006180 LIST_HEAD(prep_sgl_list);
6181 LIST_HEAD(blck_sgl_list);
6182 LIST_HEAD(allc_sgl_list);
6183 LIST_HEAD(post_sgl_list);
6184 LIST_HEAD(free_sgl_list);
6185
James Smartdafe8ce2014-09-03 12:56:40 -04006186 pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart38c20672013-03-01 16:37:44 -05006187 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04006188 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04006189 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04006190 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05006191 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006192
James Smart711ea882013-04-17 20:18:29 -04006193 total_cnt = phba->sli4_hba.els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006194 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6195 &allc_sgl_list, list) {
6196 list_del_init(&sglq_entry->list);
6197 block_cnt++;
6198 if ((last_xritag != NO_XRI) &&
6199 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6200 /* a hole in xri block, form a sgl posting block */
6201 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6202 post_cnt = block_cnt - 1;
6203 /* prepare list for next posting block */
6204 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6205 block_cnt = 1;
6206 } else {
6207 /* prepare list for next posting block */
6208 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6209 /* enough sgls for non-embed sgl mbox command */
6210 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6211 list_splice_init(&prep_sgl_list,
6212 &blck_sgl_list);
6213 post_cnt = block_cnt;
6214 block_cnt = 0;
6215 }
6216 }
6217 num_posted++;
6218
6219 /* keep track of last sgl's xritag */
6220 last_xritag = sglq_entry->sli4_xritag;
6221
6222 /* end of repost sgl list condition for els buffers */
6223 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6224 if (post_cnt == 0) {
6225 list_splice_init(&prep_sgl_list,
6226 &blck_sgl_list);
6227 post_cnt = block_cnt;
6228 } else if (block_cnt == 1) {
6229 status = lpfc_sli4_post_sgl(phba,
6230 sglq_entry->phys, 0,
6231 sglq_entry->sli4_xritag);
6232 if (!status) {
6233 /* successful, put sgl to posted list */
6234 list_add_tail(&sglq_entry->list,
6235 &post_sgl_list);
6236 } else {
6237 /* Failure, put sgl to free list */
6238 lpfc_printf_log(phba, KERN_WARNING,
6239 LOG_SLI,
6240 "3159 Failed to post els "
6241 "sgl, xritag:x%x\n",
6242 sglq_entry->sli4_xritag);
6243 list_add_tail(&sglq_entry->list,
6244 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006245 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006246 }
6247 }
6248 }
6249
6250 /* continue until a nembed page worth of sgls */
6251 if (post_cnt == 0)
6252 continue;
6253
6254 /* post the els buffer list sgls as a block */
6255 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6256 post_cnt);
6257
6258 if (!status) {
6259 /* success, put sgl list to posted sgl list */
6260 list_splice_init(&blck_sgl_list, &post_sgl_list);
6261 } else {
6262 /* Failure, put sgl list to free sgl list */
6263 sglq_entry_first = list_first_entry(&blck_sgl_list,
6264 struct lpfc_sglq,
6265 list);
6266 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6267 "3160 Failed to post els sgl-list, "
6268 "xritag:x%x-x%x\n",
6269 sglq_entry_first->sli4_xritag,
6270 (sglq_entry_first->sli4_xritag +
6271 post_cnt - 1));
6272 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006273 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006274 }
6275
6276 /* don't reset xirtag due to hole in xri block */
6277 if (block_cnt == 0)
6278 last_xritag = NO_XRI;
6279
6280 /* reset els sgl post count for next round of posting */
6281 post_cnt = 0;
6282 }
James Smart711ea882013-04-17 20:18:29 -04006283 /* update the number of XRIs posted for ELS */
6284 phba->sli4_hba.els_xri_cnt = total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006285
6286 /* free the els sgls failed to post */
6287 lpfc_free_sgl_list(phba, &free_sgl_list);
6288
6289 /* push els sgls posted to the availble list */
6290 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006291 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04006292 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04006293 list_splice_init(&post_sgl_list,
6294 &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04006295 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05006296 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006297 } else {
6298 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6299 "3161 Failure to post els sgl to port.\n");
6300 return -EIO;
6301 }
6302 return 0;
6303}
6304
6305/**
James Smartda0436e2009-05-22 14:51:39 -04006306 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6307 * @phba: Pointer to HBA context object.
6308 *
6309 * This function is the main SLI4 device intialization PCI function. This
6310 * function is called by the HBA intialization code, HBA reset code and
6311 * HBA error attention handler code. Caller is not required to hold any
6312 * locks.
6313 **/
6314int
6315lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6316{
6317 int rc;
6318 LPFC_MBOXQ_t *mboxq;
6319 struct lpfc_mqe *mqe;
6320 uint8_t *vpd;
6321 uint32_t vpd_size;
6322 uint32_t ftr_rsp = 0;
6323 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6324 struct lpfc_vport *vport = phba->pport;
6325 struct lpfc_dmabuf *mp;
6326
6327 /* Perform a PCI function reset to start from clean */
6328 rc = lpfc_pci_function_reset(phba);
6329 if (unlikely(rc))
6330 return -ENODEV;
6331
6332 /* Check the HBA Host Status Register for readyness */
6333 rc = lpfc_sli4_post_status_check(phba);
6334 if (unlikely(rc))
6335 return -ENODEV;
6336 else {
6337 spin_lock_irq(&phba->hbalock);
6338 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6339 spin_unlock_irq(&phba->hbalock);
6340 }
6341
6342 /*
6343 * Allocate a single mailbox container for initializing the
6344 * port.
6345 */
6346 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6347 if (!mboxq)
6348 return -ENOMEM;
6349
James Smartda0436e2009-05-22 14:51:39 -04006350 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006351 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006352 vpd = kzalloc(vpd_size, GFP_KERNEL);
6353 if (!vpd) {
6354 rc = -ENOMEM;
6355 goto out_free_mbox;
6356 }
6357
6358 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006359 if (unlikely(rc)) {
6360 kfree(vpd);
6361 goto out_free_mbox;
6362 }
James Smart572709e2013-07-15 18:32:43 -04006363
James Smartda0436e2009-05-22 14:51:39 -04006364 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006365 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006366 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006367 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006368 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6369 } else {
James Smart76a95d72010-11-20 23:11:48 -05006370 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006371 }
James Smart45ed1192009-10-02 15:17:02 -04006372
6373 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6374 LPFC_DCBX_CEE_MODE)
6375 phba->hba_flag |= HBA_FIP_SUPPORT;
6376 else
6377 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6378
James Smart4f2e66c2012-05-09 21:17:07 -04006379 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6380
James Smartc31098c2011-04-16 11:03:33 -04006381 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006382 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6383 "0376 READ_REV Error. SLI Level %d "
6384 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006385 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006386 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006387 kfree(vpd);
6388 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006389 }
James Smartcd1c8302011-10-10 21:33:25 -04006390
6391 /*
James Smartff78d8f2011-12-13 13:21:35 -05006392 * Continue initialization with default values even if driver failed
6393 * to read FCoE param config regions, only read parameters if the
6394 * board is FCoE
6395 */
6396 if (phba->hba_flag & HBA_FCOE_MODE &&
6397 lpfc_sli4_read_fcoe_params(phba))
6398 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6399 "2570 Failed to read FCoE parameters\n");
6400
6401 /*
James Smartcd1c8302011-10-10 21:33:25 -04006402 * Retrieve sli4 device physical port name, failure of doing it
6403 * is considered as non-fatal.
6404 */
6405 rc = lpfc_sli4_retrieve_pport_name(phba);
6406 if (!rc)
6407 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6408 "3080 Successful retrieving SLI4 device "
6409 "physical port name: %s.\n", phba->Port);
6410
James Smartda0436e2009-05-22 14:51:39 -04006411 /*
6412 * Evaluate the read rev and vpd data. Populate the driver
6413 * state with the results. If this routine fails, the failure
6414 * is not fatal as the driver will use generic values.
6415 */
6416 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6417 if (unlikely(!rc)) {
6418 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6419 "0377 Error %d parsing vpd. "
6420 "Using defaults.\n", rc);
6421 rc = 0;
6422 }
James Smart76a95d72010-11-20 23:11:48 -05006423 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006424
James Smartf1126682009-06-10 17:22:44 -04006425 /* Save information as VPD data */
6426 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6427 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6428 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6429 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6430 &mqe->un.read_rev);
6431 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6432 &mqe->un.read_rev);
6433 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6434 &mqe->un.read_rev);
6435 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6436 &mqe->un.read_rev);
6437 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6438 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6439 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6440 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6441 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6442 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6443 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6444 "(%d):0380 READ_REV Status x%x "
6445 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6446 mboxq->vport ? mboxq->vport->vpi : 0,
6447 bf_get(lpfc_mqe_status, mqe),
6448 phba->vpd.rev.opFwName,
6449 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6450 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006451
James Smart572709e2013-07-15 18:32:43 -04006452 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6453 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6454 if (phba->pport->cfg_lun_queue_depth > rc) {
6455 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6456 "3362 LUN queue depth changed from %d to %d\n",
6457 phba->pport->cfg_lun_queue_depth, rc);
6458 phba->pport->cfg_lun_queue_depth = rc;
6459 }
6460
James Smart65791f12016-07-06 12:35:56 -07006461 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006462 LPFC_SLI_INTF_IF_TYPE_0) {
6463 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6464 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6465 if (rc == MBX_SUCCESS) {
6466 phba->hba_flag |= HBA_RECOVERABLE_UE;
6467 /* Set 1Sec interval to detect UE */
6468 phba->eratt_poll_interval = 1;
6469 phba->sli4_hba.ue_to_sr = bf_get(
6470 lpfc_mbx_set_feature_UESR,
6471 &mboxq->u.mqe.un.set_feature);
6472 phba->sli4_hba.ue_to_rp = bf_get(
6473 lpfc_mbx_set_feature_UERP,
6474 &mboxq->u.mqe.un.set_feature);
6475 }
6476 }
6477
6478 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6479 /* Enable MDS Diagnostics only if the SLI Port supports it */
6480 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6481 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6482 if (rc != MBX_SUCCESS)
6483 phba->mds_diags_support = 0;
6484 }
James Smart572709e2013-07-15 18:32:43 -04006485
James Smartda0436e2009-05-22 14:51:39 -04006486 /*
6487 * Discover the port's supported feature set and match it against the
6488 * hosts requests.
6489 */
6490 lpfc_request_features(phba, mboxq);
6491 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6492 if (unlikely(rc)) {
6493 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006494 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006495 }
6496
6497 /*
6498 * The port must support FCP initiator mode as this is the
6499 * only mode running in the host.
6500 */
6501 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6502 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6503 "0378 No support for fcpi mode.\n");
6504 ftr_rsp++;
6505 }
James Smartfedd3b72011-02-16 12:39:24 -05006506 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6507 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6508 else
6509 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006510 /*
6511 * If the port cannot support the host's requested features
6512 * then turn off the global config parameters to disable the
6513 * feature in the driver. This is not a fatal error.
6514 */
James Smartbf086112011-08-21 21:48:13 -04006515 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6516 if (phba->cfg_enable_bg) {
6517 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6518 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6519 else
6520 ftr_rsp++;
6521 }
James Smartda0436e2009-05-22 14:51:39 -04006522
6523 if (phba->max_vpi && phba->cfg_enable_npiv &&
6524 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6525 ftr_rsp++;
6526
6527 if (ftr_rsp) {
6528 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6529 "0379 Feature Mismatch Data: x%08x %08x "
6530 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6531 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6532 phba->cfg_enable_npiv, phba->max_vpi);
6533 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6534 phba->cfg_enable_bg = 0;
6535 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6536 phba->cfg_enable_npiv = 0;
6537 }
6538
6539 /* These SLI3 features are assumed in SLI4 */
6540 spin_lock_irq(&phba->hbalock);
6541 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6542 spin_unlock_irq(&phba->hbalock);
6543
James Smart6d368e52011-05-24 11:44:12 -04006544 /*
6545 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6546 * calls depends on these resources to complete port setup.
6547 */
6548 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6549 if (rc) {
6550 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6551 "2920 Failed to alloc Resource IDs "
6552 "rc = x%x\n", rc);
6553 goto out_free_mbox;
6554 }
6555
James Smartda0436e2009-05-22 14:51:39 -04006556 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006557 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6558 if (rc) {
6559 phba->link_state = LPFC_HBA_ERROR;
6560 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006561 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006562 }
6563
James Smartda0436e2009-05-22 14:51:39 -04006564 mboxq->vport = vport;
6565 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6566 mp = (struct lpfc_dmabuf *) mboxq->context1;
6567 if (rc == MBX_SUCCESS) {
6568 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6569 rc = 0;
6570 }
6571
6572 /*
6573 * This memory was allocated by the lpfc_read_sparam routine. Release
6574 * it to the mbuf pool.
6575 */
6576 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6577 kfree(mp);
6578 mboxq->context1 = NULL;
6579 if (unlikely(rc)) {
6580 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6581 "0382 READ_SPARAM command failed "
6582 "status %d, mbxStatus x%x\n",
6583 rc, bf_get(lpfc_mqe_status, mqe));
6584 phba->link_state = LPFC_HBA_ERROR;
6585 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006586 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006587 }
6588
James Smart05580562011-05-24 11:40:48 -04006589 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006590
6591 /* Update the fc_host data structures with new wwn. */
6592 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6593 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6594
James Smart8a9d2e82012-05-09 21:16:12 -04006595 /* update host els and scsi xri-sgl sizes and mappings */
6596 rc = lpfc_sli4_xri_sgl_update(phba);
6597 if (unlikely(rc)) {
6598 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6599 "1400 Failed to update xri-sgl size and "
6600 "mapping: %d\n", rc);
6601 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006602 }
6603
James Smart8a9d2e82012-05-09 21:16:12 -04006604 /* register the els sgl pool to the port */
6605 rc = lpfc_sli4_repost_els_sgl_list(phba);
6606 if (unlikely(rc)) {
6607 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6608 "0582 Error %d during els sgl post "
6609 "operation\n", rc);
6610 rc = -ENODEV;
6611 goto out_free_mbox;
6612 }
6613
6614 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006615 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6616 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006617 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006618 "0383 Error %d during scsi sgl post "
6619 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006620 /* Some Scsi buffers were moved to the abort scsi list */
6621 /* A pci function reset will repost them */
6622 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006623 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006624 }
6625
6626 /* Post the rpi header region to the device. */
6627 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6628 if (unlikely(rc)) {
6629 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6630 "0393 Error %d during rpi post operation\n",
6631 rc);
6632 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006633 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006634 }
James Smart97f2ecf2012-03-01 22:35:23 -05006635 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006636
James Smart5350d872011-10-10 21:33:49 -04006637 /* Create all the SLI4 queues */
6638 rc = lpfc_sli4_queue_create(phba);
6639 if (rc) {
6640 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6641 "3089 Failed to allocate queues\n");
6642 rc = -ENODEV;
6643 goto out_stop_timers;
6644 }
James Smartda0436e2009-05-22 14:51:39 -04006645 /* Set up all the queues to the device */
6646 rc = lpfc_sli4_queue_setup(phba);
6647 if (unlikely(rc)) {
6648 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6649 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006650 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006651 }
6652
6653 /* Arm the CQs and then EQs on device */
6654 lpfc_sli4_arm_cqeq_intr(phba);
6655
6656 /* Indicate device interrupt mode */
6657 phba->sli4_hba.intr_enable = 1;
6658
6659 /* Allow asynchronous mailbox command to go through */
6660 spin_lock_irq(&phba->hbalock);
6661 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6662 spin_unlock_irq(&phba->hbalock);
6663
6664 /* Post receive buffers to the device */
6665 lpfc_sli4_rb_setup(phba);
6666
James Smartfc2b9892010-02-26 14:15:29 -05006667 /* Reset HBA FCF states after HBA reset */
6668 phba->fcf.fcf_flag = 0;
6669 phba->fcf.current_rec.flag = 0;
6670
James Smartda0436e2009-05-22 14:51:39 -04006671 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006672 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006673 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04006674
6675 /* Start heart beat timer */
6676 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006677 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006678 phba->hb_outstanding = 0;
6679 phba->last_completion_time = jiffies;
6680
6681 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04006682 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07006683 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04006684
James Smart75baf692010-06-08 18:31:21 -04006685 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6686 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6687 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6688 if (!rc) {
6689 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6690 "2829 This device supports "
6691 "Advanced Error Reporting (AER)\n");
6692 spin_lock_irq(&phba->hbalock);
6693 phba->hba_flag |= HBA_AER_ENABLED;
6694 spin_unlock_irq(&phba->hbalock);
6695 } else {
6696 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6697 "2830 This device does not support "
6698 "Advanced Error Reporting (AER)\n");
6699 phba->cfg_aer_support = 0;
6700 }
James Smart0a96e972011-07-22 18:37:28 -04006701 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006702 }
6703
James Smart76a95d72010-11-20 23:11:48 -05006704 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6705 /*
6706 * The FC Port needs to register FCFI (index 0)
6707 */
6708 lpfc_reg_fcfi(phba, mboxq);
6709 mboxq->vport = phba->pport;
6710 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006711 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006712 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006713 rc = 0;
6714 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6715 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006716
6717 /* Check if the port is configured to be disabled */
6718 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006719 }
James Smart026abb82011-12-13 13:20:45 -05006720
James Smartda0436e2009-05-22 14:51:39 -04006721 /*
6722 * The port is ready, set the host's link state to LINK_DOWN
6723 * in preparation for link interrupts.
6724 */
James Smartda0436e2009-05-22 14:51:39 -04006725 spin_lock_irq(&phba->hbalock);
6726 phba->link_state = LPFC_LINK_DOWN;
6727 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006728 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6729 (phba->hba_flag & LINK_DISABLED)) {
6730 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6731 "3103 Adapter Link is disabled.\n");
6732 lpfc_down_link(phba, mboxq);
6733 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6734 if (rc != MBX_SUCCESS) {
6735 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6736 "3104 Adapter failed to issue "
6737 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6738 goto out_unset_queue;
6739 }
6740 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006741 /* don't perform init_link on SLI4 FC port loopback test */
6742 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6743 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6744 if (rc)
6745 goto out_unset_queue;
6746 }
James Smart5350d872011-10-10 21:33:49 -04006747 }
6748 mempool_free(mboxq, phba->mbox_mem_pool);
6749 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006750out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006751 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006752 lpfc_sli4_queue_unset(phba);
6753out_destroy_queue:
6754 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006755out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006756 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006757out_free_mbox:
6758 mempool_free(mboxq, phba->mbox_mem_pool);
6759 return rc;
6760}
James Smarte59058c2008-08-24 21:49:00 -04006761
6762/**
James Smart3621a712009-04-06 18:47:14 -04006763 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006764 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006765 *
James Smarte59058c2008-08-24 21:49:00 -04006766 * This is the callback function for mailbox timer. The mailbox
6767 * timer is armed when a new mailbox command is issued and the timer
6768 * is deleted when the mailbox complete. The function is called by
6769 * the kernel timer code when a mailbox does not complete within
6770 * expected time. This function wakes up the worker thread to
6771 * process the mailbox timeout and returns. All the processing is
6772 * done by the worker thread function lpfc_mbox_timeout_handler.
6773 **/
dea31012005-04-17 16:05:31 -05006774void
6775lpfc_mbox_timeout(unsigned long ptr)
6776{
James Smart92d7f7b2007-06-17 19:56:38 -05006777 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006778 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006779 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006780
James Smart2e0fef82007-06-17 19:56:36 -05006781 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006782 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006783 if (!tmo_posted)
6784 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6785 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6786
James Smart5e9d9b82008-06-14 22:52:53 -04006787 if (!tmo_posted)
6788 lpfc_worker_wake_up(phba);
6789 return;
dea31012005-04-17 16:05:31 -05006790}
6791
James Smarte8d3c3b2013-10-10 12:21:30 -04006792/**
6793 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6794 * are pending
6795 * @phba: Pointer to HBA context object.
6796 *
6797 * This function checks if any mailbox completions are present on the mailbox
6798 * completion queue.
6799 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04006800static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04006801lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6802{
6803
6804 uint32_t idx;
6805 struct lpfc_queue *mcq;
6806 struct lpfc_mcqe *mcqe;
6807 bool pending_completions = false;
6808
6809 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6810 return false;
6811
6812 /* Check for completions on mailbox completion queue */
6813
6814 mcq = phba->sli4_hba.mbx_cq;
6815 idx = mcq->hba_index;
6816 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6817 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6818 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6819 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6820 pending_completions = true;
6821 break;
6822 }
6823 idx = (idx + 1) % mcq->entry_count;
6824 if (mcq->hba_index == idx)
6825 break;
6826 }
6827 return pending_completions;
6828
6829}
6830
6831/**
6832 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6833 * that were missed.
6834 * @phba: Pointer to HBA context object.
6835 *
6836 * For sli4, it is possible to miss an interrupt. As such mbox completions
6837 * maybe missed causing erroneous mailbox timeouts to occur. This function
6838 * checks to see if mbox completions are on the mailbox completion queue
6839 * and will process all the completions associated with the eq for the
6840 * mailbox completion queue.
6841 **/
6842bool
6843lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6844{
6845
6846 uint32_t eqidx;
6847 struct lpfc_queue *fpeq = NULL;
6848 struct lpfc_eqe *eqe;
6849 bool mbox_pending;
6850
6851 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6852 return false;
6853
6854 /* Find the eq associated with the mcq */
6855
6856 if (phba->sli4_hba.hba_eq)
6857 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6858 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6859 phba->sli4_hba.mbx_cq->assoc_qid) {
6860 fpeq = phba->sli4_hba.hba_eq[eqidx];
6861 break;
6862 }
6863 if (!fpeq)
6864 return false;
6865
6866 /* Turn off interrupts from this EQ */
6867
6868 lpfc_sli4_eq_clr_intr(fpeq);
6869
6870 /* Check to see if a mbox completion is pending */
6871
6872 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6873
6874 /*
6875 * If a mbox completion is pending, process all the events on EQ
6876 * associated with the mbox completion queue (this could include
6877 * mailbox commands, async events, els commands, receive queue data
6878 * and fcp commands)
6879 */
6880
6881 if (mbox_pending)
6882 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6883 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6884 fpeq->EQ_processed++;
6885 }
6886
6887 /* Always clear and re-arm the EQ */
6888
6889 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6890
6891 return mbox_pending;
6892
6893}
James Smarte59058c2008-08-24 21:49:00 -04006894
6895/**
James Smart3621a712009-04-06 18:47:14 -04006896 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006897 * @phba: Pointer to HBA context object.
6898 *
6899 * This function is called from worker thread when a mailbox command times out.
6900 * The caller is not required to hold any locks. This function will reset the
6901 * HBA and recover all the pending commands.
6902 **/
dea31012005-04-17 16:05:31 -05006903void
6904lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6905{
James Smart2e0fef82007-06-17 19:56:36 -05006906 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04006907 MAILBOX_t *mb = NULL;
6908
James Smart1dcb58e2007-04-25 09:51:30 -04006909 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006910
James Smarte8d3c3b2013-10-10 12:21:30 -04006911 /* If the mailbox completed, process the completion and return */
6912 if (lpfc_sli4_process_missed_mbox_completions(phba))
6913 return;
6914
James Smarteb016562014-09-03 12:58:06 -04006915 if (pmbox != NULL)
6916 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04006917 /* Check the pmbox pointer first. There is a race condition
6918 * between the mbox timeout handler getting executed in the
6919 * worklist and the mailbox actually completing. When this
6920 * race condition occurs, the mbox_active will be NULL.
6921 */
6922 spin_lock_irq(&phba->hbalock);
6923 if (pmbox == NULL) {
6924 lpfc_printf_log(phba, KERN_WARNING,
6925 LOG_MBOX | LOG_SLI,
6926 "0353 Active Mailbox cleared - mailbox timeout "
6927 "exiting\n");
6928 spin_unlock_irq(&phba->hbalock);
6929 return;
6930 }
6931
dea31012005-04-17 16:05:31 -05006932 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006933 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006934 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006935 mb->mbxCommand,
6936 phba->pport->port_state,
6937 phba->sli.sli_flag,
6938 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006939 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006940
James Smart1dcb58e2007-04-25 09:51:30 -04006941 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6942 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006943 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006944 */
James Smart2e0fef82007-06-17 19:56:36 -05006945 spin_lock_irq(&phba->pport->work_port_lock);
6946 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6947 spin_unlock_irq(&phba->pport->work_port_lock);
6948 spin_lock_irq(&phba->hbalock);
6949 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006950 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006951 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006952
James Smartdb55fba2014-04-04 13:52:02 -04006953 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04006954
6955 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006956 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006957
6958 /* Reset the HBA device */
6959 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006960}
6961
James Smarte59058c2008-08-24 21:49:00 -04006962/**
James Smart3772a992009-05-22 14:50:54 -04006963 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006964 * @phba: Pointer to HBA context object.
6965 * @pmbox: Pointer to mailbox object.
6966 * @flag: Flag indicating how the mailbox need to be processed.
6967 *
6968 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006969 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6970 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006971 * The mailbox command can be submitted in polling mode, in which case
6972 * this function will wait in a polling loop for the completion of the
6973 * mailbox.
6974 * If the mailbox is submitted in no_wait mode (not polling) the
6975 * function will submit the command and returns immediately without waiting
6976 * for the mailbox completion. The no_wait is supported only when HBA
6977 * is in SLI2/SLI3 mode - interrupts are enabled.
6978 * The SLI interface allows only one mailbox pending at a time. If the
6979 * mailbox is issued in polling mode and there is already a mailbox
6980 * pending, then the function will return an error. If the mailbox is issued
6981 * in NO_WAIT mode and there is a mailbox pending already, the function
6982 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6983 * The sli layer owns the mailbox object until the completion of mailbox
6984 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6985 * return codes the caller owns the mailbox command after the return of
6986 * the function.
6987 **/
James Smart3772a992009-05-22 14:50:54 -04006988static int
6989lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6990 uint32_t flag)
dea31012005-04-17 16:05:31 -05006991{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08006992 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05006993 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006994 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05006995 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05006996 int i;
James Smart09372822008-01-11 01:52:54 -05006997 unsigned long timeout;
dea31012005-04-17 16:05:31 -05006998 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04006999 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007000 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007001 int processing_queue = 0;
7002
7003 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7004 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007005 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007006 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007007 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7008 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7009 return MBX_SUCCESS;
7010 }
James Smart58da1ff2008-04-07 10:15:56 -04007011 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007012 pmbox = lpfc_mbox_get(phba);
7013 if (!pmbox) {
7014 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7015 return MBX_SUCCESS;
7016 }
7017 }
dea31012005-04-17 16:05:31 -05007018
James Smarted957682007-06-17 19:56:37 -05007019 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007020 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007021 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007022 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007023 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007024 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007025 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007026 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007027 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007028 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007029 }
7030 }
7031
Linas Vepstas8d63f372007-02-14 14:28:36 -06007032 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007033 if (unlikely(pci_channel_offline(phba->pcidev))) {
7034 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7035 goto out_not_finished;
7036 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007037
James Smarta257bf92009-04-06 18:48:10 -04007038 /* If HBA has a deferred error attention, fail the iocb. */
7039 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7040 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7041 goto out_not_finished;
7042 }
7043
dea31012005-04-17 16:05:31 -05007044 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007045
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007046 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007047 status = MBX_SUCCESS;
7048
James Smart2e0fef82007-06-17 19:56:36 -05007049 if (phba->link_state == LPFC_HBA_ERROR) {
7050 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007051
7052 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007053 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7054 "(%d):0311 Mailbox command x%x cannot "
7055 "issue Data: x%x x%x\n",
7056 pmbox->vport ? pmbox->vport->vpi : 0,
7057 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007058 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007059 }
7060
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007061 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007062 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7063 !(hc_copy & HC_MBINT_ENA)) {
7064 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7065 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007066 "(%d):2528 Mailbox command x%x cannot "
7067 "issue Data: x%x x%x\n",
7068 pmbox->vport ? pmbox->vport->vpi : 0,
7069 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007070 goto out_not_finished;
7071 }
James Smart92908312006-03-07 15:04:13 -05007072 }
7073
dea31012005-04-17 16:05:31 -05007074 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7075 /* Polling for a mbox command when another one is already active
7076 * is not allowed in SLI. Also, the driver must have established
7077 * SLI2 mode to queue and process multiple mbox commands.
7078 */
7079
7080 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007081 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007082
7083 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007084 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7085 "(%d):2529 Mailbox command x%x "
7086 "cannot issue Data: x%x x%x\n",
7087 pmbox->vport ? pmbox->vport->vpi : 0,
7088 pmbox->u.mb.mbxCommand,
7089 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007090 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007091 }
7092
James Smart3772a992009-05-22 14:50:54 -04007093 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007094 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007095 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007096 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7097 "(%d):2530 Mailbox command x%x "
7098 "cannot issue Data: x%x x%x\n",
7099 pmbox->vport ? pmbox->vport->vpi : 0,
7100 pmbox->u.mb.mbxCommand,
7101 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007102 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007103 }
7104
dea31012005-04-17 16:05:31 -05007105 /* Another mailbox command is still being processed, queue this
7106 * command to be processed later.
7107 */
7108 lpfc_mbox_put(phba, pmbox);
7109
7110 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007111 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007112 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007113 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007114 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007115 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007116 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007117
7118 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007119 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007120
James Smart858c9f62007-06-17 19:56:39 -05007121 if (pmbox->vport) {
7122 lpfc_debugfs_disc_trc(pmbox->vport,
7123 LPFC_DISC_TRC_MBOX_VPORT,
7124 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007125 (uint32_t)mbx->mbxCommand,
7126 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007127 }
7128 else {
7129 lpfc_debugfs_disc_trc(phba->pport,
7130 LPFC_DISC_TRC_MBOX,
7131 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007132 (uint32_t)mbx->mbxCommand,
7133 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007134 }
7135
James Smart2e0fef82007-06-17 19:56:36 -05007136 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007137 }
7138
dea31012005-04-17 16:05:31 -05007139 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7140
7141 /* If we are not polling, we MUST be in SLI2 mode */
7142 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007143 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007144 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007145 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007146 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007147 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007148 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7149 "(%d):2531 Mailbox command x%x "
7150 "cannot issue Data: x%x x%x\n",
7151 pmbox->vport ? pmbox->vport->vpi : 0,
7152 pmbox->u.mb.mbxCommand,
7153 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007154 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007155 }
7156 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007157 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7158 1000);
7159 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007160 }
7161
7162 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007163 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007164 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007165 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007166 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007167 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007168 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007169
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007170 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007171 if (pmbox->vport) {
7172 lpfc_debugfs_disc_trc(pmbox->vport,
7173 LPFC_DISC_TRC_MBOX_VPORT,
7174 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007175 (uint32_t)mbx->mbxCommand,
7176 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007177 }
7178 else {
7179 lpfc_debugfs_disc_trc(phba->pport,
7180 LPFC_DISC_TRC_MBOX,
7181 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007182 (uint32_t)mbx->mbxCommand,
7183 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007184 }
7185 }
7186
dea31012005-04-17 16:05:31 -05007187 psli->slistat.mbox_cmd++;
7188 evtctr = psli->slistat.mbox_event;
7189
7190 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007191 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007192
James Smart3772a992009-05-22 14:50:54 -04007193 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart7a470272010-03-15 11:25:20 -04007194 /* Populate mbox extension offset word. */
7195 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007196 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007197 = (uint8_t *)phba->mbox_ext
7198 - (uint8_t *)phba->mbox;
7199 }
7200
7201 /* Copy the mailbox extension data */
7202 if (pmbox->in_ext_byte_len && pmbox->context2) {
7203 lpfc_sli_pcimem_bcopy(pmbox->context2,
7204 (uint8_t *)phba->mbox_ext,
7205 pmbox->in_ext_byte_len);
7206 }
7207 /* Copy command data to host SLIM area */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007208 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007209 } else {
James Smart7a470272010-03-15 11:25:20 -04007210 /* Populate mbox extension offset word. */
7211 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007212 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007213 = MAILBOX_HBA_EXT_OFFSET;
7214
7215 /* Copy the mailbox extension data */
7216 if (pmbox->in_ext_byte_len && pmbox->context2) {
7217 lpfc_memcpy_to_slim(phba->MBslimaddr +
7218 MAILBOX_HBA_EXT_OFFSET,
7219 pmbox->context2, pmbox->in_ext_byte_len);
7220
7221 }
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007222 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007223 /* copy command data into host mbox for cmpl */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007224 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007225 }
7226
7227 /* First copy mbox command data to HBA SLIM, skip past first
7228 word */
7229 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007230 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007231 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7232
7233 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007234 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007235 to_slim = phba->MBslimaddr;
7236 writel(ldata, to_slim);
7237 readl(to_slim); /* flush */
7238
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007239 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007240 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007241 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007242 }
7243 }
7244
7245 wmb();
dea31012005-04-17 16:05:31 -05007246
7247 switch (flag) {
7248 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007249 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007250 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007251 /* Interrupt board to do it */
7252 writel(CA_MBATT, phba->CAregaddr);
7253 readl(phba->CAregaddr); /* flush */
7254 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007255 break;
7256
7257 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007258 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007259 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007260 /* Interrupt board to do it */
7261 writel(CA_MBATT, phba->CAregaddr);
7262 readl(phba->CAregaddr); /* flush */
7263
James Smart3772a992009-05-22 14:50:54 -04007264 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007265 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007266 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007267 word0 = le32_to_cpu(word0);
7268 } else {
7269 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007270 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7271 spin_unlock_irqrestore(&phba->hbalock,
7272 drvr_flag);
7273 goto out_not_finished;
7274 }
dea31012005-04-17 16:05:31 -05007275 }
7276
7277 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007278 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7279 spin_unlock_irqrestore(&phba->hbalock,
7280 drvr_flag);
7281 goto out_not_finished;
7282 }
James Smarta183a152011-10-10 21:32:43 -04007283 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7284 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007285 i = 0;
dea31012005-04-17 16:05:31 -05007286 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007287 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7288 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007289 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007290 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007291 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007292 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007293 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007294 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007295 }
7296
7297 /* Check if we took a mbox interrupt while we were
7298 polling */
7299 if (((word0 & OWN_CHIP) != OWN_CHIP)
7300 && (evtctr != psli->slistat.mbox_event))
7301 break;
7302
James Smart09372822008-01-11 01:52:54 -05007303 if (i++ > 10) {
7304 spin_unlock_irqrestore(&phba->hbalock,
7305 drvr_flag);
7306 msleep(1);
7307 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7308 }
dea31012005-04-17 16:05:31 -05007309
James Smart3772a992009-05-22 14:50:54 -04007310 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007311 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007312 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007313 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007314 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007315 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007316 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007317 /* Check real SLIM for any errors */
7318 slimword0 = readl(phba->MBslimaddr);
7319 slimmb = (MAILBOX_t *) & slimword0;
7320 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7321 && slimmb->mbxStatus) {
7322 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007323 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007324 word0 = slimword0;
7325 }
7326 }
7327 } else {
7328 /* First copy command data */
7329 word0 = readl(phba->MBslimaddr);
7330 }
7331 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007332 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7333 spin_unlock_irqrestore(&phba->hbalock,
7334 drvr_flag);
7335 goto out_not_finished;
7336 }
dea31012005-04-17 16:05:31 -05007337 }
7338
James Smart3772a992009-05-22 14:50:54 -04007339 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007340 /* copy results back to user */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007341 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007342 /* Copy the mailbox extension data */
7343 if (pmbox->out_ext_byte_len && pmbox->context2) {
7344 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7345 pmbox->context2,
7346 pmbox->out_ext_byte_len);
7347 }
dea31012005-04-17 16:05:31 -05007348 } else {
7349 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007350 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
dea31012005-04-17 16:05:31 -05007351 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007352 /* Copy the mailbox extension data */
7353 if (pmbox->out_ext_byte_len && pmbox->context2) {
7354 lpfc_memcpy_from_slim(pmbox->context2,
7355 phba->MBslimaddr +
7356 MAILBOX_HBA_EXT_OFFSET,
7357 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007358 }
7359 }
7360
7361 writel(HA_MBATT, phba->HAregaddr);
7362 readl(phba->HAregaddr); /* flush */
7363
7364 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007365 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007366 }
7367
James Smart2e0fef82007-06-17 19:56:36 -05007368 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7369 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007370
7371out_not_finished:
7372 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007373 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007374 lpfc_mbox_cmpl_put(phba, pmbox);
7375 }
7376 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007377}
7378
James Smarte59058c2008-08-24 21:49:00 -04007379/**
James Smartf1126682009-06-10 17:22:44 -04007380 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7381 * @phba: Pointer to HBA context object.
7382 *
7383 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7384 * the driver internal pending mailbox queue. It will then try to wait out the
7385 * possible outstanding mailbox command before return.
7386 *
7387 * Returns:
7388 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7389 * the outstanding mailbox command timed out.
7390 **/
7391static int
7392lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7393{
7394 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007395 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007396 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007397
7398 /* Mark the asynchronous mailbox command posting as blocked */
7399 spin_lock_irq(&phba->hbalock);
7400 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007401 /* Determine how long we might wait for the active mailbox
7402 * command to be gracefully completed by firmware.
7403 */
James Smarta183a152011-10-10 21:32:43 -04007404 if (phba->sli.mbox_active)
7405 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7406 phba->sli.mbox_active) *
7407 1000) + jiffies;
7408 spin_unlock_irq(&phba->hbalock);
7409
James Smarte8d3c3b2013-10-10 12:21:30 -04007410 /* Make sure the mailbox is really active */
7411 if (timeout)
7412 lpfc_sli4_process_missed_mbox_completions(phba);
7413
James Smartf1126682009-06-10 17:22:44 -04007414 /* Wait for the outstnading mailbox command to complete */
7415 while (phba->sli.mbox_active) {
7416 /* Check active mailbox complete status every 2ms */
7417 msleep(2);
7418 if (time_after(jiffies, timeout)) {
7419 /* Timeout, marked the outstanding cmd not complete */
7420 rc = 1;
7421 break;
7422 }
7423 }
7424
7425 /* Can not cleanly block async mailbox command, fails it */
7426 if (rc) {
7427 spin_lock_irq(&phba->hbalock);
7428 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7429 spin_unlock_irq(&phba->hbalock);
7430 }
7431 return rc;
7432}
7433
7434/**
7435 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7436 * @phba: Pointer to HBA context object.
7437 *
7438 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7439 * commands from the driver internal pending mailbox queue. It makes sure
7440 * that there is no outstanding mailbox command before resuming posting
7441 * asynchronous mailbox commands. If, for any reason, there is outstanding
7442 * mailbox command, it will try to wait it out before resuming asynchronous
7443 * mailbox command posting.
7444 **/
7445static void
7446lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7447{
7448 struct lpfc_sli *psli = &phba->sli;
7449
7450 spin_lock_irq(&phba->hbalock);
7451 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7452 /* Asynchronous mailbox posting is not blocked, do nothing */
7453 spin_unlock_irq(&phba->hbalock);
7454 return;
7455 }
7456
7457 /* Outstanding synchronous mailbox command is guaranteed to be done,
7458 * successful or timeout, after timing-out the outstanding mailbox
7459 * command shall always be removed, so just unblock posting async
7460 * mailbox command and resume
7461 */
7462 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7463 spin_unlock_irq(&phba->hbalock);
7464
7465 /* wake up worker thread to post asynchronlous mailbox command */
7466 lpfc_worker_wake_up(phba);
7467}
7468
7469/**
James Smart2d843ed2012-09-29 11:29:06 -04007470 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7471 * @phba: Pointer to HBA context object.
7472 * @mboxq: Pointer to mailbox object.
7473 *
7474 * The function waits for the bootstrap mailbox register ready bit from
7475 * port for twice the regular mailbox command timeout value.
7476 *
7477 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7478 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7479 **/
7480static int
7481lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7482{
7483 uint32_t db_ready;
7484 unsigned long timeout;
7485 struct lpfc_register bmbx_reg;
7486
7487 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7488 * 1000) + jiffies;
7489
7490 do {
7491 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7492 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7493 if (!db_ready)
7494 msleep(2);
7495
7496 if (time_after(jiffies, timeout))
7497 return MBXERR_ERROR;
7498 } while (!db_ready);
7499
7500 return 0;
7501}
7502
7503/**
James Smartda0436e2009-05-22 14:51:39 -04007504 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7505 * @phba: Pointer to HBA context object.
7506 * @mboxq: Pointer to mailbox object.
7507 *
7508 * The function posts a mailbox to the port. The mailbox is expected
7509 * to be comletely filled in and ready for the port to operate on it.
7510 * This routine executes a synchronous completion operation on the
7511 * mailbox by polling for its completion.
7512 *
7513 * The caller must not be holding any locks when calling this routine.
7514 *
7515 * Returns:
7516 * MBX_SUCCESS - mailbox posted successfully
7517 * Any of the MBX error values.
7518 **/
7519static int
7520lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7521{
7522 int rc = MBX_SUCCESS;
7523 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007524 uint32_t mcqe_status;
7525 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007526 struct lpfc_sli *psli = &phba->sli;
7527 struct lpfc_mqe *mb = &mboxq->u.mqe;
7528 struct lpfc_bmbx_create *mbox_rgn;
7529 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007530
7531 /*
7532 * Only one mailbox can be active to the bootstrap mailbox region
7533 * at a time and there is no queueing provided.
7534 */
7535 spin_lock_irqsave(&phba->hbalock, iflag);
7536 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7537 spin_unlock_irqrestore(&phba->hbalock, iflag);
7538 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007539 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007540 "cannot issue Data: x%x x%x\n",
7541 mboxq->vport ? mboxq->vport->vpi : 0,
7542 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007543 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7544 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007545 psli->sli_flag, MBX_POLL);
7546 return MBXERR_ERROR;
7547 }
7548 /* The server grabs the token and owns it until release */
7549 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7550 phba->sli.mbox_active = mboxq;
7551 spin_unlock_irqrestore(&phba->hbalock, iflag);
7552
James Smart2d843ed2012-09-29 11:29:06 -04007553 /* wait for bootstrap mbox register for readyness */
7554 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7555 if (rc)
7556 goto exit;
7557
James Smartda0436e2009-05-22 14:51:39 -04007558 /*
7559 * Initialize the bootstrap memory region to avoid stale data areas
7560 * in the mailbox post. Then copy the caller's mailbox contents to
7561 * the bmbx mailbox region.
7562 */
7563 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7564 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7565 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7566 sizeof(struct lpfc_mqe));
7567
7568 /* Post the high mailbox dma address to the port and wait for ready. */
7569 dma_address = &phba->sli4_hba.bmbx.dma_address;
7570 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7571
James Smart2d843ed2012-09-29 11:29:06 -04007572 /* wait for bootstrap mbox register for hi-address write done */
7573 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7574 if (rc)
7575 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007576
7577 /* Post the low mailbox dma address to the port. */
7578 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007579
James Smart2d843ed2012-09-29 11:29:06 -04007580 /* wait for bootstrap mbox register for low address write done */
7581 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7582 if (rc)
7583 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007584
7585 /*
7586 * Read the CQ to ensure the mailbox has completed.
7587 * If so, update the mailbox status so that the upper layers
7588 * can complete the request normally.
7589 */
7590 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7591 sizeof(struct lpfc_mqe));
7592 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7593 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7594 sizeof(struct lpfc_mcqe));
7595 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007596 /*
7597 * When the CQE status indicates a failure and the mailbox status
7598 * indicates success then copy the CQE status into the mailbox status
7599 * (and prefix it with x4000).
7600 */
James Smartda0436e2009-05-22 14:51:39 -04007601 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007602 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7603 bf_set(lpfc_mqe_status, mb,
7604 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007605 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007606 } else
7607 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007608
7609 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007610 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007611 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7612 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007613 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7614 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7615 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007616 bf_get(lpfc_mqe_status, mb),
7617 mb->un.mb_words[0], mb->un.mb_words[1],
7618 mb->un.mb_words[2], mb->un.mb_words[3],
7619 mb->un.mb_words[4], mb->un.mb_words[5],
7620 mb->un.mb_words[6], mb->un.mb_words[7],
7621 mb->un.mb_words[8], mb->un.mb_words[9],
7622 mb->un.mb_words[10], mb->un.mb_words[11],
7623 mb->un.mb_words[12], mboxq->mcqe.word0,
7624 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7625 mboxq->mcqe.trailer);
7626exit:
7627 /* We are holding the token, no needed for lock when release */
7628 spin_lock_irqsave(&phba->hbalock, iflag);
7629 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7630 phba->sli.mbox_active = NULL;
7631 spin_unlock_irqrestore(&phba->hbalock, iflag);
7632 return rc;
7633}
7634
7635/**
7636 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7637 * @phba: Pointer to HBA context object.
7638 * @pmbox: Pointer to mailbox object.
7639 * @flag: Flag indicating how the mailbox need to be processed.
7640 *
7641 * This function is called by discovery code and HBA management code to submit
7642 * a mailbox command to firmware with SLI-4 interface spec.
7643 *
7644 * Return codes the caller owns the mailbox command after the return of the
7645 * function.
7646 **/
7647static int
7648lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7649 uint32_t flag)
7650{
7651 struct lpfc_sli *psli = &phba->sli;
7652 unsigned long iflags;
7653 int rc;
7654
James Smartb76f2dc2011-07-22 18:37:42 -04007655 /* dump from issue mailbox command if setup */
7656 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7657
James Smart8fa38512009-07-19 10:01:03 -04007658 rc = lpfc_mbox_dev_check(phba);
7659 if (unlikely(rc)) {
7660 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007661 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007662 "cannot issue Data: x%x x%x\n",
7663 mboxq->vport ? mboxq->vport->vpi : 0,
7664 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007665 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7666 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007667 psli->sli_flag, flag);
7668 goto out_not_finished;
7669 }
7670
James Smartda0436e2009-05-22 14:51:39 -04007671 /* Detect polling mode and jump to a handler */
7672 if (!phba->sli4_hba.intr_enable) {
7673 if (flag == MBX_POLL)
7674 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7675 else
7676 rc = -EIO;
7677 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007678 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007679 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007680 "(x%x/x%x) failure: "
7681 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7682 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007683 mboxq->vport ? mboxq->vport->vpi : 0,
7684 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007685 lpfc_sli_config_mbox_subsys_get(phba,
7686 mboxq),
7687 lpfc_sli_config_mbox_opcode_get(phba,
7688 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007689 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7690 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7691 bf_get(lpfc_mcqe_ext_status,
7692 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007693 psli->sli_flag, flag);
7694 return rc;
7695 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007696 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7697 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007698 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007699 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007700 mboxq->vport ? mboxq->vport->vpi : 0,
7701 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007702 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7703 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007704 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007705 /* Try to block the asynchronous mailbox posting */
7706 rc = lpfc_sli4_async_mbox_block(phba);
7707 if (!rc) {
7708 /* Successfully blocked, now issue sync mbox cmd */
7709 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7710 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007711 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007712 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007713 "(%d):2597 Sync Mailbox command "
7714 "x%x (x%x/x%x) failure: "
7715 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7716 "Data: x%x x%x\n,",
7717 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007718 mboxq->u.mb.mbxCommand,
7719 lpfc_sli_config_mbox_subsys_get(phba,
7720 mboxq),
7721 lpfc_sli_config_mbox_opcode_get(phba,
7722 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007723 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7724 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7725 bf_get(lpfc_mcqe_ext_status,
7726 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007727 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007728 /* Unblock the async mailbox posting afterward */
7729 lpfc_sli4_async_mbox_unblock(phba);
7730 }
7731 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007732 }
7733
7734 /* Now, interrupt mode asynchrous mailbox command */
7735 rc = lpfc_mbox_cmd_check(phba, mboxq);
7736 if (rc) {
7737 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007738 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007739 "cannot issue Data: x%x x%x\n",
7740 mboxq->vport ? mboxq->vport->vpi : 0,
7741 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007742 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7743 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007744 psli->sli_flag, flag);
7745 goto out_not_finished;
7746 }
James Smartda0436e2009-05-22 14:51:39 -04007747
7748 /* Put the mailbox command to the driver internal FIFO */
7749 psli->slistat.mbox_busy++;
7750 spin_lock_irqsave(&phba->hbalock, iflags);
7751 lpfc_mbox_put(phba, mboxq);
7752 spin_unlock_irqrestore(&phba->hbalock, iflags);
7753 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7754 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007755 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007756 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7757 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007758 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7759 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007760 phba->pport->port_state,
7761 psli->sli_flag, MBX_NOWAIT);
7762 /* Wake up worker thread to transport mailbox command from head */
7763 lpfc_worker_wake_up(phba);
7764
7765 return MBX_BUSY;
7766
7767out_not_finished:
7768 return MBX_NOT_FINISHED;
7769}
7770
7771/**
7772 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7773 * @phba: Pointer to HBA context object.
7774 *
7775 * This function is called by worker thread to send a mailbox command to
7776 * SLI4 HBA firmware.
7777 *
7778 **/
7779int
7780lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7781{
7782 struct lpfc_sli *psli = &phba->sli;
7783 LPFC_MBOXQ_t *mboxq;
7784 int rc = MBX_SUCCESS;
7785 unsigned long iflags;
7786 struct lpfc_mqe *mqe;
7787 uint32_t mbx_cmnd;
7788
7789 /* Check interrupt mode before post async mailbox command */
7790 if (unlikely(!phba->sli4_hba.intr_enable))
7791 return MBX_NOT_FINISHED;
7792
7793 /* Check for mailbox command service token */
7794 spin_lock_irqsave(&phba->hbalock, iflags);
7795 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7796 spin_unlock_irqrestore(&phba->hbalock, iflags);
7797 return MBX_NOT_FINISHED;
7798 }
7799 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7800 spin_unlock_irqrestore(&phba->hbalock, iflags);
7801 return MBX_NOT_FINISHED;
7802 }
7803 if (unlikely(phba->sli.mbox_active)) {
7804 spin_unlock_irqrestore(&phba->hbalock, iflags);
7805 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7806 "0384 There is pending active mailbox cmd\n");
7807 return MBX_NOT_FINISHED;
7808 }
7809 /* Take the mailbox command service token */
7810 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7811
7812 /* Get the next mailbox command from head of queue */
7813 mboxq = lpfc_mbox_get(phba);
7814
7815 /* If no more mailbox command waiting for post, we're done */
7816 if (!mboxq) {
7817 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7818 spin_unlock_irqrestore(&phba->hbalock, iflags);
7819 return MBX_SUCCESS;
7820 }
7821 phba->sli.mbox_active = mboxq;
7822 spin_unlock_irqrestore(&phba->hbalock, iflags);
7823
7824 /* Check device readiness for posting mailbox command */
7825 rc = lpfc_mbox_dev_check(phba);
7826 if (unlikely(rc))
7827 /* Driver clean routine will clean up pending mailbox */
7828 goto out_not_finished;
7829
7830 /* Prepare the mbox command to be posted */
7831 mqe = &mboxq->u.mqe;
7832 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7833
7834 /* Start timer for the mbox_tmo and log some mailbox post messages */
7835 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04007836 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007837
7838 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007839 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007840 "x%x x%x\n",
7841 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007842 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7843 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007844 phba->pport->port_state, psli->sli_flag);
7845
7846 if (mbx_cmnd != MBX_HEARTBEAT) {
7847 if (mboxq->vport) {
7848 lpfc_debugfs_disc_trc(mboxq->vport,
7849 LPFC_DISC_TRC_MBOX_VPORT,
7850 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7851 mbx_cmnd, mqe->un.mb_words[0],
7852 mqe->un.mb_words[1]);
7853 } else {
7854 lpfc_debugfs_disc_trc(phba->pport,
7855 LPFC_DISC_TRC_MBOX,
7856 "MBOX Send: cmd:x%x mb:x%x x%x",
7857 mbx_cmnd, mqe->un.mb_words[0],
7858 mqe->un.mb_words[1]);
7859 }
7860 }
7861 psli->slistat.mbox_cmd++;
7862
7863 /* Post the mailbox command to the port */
7864 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7865 if (rc != MBX_SUCCESS) {
7866 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007867 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007868 "cannot issue Data: x%x x%x\n",
7869 mboxq->vport ? mboxq->vport->vpi : 0,
7870 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007871 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7872 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007873 psli->sli_flag, MBX_NOWAIT);
7874 goto out_not_finished;
7875 }
7876
7877 return rc;
7878
7879out_not_finished:
7880 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007881 if (phba->sli.mbox_active) {
7882 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7883 __lpfc_mbox_cmpl_put(phba, mboxq);
7884 /* Release the token */
7885 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7886 phba->sli.mbox_active = NULL;
7887 }
James Smartda0436e2009-05-22 14:51:39 -04007888 spin_unlock_irqrestore(&phba->hbalock, iflags);
7889
7890 return MBX_NOT_FINISHED;
7891}
7892
7893/**
7894 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7895 * @phba: Pointer to HBA context object.
7896 * @pmbox: Pointer to mailbox object.
7897 * @flag: Flag indicating how the mailbox need to be processed.
7898 *
7899 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7900 * the API jump table function pointer from the lpfc_hba struct.
7901 *
7902 * Return codes the caller owns the mailbox command after the return of the
7903 * function.
7904 **/
7905int
7906lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7907{
7908 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7909}
7910
7911/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007912 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007913 * @phba: The hba struct for which this call is being executed.
7914 * @dev_grp: The HBA PCI-Device group number.
7915 *
7916 * This routine sets up the mbox interface API function jump table in @phba
7917 * struct.
7918 * Returns: 0 - success, -ENODEV - failure.
7919 **/
7920int
7921lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7922{
7923
7924 switch (dev_grp) {
7925 case LPFC_PCI_DEV_LP:
7926 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7927 phba->lpfc_sli_handle_slow_ring_event =
7928 lpfc_sli_handle_slow_ring_event_s3;
7929 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7930 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7931 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7932 break;
7933 case LPFC_PCI_DEV_OC:
7934 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7935 phba->lpfc_sli_handle_slow_ring_event =
7936 lpfc_sli_handle_slow_ring_event_s4;
7937 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7938 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7939 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7940 break;
7941 default:
7942 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7943 "1420 Invalid HBA PCI-device group: 0x%x\n",
7944 dev_grp);
7945 return -ENODEV;
7946 break;
7947 }
7948 return 0;
7949}
7950
7951/**
James Smart3621a712009-04-06 18:47:14 -04007952 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007953 * @phba: Pointer to HBA context object.
7954 * @pring: Pointer to driver SLI ring object.
7955 * @piocb: Pointer to address of newly added command iocb.
7956 *
7957 * This function is called with hbalock held to add a command
7958 * iocb to the txq when SLI layer cannot submit the command iocb
7959 * to the ring.
7960 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007961void
James Smart92d7f7b2007-06-17 19:56:38 -05007962__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007963 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007964{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01007965 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007966 /* Insert the caller's iocb in the txq tail for later processing. */
7967 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05007968}
7969
James Smarte59058c2008-08-24 21:49:00 -04007970/**
James Smart3621a712009-04-06 18:47:14 -04007971 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007972 * @phba: Pointer to HBA context object.
7973 * @pring: Pointer to driver SLI ring object.
7974 * @piocb: Pointer to address of newly added command iocb.
7975 *
7976 * This function is called with hbalock held before a new
7977 * iocb is submitted to the firmware. This function checks
7978 * txq to flush the iocbs in txq to Firmware before
7979 * submitting new iocbs to the Firmware.
7980 * If there are iocbs in the txq which need to be submitted
7981 * to firmware, lpfc_sli_next_iocb returns the first element
7982 * of the txq after dequeuing it from txq.
7983 * If there is no iocb in the txq then the function will return
7984 * *piocb and *piocb is set to NULL. Caller needs to check
7985 * *piocb to find if there are more commands in the txq.
7986 **/
dea31012005-04-17 16:05:31 -05007987static struct lpfc_iocbq *
7988lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007989 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05007990{
7991 struct lpfc_iocbq * nextiocb;
7992
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01007993 lockdep_assert_held(&phba->hbalock);
7994
dea31012005-04-17 16:05:31 -05007995 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7996 if (!nextiocb) {
7997 nextiocb = *piocb;
7998 *piocb = NULL;
7999 }
8000
8001 return nextiocb;
8002}
8003
James Smarte59058c2008-08-24 21:49:00 -04008004/**
James Smart3772a992009-05-22 14:50:54 -04008005 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008006 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008007 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008008 * @piocb: Pointer to command iocb.
8009 * @flag: Flag indicating if this command can be put into txq.
8010 *
James Smart3772a992009-05-22 14:50:54 -04008011 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8012 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8013 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8014 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8015 * this function allows only iocbs for posting buffers. This function finds
8016 * next available slot in the command ring and posts the command to the
8017 * available slot and writes the port attention register to request HBA start
8018 * processing new iocb. If there is no slot available in the ring and
8019 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8020 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008021 *
James Smart3772a992009-05-22 14:50:54 -04008022 * This function is called with hbalock held. The function will return success
8023 * after it successfully submit the iocb to firmware or after adding to the
8024 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008025 **/
James Smart98c9ea52007-10-27 13:37:33 -04008026static int
James Smart3772a992009-05-22 14:50:54 -04008027__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008028 struct lpfc_iocbq *piocb, uint32_t flag)
8029{
8030 struct lpfc_iocbq *nextiocb;
8031 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04008032 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05008033
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008034 lockdep_assert_held(&phba->hbalock);
8035
James Smart92d7f7b2007-06-17 19:56:38 -05008036 if (piocb->iocb_cmpl && (!piocb->vport) &&
8037 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8038 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8039 lpfc_printf_log(phba, KERN_ERR,
8040 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008041 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008042 piocb->iocb.ulpCommand);
8043 dump_stack();
8044 return IOCB_ERROR;
8045 }
8046
8047
Linas Vepstas8d63f372007-02-14 14:28:36 -06008048 /* If the PCI channel is in offline state, do not post iocbs. */
8049 if (unlikely(pci_channel_offline(phba->pcidev)))
8050 return IOCB_ERROR;
8051
James Smarta257bf92009-04-06 18:48:10 -04008052 /* If HBA has a deferred error attention, fail the iocb. */
8053 if (unlikely(phba->hba_flag & DEFER_ERATT))
8054 return IOCB_ERROR;
8055
dea31012005-04-17 16:05:31 -05008056 /*
8057 * We should never get an IOCB if we are in a < LINK_DOWN state
8058 */
James Smart2e0fef82007-06-17 19:56:36 -05008059 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008060 return IOCB_ERROR;
8061
8062 /*
8063 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008064 * outstanding event.
dea31012005-04-17 16:05:31 -05008065 */
James Smart0b727fe2007-10-27 13:37:25 -04008066 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008067 goto iocb_busy;
8068
James Smart2e0fef82007-06-17 19:56:36 -05008069 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008070 /*
James Smart2680eea2007-04-25 09:52:55 -04008071 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008072 * can be issued if the link is not up.
8073 */
8074 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008075 case CMD_GEN_REQUEST64_CR:
8076 case CMD_GEN_REQUEST64_CX:
8077 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8078 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008079 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008080 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8081 MENLO_TRANSPORT_TYPE))
8082
8083 goto iocb_busy;
8084 break;
dea31012005-04-17 16:05:31 -05008085 case CMD_QUE_RING_BUF_CN:
8086 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008087 /*
8088 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8089 * completion, iocb_cmpl MUST be 0.
8090 */
8091 if (piocb->iocb_cmpl)
8092 piocb->iocb_cmpl = NULL;
8093 /*FALLTHROUGH*/
8094 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008095 case CMD_CLOSE_XRI_CN:
8096 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008097 break;
8098 default:
8099 goto iocb_busy;
8100 }
8101
8102 /*
8103 * For FCP commands, we must be in a state where we can process link
8104 * attention events.
8105 */
8106 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05008107 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008108 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008109 }
dea31012005-04-17 16:05:31 -05008110
dea31012005-04-17 16:05:31 -05008111 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8112 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8113 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8114
8115 if (iocb)
8116 lpfc_sli_update_ring(phba, pring);
8117 else
8118 lpfc_sli_update_full_ring(phba, pring);
8119
8120 if (!piocb)
8121 return IOCB_SUCCESS;
8122
8123 goto out_busy;
8124
8125 iocb_busy:
8126 pring->stats.iocb_cmd_delay++;
8127
8128 out_busy:
8129
8130 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008131 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008132 return IOCB_SUCCESS;
8133 }
8134
8135 return IOCB_BUSY;
8136}
8137
James Smart3772a992009-05-22 14:50:54 -04008138/**
James Smart4f774512009-05-22 14:52:35 -04008139 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8140 * @phba: Pointer to HBA context object.
8141 * @piocb: Pointer to command iocb.
8142 * @sglq: Pointer to the scatter gather queue object.
8143 *
8144 * This routine converts the bpl or bde that is in the IOCB
8145 * to a sgl list for the sli4 hardware. The physical address
8146 * of the bpl/bde is converted back to a virtual address.
8147 * If the IOCB contains a BPL then the list of BDE's is
8148 * converted to sli4_sge's. If the IOCB contains a single
8149 * BDE then it is converted to a single sli_sge.
8150 * The IOCB is still in cpu endianess so the contents of
8151 * the bpl can be used without byte swapping.
8152 *
8153 * Returns valid XRI = Success, NO_XRI = Failure.
8154**/
8155static uint16_t
8156lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8157 struct lpfc_sglq *sglq)
8158{
8159 uint16_t xritag = NO_XRI;
8160 struct ulp_bde64 *bpl = NULL;
8161 struct ulp_bde64 bde;
8162 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008163 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008164 IOCB_t *icmd;
8165 int numBdes = 0;
8166 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008167 uint32_t offset = 0; /* accumulated offset in the sg request list */
8168 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008169
8170 if (!piocbq || !sglq)
8171 return xritag;
8172
8173 sgl = (struct sli4_sge *)sglq->sgl;
8174 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008175 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8176 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008177 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8178 numBdes = icmd->un.genreq64.bdl.bdeSize /
8179 sizeof(struct ulp_bde64);
8180 /* The addrHigh and addrLow fields within the IOCB
8181 * have not been byteswapped yet so there is no
8182 * need to swap them back.
8183 */
James Smart1b511972011-12-13 13:23:09 -05008184 if (piocbq->context3)
8185 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8186 else
8187 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008188
James Smart1b511972011-12-13 13:23:09 -05008189 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008190 if (!bpl)
8191 return xritag;
8192
8193 for (i = 0; i < numBdes; i++) {
8194 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008195 sgl->addr_hi = bpl->addrHigh;
8196 sgl->addr_lo = bpl->addrLow;
8197
James Smart05580562011-05-24 11:40:48 -04008198 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008199 if ((i+1) == numBdes)
8200 bf_set(lpfc_sli4_sge_last, sgl, 1);
8201 else
8202 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008203 /* swap the size field back to the cpu so we
8204 * can assign it to the sgl.
8205 */
8206 bde.tus.w = le32_to_cpu(bpl->tus.w);
8207 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008208 /* The offsets in the sgl need to be accumulated
8209 * separately for the request and reply lists.
8210 * The request is always first, the reply follows.
8211 */
8212 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8213 /* add up the reply sg entries */
8214 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8215 inbound++;
8216 /* first inbound? reset the offset */
8217 if (inbound == 1)
8218 offset = 0;
8219 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008220 bf_set(lpfc_sli4_sge_type, sgl,
8221 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008222 offset += bde.tus.f.bdeSize;
8223 }
James Smart546fc852011-03-11 16:06:29 -05008224 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008225 bpl++;
8226 sgl++;
8227 }
8228 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8229 /* The addrHigh and addrLow fields of the BDE have not
8230 * been byteswapped yet so they need to be swapped
8231 * before putting them in the sgl.
8232 */
8233 sgl->addr_hi =
8234 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8235 sgl->addr_lo =
8236 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008237 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008238 bf_set(lpfc_sli4_sge_last, sgl, 1);
8239 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008240 sgl->sge_len =
8241 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008242 }
8243 return sglq->sli4_xritag;
8244}
8245
8246/**
James Smart4f774512009-05-22 14:52:35 -04008247 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8248 * @phba: Pointer to HBA context object.
8249 * @piocb: Pointer to command iocb.
8250 * @wqe: Pointer to the work queue entry.
8251 *
8252 * This routine converts the iocb command to its Work Queue Entry
8253 * equivalent. The wqe pointer should not have any fields set when
8254 * this routine is called because it will memcpy over them.
8255 * This routine does not set the CQ_ID or the WQEC bits in the
8256 * wqe.
8257 *
8258 * Returns: 0 = Success, IOCB_ERROR = Failure.
8259 **/
8260static int
8261lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8262 union lpfc_wqe *wqe)
8263{
James Smart5ffc2662009-11-18 15:39:44 -05008264 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008265 uint8_t ct = 0;
8266 uint32_t fip;
8267 uint32_t abort_tag;
8268 uint8_t command_type = ELS_COMMAND_NON_FIP;
8269 uint8_t cmnd;
8270 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008271 uint16_t abrt_iotag;
8272 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008273 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008274 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008275 int numBdes, i;
8276 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008277 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008278 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008279 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008280
James Smart45ed1192009-10-02 15:17:02 -04008281 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008282 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008283 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008284 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008285 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008286 command_type = ELS_COMMAND_FIP;
8287 else
8288 command_type = ELS_COMMAND_NON_FIP;
8289
James Smartb5c53952016-03-31 14:12:30 -07008290 if (phba->fcp_embed_io)
8291 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008292 /* Some of the fields are in the right position already */
8293 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartf0d9bcc2010-10-22 11:07:09 -04008294 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart28d7f3d2014-05-21 08:05:28 -04008295 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07008296
8297 abort_tag = (uint32_t) iocbq->iotag;
8298 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008299 /* words0-2 bpl convert bde */
8300 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008301 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8302 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008303 bpl = (struct ulp_bde64 *)
8304 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8305 if (!bpl)
8306 return IOCB_ERROR;
8307
8308 /* Should already be byte swapped. */
8309 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8310 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8311 /* swap the size field back to the cpu so we
8312 * can assign it to the sgl.
8313 */
8314 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008315 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8316 total_len = 0;
8317 for (i = 0; i < numBdes; i++) {
8318 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8319 total_len += bde.tus.f.bdeSize;
8320 }
James Smart4f774512009-05-22 14:52:35 -04008321 } else
James Smart5ffc2662009-11-18 15:39:44 -05008322 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008323
8324 iocbq->iocb.ulpIoTag = iocbq->iotag;
8325 cmnd = iocbq->iocb.ulpCommand;
8326
8327 switch (iocbq->iocb.ulpCommand) {
8328 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008329 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8330 ndlp = iocbq->context_un.ndlp;
8331 else
8332 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008333 if (!iocbq->iocb.ulpLe) {
8334 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8335 "2007 Only Limited Edition cmd Format"
8336 " supported 0x%x\n",
8337 iocbq->iocb.ulpCommand);
8338 return IOCB_ERROR;
8339 }
James Smartff78d8f2011-12-13 13:21:35 -05008340
James Smart5ffc2662009-11-18 15:39:44 -05008341 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008342 /* Els_reguest64 has a TMO */
8343 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8344 iocbq->iocb.ulpTimeout);
8345 /* Need a VF for word 4 set the vf bit*/
8346 bf_set(els_req64_vf, &wqe->els_req, 0);
8347 /* And a VFID for word 12 */
8348 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008349 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008350 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8351 iocbq->iocb.ulpContext);
8352 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8353 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008354 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008355 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008356 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8357 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008358 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8359 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008360 if_type = bf_get(lpfc_sli_intf_if_type,
8361 &phba->sli4_hba.sli_intf);
8362 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008363 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008364 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008365 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008366 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008367 *pcmd == ELS_CMD_PLOGI)) {
8368 bf_set(els_req64_sp, &wqe->els_req, 1);
8369 bf_set(els_req64_sid, &wqe->els_req,
8370 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008371 if ((*pcmd == ELS_CMD_FLOGI) &&
8372 !(phba->fc_topology ==
8373 LPFC_TOPOLOGY_LOOP))
8374 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008375 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8376 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008377 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008378 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008379 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8380 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8381 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8382 }
James Smartc8685952009-11-18 15:39:16 -05008383 }
James Smart6d368e52011-05-24 11:44:12 -04008384 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8385 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008386 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8387 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8388 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8389 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8390 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8391 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008392 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008393 break;
James Smart5ffc2662009-11-18 15:39:44 -05008394 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008395 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8396 iocbq->iocb.un.ulpWord[3]);
8397 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008398 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008399 /* The entire sequence is transmitted for this IOCB */
8400 xmit_len = total_len;
8401 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008402 if (phba->link_flag & LS_LOOPBACK_MODE)
8403 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008404 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008405 /* word3 iocb=io_tag32 wqe=reserved */
8406 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008407 /* word4 relative_offset memcpy */
8408 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008409 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8410 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8411 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8412 LPFC_WQE_IOD_WRITE);
8413 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8414 LPFC_WQE_LENLOC_WORD12);
8415 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008416 wqe->xmit_sequence.xmit_len = xmit_len;
8417 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008418 break;
James Smart4f774512009-05-22 14:52:35 -04008419 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008420 /* word3 iocb=iotag32 wqe=seq_payload_len */
8421 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008422 /* word4 iocb=rsvd wqe=rsvd */
8423 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8424 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008425 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008426 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008427 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8428 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8429 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8430 LPFC_WQE_LENLOC_WORD3);
8431 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008432 break;
James Smart4f774512009-05-22 14:52:35 -04008433 case CMD_FCP_IWRITE64_CR:
8434 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008435 /* word3 iocb=iotag wqe=payload_offset_len */
8436 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008437 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8438 xmit_len + sizeof(struct fcp_rsp));
8439 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8440 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008441 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8442 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8443 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8444 iocbq->iocb.ulpFCP2Rcvy);
8445 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8446 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008447 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8448 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8449 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008450 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008451 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008452 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8453 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008454 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8455 if (iocbq->priority) {
8456 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8457 (iocbq->priority << 1));
8458 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008459 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8460 (phba->cfg_XLanePriority << 1));
8461 }
8462 }
James Smartb5c53952016-03-31 14:12:30 -07008463 /* Note, word 10 is already initialized to 0 */
8464
8465 if (phba->fcp_embed_io) {
8466 struct lpfc_scsi_buf *lpfc_cmd;
8467 struct sli4_sge *sgl;
8468 union lpfc_wqe128 *wqe128;
8469 struct fcp_cmnd *fcp_cmnd;
8470 uint32_t *ptr;
8471
8472 /* 128 byte wqe support here */
8473 wqe128 = (union lpfc_wqe128 *)wqe;
8474
8475 lpfc_cmd = iocbq->context1;
8476 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8477 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8478
8479 /* Word 0-2 - FCP_CMND */
8480 wqe128->generic.bde.tus.f.bdeFlags =
8481 BUFF_TYPE_BDE_IMMED;
8482 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8483 wqe128->generic.bde.addrHigh = 0;
8484 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8485
8486 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8487
8488 /* Word 22-29 FCP CMND Payload */
8489 ptr = &wqe128->words[22];
8490 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8491 }
James Smart7851fe22011-07-22 18:36:52 -04008492 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008493 case CMD_FCP_IREAD64_CR:
8494 /* word3 iocb=iotag wqe=payload_offset_len */
8495 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008496 bf_set(payload_offset_len, &wqe->fcp_iread,
8497 xmit_len + sizeof(struct fcp_rsp));
8498 bf_set(cmd_buff_len, &wqe->fcp_iread,
8499 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008500 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8501 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8502 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8503 iocbq->iocb.ulpFCP2Rcvy);
8504 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008505 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008506 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8507 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8508 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008509 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008510 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008511 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8512 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008513 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8514 if (iocbq->priority) {
8515 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8516 (iocbq->priority << 1));
8517 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008518 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8519 (phba->cfg_XLanePriority << 1));
8520 }
8521 }
James Smartb5c53952016-03-31 14:12:30 -07008522 /* Note, word 10 is already initialized to 0 */
8523
8524 if (phba->fcp_embed_io) {
8525 struct lpfc_scsi_buf *lpfc_cmd;
8526 struct sli4_sge *sgl;
8527 union lpfc_wqe128 *wqe128;
8528 struct fcp_cmnd *fcp_cmnd;
8529 uint32_t *ptr;
8530
8531 /* 128 byte wqe support here */
8532 wqe128 = (union lpfc_wqe128 *)wqe;
8533
8534 lpfc_cmd = iocbq->context1;
8535 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8536 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8537
8538 /* Word 0-2 - FCP_CMND */
8539 wqe128->generic.bde.tus.f.bdeFlags =
8540 BUFF_TYPE_BDE_IMMED;
8541 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8542 wqe128->generic.bde.addrHigh = 0;
8543 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8544
8545 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8546
8547 /* Word 22-29 FCP CMND Payload */
8548 ptr = &wqe128->words[22];
8549 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8550 }
James Smart7851fe22011-07-22 18:36:52 -04008551 break;
James Smartf1126682009-06-10 17:22:44 -04008552 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008553 /* word3 iocb=iotag wqe=payload_offset_len */
8554 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8555 bf_set(payload_offset_len, &wqe->fcp_icmd,
8556 xmit_len + sizeof(struct fcp_rsp));
8557 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8558 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008559 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008560 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008561 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008562 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8563 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8564 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8565 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8566 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008567 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8568 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008569 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8570 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008571 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8572 if (iocbq->priority) {
8573 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8574 (iocbq->priority << 1));
8575 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008576 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8577 (phba->cfg_XLanePriority << 1));
8578 }
8579 }
James Smartb5c53952016-03-31 14:12:30 -07008580 /* Note, word 10 is already initialized to 0 */
8581
8582 if (phba->fcp_embed_io) {
8583 struct lpfc_scsi_buf *lpfc_cmd;
8584 struct sli4_sge *sgl;
8585 union lpfc_wqe128 *wqe128;
8586 struct fcp_cmnd *fcp_cmnd;
8587 uint32_t *ptr;
8588
8589 /* 128 byte wqe support here */
8590 wqe128 = (union lpfc_wqe128 *)wqe;
8591
8592 lpfc_cmd = iocbq->context1;
8593 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8594 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8595
8596 /* Word 0-2 - FCP_CMND */
8597 wqe128->generic.bde.tus.f.bdeFlags =
8598 BUFF_TYPE_BDE_IMMED;
8599 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8600 wqe128->generic.bde.addrHigh = 0;
8601 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8602
8603 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8604
8605 /* Word 22-29 FCP CMND Payload */
8606 ptr = &wqe128->words[22];
8607 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8608 }
James Smart7851fe22011-07-22 18:36:52 -04008609 break;
James Smart4f774512009-05-22 14:52:35 -04008610 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008611 /* For this command calculate the xmit length of the
8612 * request bde.
8613 */
8614 xmit_len = 0;
8615 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8616 sizeof(struct ulp_bde64);
8617 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008618 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008619 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8620 break;
James Smart63e801c2010-11-20 23:14:19 -05008621 xmit_len += bde.tus.f.bdeSize;
8622 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008623 /* word3 iocb=IO_TAG wqe=request_payload_len */
8624 wqe->gen_req.request_payload_len = xmit_len;
8625 /* word4 iocb=parameter wqe=relative_offset memcpy */
8626 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008627 /* word6 context tag copied in memcpy */
8628 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8629 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8630 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8631 "2015 Invalid CT %x command 0x%x\n",
8632 ct, iocbq->iocb.ulpCommand);
8633 return IOCB_ERROR;
8634 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008635 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8636 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8637 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8638 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8639 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8640 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8641 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8642 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008643 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008644 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008645 break;
James Smart4f774512009-05-22 14:52:35 -04008646 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008647 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008648 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008649 /* word3 iocb=iotag32 wqe=response_payload_len */
8650 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008651 /* word4 */
8652 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008653 /* word5 iocb=rsvd wge=did */
8654 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008655 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8656
8657 if_type = bf_get(lpfc_sli_intf_if_type,
8658 &phba->sli4_hba.sli_intf);
8659 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8660 if (iocbq->vport->fc_flag & FC_PT2PT) {
8661 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8662 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8663 iocbq->vport->fc_myDID);
8664 if (iocbq->vport->fc_myDID == Fabric_DID) {
8665 bf_set(wqe_els_did,
8666 &wqe->xmit_els_rsp.wqe_dest, 0);
8667 }
8668 }
8669 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008670 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8671 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8672 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8673 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008674 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008675 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008676 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008677 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008678 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8679 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8680 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8681 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8682 LPFC_WQE_LENLOC_WORD3);
8683 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008684 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8685 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008686 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8687 iocbq->context2)->virt);
8688 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008689 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8690 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008691 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008692 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8693 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008694 phba->vpi_ids[phba->pport->vpi]);
8695 }
James Smart4f774512009-05-22 14:52:35 -04008696 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008697 break;
James Smart4f774512009-05-22 14:52:35 -04008698 case CMD_CLOSE_XRI_CN:
8699 case CMD_ABORT_XRI_CN:
8700 case CMD_ABORT_XRI_CX:
8701 /* words 0-2 memcpy should be 0 rserved */
8702 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008703 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8704 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8705 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8706 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8707 } else
8708 fip = 0;
8709
8710 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008711 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008712 * The link is down, or the command was ELS_FIP
8713 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008714 * on the wire.
8715 */
8716 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8717 else
8718 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8719 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008720 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8721 wqe->abort_cmd.rsrvd5 = 0;
8722 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008723 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8724 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008725 /*
8726 * The abort handler will send us CMD_ABORT_XRI_CN or
8727 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8728 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008729 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8730 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8731 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8732 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008733 cmnd = CMD_ABORT_XRI_CX;
8734 command_type = OTHER_COMMAND;
8735 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008736 break;
James Smart6669f9b2009-10-02 15:16:45 -04008737 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008738 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008739 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008740 * we re-construct this WQE here based on information in
8741 * iocbq from scratch.
8742 */
8743 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008744 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008745 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008746 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8747 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008748 LPFC_ABTS_UNSOL_INT) {
8749 /* ABTS sent by initiator to CT exchange, the
8750 * RX_ID field will be filled with the newly
8751 * allocated responder XRI.
8752 */
8753 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8754 iocbq->sli4_xritag);
8755 } else {
8756 /* ABTS sent by responder to CT exchange, the
8757 * RX_ID field will be filled with the responder
8758 * RX_ID from ABTS.
8759 */
8760 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008761 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008762 }
James Smart6669f9b2009-10-02 15:16:45 -04008763 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8764 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008765
8766 /* Use CT=VPI */
8767 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8768 ndlp->nlp_DID);
8769 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8770 iocbq->iocb.ulpContext);
8771 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008772 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008773 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008774 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8775 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8776 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008777 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8778 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008779 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8780 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8781 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8782 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8783 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8784 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8785 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8786 }
8787
James Smart7851fe22011-07-22 18:36:52 -04008788 break;
James Smart4f774512009-05-22 14:52:35 -04008789 case CMD_XRI_ABORTED_CX:
8790 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008791 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8792 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8793 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8794 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8795 default:
8796 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8797 "2014 Invalid command 0x%x\n",
8798 iocbq->iocb.ulpCommand);
8799 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008800 break;
James Smart4f774512009-05-22 14:52:35 -04008801 }
James Smart6d368e52011-05-24 11:44:12 -04008802
James Smart8012cc32012-10-31 14:44:49 -04008803 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8804 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8805 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8806 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8807 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8808 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8809 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8810 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008811 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8812 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8813 wqe->generic.wqe_com.abort_tag = abort_tag;
8814 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8815 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8816 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8817 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008818 return 0;
8819}
8820
8821/**
8822 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8823 * @phba: Pointer to HBA context object.
8824 * @ring_number: SLI ring number to issue iocb on.
8825 * @piocb: Pointer to command iocb.
8826 * @flag: Flag indicating if this command can be put into txq.
8827 *
8828 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8829 * an iocb command to an HBA with SLI-4 interface spec.
8830 *
8831 * This function is called with hbalock held. The function will return success
8832 * after it successfully submit the iocb to firmware or after adding to the
8833 * txq.
8834 **/
8835static int
8836__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8837 struct lpfc_iocbq *piocb, uint32_t flag)
8838{
8839 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07008840 union lpfc_wqe *wqe;
8841 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05008842 struct lpfc_queue *wq;
James Smart4f774512009-05-22 14:52:35 -04008843 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008844
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008845 lockdep_assert_held(&phba->hbalock);
8846
James Smartb5c53952016-03-31 14:12:30 -07008847 /*
8848 * The WQE can be either 64 or 128 bytes,
8849 * so allocate space on the stack assuming the largest.
8850 */
8851 wqe = (union lpfc_wqe *)&wqe128;
8852
James Smart4f774512009-05-22 14:52:35 -04008853 if (piocb->sli4_xritag == NO_XRI) {
8854 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008855 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008856 sglq = NULL;
8857 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05008858 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04008859 if (!(flag & SLI_IOCB_RET_IOCB)) {
8860 __lpfc_sli_ringtx_put(phba,
8861 pring, piocb);
8862 return IOCB_SUCCESS;
8863 } else {
8864 return IOCB_BUSY;
8865 }
8866 } else {
James Smart6d368e52011-05-24 11:44:12 -04008867 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008868 if (!sglq) {
8869 if (!(flag & SLI_IOCB_RET_IOCB)) {
8870 __lpfc_sli_ringtx_put(phba,
8871 pring,
8872 piocb);
8873 return IOCB_SUCCESS;
8874 } else
8875 return IOCB_BUSY;
8876 }
8877 }
James Smart4f774512009-05-22 14:52:35 -04008878 }
8879 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008880 /* These IO's already have an XRI and a mapped sgl. */
8881 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008882 } else {
James Smart6d368e52011-05-24 11:44:12 -04008883 /*
8884 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008885 * sglq is on the active list
8886 */
James Smartedccdc12013-01-03 15:43:45 -05008887 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04008888 if (!sglq)
8889 return IOCB_ERROR;
8890 }
8891
8892 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008893 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008894 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008895 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008896 return IOCB_ERROR;
8897 }
8898
James Smartb5c53952016-03-31 14:12:30 -07008899 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04008900 return IOCB_ERROR;
8901
James Smart341af102010-01-26 23:07:37 -05008902 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
James Smart1ba981f2014-02-20 09:56:45 -05008903 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartf38fa0b2014-04-04 13:52:21 -04008904 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
James Smart1ba981f2014-02-20 09:56:45 -05008905 wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
8906 } else {
8907 wq = phba->sli4_hba.oas_wq;
8908 }
James Smartb5c53952016-03-31 14:12:30 -07008909 if (lpfc_sli4_wq_put(wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008910 return IOCB_ERROR;
8911 } else {
James Smartea714f32013-04-17 20:18:47 -04008912 if (unlikely(!phba->sli4_hba.els_wq))
8913 return IOCB_ERROR;
James Smartb5c53952016-03-31 14:12:30 -07008914 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008915 return IOCB_ERROR;
8916 }
8917 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8918
8919 return 0;
8920}
8921
8922/**
James Smart3772a992009-05-22 14:50:54 -04008923 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8924 *
8925 * This routine wraps the actual lockless version for issusing IOCB function
8926 * pointer from the lpfc_hba struct.
8927 *
8928 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07008929 * IOCB_ERROR - Error
8930 * IOCB_SUCCESS - Success
8931 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04008932 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008933int
James Smart3772a992009-05-22 14:50:54 -04008934__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8935 struct lpfc_iocbq *piocb, uint32_t flag)
8936{
8937 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8938}
8939
8940/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008941 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008942 * @phba: The hba struct for which this call is being executed.
8943 * @dev_grp: The HBA PCI-Device group number.
8944 *
8945 * This routine sets up the SLI interface API function jump table in @phba
8946 * struct.
8947 * Returns: 0 - success, -ENODEV - failure.
8948 **/
8949int
8950lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8951{
8952
8953 switch (dev_grp) {
8954 case LPFC_PCI_DEV_LP:
8955 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8956 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8957 break;
James Smart4f774512009-05-22 14:52:35 -04008958 case LPFC_PCI_DEV_OC:
8959 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8960 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8961 break;
James Smart3772a992009-05-22 14:50:54 -04008962 default:
8963 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8964 "1419 Invalid HBA PCI-device group: 0x%x\n",
8965 dev_grp);
8966 return -ENODEV;
8967 break;
8968 }
8969 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8970 return 0;
8971}
James Smart92d7f7b2007-06-17 19:56:38 -05008972
James Smarta1efe162015-05-21 13:55:20 -04008973/**
8974 * lpfc_sli_calc_ring - Calculates which ring to use
8975 * @phba: Pointer to HBA context object.
8976 * @ring_number: Initial ring
8977 * @piocb: Pointer to command iocb.
8978 *
8979 * For SLI4, FCP IO can deferred to one fo many WQs, based on
8980 * fcp_wqidx, thus we need to calculate the corresponding ring.
8981 * Since ABORTS must go on the same WQ of the command they are
8982 * aborting, we use command's fcp_wqidx.
8983 */
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08008984static int
James Smart9bd2bff52014-09-03 12:57:30 -04008985lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
8986 struct lpfc_iocbq *piocb)
8987{
James Smart8b0dff12015-05-22 10:42:38 -04008988 if (phba->sli_rev < LPFC_SLI_REV4)
8989 return ring_number;
8990
8991 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8992 if (!(phba->cfg_fof) ||
8993 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8994 if (unlikely(!phba->sli4_hba.fcp_wq))
8995 return LPFC_HBA_ERROR;
8996 /*
8997 * for abort iocb fcp_wqidx should already
8998 * be setup based on what work queue we used.
8999 */
9000 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
9001 piocb->fcp_wqidx =
9002 lpfc_sli4_scmd_to_wqidx_distr(phba,
9003 piocb->context1);
9004 ring_number = MAX_SLI3_CONFIGURED_RINGS +
9005 piocb->fcp_wqidx;
9006 } else {
9007 if (unlikely(!phba->sli4_hba.oas_wq))
9008 return LPFC_HBA_ERROR;
9009 piocb->fcp_wqidx = 0;
9010 ring_number = LPFC_FCP_OAS_RING;
James Smart9bd2bff52014-09-03 12:57:30 -04009011 }
9012 }
9013 return ring_number;
9014}
9015
James Smarte59058c2008-08-24 21:49:00 -04009016/**
James Smart3621a712009-04-06 18:47:14 -04009017 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009018 * @phba: Pointer to HBA context object.
9019 * @pring: Pointer to driver SLI ring object.
9020 * @piocb: Pointer to command iocb.
9021 * @flag: Flag indicating if this command can be put into txq.
9022 *
9023 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9024 * function. This function gets the hbalock and calls
9025 * __lpfc_sli_issue_iocb function and will return the error returned
9026 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9027 * functions which do not hold hbalock.
9028 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009029int
James Smart3772a992009-05-22 14:50:54 -04009030lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009031 struct lpfc_iocbq *piocb, uint32_t flag)
9032{
James Smartba20c852012-08-03 12:36:52 -04009033 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009034 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009035 struct lpfc_queue *fpeq;
9036 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009037 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009038 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009039
James Smart7e56aa22012-08-03 12:35:34 -04009040 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -04009041 ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
9042 if (unlikely(ring_number == LPFC_HBA_ERROR))
9043 return IOCB_ERROR;
9044 idx = piocb->fcp_wqidx;
James Smartba20c852012-08-03 12:36:52 -04009045
James Smart9bd2bff52014-09-03 12:57:30 -04009046 pring = &phba->sli.ring[ring_number];
9047 spin_lock_irqsave(&pring->ring_lock, iflags);
9048 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9049 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009050
James Smart9bd2bff52014-09-03 12:57:30 -04009051 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
9052 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009053
James Smart9bd2bff52014-09-03 12:57:30 -04009054 if (atomic_dec_and_test(&fcp_eq_hdl->
9055 fcp_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009056
James Smart9bd2bff52014-09-03 12:57:30 -04009057 /* Get associated EQ with this index */
9058 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009059
James Smart9bd2bff52014-09-03 12:57:30 -04009060 /* Turn off interrupts from this EQ */
9061 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009062
James Smart9bd2bff52014-09-03 12:57:30 -04009063 /*
9064 * Process all the events on FCP EQ
9065 */
9066 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9067 lpfc_sli4_hba_handle_eqe(phba,
9068 eqe, idx);
9069 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009070 }
James Smartba20c852012-08-03 12:36:52 -04009071
James Smart9bd2bff52014-09-03 12:57:30 -04009072 /* Always clear and re-arm the EQ */
9073 lpfc_sli4_eq_release(fpeq,
9074 LPFC_QUEUE_REARM);
9075 }
9076 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009077 }
James Smart7e56aa22012-08-03 12:35:34 -04009078 } else {
9079 /* For now, SLI2/3 will still use hbalock */
9080 spin_lock_irqsave(&phba->hbalock, iflags);
9081 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9082 spin_unlock_irqrestore(&phba->hbalock, iflags);
9083 }
James Smart92d7f7b2007-06-17 19:56:38 -05009084 return rc;
9085}
9086
James Smarte59058c2008-08-24 21:49:00 -04009087/**
James Smart3621a712009-04-06 18:47:14 -04009088 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009089 * @phba: Pointer to HBA context object.
9090 *
9091 * This function is called while driver attaches with the
9092 * HBA to setup the extra ring. The extra ring is used
9093 * only when driver needs to support target mode functionality
9094 * or IP over FC functionalities.
9095 *
9096 * This function is called with no lock held.
9097 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009098static int
9099lpfc_extra_ring_setup( struct lpfc_hba *phba)
9100{
9101 struct lpfc_sli *psli;
9102 struct lpfc_sli_ring *pring;
9103
9104 psli = &phba->sli;
9105
9106 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009107
9108 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009109 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04009110 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9111 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9112 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9113 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009114
James Smarta4bc3372006-12-02 13:34:16 -05009115 /* and give them to the extra ring */
9116 pring = &psli->ring[psli->extra_ring];
9117
James Smart7e56aa22012-08-03 12:35:34 -04009118 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9119 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9120 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9121 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009122
9123 /* Setup default profile for this ring */
9124 pring->iotag_max = 4096;
9125 pring->num_mask = 1;
9126 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009127 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9128 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009129 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9130 return 0;
9131}
9132
James Smartcb69f7d2011-12-13 13:21:57 -05009133/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9134 * @phba: Pointer to HBA context object.
9135 * @iocbq: Pointer to iocb object.
9136 *
9137 * The async_event handler calls this routine when it receives
9138 * an ASYNC_STATUS_CN event from the port. The port generates
9139 * this event when an Abort Sequence request to an rport fails
9140 * twice in succession. The abort could be originated by the
9141 * driver or by the port. The ABTS could have been for an ELS
9142 * or FCP IO. The port only generates this event when an ABTS
9143 * fails to complete after one retry.
9144 */
9145static void
9146lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9147 struct lpfc_iocbq *iocbq)
9148{
9149 struct lpfc_nodelist *ndlp = NULL;
9150 uint16_t rpi = 0, vpi = 0;
9151 struct lpfc_vport *vport = NULL;
9152
9153 /* The rpi in the ulpContext is vport-sensitive. */
9154 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9155 rpi = iocbq->iocb.ulpContext;
9156
9157 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9158 "3092 Port generated ABTS async event "
9159 "on vpi %d rpi %d status 0x%x\n",
9160 vpi, rpi, iocbq->iocb.ulpStatus);
9161
9162 vport = lpfc_find_vport_by_vpid(phba, vpi);
9163 if (!vport)
9164 goto err_exit;
9165 ndlp = lpfc_findnode_rpi(vport, rpi);
9166 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9167 goto err_exit;
9168
9169 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9170 lpfc_sli_abts_recover_port(vport, ndlp);
9171 return;
9172
9173 err_exit:
9174 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9175 "3095 Event Context not found, no "
9176 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9177 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9178 vpi, rpi);
9179}
9180
9181/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9182 * @phba: pointer to HBA context object.
9183 * @ndlp: nodelist pointer for the impacted rport.
9184 * @axri: pointer to the wcqe containing the failed exchange.
9185 *
9186 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9187 * port. The port generates this event when an abort exchange request to an
9188 * rport fails twice in succession with no reply. The abort could be originated
9189 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9190 */
9191void
9192lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9193 struct lpfc_nodelist *ndlp,
9194 struct sli4_wcqe_xri_aborted *axri)
9195{
9196 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009197 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009198
James Smart6b5151f2012-01-18 16:24:06 -05009199 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009200 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9201 "3115 Node Context not found, driver "
9202 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009203 return;
9204 }
9205
James Smartcb69f7d2011-12-13 13:21:57 -05009206 vport = ndlp->vport;
9207 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9208 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009209 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009210 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009211 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009212 bf_get(lpfc_wcqe_xa_status, axri),
9213 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009214
James Smart5c1db2a2012-03-01 22:34:36 -05009215 /*
9216 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9217 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9218 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9219 */
James Smarte3d2b802012-08-14 14:25:43 -04009220 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009221 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9222 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009223 lpfc_sli_abts_recover_port(vport, ndlp);
9224}
9225
James Smarte59058c2008-08-24 21:49:00 -04009226/**
James Smart3621a712009-04-06 18:47:14 -04009227 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009228 * @phba: Pointer to HBA context object.
9229 * @pring: Pointer to driver SLI ring object.
9230 * @iocbq: Pointer to iocb object.
9231 *
9232 * This function is called by the slow ring event handler
9233 * function when there is an ASYNC event iocb in the ring.
9234 * This function is called with no lock held.
9235 * Currently this function handles only temperature related
9236 * ASYNC events. The function decodes the temperature sensor
9237 * event message and posts events for the management applications.
9238 **/
James Smart98c9ea52007-10-27 13:37:33 -04009239static void
James Smart57127f12007-10-27 13:37:05 -04009240lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9241 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9242{
9243 IOCB_t *icmd;
9244 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009245 struct temp_event temp_event_data;
9246 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009247 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009248
9249 icmd = &iocbq->iocb;
9250 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009251
James Smartcb69f7d2011-12-13 13:21:57 -05009252 switch (evt_code) {
9253 case ASYNC_TEMP_WARN:
9254 case ASYNC_TEMP_SAFE:
9255 temp_event_data.data = (uint32_t) icmd->ulpContext;
9256 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9257 if (evt_code == ASYNC_TEMP_WARN) {
9258 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9259 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9260 "0347 Adapter is very hot, please take "
9261 "corrective action. temperature : %d Celsius\n",
9262 (uint32_t) icmd->ulpContext);
9263 } else {
9264 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9265 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9266 "0340 Adapter temperature is OK now. "
9267 "temperature : %d Celsius\n",
9268 (uint32_t) icmd->ulpContext);
9269 }
9270
9271 /* Send temperature change event to applications */
9272 shost = lpfc_shost_from_vport(phba->pport);
9273 fc_host_post_vendor_event(shost, fc_get_event_number(),
9274 sizeof(temp_event_data), (char *) &temp_event_data,
9275 LPFC_NL_VENDOR_ID);
9276 break;
9277 case ASYNC_STATUS_CN:
9278 lpfc_sli_abts_err_handler(phba, iocbq);
9279 break;
9280 default:
James Smarta257bf92009-04-06 18:48:10 -04009281 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009282 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009283 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009284 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009285 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9286 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9287 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9288 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009289 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009290 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9291 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9292 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9293 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9294
James Smartcb69f7d2011-12-13 13:21:57 -05009295 break;
James Smart57127f12007-10-27 13:37:05 -04009296 }
James Smart57127f12007-10-27 13:37:05 -04009297}
9298
9299
James Smarte59058c2008-08-24 21:49:00 -04009300/**
James Smart3621a712009-04-06 18:47:14 -04009301 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009302 * @phba: Pointer to HBA context object.
9303 *
9304 * lpfc_sli_setup sets up rings of the SLI interface with
9305 * number of iocbs per ring and iotags. This function is
9306 * called while driver attach to the HBA and before the
9307 * interrupts are enabled. So there is no need for locking.
9308 *
9309 * This function always returns 0.
9310 **/
dea31012005-04-17 16:05:31 -05009311int
9312lpfc_sli_setup(struct lpfc_hba *phba)
9313{
James Smarted957682007-06-17 19:56:37 -05009314 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009315 struct lpfc_sli *psli = &phba->sli;
9316 struct lpfc_sli_ring *pring;
9317
James Smart2a76a282012-08-03 12:35:54 -04009318 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9319 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04009320 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05009321 psli->sli_flag = 0;
9322 psli->fcp_ring = LPFC_FCP_RING;
9323 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05009324 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05009325
James Bottomley604a3e32005-10-29 10:28:33 -05009326 psli->iocbq_lookup = NULL;
9327 psli->iocbq_lookup_len = 0;
9328 psli->last_iotag = 0;
9329
dea31012005-04-17 16:05:31 -05009330 for (i = 0; i < psli->num_rings; i++) {
9331 pring = &psli->ring[i];
9332 switch (i) {
9333 case LPFC_FCP_RING: /* ring 0 - FCP */
9334 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009335 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9336 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9337 pring->sli.sli3.numCiocb +=
9338 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9339 pring->sli.sli3.numRiocb +=
9340 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9341 pring->sli.sli3.numCiocb +=
9342 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9343 pring->sli.sli3.numRiocb +=
9344 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9345 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009346 SLI3_IOCB_CMD_SIZE :
9347 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009348 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009349 SLI3_IOCB_RSP_SIZE :
9350 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009351 pring->iotag_ctr = 0;
9352 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009353 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009354 pring->fast_iotag = pring->iotag_max;
9355 pring->num_mask = 0;
9356 break;
James Smarta4bc3372006-12-02 13:34:16 -05009357 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009358 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009359 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9360 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9361 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009362 SLI3_IOCB_CMD_SIZE :
9363 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009364 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009365 SLI3_IOCB_RSP_SIZE :
9366 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009367 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009368 pring->num_mask = 0;
9369 break;
9370 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9371 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009372 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9373 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9374 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009375 SLI3_IOCB_CMD_SIZE :
9376 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009377 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009378 SLI3_IOCB_RSP_SIZE :
9379 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009380 pring->fast_iotag = 0;
9381 pring->iotag_ctr = 0;
9382 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009383 pring->lpfc_sli_rcv_async_status =
9384 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009385 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009386 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009387 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9388 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009389 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009390 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009391 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009392 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9393 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009394 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009395 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009396 pring->prt[2].profile = 0; /* Mask 2 */
9397 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009398 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009399 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009400 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009401 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009402 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009403 pring->prt[3].profile = 0; /* Mask 3 */
9404 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009405 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009406 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009407 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009408 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009409 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009410 break;
9411 }
James Smart7e56aa22012-08-03 12:35:34 -04009412 totiocbsize += (pring->sli.sli3.numCiocb *
9413 pring->sli.sli3.sizeCiocb) +
9414 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009415 }
James Smarted957682007-06-17 19:56:37 -05009416 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009417 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009418 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9419 "SLI2 SLIM Data: x%x x%lx\n",
9420 phba->brd_no, totiocbsize,
9421 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009422 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009423 if (phba->cfg_multi_ring_support == 2)
9424 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009425
9426 return 0;
9427}
9428
James Smarte59058c2008-08-24 21:49:00 -04009429/**
James Smart3621a712009-04-06 18:47:14 -04009430 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009431 * @phba: Pointer to HBA context object.
9432 *
9433 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9434 * ring. This function also initializes ring indices of each ring.
9435 * This function is called during the initialization of the SLI
9436 * interface of an HBA.
9437 * This function is called with no lock held and always returns
9438 * 1.
9439 **/
dea31012005-04-17 16:05:31 -05009440int
James Smart2e0fef82007-06-17 19:56:36 -05009441lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009442{
9443 struct lpfc_sli *psli;
9444 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009445 int i;
dea31012005-04-17 16:05:31 -05009446
9447 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009448 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009449 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009450 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009451 /* Initialize list headers for txq and txcmplq as double linked lists */
9452 for (i = 0; i < psli->num_rings; i++) {
9453 pring = &psli->ring[i];
9454 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009455 pring->sli.sli3.next_cmdidx = 0;
9456 pring->sli.sli3.local_getidx = 0;
9457 pring->sli.sli3.cmdidx = 0;
James Smart68e814f2014-05-21 08:04:59 -04009458 pring->flag = 0;
dea31012005-04-17 16:05:31 -05009459 INIT_LIST_HEAD(&pring->txq);
9460 INIT_LIST_HEAD(&pring->txcmplq);
9461 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009462 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009463 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04009464 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009465 }
James Smart2e0fef82007-06-17 19:56:36 -05009466 spin_unlock_irq(&phba->hbalock);
9467 return 1;
dea31012005-04-17 16:05:31 -05009468}
9469
James Smarte59058c2008-08-24 21:49:00 -04009470/**
James Smart04c68492009-05-22 14:52:52 -04009471 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9472 * @phba: Pointer to HBA context object.
9473 *
9474 * This routine flushes the mailbox command subsystem. It will unconditionally
9475 * flush all the mailbox commands in the three possible stages in the mailbox
9476 * command sub-system: pending mailbox command queue; the outstanding mailbox
9477 * command; and completed mailbox command queue. It is caller's responsibility
9478 * to make sure that the driver is in the proper state to flush the mailbox
9479 * command sub-system. Namely, the posting of mailbox commands into the
9480 * pending mailbox command queue from the various clients must be stopped;
9481 * either the HBA is in a state that it will never works on the outstanding
9482 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9483 * mailbox command has been completed.
9484 **/
9485static void
9486lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9487{
9488 LIST_HEAD(completions);
9489 struct lpfc_sli *psli = &phba->sli;
9490 LPFC_MBOXQ_t *pmb;
9491 unsigned long iflag;
9492
9493 /* Flush all the mailbox commands in the mbox system */
9494 spin_lock_irqsave(&phba->hbalock, iflag);
9495 /* The pending mailbox command queue */
9496 list_splice_init(&phba->sli.mboxq, &completions);
9497 /* The outstanding active mailbox command */
9498 if (psli->mbox_active) {
9499 list_add_tail(&psli->mbox_active->list, &completions);
9500 psli->mbox_active = NULL;
9501 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9502 }
9503 /* The completed mailbox command queue */
9504 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9505 spin_unlock_irqrestore(&phba->hbalock, iflag);
9506
9507 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9508 while (!list_empty(&completions)) {
9509 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9510 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9511 if (pmb->mbox_cmpl)
9512 pmb->mbox_cmpl(phba, pmb);
9513 }
9514}
9515
9516/**
James Smart3621a712009-04-06 18:47:14 -04009517 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009518 * @vport: Pointer to virtual port object.
9519 *
9520 * lpfc_sli_host_down is called to clean up the resources
9521 * associated with a vport before destroying virtual
9522 * port data structures.
9523 * This function does following operations:
9524 * - Free discovery resources associated with this virtual
9525 * port.
9526 * - Free iocbs associated with this virtual port in
9527 * the txq.
9528 * - Send abort for all iocb commands associated with this
9529 * vport in txcmplq.
9530 *
9531 * This function is called with no lock held and always returns 1.
9532 **/
dea31012005-04-17 16:05:31 -05009533int
James Smart92d7f7b2007-06-17 19:56:38 -05009534lpfc_sli_host_down(struct lpfc_vport *vport)
9535{
James Smart858c9f62007-06-17 19:56:39 -05009536 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009537 struct lpfc_hba *phba = vport->phba;
9538 struct lpfc_sli *psli = &phba->sli;
9539 struct lpfc_sli_ring *pring;
9540 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009541 int i;
9542 unsigned long flags = 0;
9543 uint16_t prev_pring_flag;
9544
9545 lpfc_cleanup_discovery_resources(vport);
9546
9547 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009548 for (i = 0; i < psli->num_rings; i++) {
9549 pring = &psli->ring[i];
9550 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009551 /* Only slow rings */
9552 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009553 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009554 /* Set the lpfc data pending flag */
9555 set_bit(LPFC_DATA_READY, &phba->data_flags);
9556 }
James Smart92d7f7b2007-06-17 19:56:38 -05009557 /*
9558 * Error everything on the txq since these iocbs have not been
9559 * given to the FW yet.
9560 */
James Smart92d7f7b2007-06-17 19:56:38 -05009561 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9562 if (iocb->vport != vport)
9563 continue;
James Smart858c9f62007-06-17 19:56:39 -05009564 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009565 }
9566
9567 /* Next issue ABTS for everything on the txcmplq */
9568 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9569 list) {
9570 if (iocb->vport != vport)
9571 continue;
9572 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9573 }
9574
9575 pring->flag = prev_pring_flag;
9576 }
9577
9578 spin_unlock_irqrestore(&phba->hbalock, flags);
9579
James Smarta257bf92009-04-06 18:48:10 -04009580 /* Cancel all the IOCBs from the completions list */
9581 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9582 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009583 return 1;
9584}
9585
James Smarte59058c2008-08-24 21:49:00 -04009586/**
James Smart3621a712009-04-06 18:47:14 -04009587 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009588 * @phba: Pointer to HBA context object.
9589 *
9590 * This function cleans up all iocb, buffers, mailbox commands
9591 * while shutting down the HBA. This function is called with no
9592 * lock held and always returns 1.
9593 * This function does the following to cleanup driver resources:
9594 * - Free discovery resources for each virtual port
9595 * - Cleanup any pending fabric iocbs
9596 * - Iterate through the iocb txq and free each entry
9597 * in the list.
9598 * - Free up any buffer posted to the HBA
9599 * - Free mailbox commands in the mailbox queue.
9600 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009601int
James Smart2e0fef82007-06-17 19:56:36 -05009602lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009603{
James Smart2534ba72007-04-25 09:52:20 -04009604 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009605 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009606 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009607 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009608 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009609 int i;
9610
9611 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009612 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009613
dea31012005-04-17 16:05:31 -05009614 lpfc_hba_down_prep(phba);
9615
James Smart92d7f7b2007-06-17 19:56:38 -05009616 lpfc_fabric_abort_hba(phba);
9617
James Smart2e0fef82007-06-17 19:56:36 -05009618 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009619 for (i = 0; i < psli->num_rings; i++) {
9620 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009621 /* Only slow rings */
9622 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009623 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009624 /* Set the lpfc data pending flag */
9625 set_bit(LPFC_DATA_READY, &phba->data_flags);
9626 }
dea31012005-04-17 16:05:31 -05009627
9628 /*
9629 * Error everything on the txq since these iocbs have not been
9630 * given to the FW yet.
9631 */
James Smart2534ba72007-04-25 09:52:20 -04009632 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009633 }
James Smart2e0fef82007-06-17 19:56:36 -05009634 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009635
James Smarta257bf92009-04-06 18:48:10 -04009636 /* Cancel all the IOCBs from the completions list */
9637 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9638 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009639
James Smart0ff10d42008-01-11 01:52:36 -05009640 spin_lock_irqsave(&phba->hbalock, flags);
9641 list_splice_init(&phba->elsbuf, &completions);
9642 phba->elsbuf_cnt = 0;
9643 phba->elsbuf_prev_cnt = 0;
9644 spin_unlock_irqrestore(&phba->hbalock, flags);
9645
9646 while (!list_empty(&completions)) {
9647 list_remove_head(&completions, buf_ptr,
9648 struct lpfc_dmabuf, list);
9649 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9650 kfree(buf_ptr);
9651 }
9652
dea31012005-04-17 16:05:31 -05009653 /* Return any active mbox cmds */
9654 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009655
James Smartda0436e2009-05-22 14:51:39 -04009656 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009657 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009658 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009659
James Smartda0436e2009-05-22 14:51:39 -04009660 return 1;
9661}
James Smart92d7f7b2007-06-17 19:56:38 -05009662
James Smartda0436e2009-05-22 14:51:39 -04009663/**
James Smart3621a712009-04-06 18:47:14 -04009664 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009665 * @srcp: Source memory pointer.
9666 * @destp: Destination memory pointer.
9667 * @cnt: Number of words required to be copied.
9668 *
9669 * This function is used for copying data between driver memory
9670 * and the SLI memory. This function also changes the endianness
9671 * of each word if native endianness is different from SLI
9672 * endianness. This function can be called with or without
9673 * lock.
9674 **/
dea31012005-04-17 16:05:31 -05009675void
9676lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9677{
9678 uint32_t *src = srcp;
9679 uint32_t *dest = destp;
9680 uint32_t ldata;
9681 int i;
9682
9683 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9684 ldata = *src;
9685 ldata = le32_to_cpu(ldata);
9686 *dest = ldata;
9687 src++;
9688 dest++;
9689 }
9690}
9691
James Smarte59058c2008-08-24 21:49:00 -04009692
9693/**
James Smarta0c87cb2009-07-19 10:01:10 -04009694 * lpfc_sli_bemem_bcopy - SLI memory copy function
9695 * @srcp: Source memory pointer.
9696 * @destp: Destination memory pointer.
9697 * @cnt: Number of words required to be copied.
9698 *
9699 * This function is used for copying data between a data structure
9700 * with big endian representation to local endianness.
9701 * This function can be called with or without lock.
9702 **/
9703void
9704lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9705{
9706 uint32_t *src = srcp;
9707 uint32_t *dest = destp;
9708 uint32_t ldata;
9709 int i;
9710
9711 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9712 ldata = *src;
9713 ldata = be32_to_cpu(ldata);
9714 *dest = ldata;
9715 src++;
9716 dest++;
9717 }
9718}
9719
9720/**
James Smart3621a712009-04-06 18:47:14 -04009721 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009722 * @phba: Pointer to HBA context object.
9723 * @pring: Pointer to driver SLI ring object.
9724 * @mp: Pointer to driver buffer object.
9725 *
9726 * This function is called with no lock held.
9727 * It always return zero after adding the buffer to the postbufq
9728 * buffer list.
9729 **/
dea31012005-04-17 16:05:31 -05009730int
James Smart2e0fef82007-06-17 19:56:36 -05009731lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9732 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009733{
9734 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9735 later */
James Smart2e0fef82007-06-17 19:56:36 -05009736 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009737 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009738 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009739 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009740 return 0;
9741}
9742
James Smarte59058c2008-08-24 21:49:00 -04009743/**
James Smart3621a712009-04-06 18:47:14 -04009744 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009745 * @phba: Pointer to HBA context object.
9746 *
9747 * When HBQ is enabled, buffers are searched based on tags. This function
9748 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9749 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9750 * does not conflict with tags of buffer posted for unsolicited events.
9751 * The function returns the allocated tag. The function is called with
9752 * no locks held.
9753 **/
James Smart76bb24e2007-10-27 13:38:00 -04009754uint32_t
9755lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9756{
9757 spin_lock_irq(&phba->hbalock);
9758 phba->buffer_tag_count++;
9759 /*
9760 * Always set the QUE_BUFTAG_BIT to distiguish between
9761 * a tag assigned by HBQ.
9762 */
9763 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9764 spin_unlock_irq(&phba->hbalock);
9765 return phba->buffer_tag_count;
9766}
9767
James Smarte59058c2008-08-24 21:49:00 -04009768/**
James Smart3621a712009-04-06 18:47:14 -04009769 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009770 * @phba: Pointer to HBA context object.
9771 * @pring: Pointer to driver SLI ring object.
9772 * @tag: Buffer tag.
9773 *
9774 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9775 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9776 * iocb is posted to the response ring with the tag of the buffer.
9777 * This function searches the pring->postbufq list using the tag
9778 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9779 * iocb. If the buffer is found then lpfc_dmabuf object of the
9780 * buffer is returned to the caller else NULL is returned.
9781 * This function is called with no lock held.
9782 **/
James Smart76bb24e2007-10-27 13:38:00 -04009783struct lpfc_dmabuf *
9784lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9785 uint32_t tag)
9786{
9787 struct lpfc_dmabuf *mp, *next_mp;
9788 struct list_head *slp = &pring->postbufq;
9789
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009790 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009791 spin_lock_irq(&phba->hbalock);
9792 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9793 if (mp->buffer_tag == tag) {
9794 list_del_init(&mp->list);
9795 pring->postbufq_cnt--;
9796 spin_unlock_irq(&phba->hbalock);
9797 return mp;
9798 }
9799 }
9800
9801 spin_unlock_irq(&phba->hbalock);
9802 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009803 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009804 "ring %d Data x%lx x%p x%p x%x\n",
9805 pring->ringno, (unsigned long) tag,
9806 slp->next, slp->prev, pring->postbufq_cnt);
9807
9808 return NULL;
9809}
dea31012005-04-17 16:05:31 -05009810
James Smarte59058c2008-08-24 21:49:00 -04009811/**
James Smart3621a712009-04-06 18:47:14 -04009812 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009813 * @phba: Pointer to HBA context object.
9814 * @pring: Pointer to driver SLI ring object.
9815 * @phys: DMA address of the buffer.
9816 *
9817 * This function searches the buffer list using the dma_address
9818 * of unsolicited event to find the driver's lpfc_dmabuf object
9819 * corresponding to the dma_address. The function returns the
9820 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9821 * This function is called by the ct and els unsolicited event
9822 * handlers to get the buffer associated with the unsolicited
9823 * event.
9824 *
9825 * This function is called with no lock held.
9826 **/
dea31012005-04-17 16:05:31 -05009827struct lpfc_dmabuf *
9828lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9829 dma_addr_t phys)
9830{
9831 struct lpfc_dmabuf *mp, *next_mp;
9832 struct list_head *slp = &pring->postbufq;
9833
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009834 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009835 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009836 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9837 if (mp->phys == phys) {
9838 list_del_init(&mp->list);
9839 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009840 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009841 return mp;
9842 }
9843 }
9844
James Smart2e0fef82007-06-17 19:56:36 -05009845 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009846 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009847 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009848 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009849 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009850 slp->next, slp->prev, pring->postbufq_cnt);
9851 return NULL;
9852}
9853
James Smarte59058c2008-08-24 21:49:00 -04009854/**
James Smart3621a712009-04-06 18:47:14 -04009855 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009856 * @phba: Pointer to HBA context object.
9857 * @cmdiocb: Pointer to driver command iocb object.
9858 * @rspiocb: Pointer to driver response iocb object.
9859 *
9860 * This function is the completion handler for the abort iocbs for
9861 * ELS commands. This function is called from the ELS ring event
9862 * handler with no lock held. This function frees memory resources
9863 * associated with the abort iocb.
9864 **/
dea31012005-04-17 16:05:31 -05009865static void
James Smart2e0fef82007-06-17 19:56:36 -05009866lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9867 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009868{
James Smart2e0fef82007-06-17 19:56:36 -05009869 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009870 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009871 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009872
9873 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009874
9875 /*
9876 * Assume that the port already completed and returned, or
9877 * will return the iocb. Just Log the message.
9878 */
James Smart2680eea2007-04-25 09:52:55 -04009879 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9880 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9881
James Smart2e0fef82007-06-17 19:56:36 -05009882 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009883 if (phba->sli_rev < LPFC_SLI_REV4) {
9884 if (abort_iotag != 0 &&
9885 abort_iotag <= phba->sli.last_iotag)
9886 abort_iocb =
9887 phba->sli.iocbq_lookup[abort_iotag];
9888 } else
9889 /* For sli4 the abort_tag is the XRI,
9890 * so the abort routine puts the iotag of the iocb
9891 * being aborted in the context field of the abort
9892 * IOCB.
9893 */
9894 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009895
James Smart2a9bf3d2010-06-07 15:24:45 -04009896 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9897 "0327 Cannot abort els iocb %p "
9898 "with tag %x context %x, abort status %x, "
9899 "abort code %x\n",
9900 abort_iocb, abort_iotag, abort_context,
9901 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009902
James Smartff78d8f2011-12-13 13:21:35 -05009903 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009904 }
James Bottomley604a3e32005-10-29 10:28:33 -05009905 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009906 return;
9907}
9908
James Smarte59058c2008-08-24 21:49:00 -04009909/**
James Smart3621a712009-04-06 18:47:14 -04009910 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009911 * @phba: Pointer to HBA context object.
9912 * @cmdiocb: Pointer to driver command iocb object.
9913 * @rspiocb: Pointer to driver response iocb object.
9914 *
9915 * The function is called from SLI ring event handler with no
9916 * lock held. This function is the completion handler for ELS commands
9917 * which are aborted. The function frees memory resources used for
9918 * the aborted ELS commands.
9919 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009920static void
9921lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9922 struct lpfc_iocbq *rspiocb)
9923{
9924 IOCB_t *irsp = &rspiocb->iocb;
9925
9926 /* ELS cmd tag <ulpIoTag> completes */
9927 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009928 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009929 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009930 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009931 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009932 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9933 lpfc_ct_free_iocb(phba, cmdiocb);
9934 else
9935 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009936 return;
9937}
9938
James Smarte59058c2008-08-24 21:49:00 -04009939/**
James Smart5af5eee2010-10-22 11:06:38 -04009940 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009941 * @phba: Pointer to HBA context object.
9942 * @pring: Pointer to driver SLI ring object.
9943 * @cmdiocb: Pointer to driver command iocb object.
9944 *
James Smart5af5eee2010-10-22 11:06:38 -04009945 * This function issues an abort iocb for the provided command iocb down to
9946 * the port. Other than the case the outstanding command iocb is an abort
9947 * request, this function issues abort out unconditionally. This function is
9948 * called with hbalock held. The function returns 0 when it fails due to
9949 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009950 **/
James Smart5af5eee2010-10-22 11:06:38 -04009951static int
9952lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009953 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009954{
James Smart2e0fef82007-06-17 19:56:36 -05009955 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009956 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009957 IOCB_t *icmd = NULL;
9958 IOCB_t *iabt = NULL;
James Smart9bd2bff52014-09-03 12:57:30 -04009959 int ring_number;
James Smart5af5eee2010-10-22 11:06:38 -04009960 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009961 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009962
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009963 lockdep_assert_held(&phba->hbalock);
9964
James Smart92d7f7b2007-06-17 19:56:38 -05009965 /*
9966 * There are certain command types we don't want to abort. And we
9967 * don't want to abort commands that are already in the process of
9968 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009969 */
9970 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009971 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009972 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9973 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009974 return 0;
9975
dea31012005-04-17 16:05:31 -05009976 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009977 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009978 if (abtsiocbp == NULL)
9979 return 0;
dea31012005-04-17 16:05:31 -05009980
James Smart07951072007-04-25 09:51:38 -04009981 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009982 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009983 */
9984 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9985
dea31012005-04-17 16:05:31 -05009986 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04009987 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9988 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -04009989 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04009990 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -04009991 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9992 }
James Smartda0436e2009-05-22 14:51:39 -04009993 else
9994 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05009995 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04009996 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05009997
James Smart5ffc2662009-11-18 15:39:44 -05009998 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9999 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -050010000 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10001 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010002 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10003 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010004
James Smart2e0fef82007-06-17 19:56:36 -050010005 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010006 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10007 else
10008 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10009
10010 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart235d82c2016-12-19 15:07:23 -080010011 abtsiocbp->vport = vport;
James Smart5b8bd0c2007-04-25 09:52:49 -040010012
James Smarte8b62012007-08-02 11:10:09 -040010013 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10014 "0339 Abort xri x%x, original iotag x%x, "
10015 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010016 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010017 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010018 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010019
10020 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -040010021 ring_number =
10022 lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
10023 if (unlikely(ring_number == LPFC_HBA_ERROR))
10024 return 0;
10025 pring = &phba->sli.ring[ring_number];
James Smart7e56aa22012-08-03 12:35:34 -040010026 /* Note: both hbalock and ring_lock need to be set here */
10027 spin_lock_irqsave(&pring->ring_lock, iflags);
10028 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10029 abtsiocbp, 0);
10030 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10031 } else {
10032 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10033 abtsiocbp, 0);
10034 }
James Smart07951072007-04-25 09:51:38 -040010035
James Smartd7c255b2008-08-24 21:50:00 -040010036 if (retval)
10037 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010038
10039 /*
10040 * Caller to this routine should check for IOCB_ERROR
10041 * and handle it properly. This routine no longer removes
10042 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10043 */
10044 return retval;
10045}
10046
10047/**
10048 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10049 * @phba: Pointer to HBA context object.
10050 * @pring: Pointer to driver SLI ring object.
10051 * @cmdiocb: Pointer to driver command iocb object.
10052 *
10053 * This function issues an abort iocb for the provided command iocb. In case
10054 * of unloading, the abort iocb will not be issued to commands on the ELS
10055 * ring. Instead, the callback function shall be changed to those commands
10056 * so that nothing happens when them finishes. This function is called with
10057 * hbalock held. The function returns 0 when the command iocb is an abort
10058 * request.
10059 **/
10060int
10061lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10062 struct lpfc_iocbq *cmdiocb)
10063{
10064 struct lpfc_vport *vport = cmdiocb->vport;
10065 int retval = IOCB_ERROR;
10066 IOCB_t *icmd = NULL;
10067
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010068 lockdep_assert_held(&phba->hbalock);
10069
James Smart5af5eee2010-10-22 11:06:38 -040010070 /*
10071 * There are certain command types we don't want to abort. And we
10072 * don't want to abort commands that are already in the process of
10073 * being aborted.
10074 */
10075 icmd = &cmdiocb->iocb;
10076 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10077 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10078 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10079 return 0;
10080
10081 /*
10082 * If we're unloading, don't abort iocb on the ELS ring, but change
10083 * the callback so that nothing happens when it finishes.
10084 */
10085 if ((vport->load_flag & FC_UNLOADING) &&
10086 (pring->ringno == LPFC_ELS_RING)) {
10087 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10088 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10089 else
10090 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10091 goto abort_iotag_exit;
10092 }
10093
10094 /* Now, we try to issue the abort to the cmdiocb out */
10095 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10096
James Smart07951072007-04-25 09:51:38 -040010097abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010098 /*
10099 * Caller to this routine should check for IOCB_ERROR
10100 * and handle it properly. This routine no longer removes
10101 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010102 */
James Smart2e0fef82007-06-17 19:56:36 -050010103 return retval;
dea31012005-04-17 16:05:31 -050010104}
10105
James Smarte59058c2008-08-24 21:49:00 -040010106/**
James Smart5af5eee2010-10-22 11:06:38 -040010107 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10108 * @phba: pointer to lpfc HBA data structure.
10109 *
10110 * This routine will abort all pending and outstanding iocbs to an HBA.
10111 **/
10112void
10113lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10114{
10115 struct lpfc_sli *psli = &phba->sli;
10116 struct lpfc_sli_ring *pring;
10117 int i;
10118
10119 for (i = 0; i < psli->num_rings; i++) {
10120 pring = &psli->ring[i];
James Smartdb55fba2014-04-04 13:52:02 -040010121 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010122 }
10123}
10124
10125/**
James Smart3621a712009-04-06 18:47:14 -040010126 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010127 * @iocbq: Pointer to driver iocb object.
10128 * @vport: Pointer to driver virtual port object.
10129 * @tgt_id: SCSI ID of the target.
10130 * @lun_id: LUN ID of the scsi device.
10131 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10132 *
James Smart3621a712009-04-06 18:47:14 -040010133 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010134 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10135 * 0 if the filtering criteria is met for the given iocb and will return
10136 * 1 if the filtering criteria is not met.
10137 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10138 * given iocb is for the SCSI device specified by vport, tgt_id and
10139 * lun_id parameter.
10140 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10141 * given iocb is for the SCSI target specified by vport and tgt_id
10142 * parameters.
10143 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10144 * given iocb is for the SCSI host associated with the given vport.
10145 * This function is called with no locks held.
10146 **/
dea31012005-04-17 16:05:31 -050010147static int
James Smart51ef4c22007-08-02 11:10:31 -040010148lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10149 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010150 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010151{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010152 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010153 int rc = 1;
10154
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010155 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10156 return rc;
10157
James Smart51ef4c22007-08-02 11:10:31 -040010158 if (iocbq->vport != vport)
10159 return rc;
10160
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010161 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010162
James Smart495a7142008-06-14 22:52:59 -040010163 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010164 return rc;
10165
10166 switch (ctx_cmd) {
10167 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010168 if ((lpfc_cmd->rdata->pnode) &&
10169 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10170 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010171 rc = 0;
10172 break;
10173 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010174 if ((lpfc_cmd->rdata->pnode) &&
10175 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010176 rc = 0;
10177 break;
dea31012005-04-17 16:05:31 -050010178 case LPFC_CTX_HOST:
10179 rc = 0;
10180 break;
10181 default:
10182 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010183 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010184 break;
10185 }
10186
10187 return rc;
10188}
10189
James Smarte59058c2008-08-24 21:49:00 -040010190/**
James Smart3621a712009-04-06 18:47:14 -040010191 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010192 * @vport: Pointer to virtual port.
10193 * @tgt_id: SCSI ID of the target.
10194 * @lun_id: LUN ID of the scsi device.
10195 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10196 *
10197 * This function returns number of FCP commands pending for the vport.
10198 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10199 * commands pending on the vport associated with SCSI device specified
10200 * by tgt_id and lun_id parameters.
10201 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10202 * commands pending on the vport associated with SCSI target specified
10203 * by tgt_id parameter.
10204 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10205 * commands pending on the vport.
10206 * This function returns the number of iocbs which satisfy the filter.
10207 * This function is called without any lock held.
10208 **/
dea31012005-04-17 16:05:31 -050010209int
James Smart51ef4c22007-08-02 11:10:31 -040010210lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10211 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010212{
James Smart51ef4c22007-08-02 11:10:31 -040010213 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010214 struct lpfc_iocbq *iocbq;
10215 int sum, i;
dea31012005-04-17 16:05:31 -050010216
Johannes Thumshirn31979002016-07-18 16:06:03 +020010217 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010218 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10219 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010220
James Smart51ef4c22007-08-02 11:10:31 -040010221 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10222 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010223 sum++;
dea31012005-04-17 16:05:31 -050010224 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010225 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010226
dea31012005-04-17 16:05:31 -050010227 return sum;
10228}
10229
James Smarte59058c2008-08-24 21:49:00 -040010230/**
James Smart3621a712009-04-06 18:47:14 -040010231 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010232 * @phba: Pointer to HBA context object
10233 * @cmdiocb: Pointer to command iocb object.
10234 * @rspiocb: Pointer to response iocb object.
10235 *
10236 * This function is called when an aborted FCP iocb completes. This
10237 * function is called by the ring event handler with no lock held.
10238 * This function frees the iocb.
10239 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010240void
James Smart2e0fef82007-06-17 19:56:36 -050010241lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10242 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010243{
James Smartcb69f7d2011-12-13 13:21:57 -050010244 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010245 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010246 "original iotag x%x, abort cmd iotag x%x "
10247 "status 0x%x, reason 0x%x\n",
10248 cmdiocb->iocb.un.acxri.abortContextTag,
10249 cmdiocb->iocb.un.acxri.abortIoTag,
10250 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10251 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010252 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010253 return;
10254}
10255
James Smarte59058c2008-08-24 21:49:00 -040010256/**
James Smart3621a712009-04-06 18:47:14 -040010257 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010258 * @vport: Pointer to virtual port.
10259 * @pring: Pointer to driver SLI ring object.
10260 * @tgt_id: SCSI ID of the target.
10261 * @lun_id: LUN ID of the scsi device.
10262 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10263 *
10264 * This function sends an abort command for every SCSI command
10265 * associated with the given virtual port pending on the ring
10266 * filtered by lpfc_sli_validate_fcp_iocb function.
10267 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10268 * FCP iocbs associated with lun specified by tgt_id and lun_id
10269 * parameters
10270 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10271 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10272 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10273 * FCP iocbs associated with virtual port.
10274 * This function returns number of iocbs it failed to abort.
10275 * This function is called with no locks held.
10276 **/
dea31012005-04-17 16:05:31 -050010277int
James Smart51ef4c22007-08-02 11:10:31 -040010278lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10279 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010280{
James Smart51ef4c22007-08-02 11:10:31 -040010281 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010282 struct lpfc_iocbq *iocbq;
10283 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010284 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010285 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010286 int i;
dea31012005-04-17 16:05:31 -050010287
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010288 for (i = 1; i <= phba->sli.last_iotag; i++) {
10289 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010290
James Smart51ef4c22007-08-02 11:10:31 -040010291 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010292 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010293 continue;
10294
James Smartafbd8d82013-09-06 12:22:13 -040010295 /*
10296 * If the iocbq is already being aborted, don't take a second
10297 * action, but do count it.
10298 */
10299 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10300 continue;
10301
dea31012005-04-17 16:05:31 -050010302 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010303 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010304 if (abtsiocb == NULL) {
10305 errcnt++;
10306 continue;
10307 }
dea31012005-04-17 16:05:31 -050010308
James Smartafbd8d82013-09-06 12:22:13 -040010309 /* indicate the IO is being aborted by the driver. */
10310 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10311
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010312 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010313 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10314 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010315 if (phba->sli_rev == LPFC_SLI_REV4)
10316 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10317 else
10318 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010319 abtsiocb->iocb.ulpLe = 1;
10320 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010321 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010322
James Smart5ffc2662009-11-18 15:39:44 -050010323 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10324 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -050010325 if (iocbq->iocb_flag & LPFC_IO_FCP)
10326 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010327 if (iocbq->iocb_flag & LPFC_IO_FOF)
10328 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010329
James Smart2e0fef82007-06-17 19:56:36 -050010330 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010331 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10332 else
10333 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10334
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010335 /* Setup callback routine and issue the command. */
10336 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010337 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10338 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010339 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010340 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010341 errcnt++;
10342 continue;
10343 }
10344 }
10345
10346 return errcnt;
10347}
10348
James Smarte59058c2008-08-24 21:49:00 -040010349/**
James Smart98912dd2014-04-04 13:52:31 -040010350 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10351 * @vport: Pointer to virtual port.
10352 * @pring: Pointer to driver SLI ring object.
10353 * @tgt_id: SCSI ID of the target.
10354 * @lun_id: LUN ID of the scsi device.
10355 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10356 *
10357 * This function sends an abort command for every SCSI command
10358 * associated with the given virtual port pending on the ring
10359 * filtered by lpfc_sli_validate_fcp_iocb function.
10360 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10361 * FCP iocbs associated with lun specified by tgt_id and lun_id
10362 * parameters
10363 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10364 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10365 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10366 * FCP iocbs associated with virtual port.
10367 * This function returns number of iocbs it aborted .
10368 * This function is called with no locks held right after a taskmgmt
10369 * command is sent.
10370 **/
10371int
10372lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10373 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10374{
10375 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040010376 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dd2014-04-04 13:52:31 -040010377 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040010378 struct lpfc_nodelist *ndlp;
James Smart98912dd2014-04-04 13:52:31 -040010379 struct lpfc_iocbq *iocbq;
10380 IOCB_t *icmd;
10381 int sum, i, ret_val;
10382 unsigned long iflags;
10383 struct lpfc_sli_ring *pring_s4;
10384 uint32_t ring_number;
10385
10386 spin_lock_irq(&phba->hbalock);
10387
10388 /* all I/Os are in process of being flushed */
10389 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10390 spin_unlock_irq(&phba->hbalock);
10391 return 0;
10392 }
10393 sum = 0;
10394
10395 for (i = 1; i <= phba->sli.last_iotag; i++) {
10396 iocbq = phba->sli.iocbq_lookup[i];
10397
10398 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10399 cmd) != 0)
10400 continue;
10401
10402 /*
10403 * If the iocbq is already being aborted, don't take a second
10404 * action, but do count it.
10405 */
10406 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10407 continue;
10408
10409 /* issue ABTS for this IOCB based on iotag */
10410 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10411 if (abtsiocbq == NULL)
10412 continue;
10413
10414 icmd = &iocbq->iocb;
10415 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10416 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10417 if (phba->sli_rev == LPFC_SLI_REV4)
10418 abtsiocbq->iocb.un.acxri.abortIoTag =
10419 iocbq->sli4_xritag;
10420 else
10421 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10422 abtsiocbq->iocb.ulpLe = 1;
10423 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10424 abtsiocbq->vport = vport;
10425
10426 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10427 abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
10428 if (iocbq->iocb_flag & LPFC_IO_FCP)
10429 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010430 if (iocbq->iocb_flag & LPFC_IO_FOF)
10431 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dd2014-04-04 13:52:31 -040010432
James Smart8c50d252014-09-03 12:58:16 -040010433 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10434 ndlp = lpfc_cmd->rdata->pnode;
10435
10436 if (lpfc_is_link_up(phba) &&
10437 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dd2014-04-04 13:52:31 -040010438 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10439 else
10440 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10441
10442 /* Setup callback routine and issue the command. */
10443 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10444
10445 /*
10446 * Indicate the IO is being aborted by the driver and set
10447 * the caller's flag into the aborted IO.
10448 */
10449 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10450
10451 if (phba->sli_rev == LPFC_SLI_REV4) {
10452 ring_number = MAX_SLI3_CONFIGURED_RINGS +
10453 iocbq->fcp_wqidx;
10454 pring_s4 = &phba->sli.ring[ring_number];
10455 /* Note: both hbalock and ring_lock must be set here */
10456 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10457 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10458 abtsiocbq, 0);
10459 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10460 } else {
10461 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10462 abtsiocbq, 0);
10463 }
10464
10465
10466 if (ret_val == IOCB_ERROR)
10467 __lpfc_sli_release_iocbq(phba, abtsiocbq);
10468 else
10469 sum++;
10470 }
10471 spin_unlock_irq(&phba->hbalock);
10472 return sum;
10473}
10474
10475/**
James Smart3621a712009-04-06 18:47:14 -040010476 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040010477 * @phba: Pointer to HBA context object.
10478 * @cmdiocbq: Pointer to command iocb.
10479 * @rspiocbq: Pointer to response iocb.
10480 *
10481 * This function is the completion handler for iocbs issued using
10482 * lpfc_sli_issue_iocb_wait function. This function is called by the
10483 * ring event handler function without any lock held. This function
10484 * can be called from both worker thread context and interrupt
10485 * context. This function also can be called from other thread which
10486 * cleans up the SLI layer objects.
10487 * This function copy the contents of the response iocb to the
10488 * response iocb memory object provided by the caller of
10489 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10490 * sleeps for the iocb completion.
10491 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010492static void
10493lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10494 struct lpfc_iocbq *cmdiocbq,
10495 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050010496{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010497 wait_queue_head_t *pdone_q;
10498 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050010499 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010500
James Smart2e0fef82007-06-17 19:56:36 -050010501 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040010502 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10503
10504 /*
10505 * A time out has occurred for the iocb. If a time out
10506 * completion handler has been supplied, call it. Otherwise,
10507 * just free the iocbq.
10508 */
10509
10510 spin_unlock_irqrestore(&phba->hbalock, iflags);
10511 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10512 cmdiocbq->wait_iocb_cmpl = NULL;
10513 if (cmdiocbq->iocb_cmpl)
10514 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10515 else
10516 lpfc_sli_release_iocbq(phba, cmdiocbq);
10517 return;
10518 }
10519
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010520 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10521 if (cmdiocbq->context2 && rspiocbq)
10522 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10523 &rspiocbq->iocb, sizeof(IOCB_t));
10524
James Smart0f65ff62010-02-26 14:14:23 -050010525 /* Set the exchange busy flag for task management commands */
10526 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10527 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10528 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10529 cur_iocbq);
10530 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10531 }
10532
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010533 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010534 if (pdone_q)
10535 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050010536 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050010537 return;
10538}
10539
James Smarte59058c2008-08-24 21:49:00 -040010540/**
James Smartd11e31d2009-06-10 17:23:06 -040010541 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10542 * @phba: Pointer to HBA context object..
10543 * @piocbq: Pointer to command iocb.
10544 * @flag: Flag to test.
10545 *
10546 * This routine grabs the hbalock and then test the iocb_flag to
10547 * see if the passed in flag is set.
10548 * Returns:
10549 * 1 if flag is set.
10550 * 0 if flag is not set.
10551 **/
10552static int
10553lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10554 struct lpfc_iocbq *piocbq, uint32_t flag)
10555{
10556 unsigned long iflags;
10557 int ret;
10558
10559 spin_lock_irqsave(&phba->hbalock, iflags);
10560 ret = piocbq->iocb_flag & flag;
10561 spin_unlock_irqrestore(&phba->hbalock, iflags);
10562 return ret;
10563
10564}
10565
10566/**
James Smart3621a712009-04-06 18:47:14 -040010567 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040010568 * @phba: Pointer to HBA context object..
10569 * @pring: Pointer to sli ring.
10570 * @piocb: Pointer to command iocb.
10571 * @prspiocbq: Pointer to response iocb.
10572 * @timeout: Timeout in number of seconds.
10573 *
10574 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040010575 * iocb to complete. The iocb_cmpl field of the shall be used
10576 * to handle iocbs which time out. If the field is NULL, the
10577 * function shall free the iocbq structure. If more clean up is
10578 * needed, the caller is expected to provide a completion function
10579 * that will provide the needed clean up. If the iocb command is
10580 * not completed within timeout seconds, the function will either
10581 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10582 * completion function set in the iocb_cmpl field and then return
10583 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10584 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040010585 * The function waits for the iocb completion using an
10586 * non-interruptible wait.
10587 * This function will sleep while waiting for iocb completion.
10588 * So, this function should not be called from any context which
10589 * does not allow sleeping. Due to the same reason, this function
10590 * cannot be called with interrupt disabled.
10591 * This function assumes that the iocb completions occur while
10592 * this function sleep. So, this function cannot be called from
10593 * the thread which process iocb completion for this ring.
10594 * This function clears the iocb_flag of the iocb object before
10595 * issuing the iocb and the iocb completion handler sets this
10596 * flag and wakes this thread when the iocb completes.
10597 * The contents of the response iocb will be copied to prspiocbq
10598 * by the completion handler when the command completes.
10599 * This function returns IOCB_SUCCESS when success.
10600 * This function is called with no lock held.
10601 **/
dea31012005-04-17 16:05:31 -050010602int
James Smart2e0fef82007-06-17 19:56:36 -050010603lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040010604 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050010605 struct lpfc_iocbq *piocb,
10606 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010607 uint32_t timeout)
dea31012005-04-17 16:05:31 -050010608{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010609 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010610 long timeleft, timeout_req = 0;
10611 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010612 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050010613 struct lpfc_iocbq *iocb;
10614 int txq_cnt = 0;
10615 int txcmplq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040010616 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart5a0916b2013-07-15 18:31:42 -040010617 unsigned long iflags;
10618 bool iocb_completed = true;
10619
dea31012005-04-17 16:05:31 -050010620 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010621 * If the caller has provided a response iocbq buffer, then context2
10622 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050010623 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010624 if (prspiocbq) {
10625 if (piocb->context2)
10626 return IOCB_ERROR;
10627 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050010628 }
10629
James Smart5a0916b2013-07-15 18:31:42 -040010630 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010631 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10632 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040010633 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050010634
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010635 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010636 if (lpfc_readl(phba->HCregaddr, &creg_val))
10637 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010638 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10639 writel(creg_val, phba->HCregaddr);
10640 readl(phba->HCregaddr); /* flush */
10641 }
10642
James Smart2a9bf3d2010-06-07 15:24:45 -040010643 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10644 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010645 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040010646 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010647 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040010648 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010649 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040010650 spin_lock_irqsave(&phba->hbalock, iflags);
10651 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050010652
James Smart5a0916b2013-07-15 18:31:42 -040010653 /*
10654 * IOCB timed out. Inform the wake iocb wait
10655 * completion function and set local status
10656 */
10657
10658 iocb_completed = false;
10659 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10660 }
10661 spin_unlock_irqrestore(&phba->hbalock, iflags);
10662 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040010663 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010664 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040010665 /* Note: we are not indicating if the IOCB has a success
10666 * status or not - that's for the caller to check.
10667 * IOCB_SUCCESS means just that the command was sent and
10668 * completed. Not that it completed successfully.
10669 * */
James Smart7054a602007-04-25 09:52:34 -040010670 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010671 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010672 "0338 IOCB wait timeout error - no "
10673 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010674 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040010675 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010676 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010677 "0330 IOCB wake NOT set, "
10678 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010679 timeout, (timeleft / jiffies));
10680 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010681 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010682 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050010683 if (phba->cfg_log_verbose & LOG_SLI) {
10684 list_for_each_entry(iocb, &pring->txq, list) {
10685 txq_cnt++;
10686 }
10687 list_for_each_entry(iocb, &pring->txcmplq, list) {
10688 txcmplq_cnt++;
10689 }
10690 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10691 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10692 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10693 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010694 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010695 } else {
10696 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010697 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010698 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010699 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010700 }
10701
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010702 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010703 if (lpfc_readl(phba->HCregaddr, &creg_val))
10704 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010705 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10706 writel(creg_val, phba->HCregaddr);
10707 readl(phba->HCregaddr); /* flush */
10708 }
10709
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010710 if (prspiocbq)
10711 piocb->context2 = NULL;
10712
10713 piocb->context_un.wait_queue = NULL;
10714 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010715 return retval;
10716}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010717
James Smarte59058c2008-08-24 21:49:00 -040010718/**
James Smart3621a712009-04-06 18:47:14 -040010719 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010720 * @phba: Pointer to HBA context object.
10721 * @pmboxq: Pointer to driver mailbox object.
10722 * @timeout: Timeout in number of seconds.
10723 *
10724 * This function issues the mailbox to firmware and waits for the
10725 * mailbox command to complete. If the mailbox command is not
10726 * completed within timeout seconds, it returns MBX_TIMEOUT.
10727 * The function waits for the mailbox completion using an
10728 * interruptible wait. If the thread is woken up due to a
10729 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10730 * should not free the mailbox resources, if this function returns
10731 * MBX_TIMEOUT.
10732 * This function will sleep while waiting for mailbox completion.
10733 * So, this function should not be called from any context which
10734 * does not allow sleeping. Due to the same reason, this function
10735 * cannot be called with interrupt disabled.
10736 * This function assumes that the mailbox completion occurs while
10737 * this function sleep. So, this function cannot be called from
10738 * the worker thread which processes mailbox completion.
10739 * This function is called in the context of HBA management
10740 * applications.
10741 * This function returns MBX_SUCCESS when successful.
10742 * This function is called with no lock held.
10743 **/
dea31012005-04-17 16:05:31 -050010744int
James Smart2e0fef82007-06-17 19:56:36 -050010745lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010746 uint32_t timeout)
10747{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010748 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040010749 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050010750 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010751 unsigned long flag;
dea31012005-04-17 16:05:31 -050010752
James Smartb230b8a2013-05-31 17:05:27 -040010753 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040010754 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040010755 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050010756
James Smart495a7142008-06-14 22:52:59 -040010757 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010758 /* setup wake call as IOCB callback */
10759 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10760 /* setup context field to pass wait_queue pointer to wake function */
10761 pmboxq->context1 = &done_q;
10762
dea31012005-04-17 16:05:31 -050010763 /* now issue the command */
10764 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010765 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010766 wait_event_interruptible_timeout(done_q,
10767 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040010768 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040010769
James Smart858c9f62007-06-17 19:56:39 -050010770 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010771 /* restore the possible extended buffer for free resource */
10772 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040010773 /*
10774 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10775 * else do not free the resources.
10776 */
James Smartd7c47992010-06-08 18:31:54 -040010777 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010778 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010779 } else {
James Smart7054a602007-04-25 09:52:34 -040010780 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010781 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10782 }
10783 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010784 } else {
10785 /* restore the possible extended buffer for free resource */
10786 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050010787 }
10788
dea31012005-04-17 16:05:31 -050010789 return retval;
10790}
10791
James Smarte59058c2008-08-24 21:49:00 -040010792/**
James Smart3772a992009-05-22 14:50:54 -040010793 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010794 * @phba: Pointer to HBA context.
10795 *
James Smart3772a992009-05-22 14:50:54 -040010796 * This function is called to shutdown the driver's mailbox sub-system.
10797 * It first marks the mailbox sub-system is in a block state to prevent
10798 * the asynchronous mailbox command from issued off the pending mailbox
10799 * command queue. If the mailbox command sub-system shutdown is due to
10800 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10801 * the mailbox sub-system flush routine to forcefully bring down the
10802 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10803 * as with offline or HBA function reset), this routine will wait for the
10804 * outstanding mailbox command to complete before invoking the mailbox
10805 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010806 **/
James Smart3772a992009-05-22 14:50:54 -040010807void
James Smart618a5232012-06-12 13:54:36 -040010808lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010809{
James Smart3772a992009-05-22 14:50:54 -040010810 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010811 unsigned long timeout;
10812
James Smart618a5232012-06-12 13:54:36 -040010813 if (mbx_action == LPFC_MBX_NO_WAIT) {
10814 /* delay 100ms for port state */
10815 msleep(100);
10816 lpfc_sli_mbox_sys_flush(phba);
10817 return;
10818 }
James Smarta183a152011-10-10 21:32:43 -040010819 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010820
James Smart3772a992009-05-22 14:50:54 -040010821 spin_lock_irq(&phba->hbalock);
10822 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010823
10824 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010825 /* Determine how long we might wait for the active mailbox
10826 * command to be gracefully completed by firmware.
10827 */
James Smarta183a152011-10-10 21:32:43 -040010828 if (phba->sli.mbox_active)
10829 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10830 phba->sli.mbox_active) *
10831 1000) + jiffies;
10832 spin_unlock_irq(&phba->hbalock);
10833
James Smart3772a992009-05-22 14:50:54 -040010834 while (phba->sli.mbox_active) {
10835 /* Check active mailbox complete status every 2ms */
10836 msleep(2);
10837 if (time_after(jiffies, timeout))
10838 /* Timeout, let the mailbox flush routine to
10839 * forcefully release active mailbox command
10840 */
10841 break;
10842 }
James Smartd7069f02012-03-01 22:36:29 -050010843 } else
10844 spin_unlock_irq(&phba->hbalock);
10845
James Smart3772a992009-05-22 14:50:54 -040010846 lpfc_sli_mbox_sys_flush(phba);
10847}
10848
10849/**
10850 * lpfc_sli_eratt_read - read sli-3 error attention events
10851 * @phba: Pointer to HBA context.
10852 *
10853 * This function is called to read the SLI3 device error attention registers
10854 * for possible error attention events. The caller must hold the hostlock
10855 * with spin_lock_irq().
10856 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010857 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010858 * Register and returns 0 otherwise.
10859 **/
10860static int
10861lpfc_sli_eratt_read(struct lpfc_hba *phba)
10862{
James Smarted957682007-06-17 19:56:37 -050010863 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010864
James Smart3772a992009-05-22 14:50:54 -040010865 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010866 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10867 goto unplug_err;
10868
James Smart3772a992009-05-22 14:50:54 -040010869 if (ha_copy & HA_ERATT) {
10870 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010871 if (lpfc_sli_read_hs(phba))
10872 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010873
James Smart3772a992009-05-22 14:50:54 -040010874 /* Check if there is a deferred error condition is active */
10875 if ((HS_FFER1 & phba->work_hs) &&
10876 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010877 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010878 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010879 /* Clear all interrupt enable conditions */
10880 writel(0, phba->HCregaddr);
10881 readl(phba->HCregaddr);
10882 }
10883
10884 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010885 phba->work_ha |= HA_ERATT;
10886 /* Indicate polling handles this ERATT */
10887 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010888 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010889 }
James Smart3772a992009-05-22 14:50:54 -040010890 return 0;
James Smart9940b972011-03-11 16:06:12 -050010891
10892unplug_err:
10893 /* Set the driver HS work bitmap */
10894 phba->work_hs |= UNPLUG_ERR;
10895 /* Set the driver HA work bitmap */
10896 phba->work_ha |= HA_ERATT;
10897 /* Indicate polling handles this ERATT */
10898 phba->hba_flag |= HBA_ERATT_HANDLED;
10899 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010900}
10901
James Smarte59058c2008-08-24 21:49:00 -040010902/**
James Smartda0436e2009-05-22 14:51:39 -040010903 * lpfc_sli4_eratt_read - read sli-4 error attention events
10904 * @phba: Pointer to HBA context.
10905 *
10906 * This function is called to read the SLI4 device error attention registers
10907 * for possible error attention events. The caller must hold the hostlock
10908 * with spin_lock_irq().
10909 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010910 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010911 * Register and returns 0 otherwise.
10912 **/
10913static int
10914lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10915{
10916 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010917 uint32_t if_type, portsmphr;
10918 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010919
James Smart2fcee4b2010-12-15 17:57:46 -050010920 /*
10921 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010922 * registers for error attention. This can be changed later.
10923 */
James Smart2fcee4b2010-12-15 17:57:46 -050010924 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10925 switch (if_type) {
10926 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010927 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10928 &uerr_sta_lo) ||
10929 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10930 &uerr_sta_hi)) {
10931 phba->work_hs |= UNPLUG_ERR;
10932 phba->work_ha |= HA_ERATT;
10933 phba->hba_flag |= HBA_ERATT_HANDLED;
10934 return 1;
10935 }
James Smart2fcee4b2010-12-15 17:57:46 -050010936 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10937 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10938 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10939 "1423 HBA Unrecoverable error: "
10940 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10941 "ue_mask_lo_reg=0x%x, "
10942 "ue_mask_hi_reg=0x%x\n",
10943 uerr_sta_lo, uerr_sta_hi,
10944 phba->sli4_hba.ue_mask_lo,
10945 phba->sli4_hba.ue_mask_hi);
10946 phba->work_status[0] = uerr_sta_lo;
10947 phba->work_status[1] = uerr_sta_hi;
10948 phba->work_ha |= HA_ERATT;
10949 phba->hba_flag |= HBA_ERATT_HANDLED;
10950 return 1;
10951 }
10952 break;
10953 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010954 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10955 &portstat_reg.word0) ||
10956 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10957 &portsmphr)){
10958 phba->work_hs |= UNPLUG_ERR;
10959 phba->work_ha |= HA_ERATT;
10960 phba->hba_flag |= HBA_ERATT_HANDLED;
10961 return 1;
10962 }
James Smart2fcee4b2010-12-15 17:57:46 -050010963 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10964 phba->work_status[0] =
10965 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10966 phba->work_status[1] =
10967 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10968 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010969 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010970 "port status reg 0x%x, "
10971 "port smphr reg 0x%x, "
10972 "error 1=0x%x, error 2=0x%x\n",
10973 portstat_reg.word0,
10974 portsmphr,
10975 phba->work_status[0],
10976 phba->work_status[1]);
10977 phba->work_ha |= HA_ERATT;
10978 phba->hba_flag |= HBA_ERATT_HANDLED;
10979 return 1;
10980 }
10981 break;
10982 case LPFC_SLI_INTF_IF_TYPE_1:
10983 default:
James Smarta747c9c2009-11-18 15:41:10 -050010984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050010985 "2886 HBA Error Attention on unsupported "
10986 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050010987 return 1;
James Smartda0436e2009-05-22 14:51:39 -040010988 }
James Smart2fcee4b2010-12-15 17:57:46 -050010989
James Smartda0436e2009-05-22 14:51:39 -040010990 return 0;
10991}
10992
10993/**
James Smart3621a712009-04-06 18:47:14 -040010994 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040010995 * @phba: Pointer to HBA context.
10996 *
James Smart3772a992009-05-22 14:50:54 -040010997 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040010998 * error attention register bit for error attention events.
10999 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011000 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011001 * Register and returns 0 otherwise.
11002 **/
11003int
11004lpfc_sli_check_eratt(struct lpfc_hba *phba)
11005{
11006 uint32_t ha_copy;
11007
11008 /* If somebody is waiting to handle an eratt, don't process it
11009 * here. The brdkill function will do this.
11010 */
11011 if (phba->link_flag & LS_IGNORE_ERATT)
11012 return 0;
11013
11014 /* Check if interrupt handler handles this ERATT */
11015 spin_lock_irq(&phba->hbalock);
11016 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11017 /* Interrupt handler has handled ERATT */
11018 spin_unlock_irq(&phba->hbalock);
11019 return 0;
11020 }
11021
James Smarta257bf92009-04-06 18:48:10 -040011022 /*
11023 * If there is deferred error attention, do not check for error
11024 * attention
11025 */
11026 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11027 spin_unlock_irq(&phba->hbalock);
11028 return 0;
11029 }
11030
James Smart3772a992009-05-22 14:50:54 -040011031 /* If PCI channel is offline, don't process it */
11032 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011033 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011034 return 0;
11035 }
11036
11037 switch (phba->sli_rev) {
11038 case LPFC_SLI_REV2:
11039 case LPFC_SLI_REV3:
11040 /* Read chip Host Attention (HA) register */
11041 ha_copy = lpfc_sli_eratt_read(phba);
11042 break;
James Smartda0436e2009-05-22 14:51:39 -040011043 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011044 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011045 ha_copy = lpfc_sli4_eratt_read(phba);
11046 break;
James Smart3772a992009-05-22 14:50:54 -040011047 default:
11048 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11049 "0299 Invalid SLI revision (%d)\n",
11050 phba->sli_rev);
11051 ha_copy = 0;
11052 break;
James Smart93996272008-08-24 21:50:30 -040011053 }
11054 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011055
11056 return ha_copy;
11057}
11058
11059/**
11060 * lpfc_intr_state_check - Check device state for interrupt handling
11061 * @phba: Pointer to HBA context.
11062 *
11063 * This inline routine checks whether a device or its PCI slot is in a state
11064 * that the interrupt should be handled.
11065 *
11066 * This function returns 0 if the device or the PCI slot is in a state that
11067 * interrupt should be handled, otherwise -EIO.
11068 */
11069static inline int
11070lpfc_intr_state_check(struct lpfc_hba *phba)
11071{
11072 /* If the pci channel is offline, ignore all the interrupts */
11073 if (unlikely(pci_channel_offline(phba->pcidev)))
11074 return -EIO;
11075
11076 /* Update device level interrupt statistics */
11077 phba->sli.slistat.sli_intr++;
11078
11079 /* Ignore all interrupts during initialization. */
11080 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11081 return -EIO;
11082
James Smart93996272008-08-24 21:50:30 -040011083 return 0;
11084}
11085
11086/**
James Smart3772a992009-05-22 14:50:54 -040011087 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011088 * @irq: Interrupt number.
11089 * @dev_id: The device context pointer.
11090 *
James Smart93996272008-08-24 21:50:30 -040011091 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011092 * service routine when device with SLI-3 interface spec is enabled with
11093 * MSI-X multi-message interrupt mode and there are slow-path events in
11094 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11095 * interrupt mode, this function is called as part of the device-level
11096 * interrupt handler. When the PCI slot is in error recovery or the HBA
11097 * is undergoing initialization, the interrupt handler will not process
11098 * the interrupt. The link attention and ELS ring attention events are
11099 * handled by the worker thread. The interrupt handler signals the worker
11100 * thread and returns for these events. This function is called without
11101 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011102 * structures.
11103 *
11104 * This function returns IRQ_HANDLED when interrupt is handled else it
11105 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011106 **/
dea31012005-04-17 16:05:31 -050011107irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011108lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011109{
James Smart2e0fef82007-06-17 19:56:36 -050011110 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011111 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011112 uint32_t work_ha_copy;
11113 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011114 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011115 uint32_t control;
11116
James Smart92d7f7b2007-06-17 19:56:38 -050011117 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011118 struct lpfc_vport *vport;
11119 struct lpfc_nodelist *ndlp;
11120 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011121 LPFC_MBOXQ_t *pmb;
11122 int rc;
11123
dea31012005-04-17 16:05:31 -050011124 /*
11125 * Get the driver's phba structure from the dev_id and
11126 * assume the HBA is not interrupting.
11127 */
James Smart93996272008-08-24 21:50:30 -040011128 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011129
11130 if (unlikely(!phba))
11131 return IRQ_NONE;
11132
dea31012005-04-17 16:05:31 -050011133 /*
James Smart93996272008-08-24 21:50:30 -040011134 * Stuff needs to be attented to when this function is invoked as an
11135 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011136 */
James Smart93996272008-08-24 21:50:30 -040011137 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011138 /* Check device state for handling interrupt */
11139 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011140 return IRQ_NONE;
11141 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011142 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011143 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11144 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011145 /* If somebody is waiting to handle an eratt don't process it
11146 * here. The brdkill function will do this.
11147 */
11148 if (phba->link_flag & LS_IGNORE_ERATT)
11149 ha_copy &= ~HA_ERATT;
11150 /* Check the need for handling ERATT in interrupt handler */
11151 if (ha_copy & HA_ERATT) {
11152 if (phba->hba_flag & HBA_ERATT_HANDLED)
11153 /* ERATT polling has handled ERATT */
11154 ha_copy &= ~HA_ERATT;
11155 else
11156 /* Indicate interrupt handler handles ERATT */
11157 phba->hba_flag |= HBA_ERATT_HANDLED;
11158 }
James Smarta257bf92009-04-06 18:48:10 -040011159
11160 /*
11161 * If there is deferred error attention, do not check for any
11162 * interrupt.
11163 */
11164 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011165 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011166 return IRQ_NONE;
11167 }
11168
James Smart93996272008-08-24 21:50:30 -040011169 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011170 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11171 goto unplug_error;
11172
James Smarta747c9c2009-11-18 15:41:10 -050011173 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11174 HC_LAINT_ENA | HC_ERINT_ENA),
11175 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011176 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11177 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011178 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011179 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011180 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011181 } else
11182 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011183
dea31012005-04-17 16:05:31 -050011184 work_ha_copy = ha_copy & phba->work_ha_mask;
11185
James Smart93996272008-08-24 21:50:30 -040011186 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011187 if (work_ha_copy & HA_LATT) {
11188 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11189 /*
11190 * Turn off Link Attention interrupts
11191 * until CLEAR_LA done
11192 */
James Smart5b75da22008-12-04 22:39:35 -050011193 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011194 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011195 if (lpfc_readl(phba->HCregaddr, &control))
11196 goto unplug_error;
dea31012005-04-17 16:05:31 -050011197 control &= ~HC_LAINT_ENA;
11198 writel(control, phba->HCregaddr);
11199 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011200 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011201 }
11202 else
11203 work_ha_copy &= ~HA_LATT;
11204 }
11205
James Smart93996272008-08-24 21:50:30 -040011206 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011207 /*
11208 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11209 * the only slow ring.
11210 */
11211 status = (work_ha_copy &
11212 (HA_RXMASK << (4*LPFC_ELS_RING)));
11213 status >>= (4*LPFC_ELS_RING);
11214 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011215 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011216 if (lpfc_readl(phba->HCregaddr, &control))
11217 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011218
11219 lpfc_debugfs_slow_ring_trc(phba,
11220 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11221 control, status,
11222 (uint32_t)phba->sli.slistat.sli_intr);
11223
James Smart858c9f62007-06-17 19:56:39 -050011224 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011225 lpfc_debugfs_slow_ring_trc(phba,
11226 "ISR Disable ring:"
11227 "pwork:x%x hawork:x%x wait:x%x",
11228 phba->work_ha, work_ha_copy,
11229 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011230 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011231
James Smart858c9f62007-06-17 19:56:39 -050011232 control &=
11233 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011234 writel(control, phba->HCregaddr);
11235 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011236 }
James Smarta58cbd52007-08-02 11:09:43 -040011237 else {
11238 lpfc_debugfs_slow_ring_trc(phba,
11239 "ISR slow ring: pwork:"
11240 "x%x hawork:x%x wait:x%x",
11241 phba->work_ha, work_ha_copy,
11242 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011243 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011244 }
James Smart5b75da22008-12-04 22:39:35 -050011245 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011246 }
11247 }
James Smart5b75da22008-12-04 22:39:35 -050011248 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011249 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011250 if (lpfc_sli_read_hs(phba))
11251 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011252 /*
11253 * Check if there is a deferred error condition
11254 * is active
11255 */
11256 if ((HS_FFER1 & phba->work_hs) &&
11257 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011258 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11259 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011260 phba->hba_flag |= DEFER_ERATT;
11261 /* Clear all interrupt enable conditions */
11262 writel(0, phba->HCregaddr);
11263 readl(phba->HCregaddr);
11264 }
11265 }
11266
James Smart93996272008-08-24 21:50:30 -040011267 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011268 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011269 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011270 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011271 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011272
11273 /* First check out the status word */
11274 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11275 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011276 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011277 /*
11278 * Stray Mailbox Interrupt, mbxCommand <cmd>
11279 * mbxStatus <status>
11280 */
James Smart09372822008-01-11 01:52:54 -050011281 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011282 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011283 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011284 "Interrupt mbxCommand x%x "
11285 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011286 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011287 pmbox->mbxCommand,
11288 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011289 /* clear mailbox attention bit */
11290 work_ha_copy &= ~HA_MBATT;
11291 } else {
James Smart97eab632008-04-07 10:16:05 -040011292 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011293 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011294 phba->last_completion_time = jiffies;
11295 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011296 if (pmb->mbox_cmpl) {
11297 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11298 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011299 if (pmb->out_ext_byte_len &&
11300 pmb->context2)
11301 lpfc_sli_pcimem_bcopy(
11302 phba->mbox_ext,
11303 pmb->context2,
11304 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011305 }
James Smart09372822008-01-11 01:52:54 -050011306 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11307 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11308
11309 lpfc_debugfs_disc_trc(vport,
11310 LPFC_DISC_TRC_MBOX_VPORT,
11311 "MBOX dflt rpi: : "
11312 "status:x%x rpi:x%x",
11313 (uint32_t)pmbox->mbxStatus,
11314 pmbox->un.varWords[0], 0);
11315
11316 if (!pmbox->mbxStatus) {
11317 mp = (struct lpfc_dmabuf *)
11318 (pmb->context1);
11319 ndlp = (struct lpfc_nodelist *)
11320 pmb->context2;
11321
11322 /* Reg_LOGIN of dflt RPI was
11323 * successful. new lets get
11324 * rid of the RPI using the
11325 * same mbox buffer.
11326 */
11327 lpfc_unreg_login(phba,
11328 vport->vpi,
11329 pmbox->un.varWords[0],
11330 pmb);
11331 pmb->mbox_cmpl =
11332 lpfc_mbx_cmpl_dflt_rpi;
11333 pmb->context1 = mp;
11334 pmb->context2 = ndlp;
11335 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040011336 rc = lpfc_sli_issue_mbox(phba,
11337 pmb,
11338 MBX_NOWAIT);
11339 if (rc != MBX_BUSY)
11340 lpfc_printf_log(phba,
11341 KERN_ERR,
11342 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011343 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040011344 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040011345 if (rc != MBX_NOT_FINISHED)
11346 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050011347 }
11348 }
James Smart5b75da22008-12-04 22:39:35 -050011349 spin_lock_irqsave(
11350 &phba->pport->work_port_lock,
11351 iflag);
James Smart09372822008-01-11 01:52:54 -050011352 phba->pport->work_port_events &=
11353 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050011354 spin_unlock_irqrestore(
11355 &phba->pport->work_port_lock,
11356 iflag);
James Smart09372822008-01-11 01:52:54 -050011357 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050011358 }
James Smart97eab632008-04-07 10:16:05 -040011359 } else
James Smart5b75da22008-12-04 22:39:35 -050011360 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011361
James Smart92d7f7b2007-06-17 19:56:38 -050011362 if ((work_ha_copy & HA_MBATT) &&
11363 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050011364send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050011365 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040011366 do {
11367 rc = lpfc_sli_issue_mbox(phba, NULL,
11368 MBX_NOWAIT);
11369 } while (rc == MBX_NOT_FINISHED);
11370 if (rc != MBX_SUCCESS)
11371 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11372 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040011373 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050011374 }
11375
James Smart5b75da22008-12-04 22:39:35 -050011376 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011377 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050011378 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040011379 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050011380 }
James Smart93996272008-08-24 21:50:30 -040011381 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050011382unplug_error:
11383 spin_unlock_irqrestore(&phba->hbalock, iflag);
11384 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050011385
James Smart3772a992009-05-22 14:50:54 -040011386} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040011387
11388/**
James Smart3772a992009-05-22 14:50:54 -040011389 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040011390 * @irq: Interrupt number.
11391 * @dev_id: The device context pointer.
11392 *
11393 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011394 * service routine when device with SLI-3 interface spec is enabled with
11395 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11396 * ring event in the HBA. However, when the device is enabled with either
11397 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11398 * device-level interrupt handler. When the PCI slot is in error recovery
11399 * or the HBA is undergoing initialization, the interrupt handler will not
11400 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11401 * the intrrupt context. This function is called without any lock held.
11402 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011403 *
11404 * This function returns IRQ_HANDLED when interrupt is handled else it
11405 * returns IRQ_NONE.
11406 **/
11407irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011408lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011409{
11410 struct lpfc_hba *phba;
11411 uint32_t ha_copy;
11412 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011413 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040011414
11415 /* Get the driver's phba structure from the dev_id and
11416 * assume the HBA is not interrupting.
11417 */
11418 phba = (struct lpfc_hba *) dev_id;
11419
11420 if (unlikely(!phba))
11421 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050011422
11423 /*
James Smart93996272008-08-24 21:50:30 -040011424 * Stuff needs to be attented to when this function is invoked as an
11425 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011426 */
James Smart93996272008-08-24 21:50:30 -040011427 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011428 /* Check device state for handling interrupt */
11429 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011430 return IRQ_NONE;
11431 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050011432 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11433 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040011434 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050011435 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011436 /*
11437 * If there is deferred error attention, do not check for
11438 * any interrupt.
11439 */
11440 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011441 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011442 return IRQ_NONE;
11443 }
James Smart93996272008-08-24 21:50:30 -040011444 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11445 phba->HAregaddr);
11446 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011447 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011448 } else
11449 ha_copy = phba->ha_copy;
11450
11451 /*
11452 * Process all events on FCP ring. Take the optimized path for FCP IO.
11453 */
11454 ha_copy &= ~(phba->work_ha_mask);
11455
11456 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050011457 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050011458 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050011459 lpfc_sli_handle_fast_ring_event(phba,
11460 &phba->sli.ring[LPFC_FCP_RING],
11461 status);
James Smarta4bc3372006-12-02 13:34:16 -050011462
11463 if (phba->cfg_multi_ring_support == 2) {
11464 /*
James Smart93996272008-08-24 21:50:30 -040011465 * Process all events on extra ring. Take the optimized path
11466 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050011467 */
James Smart93996272008-08-24 21:50:30 -040011468 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050011469 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050011470 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050011471 lpfc_sli_handle_fast_ring_event(phba,
11472 &phba->sli.ring[LPFC_EXTRA_RING],
11473 status);
11474 }
11475 }
dea31012005-04-17 16:05:31 -050011476 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011477} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050011478
James Smart93996272008-08-24 21:50:30 -040011479/**
James Smart3772a992009-05-22 14:50:54 -040011480 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040011481 * @irq: Interrupt number.
11482 * @dev_id: The device context pointer.
11483 *
James Smart3772a992009-05-22 14:50:54 -040011484 * This function is the HBA device-level interrupt handler to device with
11485 * SLI-3 interface spec, called from the PCI layer when either MSI or
11486 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11487 * requires driver attention. This function invokes the slow-path interrupt
11488 * attention handling function and fast-path interrupt attention handling
11489 * function in turn to process the relevant HBA attention events. This
11490 * function is called without any lock held. It gets the hbalock to access
11491 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011492 *
11493 * This function returns IRQ_HANDLED when interrupt is handled, else it
11494 * returns IRQ_NONE.
11495 **/
11496irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011497lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011498{
11499 struct lpfc_hba *phba;
11500 irqreturn_t sp_irq_rc, fp_irq_rc;
11501 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050011502 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040011503
11504 /*
11505 * Get the driver's phba structure from the dev_id and
11506 * assume the HBA is not interrupting.
11507 */
11508 phba = (struct lpfc_hba *) dev_id;
11509
11510 if (unlikely(!phba))
11511 return IRQ_NONE;
11512
James Smart3772a992009-05-22 14:50:54 -040011513 /* Check device state for handling interrupt */
11514 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011515 return IRQ_NONE;
11516
11517 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050011518 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11519 spin_unlock(&phba->hbalock);
11520 return IRQ_HANDLED;
11521 }
11522
James Smart93996272008-08-24 21:50:30 -040011523 if (unlikely(!phba->ha_copy)) {
11524 spin_unlock(&phba->hbalock);
11525 return IRQ_NONE;
11526 } else if (phba->ha_copy & HA_ERATT) {
11527 if (phba->hba_flag & HBA_ERATT_HANDLED)
11528 /* ERATT polling has handled ERATT */
11529 phba->ha_copy &= ~HA_ERATT;
11530 else
11531 /* Indicate interrupt handler handles ERATT */
11532 phba->hba_flag |= HBA_ERATT_HANDLED;
11533 }
11534
James Smarta257bf92009-04-06 18:48:10 -040011535 /*
11536 * If there is deferred error attention, do not check for any interrupt.
11537 */
11538 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020011539 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040011540 return IRQ_NONE;
11541 }
11542
James Smart93996272008-08-24 21:50:30 -040011543 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050011544 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11545 spin_unlock(&phba->hbalock);
11546 return IRQ_HANDLED;
11547 }
James Smarta747c9c2009-11-18 15:41:10 -050011548 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11549 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11550 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011551 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011552 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011553 readl(phba->HAregaddr); /* flush */
11554 spin_unlock(&phba->hbalock);
11555
11556 /*
11557 * Invokes slow-path host attention interrupt handling as appropriate.
11558 */
11559
11560 /* status of events with mailbox and link attention */
11561 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11562
11563 /* status of events with ELS ring */
11564 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11565 status2 >>= (4*LPFC_ELS_RING);
11566
11567 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011568 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011569 else
11570 sp_irq_rc = IRQ_NONE;
11571
11572 /*
11573 * Invoke fast-path host attention interrupt handling as appropriate.
11574 */
11575
11576 /* status of events with FCP ring */
11577 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11578 status1 >>= (4*LPFC_FCP_RING);
11579
11580 /* status of events with extra ring */
11581 if (phba->cfg_multi_ring_support == 2) {
11582 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11583 status2 >>= (4*LPFC_EXTRA_RING);
11584 } else
11585 status2 = 0;
11586
11587 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011588 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011589 else
11590 fp_irq_rc = IRQ_NONE;
11591
11592 /* Return device-level interrupt handling status */
11593 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040011594} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040011595
11596/**
11597 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11598 * @phba: pointer to lpfc hba data structure.
11599 *
11600 * This routine is invoked by the worker thread to process all the pending
11601 * SLI4 FCP abort XRI events.
11602 **/
11603void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11604{
11605 struct lpfc_cq_event *cq_event;
11606
11607 /* First, declare the fcp xri abort event has been handled */
11608 spin_lock_irq(&phba->hbalock);
11609 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11610 spin_unlock_irq(&phba->hbalock);
11611 /* Now, handle all the fcp xri abort events */
11612 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11613 /* Get the first event from the head of the event queue */
11614 spin_lock_irq(&phba->hbalock);
11615 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11616 cq_event, struct lpfc_cq_event, list);
11617 spin_unlock_irq(&phba->hbalock);
11618 /* Notify aborted XRI for FCP work queue */
11619 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11620 /* Free the event processed back to the free pool */
11621 lpfc_sli4_cq_event_release(phba, cq_event);
11622 }
11623}
11624
11625/**
11626 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11627 * @phba: pointer to lpfc hba data structure.
11628 *
11629 * This routine is invoked by the worker thread to process all the pending
11630 * SLI4 els abort xri events.
11631 **/
11632void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11633{
11634 struct lpfc_cq_event *cq_event;
11635
11636 /* First, declare the els xri abort event has been handled */
11637 spin_lock_irq(&phba->hbalock);
11638 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11639 spin_unlock_irq(&phba->hbalock);
11640 /* Now, handle all the els xri abort events */
11641 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11642 /* Get the first event from the head of the event queue */
11643 spin_lock_irq(&phba->hbalock);
11644 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11645 cq_event, struct lpfc_cq_event, list);
11646 spin_unlock_irq(&phba->hbalock);
11647 /* Notify aborted XRI for ELS work queue */
11648 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11649 /* Free the event processed back to the free pool */
11650 lpfc_sli4_cq_event_release(phba, cq_event);
11651 }
11652}
11653
James Smart341af102010-01-26 23:07:37 -050011654/**
11655 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11656 * @phba: pointer to lpfc hba data structure
11657 * @pIocbIn: pointer to the rspiocbq
11658 * @pIocbOut: pointer to the cmdiocbq
11659 * @wcqe: pointer to the complete wcqe
11660 *
11661 * This routine transfers the fields of a command iocbq to a response iocbq
11662 * by copying all the IOCB fields from command iocbq and transferring the
11663 * completion status information from the complete wcqe.
11664 **/
James Smart4f774512009-05-22 14:52:35 -040011665static void
James Smart341af102010-01-26 23:07:37 -050011666lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11667 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040011668 struct lpfc_iocbq *pIocbOut,
11669 struct lpfc_wcqe_complete *wcqe)
11670{
James Smartaf227412013-10-10 12:23:10 -040011671 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050011672 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040011673 uint32_t status, max_response;
11674 struct lpfc_dmabuf *dmabuf;
11675 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040011676 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11677
11678 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11679 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040011680 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050011681 status = bf_get(lpfc_wcqe_c_status, wcqe);
11682 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040011683 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11684 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11685 pIocbIn->iocb.un.fcpi.fcpi_parm =
11686 pIocbOut->iocb.un.fcpi.fcpi_parm -
11687 wcqe->total_data_placed;
11688 else
11689 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011690 else {
James Smart4f774512009-05-22 14:52:35 -040011691 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040011692 switch (pIocbOut->iocb.ulpCommand) {
11693 case CMD_ELS_REQUEST64_CR:
11694 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11695 bpl = (struct ulp_bde64 *)dmabuf->virt;
11696 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11697 max_response = bde.tus.f.bdeSize;
11698 break;
11699 case CMD_GEN_REQUEST64_CR:
11700 max_response = 0;
11701 if (!pIocbOut->context3)
11702 break;
11703 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11704 sizeof(struct ulp_bde64);
11705 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11706 bpl = (struct ulp_bde64 *)dmabuf->virt;
11707 for (i = 0; i < numBdes; i++) {
11708 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11709 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11710 max_response += bde.tus.f.bdeSize;
11711 }
11712 break;
11713 default:
11714 max_response = wcqe->total_data_placed;
11715 break;
11716 }
11717 if (max_response < wcqe->total_data_placed)
11718 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11719 else
11720 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11721 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050011722 }
James Smart341af102010-01-26 23:07:37 -050011723
James Smartacd68592012-01-18 16:25:09 -050011724 /* Convert BG errors for completion status */
11725 if (status == CQE_STATUS_DI_ERROR) {
11726 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11727
11728 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11729 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11730 else
11731 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11732
11733 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11734 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11735 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11736 BGS_GUARD_ERR_MASK;
11737 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11738 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11739 BGS_APPTAG_ERR_MASK;
11740 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11741 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11742 BGS_REFTAG_ERR_MASK;
11743
11744 /* Check to see if there was any good data before the error */
11745 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11746 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11747 BGS_HI_WATER_MARK_PRESENT_MASK;
11748 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11749 wcqe->total_data_placed;
11750 }
11751
11752 /*
11753 * Set ALL the error bits to indicate we don't know what
11754 * type of error it is.
11755 */
11756 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11757 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11758 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11759 BGS_GUARD_ERR_MASK);
11760 }
11761
James Smart341af102010-01-26 23:07:37 -050011762 /* Pick up HBA exchange busy condition */
11763 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11764 spin_lock_irqsave(&phba->hbalock, iflags);
11765 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11766 spin_unlock_irqrestore(&phba->hbalock, iflags);
11767 }
James Smart4f774512009-05-22 14:52:35 -040011768}
11769
11770/**
James Smart45ed1192009-10-02 15:17:02 -040011771 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11772 * @phba: Pointer to HBA context object.
11773 * @wcqe: Pointer to work-queue completion queue entry.
11774 *
11775 * This routine handles an ELS work-queue completion event and construct
11776 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11777 * discovery engine to handle.
11778 *
11779 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11780 **/
11781static struct lpfc_iocbq *
11782lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11783 struct lpfc_iocbq *irspiocbq)
11784{
11785 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11786 struct lpfc_iocbq *cmdiocbq;
11787 struct lpfc_wcqe_complete *wcqe;
11788 unsigned long iflags;
11789
11790 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011791 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011792 pring->stats.iocb_event++;
11793 /* Look up the ELS command IOCB and create pseudo response IOCB */
11794 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11795 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011796 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011797
11798 if (unlikely(!cmdiocbq)) {
11799 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11800 "0386 ELS complete with no corresponding "
11801 "cmdiocb: iotag (%d)\n",
11802 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11803 lpfc_sli_release_iocbq(phba, irspiocbq);
11804 return NULL;
11805 }
11806
11807 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011808 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011809
11810 return irspiocbq;
11811}
11812
11813/**
James Smart04c68492009-05-22 14:52:52 -040011814 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11815 * @phba: Pointer to HBA context object.
11816 * @cqe: Pointer to mailbox completion queue entry.
11817 *
11818 * This routine process a mailbox completion queue entry with asynchrous
11819 * event.
11820 *
11821 * Return: true if work posted to worker thread, otherwise false.
11822 **/
11823static bool
11824lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11825{
11826 struct lpfc_cq_event *cq_event;
11827 unsigned long iflags;
11828
11829 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11830 "0392 Async Event: word0:x%x, word1:x%x, "
11831 "word2:x%x, word3:x%x\n", mcqe->word0,
11832 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11833
11834 /* Allocate a new internal CQ_EVENT entry */
11835 cq_event = lpfc_sli4_cq_event_alloc(phba);
11836 if (!cq_event) {
11837 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11838 "0394 Failed to allocate CQ_EVENT entry\n");
11839 return false;
11840 }
11841
11842 /* Move the CQE into an asynchronous event entry */
11843 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11844 spin_lock_irqsave(&phba->hbalock, iflags);
11845 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11846 /* Set the async event flag */
11847 phba->hba_flag |= ASYNC_EVENT;
11848 spin_unlock_irqrestore(&phba->hbalock, iflags);
11849
11850 return true;
11851}
11852
11853/**
11854 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11855 * @phba: Pointer to HBA context object.
11856 * @cqe: Pointer to mailbox completion queue entry.
11857 *
11858 * This routine process a mailbox completion queue entry with mailbox
11859 * completion event.
11860 *
11861 * Return: true if work posted to worker thread, otherwise false.
11862 **/
11863static bool
11864lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11865{
11866 uint32_t mcqe_status;
11867 MAILBOX_t *mbox, *pmbox;
11868 struct lpfc_mqe *mqe;
11869 struct lpfc_vport *vport;
11870 struct lpfc_nodelist *ndlp;
11871 struct lpfc_dmabuf *mp;
11872 unsigned long iflags;
11873 LPFC_MBOXQ_t *pmb;
11874 bool workposted = false;
11875 int rc;
11876
11877 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11878 if (!bf_get(lpfc_trailer_completed, mcqe))
11879 goto out_no_mqe_complete;
11880
11881 /* Get the reference to the active mbox command */
11882 spin_lock_irqsave(&phba->hbalock, iflags);
11883 pmb = phba->sli.mbox_active;
11884 if (unlikely(!pmb)) {
11885 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11886 "1832 No pending MBOX command to handle\n");
11887 spin_unlock_irqrestore(&phba->hbalock, iflags);
11888 goto out_no_mqe_complete;
11889 }
11890 spin_unlock_irqrestore(&phba->hbalock, iflags);
11891 mqe = &pmb->u.mqe;
11892 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11893 mbox = phba->mbox;
11894 vport = pmb->vport;
11895
11896 /* Reset heartbeat timer */
11897 phba->last_completion_time = jiffies;
11898 del_timer(&phba->sli.mbox_tmo);
11899
11900 /* Move mbox data to caller's mailbox region, do endian swapping */
11901 if (pmb->mbox_cmpl && mbox)
11902 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011903
James Smart73d91e52011-10-10 21:32:10 -040011904 /*
11905 * For mcqe errors, conditionally move a modified error code to
11906 * the mbox so that the error will not be missed.
11907 */
11908 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11909 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11910 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11911 bf_set(lpfc_mqe_status, mqe,
11912 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11913 }
James Smart04c68492009-05-22 14:52:52 -040011914 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11915 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11916 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11917 "MBOX dflt rpi: status:x%x rpi:x%x",
11918 mcqe_status,
11919 pmbox->un.varWords[0], 0);
11920 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11921 mp = (struct lpfc_dmabuf *)(pmb->context1);
11922 ndlp = (struct lpfc_nodelist *)pmb->context2;
11923 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11924 * RID of the PPI using the same mbox buffer.
11925 */
11926 lpfc_unreg_login(phba, vport->vpi,
11927 pmbox->un.varWords[0], pmb);
11928 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11929 pmb->context1 = mp;
11930 pmb->context2 = ndlp;
11931 pmb->vport = vport;
11932 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11933 if (rc != MBX_BUSY)
11934 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11935 LOG_SLI, "0385 rc should "
11936 "have been MBX_BUSY\n");
11937 if (rc != MBX_NOT_FINISHED)
11938 goto send_current_mbox;
11939 }
11940 }
11941 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11942 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11943 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11944
11945 /* There is mailbox completion work to do */
11946 spin_lock_irqsave(&phba->hbalock, iflags);
11947 __lpfc_mbox_cmpl_put(phba, pmb);
11948 phba->work_ha |= HA_MBATT;
11949 spin_unlock_irqrestore(&phba->hbalock, iflags);
11950 workposted = true;
11951
11952send_current_mbox:
11953 spin_lock_irqsave(&phba->hbalock, iflags);
11954 /* Release the mailbox command posting token */
11955 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11956 /* Setting active mailbox pointer need to be in sync to flag clear */
11957 phba->sli.mbox_active = NULL;
11958 spin_unlock_irqrestore(&phba->hbalock, iflags);
11959 /* Wake up worker thread to post the next pending mailbox command */
11960 lpfc_worker_wake_up(phba);
11961out_no_mqe_complete:
11962 if (bf_get(lpfc_trailer_consumed, mcqe))
11963 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11964 return workposted;
11965}
11966
11967/**
11968 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11969 * @phba: Pointer to HBA context object.
11970 * @cqe: Pointer to mailbox completion queue entry.
11971 *
11972 * This routine process a mailbox completion queue entry, it invokes the
11973 * proper mailbox complete handling or asynchrous event handling routine
11974 * according to the MCQE's async bit.
11975 *
11976 * Return: true if work posted to worker thread, otherwise false.
11977 **/
11978static bool
11979lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11980{
11981 struct lpfc_mcqe mcqe;
11982 bool workposted;
11983
11984 /* Copy the mailbox MCQE and convert endian order as needed */
11985 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11986
11987 /* Invoke the proper event handling routine */
11988 if (!bf_get(lpfc_trailer_async, &mcqe))
11989 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11990 else
11991 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11992 return workposted;
11993}
11994
11995/**
James Smart4f774512009-05-22 14:52:35 -040011996 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11997 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040011998 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040011999 * @wcqe: Pointer to work-queue completion queue entry.
12000 *
12001 * This routine handles an ELS work-queue completion event.
12002 *
12003 * Return: true if work posted to worker thread, otherwise false.
12004 **/
12005static bool
James Smart2a76a282012-08-03 12:35:54 -040012006lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012007 struct lpfc_wcqe_complete *wcqe)
12008{
James Smart4f774512009-05-22 14:52:35 -040012009 struct lpfc_iocbq *irspiocbq;
12010 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012011 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012012 int txq_cnt = 0;
12013 int txcmplq_cnt = 0;
12014 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012015
James Smart45ed1192009-10-02 15:17:02 -040012016 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012017 irspiocbq = lpfc_sli_get_iocbq(phba);
12018 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012019 if (!list_empty(&pring->txq))
12020 txq_cnt++;
12021 if (!list_empty(&pring->txcmplq))
12022 txcmplq_cnt++;
12023 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
12024 fcp_txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012025 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012026 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12027 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012028 txq_cnt, phba->iocb_cnt,
12029 fcp_txcmplq_cnt,
12030 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012031 return false;
James Smart4f774512009-05-22 14:52:35 -040012032 }
James Smart4f774512009-05-22 14:52:35 -040012033
James Smart45ed1192009-10-02 15:17:02 -040012034 /* Save off the slow-path queue event for work thread to process */
12035 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012036 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012037 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012038 &phba->sli4_hba.sp_queue_event);
12039 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012040 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012041
James Smart45ed1192009-10-02 15:17:02 -040012042 return true;
James Smart4f774512009-05-22 14:52:35 -040012043}
12044
12045/**
12046 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12047 * @phba: Pointer to HBA context object.
12048 * @wcqe: Pointer to work-queue completion queue entry.
12049 *
12050 * This routine handles slow-path WQ entry comsumed event by invoking the
12051 * proper WQ release routine to the slow-path WQ.
12052 **/
12053static void
12054lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12055 struct lpfc_wcqe_release *wcqe)
12056{
James Smart2e90f4b2011-12-13 13:22:37 -050012057 /* sanity check on queue memory */
12058 if (unlikely(!phba->sli4_hba.els_wq))
12059 return;
James Smart4f774512009-05-22 14:52:35 -040012060 /* Check for the slow-path ELS work queue */
12061 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12062 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12063 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12064 else
12065 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12066 "2579 Slow-path wqe consume event carries "
12067 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12068 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12069 phba->sli4_hba.els_wq->queue_id);
12070}
12071
12072/**
12073 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12074 * @phba: Pointer to HBA context object.
12075 * @cq: Pointer to a WQ completion queue.
12076 * @wcqe: Pointer to work-queue completion queue entry.
12077 *
12078 * This routine handles an XRI abort event.
12079 *
12080 * Return: true if work posted to worker thread, otherwise false.
12081 **/
12082static bool
12083lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12084 struct lpfc_queue *cq,
12085 struct sli4_wcqe_xri_aborted *wcqe)
12086{
12087 bool workposted = false;
12088 struct lpfc_cq_event *cq_event;
12089 unsigned long iflags;
12090
12091 /* Allocate a new internal CQ_EVENT entry */
12092 cq_event = lpfc_sli4_cq_event_alloc(phba);
12093 if (!cq_event) {
12094 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12095 "0602 Failed to allocate CQ_EVENT entry\n");
12096 return false;
12097 }
12098
12099 /* Move the CQE into the proper xri abort event list */
12100 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12101 switch (cq->subtype) {
12102 case LPFC_FCP:
12103 spin_lock_irqsave(&phba->hbalock, iflags);
12104 list_add_tail(&cq_event->list,
12105 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12106 /* Set the fcp xri abort event flag */
12107 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12108 spin_unlock_irqrestore(&phba->hbalock, iflags);
12109 workposted = true;
12110 break;
12111 case LPFC_ELS:
12112 spin_lock_irqsave(&phba->hbalock, iflags);
12113 list_add_tail(&cq_event->list,
12114 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12115 /* Set the els xri abort event flag */
12116 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12117 spin_unlock_irqrestore(&phba->hbalock, iflags);
12118 workposted = true;
12119 break;
12120 default:
12121 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12122 "0603 Invalid work queue CQE subtype (x%x)\n",
12123 cq->subtype);
12124 workposted = false;
12125 break;
12126 }
12127 return workposted;
12128}
12129
12130/**
James Smart4d9ab992009-10-02 15:16:39 -040012131 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012132 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012133 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012134 *
James Smart4d9ab992009-10-02 15:16:39 -040012135 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012136 *
12137 * Return: true if work posted to worker thread, otherwise false.
12138 **/
12139static bool
James Smart4d9ab992009-10-02 15:16:39 -040012140lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12141{
12142 bool workposted = false;
12143 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12144 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12145 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012146 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012147 unsigned long iflags;
12148
James Smart2e90f4b2011-12-13 13:22:37 -050012149 /* sanity check on queue memory */
12150 if (unlikely(!hrq) || unlikely(!drq))
12151 return workposted;
12152
James Smart7851fe22011-07-22 18:36:52 -040012153 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12154 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12155 else
12156 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12157 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012158 goto out;
12159
12160 status = bf_get(lpfc_rcqe_status, rcqe);
12161 switch (status) {
12162 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12163 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12164 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040012165 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040012166 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012167 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012168 spin_lock_irqsave(&phba->hbalock, iflags);
12169 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12170 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012171 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012172 spin_unlock_irqrestore(&phba->hbalock, iflags);
12173 goto out;
12174 }
James Smartb84daac2012-08-03 12:35:13 -040012175 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012176 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
12177 /* save off the frame for the word thread to process */
12178 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012179 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012180 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012181 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012182 spin_unlock_irqrestore(&phba->hbalock, iflags);
12183 workposted = true;
12184 break;
12185 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12186 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040012187 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012188 /* Post more buffers if possible */
12189 spin_lock_irqsave(&phba->hbalock, iflags);
12190 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12191 spin_unlock_irqrestore(&phba->hbalock, iflags);
12192 workposted = true;
12193 break;
12194 }
12195out:
12196 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012197}
12198
12199/**
12200 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12201 * @phba: Pointer to HBA context object.
12202 * @cq: Pointer to the completion queue.
12203 * @wcqe: Pointer to a completion queue entry.
12204 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012205 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012206 * entry.
12207 *
12208 * Return: true if work posted to worker thread, otherwise false.
12209 **/
12210static bool
12211lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012212 struct lpfc_cqe *cqe)
12213{
James Smart45ed1192009-10-02 15:17:02 -040012214 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012215 bool workposted = false;
12216
12217 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012218 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012219
12220 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012221 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012222 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012223 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012224 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012225 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012226 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012227 break;
12228 case CQE_CODE_RELEASE_WQE:
12229 /* Process the WQ release event */
12230 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012231 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012232 break;
12233 case CQE_CODE_XRI_ABORTED:
12234 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012235 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012236 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012237 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012238 break;
James Smart4d9ab992009-10-02 15:16:39 -040012239 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012240 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012241 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012242 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012243 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012244 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012245 break;
James Smart4f774512009-05-22 14:52:35 -040012246 default:
12247 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12248 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012249 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012250 break;
12251 }
12252 return workposted;
12253}
12254
12255/**
James Smart4f774512009-05-22 14:52:35 -040012256 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12257 * @phba: Pointer to HBA context object.
12258 * @eqe: Pointer to fast-path event queue entry.
12259 *
12260 * This routine process a event queue entry from the slow-path event queue.
12261 * It will check the MajorCode and MinorCode to determine this is for a
12262 * completion event on a completion queue, if not, an error shall be logged
12263 * and just return. Otherwise, it will get to the corresponding completion
12264 * queue and process all the entries on that completion queue, rearm the
12265 * completion queue, and then return.
12266 *
12267 **/
12268static void
James Smart67d12732012-08-03 12:36:13 -040012269lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12270 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040012271{
James Smart67d12732012-08-03 12:36:13 -040012272 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040012273 struct lpfc_cqe *cqe;
12274 bool workposted = false;
12275 int ecount = 0;
12276 uint16_t cqid;
12277
James Smart4f774512009-05-22 14:52:35 -040012278 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040012279 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040012280
James Smart4f774512009-05-22 14:52:35 -040012281 list_for_each_entry(childq, &speq->child_list, list) {
12282 if (childq->queue_id == cqid) {
12283 cq = childq;
12284 break;
12285 }
12286 }
12287 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012288 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12289 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12290 "0365 Slow-path CQ identifier "
12291 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040012292 return;
12293 }
12294
12295 /* Process all the entries to the CQ */
12296 switch (cq->type) {
12297 case LPFC_MCQ:
12298 while ((cqe = lpfc_sli4_cq_get(cq))) {
12299 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012300 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012301 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012302 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040012303 }
12304 break;
12305 case LPFC_WCQ:
12306 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040012307 if (cq->subtype == LPFC_FCP)
12308 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
12309 cqe);
12310 else
12311 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12312 cqe);
James Smart73d91e52011-10-10 21:32:10 -040012313 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012314 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12315 }
James Smartb84daac2012-08-03 12:35:13 -040012316
12317 /* Track the max number of CQEs processed in 1 EQ */
12318 if (ecount > cq->CQ_max_cqe)
12319 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040012320 break;
12321 default:
12322 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12323 "0370 Invalid completion queue type (%d)\n",
12324 cq->type);
12325 return;
12326 }
12327
12328 /* Catch the no cq entry condition, log an error */
12329 if (unlikely(ecount == 0))
12330 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12331 "0371 No entry from the CQ: identifier "
12332 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12333
12334 /* In any case, flash and re-arm the RCQ */
12335 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12336
12337 /* wake up worker thread if there are works to be done */
12338 if (workposted)
12339 lpfc_worker_wake_up(phba);
12340}
12341
12342/**
12343 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040012344 * @phba: Pointer to HBA context object.
12345 * @cq: Pointer to associated CQ
12346 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012347 *
12348 * This routine process a fast-path work queue completion entry from fast-path
12349 * event queue for FCP command response completion.
12350 **/
12351static void
James Smart2a76a282012-08-03 12:35:54 -040012352lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012353 struct lpfc_wcqe_complete *wcqe)
12354{
James Smart2a76a282012-08-03 12:35:54 -040012355 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040012356 struct lpfc_iocbq *cmdiocbq;
12357 struct lpfc_iocbq irspiocbq;
12358 unsigned long iflags;
12359
James Smart4f774512009-05-22 14:52:35 -040012360 /* Check for response status */
12361 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12362 /* If resource errors reported from HBA, reduce queue
12363 * depth of the SCSI device.
12364 */
James Smarte3d2b802012-08-14 14:25:43 -040012365 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12366 IOSTAT_LOCAL_REJECT)) &&
12367 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12368 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040012369 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040012370
James Smart4f774512009-05-22 14:52:35 -040012371 /* Log the error status */
12372 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12373 "0373 FCP complete error: status=x%x, "
12374 "hw_status=x%x, total_data_specified=%d, "
12375 "parameter=x%x, word3=x%x\n",
12376 bf_get(lpfc_wcqe_c_status, wcqe),
12377 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12378 wcqe->total_data_placed, wcqe->parameter,
12379 wcqe->word3);
12380 }
12381
12382 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040012383 spin_lock_irqsave(&pring->ring_lock, iflags);
12384 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040012385 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12386 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040012387 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012388 if (unlikely(!cmdiocbq)) {
12389 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12390 "0374 FCP complete with no corresponding "
12391 "cmdiocb: iotag (%d)\n",
12392 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12393 return;
12394 }
12395 if (unlikely(!cmdiocbq->iocb_cmpl)) {
12396 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12397 "0375 FCP cmdiocb not callback function "
12398 "iotag: (%d)\n",
12399 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12400 return;
12401 }
12402
12403 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012404 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040012405
James Smart0f65ff62010-02-26 14:14:23 -050012406 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12407 spin_lock_irqsave(&phba->hbalock, iflags);
12408 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12409 spin_unlock_irqrestore(&phba->hbalock, iflags);
12410 }
12411
James Smart4f774512009-05-22 14:52:35 -040012412 /* Pass the cmd_iocb and the rsp state to the upper layer */
12413 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12414}
12415
12416/**
12417 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12418 * @phba: Pointer to HBA context object.
12419 * @cq: Pointer to completion queue.
12420 * @wcqe: Pointer to work-queue completion queue entry.
12421 *
12422 * This routine handles an fast-path WQ entry comsumed event by invoking the
12423 * proper WQ release routine to the slow-path WQ.
12424 **/
12425static void
12426lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12427 struct lpfc_wcqe_release *wcqe)
12428{
12429 struct lpfc_queue *childwq;
12430 bool wqid_matched = false;
12431 uint16_t fcp_wqid;
12432
12433 /* Check for fast-path FCP work queue release */
12434 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
12435 list_for_each_entry(childwq, &cq->child_list, list) {
12436 if (childwq->queue_id == fcp_wqid) {
12437 lpfc_sli4_wq_release(childwq,
12438 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12439 wqid_matched = true;
12440 break;
12441 }
12442 }
12443 /* Report warning log message if no match found */
12444 if (wqid_matched != true)
12445 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12446 "2580 Fast-path wqe consume event carries "
12447 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
12448}
12449
12450/**
12451 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
12452 * @cq: Pointer to the completion queue.
12453 * @eqe: Pointer to fast-path completion queue entry.
12454 *
12455 * This routine process a fast-path work queue completion entry from fast-path
12456 * event queue for FCP command response completion.
12457 **/
12458static int
12459lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12460 struct lpfc_cqe *cqe)
12461{
12462 struct lpfc_wcqe_release wcqe;
12463 bool workposted = false;
12464
12465 /* Copy the work queue CQE and convert endian order if needed */
12466 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12467
12468 /* Check and process for different type of WCQE and dispatch */
12469 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12470 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012471 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040012472 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040012473 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012474 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040012475 (struct lpfc_wcqe_complete *)&wcqe);
12476 break;
12477 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012478 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040012479 /* Process the WQ release event */
12480 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12481 (struct lpfc_wcqe_release *)&wcqe);
12482 break;
12483 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040012484 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040012485 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012486 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012487 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12488 (struct sli4_wcqe_xri_aborted *)&wcqe);
12489 break;
12490 default:
12491 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12492 "0144 Not a valid WCQE code: x%x\n",
12493 bf_get(lpfc_wcqe_c_code, &wcqe));
12494 break;
12495 }
12496 return workposted;
12497}
12498
12499/**
James Smart67d12732012-08-03 12:36:13 -040012500 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040012501 * @phba: Pointer to HBA context object.
12502 * @eqe: Pointer to fast-path event queue entry.
12503 *
12504 * This routine process a event queue entry from the fast-path event queue.
12505 * It will check the MajorCode and MinorCode to determine this is for a
12506 * completion event on a completion queue, if not, an error shall be logged
12507 * and just return. Otherwise, it will get to the corresponding completion
12508 * queue and process all the entries on the completion queue, rearm the
12509 * completion queue, and then return.
12510 **/
12511static void
James Smart67d12732012-08-03 12:36:13 -040012512lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12513 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040012514{
12515 struct lpfc_queue *cq;
12516 struct lpfc_cqe *cqe;
12517 bool workposted = false;
12518 uint16_t cqid;
12519 int ecount = 0;
12520
James Smartcb5172e2010-03-15 11:25:07 -040012521 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040012522 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040012523 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040012524 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040012525 bf_get_le32(lpfc_eqe_major_code, eqe),
12526 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040012527 return;
12528 }
12529
James Smart67d12732012-08-03 12:36:13 -040012530 /* Get the reference to the corresponding CQ */
12531 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12532
12533 /* Check if this is a Slow path event */
12534 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12535 lpfc_sli4_sp_handle_eqe(phba, eqe,
12536 phba->sli4_hba.hba_eq[qidx]);
12537 return;
12538 }
12539
James Smart2e90f4b2011-12-13 13:22:37 -050012540 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12541 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12542 "3146 Fast-path completion queues "
12543 "does not exist\n");
12544 return;
12545 }
James Smart67d12732012-08-03 12:36:13 -040012546 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040012547 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012548 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12549 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12550 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040012551 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040012552 return;
12553 }
12554
James Smart4f774512009-05-22 14:52:35 -040012555 if (unlikely(cqid != cq->queue_id)) {
12556 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12557 "0368 Miss-matched fast-path completion "
12558 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12559 cqid, cq->queue_id);
12560 return;
12561 }
12562
12563 /* Process all the entries to the CQ */
12564 while ((cqe = lpfc_sli4_cq_get(cq))) {
12565 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012566 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012567 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12568 }
12569
James Smartb84daac2012-08-03 12:35:13 -040012570 /* Track the max number of CQEs processed in 1 EQ */
12571 if (ecount > cq->CQ_max_cqe)
12572 cq->CQ_max_cqe = ecount;
12573
James Smart4f774512009-05-22 14:52:35 -040012574 /* Catch the no cq entry condition */
12575 if (unlikely(ecount == 0))
12576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12577 "0369 No entry from fast-path completion "
12578 "queue fcpcqid=%d\n", cq->queue_id);
12579
12580 /* In any case, flash and re-arm the CQ */
12581 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12582
12583 /* wake up worker thread if there are works to be done */
12584 if (workposted)
12585 lpfc_worker_wake_up(phba);
12586}
12587
12588static void
12589lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12590{
12591 struct lpfc_eqe *eqe;
12592
12593 /* walk all the EQ entries and drop on the floor */
12594 while ((eqe = lpfc_sli4_eq_get(eq)))
12595 ;
12596
12597 /* Clear and re-arm the EQ */
12598 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12599}
12600
James Smart1ba981f2014-02-20 09:56:45 -050012601
12602/**
12603 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
12604 * entry
12605 * @phba: Pointer to HBA context object.
12606 * @eqe: Pointer to fast-path event queue entry.
12607 *
12608 * This routine process a event queue entry from the Flash Optimized Fabric
12609 * event queue. It will check the MajorCode and MinorCode to determine this
12610 * is for a completion event on a completion queue, if not, an error shall be
12611 * logged and just return. Otherwise, it will get to the corresponding
12612 * completion queue and process all the entries on the completion queue, rearm
12613 * the completion queue, and then return.
12614 **/
12615static void
12616lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
12617{
12618 struct lpfc_queue *cq;
12619 struct lpfc_cqe *cqe;
12620 bool workposted = false;
12621 uint16_t cqid;
12622 int ecount = 0;
12623
12624 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12625 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12626 "9147 Not a valid completion "
12627 "event: majorcode=x%x, minorcode=x%x\n",
12628 bf_get_le32(lpfc_eqe_major_code, eqe),
12629 bf_get_le32(lpfc_eqe_minor_code, eqe));
12630 return;
12631 }
12632
12633 /* Get the reference to the corresponding CQ */
12634 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12635
12636 /* Next check for OAS */
12637 cq = phba->sli4_hba.oas_cq;
12638 if (unlikely(!cq)) {
12639 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12640 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12641 "9148 OAS completion queue "
12642 "does not exist\n");
12643 return;
12644 }
12645
12646 if (unlikely(cqid != cq->queue_id)) {
12647 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12648 "9149 Miss-matched fast-path compl "
12649 "queue id: eqcqid=%d, fcpcqid=%d\n",
12650 cqid, cq->queue_id);
12651 return;
12652 }
12653
12654 /* Process all the entries to the OAS CQ */
12655 while ((cqe = lpfc_sli4_cq_get(cq))) {
12656 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12657 if (!(++ecount % cq->entry_repost))
12658 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12659 }
12660
12661 /* Track the max number of CQEs processed in 1 EQ */
12662 if (ecount > cq->CQ_max_cqe)
12663 cq->CQ_max_cqe = ecount;
12664
12665 /* Catch the no cq entry condition */
12666 if (unlikely(ecount == 0))
12667 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12668 "9153 No entry from fast-path completion "
12669 "queue fcpcqid=%d\n", cq->queue_id);
12670
12671 /* In any case, flash and re-arm the CQ */
12672 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12673
12674 /* wake up worker thread if there are works to be done */
12675 if (workposted)
12676 lpfc_worker_wake_up(phba);
12677}
12678
12679/**
12680 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
12681 * @irq: Interrupt number.
12682 * @dev_id: The device context pointer.
12683 *
12684 * This function is directly called from the PCI layer as an interrupt
12685 * service routine when device with SLI-4 interface spec is enabled with
12686 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
12687 * IOCB ring event in the HBA. However, when the device is enabled with either
12688 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12689 * device-level interrupt handler. When the PCI slot is in error recovery
12690 * or the HBA is undergoing initialization, the interrupt handler will not
12691 * process the interrupt. The Flash Optimized Fabric ring event are handled in
12692 * the intrrupt context. This function is called without any lock held.
12693 * It gets the hbalock to access and update SLI data structures. Note that,
12694 * the EQ to CQ are one-to-one map such that the EQ index is
12695 * equal to that of CQ index.
12696 *
12697 * This function returns IRQ_HANDLED when interrupt is handled else it
12698 * returns IRQ_NONE.
12699 **/
12700irqreturn_t
12701lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
12702{
12703 struct lpfc_hba *phba;
12704 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12705 struct lpfc_queue *eq;
12706 struct lpfc_eqe *eqe;
12707 unsigned long iflag;
12708 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050012709
12710 /* Get the driver's phba structure from the dev_id */
12711 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12712 phba = fcp_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050012713
12714 if (unlikely(!phba))
12715 return IRQ_NONE;
12716
12717 /* Get to the EQ struct associated with this vector */
12718 eq = phba->sli4_hba.fof_eq;
12719 if (unlikely(!eq))
12720 return IRQ_NONE;
12721
12722 /* Check device state for handling interrupt */
12723 if (unlikely(lpfc_intr_state_check(phba))) {
12724 eq->EQ_badstate++;
12725 /* Check again for link_state with lock held */
12726 spin_lock_irqsave(&phba->hbalock, iflag);
12727 if (phba->link_state < LPFC_LINK_DOWN)
12728 /* Flush, clear interrupt, and rearm the EQ */
12729 lpfc_sli4_eq_flush(phba, eq);
12730 spin_unlock_irqrestore(&phba->hbalock, iflag);
12731 return IRQ_NONE;
12732 }
12733
12734 /*
12735 * Process all the event on FCP fast-path EQ
12736 */
12737 while ((eqe = lpfc_sli4_eq_get(eq))) {
12738 lpfc_sli4_fof_handle_eqe(phba, eqe);
12739 if (!(++ecount % eq->entry_repost))
12740 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
12741 eq->EQ_processed++;
12742 }
12743
12744 /* Track the max number of EQEs processed in 1 intr */
12745 if (ecount > eq->EQ_max_eqe)
12746 eq->EQ_max_eqe = ecount;
12747
12748
12749 if (unlikely(ecount == 0)) {
12750 eq->EQ_no_entry++;
12751
12752 if (phba->intr_type == MSIX)
12753 /* MSI-X treated interrupt served as no EQ share INT */
12754 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12755 "9145 MSI-X interrupt with no EQE\n");
12756 else {
12757 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12758 "9146 ISR interrupt with no EQE\n");
12759 /* Non MSI-X treated on interrupt as EQ share INT */
12760 return IRQ_NONE;
12761 }
12762 }
12763 /* Always clear and re-arm the fast-path EQ */
12764 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12765 return IRQ_HANDLED;
12766}
12767
James Smart4f774512009-05-22 14:52:35 -040012768/**
James Smart67d12732012-08-03 12:36:13 -040012769 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040012770 * @irq: Interrupt number.
12771 * @dev_id: The device context pointer.
12772 *
12773 * This function is directly called from the PCI layer as an interrupt
12774 * service routine when device with SLI-4 interface spec is enabled with
12775 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12776 * ring event in the HBA. However, when the device is enabled with either
12777 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12778 * device-level interrupt handler. When the PCI slot is in error recovery
12779 * or the HBA is undergoing initialization, the interrupt handler will not
12780 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12781 * the intrrupt context. This function is called without any lock held.
12782 * It gets the hbalock to access and update SLI data structures. Note that,
12783 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12784 * equal to that of FCP CQ index.
12785 *
James Smart67d12732012-08-03 12:36:13 -040012786 * The link attention and ELS ring attention events are handled
12787 * by the worker thread. The interrupt handler signals the worker thread
12788 * and returns for these events. This function is called without any lock
12789 * held. It gets the hbalock to access and update SLI data structures.
12790 *
James Smart4f774512009-05-22 14:52:35 -040012791 * This function returns IRQ_HANDLED when interrupt is handled else it
12792 * returns IRQ_NONE.
12793 **/
12794irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040012795lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040012796{
12797 struct lpfc_hba *phba;
12798 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12799 struct lpfc_queue *fpeq;
12800 struct lpfc_eqe *eqe;
12801 unsigned long iflag;
12802 int ecount = 0;
James Smart962bc512013-01-03 15:44:00 -050012803 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012804
12805 /* Get the driver's phba structure from the dev_id */
12806 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12807 phba = fcp_eq_hdl->phba;
12808 fcp_eqidx = fcp_eq_hdl->idx;
12809
12810 if (unlikely(!phba))
12811 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040012812 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040012813 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012814
12815 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040012816 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050012817 if (unlikely(!fpeq))
12818 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012819
James Smartba20c852012-08-03 12:36:52 -040012820 if (lpfc_fcp_look_ahead) {
12821 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12822 lpfc_sli4_eq_clr_intr(fpeq);
12823 else {
12824 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12825 return IRQ_NONE;
12826 }
12827 }
12828
James Smart4f774512009-05-22 14:52:35 -040012829 /* Check device state for handling interrupt */
12830 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040012831 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040012832 /* Check again for link_state with lock held */
12833 spin_lock_irqsave(&phba->hbalock, iflag);
12834 if (phba->link_state < LPFC_LINK_DOWN)
12835 /* Flush, clear interrupt, and rearm the EQ */
12836 lpfc_sli4_eq_flush(phba, fpeq);
12837 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040012838 if (lpfc_fcp_look_ahead)
12839 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012840 return IRQ_NONE;
12841 }
12842
12843 /*
12844 * Process all the event on FCP fast-path EQ
12845 */
12846 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040012847 if (eqe == NULL)
12848 break;
12849
James Smart67d12732012-08-03 12:36:13 -040012850 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040012851 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012852 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012853 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040012854 }
12855
James Smartb84daac2012-08-03 12:35:13 -040012856 /* Track the max number of EQEs processed in 1 intr */
12857 if (ecount > fpeq->EQ_max_eqe)
12858 fpeq->EQ_max_eqe = ecount;
12859
James Smart4f774512009-05-22 14:52:35 -040012860 /* Always clear and re-arm the fast-path EQ */
12861 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12862
12863 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040012864 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040012865
12866 if (lpfc_fcp_look_ahead) {
12867 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12868 return IRQ_NONE;
12869 }
12870
James Smart4f774512009-05-22 14:52:35 -040012871 if (phba->intr_type == MSIX)
12872 /* MSI-X treated interrupt served as no EQ share INT */
12873 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12874 "0358 MSI-X interrupt with no EQE\n");
12875 else
12876 /* Non MSI-X treated on interrupt as EQ share INT */
12877 return IRQ_NONE;
12878 }
12879
James Smartba20c852012-08-03 12:36:52 -040012880 if (lpfc_fcp_look_ahead)
12881 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012882 return IRQ_HANDLED;
12883} /* lpfc_sli4_fp_intr_handler */
12884
12885/**
12886 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12887 * @irq: Interrupt number.
12888 * @dev_id: The device context pointer.
12889 *
12890 * This function is the device-level interrupt handler to device with SLI-4
12891 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12892 * interrupt mode is enabled and there is an event in the HBA which requires
12893 * driver attention. This function invokes the slow-path interrupt attention
12894 * handling function and fast-path interrupt attention handling function in
12895 * turn to process the relevant HBA attention events. This function is called
12896 * without any lock held. It gets the hbalock to access and update SLI data
12897 * structures.
12898 *
12899 * This function returns IRQ_HANDLED when interrupt is handled, else it
12900 * returns IRQ_NONE.
12901 **/
12902irqreturn_t
12903lpfc_sli4_intr_handler(int irq, void *dev_id)
12904{
12905 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012906 irqreturn_t hba_irq_rc;
12907 bool hba_handled = false;
James Smart962bc512013-01-03 15:44:00 -050012908 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012909
12910 /* Get the driver's phba structure from the dev_id */
12911 phba = (struct lpfc_hba *)dev_id;
12912
12913 if (unlikely(!phba))
12914 return IRQ_NONE;
12915
12916 /*
James Smart4f774512009-05-22 14:52:35 -040012917 * Invoke fast-path host attention interrupt handling as appropriate.
12918 */
James Smart67d12732012-08-03 12:36:13 -040012919 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12920 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012921 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012922 if (hba_irq_rc == IRQ_HANDLED)
12923 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012924 }
12925
James Smart1ba981f2014-02-20 09:56:45 -050012926 if (phba->cfg_fof) {
12927 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
12928 &phba->sli4_hba.fcp_eq_hdl[0]);
12929 if (hba_irq_rc == IRQ_HANDLED)
12930 hba_handled |= true;
12931 }
12932
James Smart67d12732012-08-03 12:36:13 -040012933 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012934} /* lpfc_sli4_intr_handler */
12935
12936/**
12937 * lpfc_sli4_queue_free - free a queue structure and associated memory
12938 * @queue: The queue structure to free.
12939 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012940 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012941 * the host resident queue. This function must be called after destroying the
12942 * queue on the HBA.
12943 **/
12944void
12945lpfc_sli4_queue_free(struct lpfc_queue *queue)
12946{
12947 struct lpfc_dmabuf *dmabuf;
12948
12949 if (!queue)
12950 return;
12951
12952 while (!list_empty(&queue->page_list)) {
12953 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12954 list);
James Smart49198b32010-04-06 15:04:33 -040012955 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012956 dmabuf->virt, dmabuf->phys);
12957 kfree(dmabuf);
12958 }
12959 kfree(queue);
12960 return;
12961}
12962
12963/**
12964 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12965 * @phba: The HBA that this queue is being created on.
12966 * @entry_size: The size of each queue entry for this queue.
12967 * @entry count: The number of entries that this queue will handle.
12968 *
12969 * This function allocates a queue structure and the DMAable memory used for
12970 * the host resident queue. This function must be called before creating the
12971 * queue on the HBA.
12972 **/
12973struct lpfc_queue *
12974lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12975 uint32_t entry_count)
12976{
12977 struct lpfc_queue *queue;
12978 struct lpfc_dmabuf *dmabuf;
12979 int x, total_qe_count;
12980 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012981 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012982
James Smartcb5172e2010-03-15 11:25:07 -040012983 if (!phba->sli4_hba.pc_sli4_params.supported)
12984 hw_page_size = SLI4_PAGE_SIZE;
12985
James Smart4f774512009-05-22 14:52:35 -040012986 queue = kzalloc(sizeof(struct lpfc_queue) +
12987 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12988 if (!queue)
12989 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040012990 queue->page_count = (ALIGN(entry_size * entry_count,
12991 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040012992 INIT_LIST_HEAD(&queue->list);
12993 INIT_LIST_HEAD(&queue->page_list);
12994 INIT_LIST_HEAD(&queue->child_list);
12995 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12996 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12997 if (!dmabuf)
12998 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040012999 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13000 hw_page_size, &dmabuf->phys,
13001 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013002 if (!dmabuf->virt) {
13003 kfree(dmabuf);
13004 goto out_fail;
13005 }
13006 dmabuf->buffer_tag = x;
13007 list_add_tail(&dmabuf->list, &queue->page_list);
13008 /* initialize queue's entry array */
13009 dma_pointer = dmabuf->virt;
13010 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040013011 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040013012 total_qe_count++, dma_pointer += entry_size) {
13013 queue->qe[total_qe_count].address = dma_pointer;
13014 }
13015 }
13016 queue->entry_size = entry_size;
13017 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040013018
13019 /*
13020 * entry_repost is calculated based on the number of entries in the
13021 * queue. This works out except for RQs. If buffers are NOT initially
13022 * posted for every RQE, entry_repost should be adjusted accordingly.
13023 */
13024 queue->entry_repost = (entry_count >> 3);
13025 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13026 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040013027 queue->phba = phba;
13028
13029 return queue;
13030out_fail:
13031 lpfc_sli4_queue_free(queue);
13032 return NULL;
13033}
13034
13035/**
James Smart962bc512013-01-03 15:44:00 -050013036 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13037 * @phba: HBA structure that indicates port to create a queue on.
13038 * @pci_barset: PCI BAR set flag.
13039 *
13040 * This function shall perform iomap of the specified PCI BAR address to host
13041 * memory address if not already done so and return it. The returned host
13042 * memory address can be NULL.
13043 */
13044static void __iomem *
13045lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13046{
James Smart962bc512013-01-03 15:44:00 -050013047 if (!phba->pcidev)
13048 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013049
13050 switch (pci_barset) {
13051 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013052 return phba->pci_bar0_memmap_p;
13053 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013054 return phba->pci_bar2_memmap_p;
13055 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013056 return phba->pci_bar4_memmap_p;
13057 default:
13058 break;
13059 }
13060 return NULL;
13061}
13062
13063/**
James Smart173edbb2012-06-12 13:54:50 -040013064 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
13065 * @phba: HBA structure that indicates port to create a queue on.
13066 * @startq: The starting FCP EQ to modify
13067 *
13068 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13069 *
13070 * The @phba struct is used to send mailbox command to HBA. The @startq
13071 * is used to get the starting FCP EQ to change.
13072 * This function is asynchronous and will wait for the mailbox
13073 * command to finish before continuing.
13074 *
13075 * On success this function will return a zero. If unable to allocate enough
13076 * memory this function will return -ENOMEM. If the queue create mailbox command
13077 * fails this function will return -ENXIO.
13078 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013079int
James Smart2c9c5a02015-04-07 15:07:15 -040013080lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013081{
13082 struct lpfc_mbx_modify_eq_delay *eq_delay;
13083 LPFC_MBOXQ_t *mbox;
13084 struct lpfc_queue *eq;
13085 int cnt, rc, length, status = 0;
13086 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013087 uint32_t result;
James Smart173edbb2012-06-12 13:54:50 -040013088 int fcp_eqidx;
13089 union lpfc_sli4_cfg_shdr *shdr;
13090 uint16_t dmult;
13091
James Smart67d12732012-08-03 12:36:13 -040013092 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040013093 return 0;
13094
13095 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13096 if (!mbox)
13097 return -ENOMEM;
13098 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13099 sizeof(struct lpfc_sli4_cfg_mhdr));
13100 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13101 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13102 length, LPFC_SLI4_MBX_EMBED);
13103 eq_delay = &mbox->u.mqe.un.eq_delay;
13104
13105 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040013106 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
13107 if (result > LPFC_DMULT_CONST)
13108 dmult = 0;
13109 else
13110 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040013111
13112 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040013113 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040013114 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040013115 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040013116 if (!eq)
13117 continue;
13118 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13119 eq_delay->u.request.eq[cnt].phase = 0;
13120 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13121 cnt++;
13122 if (cnt >= LPFC_MAX_EQ_DELAY)
13123 break;
13124 }
13125 eq_delay->u.request.num_eq = cnt;
13126
13127 mbox->vport = phba->pport;
13128 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13129 mbox->context1 = NULL;
13130 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13131 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13132 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13133 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13134 if (shdr_status || shdr_add_status || rc) {
13135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13136 "2512 MODIFY_EQ_DELAY mailbox failed with "
13137 "status x%x add_status x%x, mbx status x%x\n",
13138 shdr_status, shdr_add_status, rc);
13139 status = -ENXIO;
13140 }
13141 mempool_free(mbox, phba->mbox_mem_pool);
13142 return status;
13143}
13144
13145/**
James Smart4f774512009-05-22 14:52:35 -040013146 * lpfc_eq_create - Create an Event Queue on the HBA
13147 * @phba: HBA structure that indicates port to create a queue on.
13148 * @eq: The queue structure to use to create the event queue.
13149 * @imax: The maximum interrupt per second limit.
13150 *
13151 * This function creates an event queue, as detailed in @eq, on a port,
13152 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13153 *
13154 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13155 * is used to get the entry count and entry size that are necessary to
13156 * determine the number of pages to allocate and use for this queue. This
13157 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13158 * event queue. This function is asynchronous and will wait for the mailbox
13159 * command to finish before continuing.
13160 *
13161 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013162 * memory this function will return -ENOMEM. If the queue create mailbox command
13163 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013164 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013165int
James Smartee020062012-09-29 11:28:52 -040013166lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040013167{
13168 struct lpfc_mbx_eq_create *eq_create;
13169 LPFC_MBOXQ_t *mbox;
13170 int rc, length, status = 0;
13171 struct lpfc_dmabuf *dmabuf;
13172 uint32_t shdr_status, shdr_add_status;
13173 union lpfc_sli4_cfg_shdr *shdr;
13174 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040013175 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13176
James Smart2e90f4b2011-12-13 13:22:37 -050013177 /* sanity check on queue memory */
13178 if (!eq)
13179 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013180 if (!phba->sli4_hba.pc_sli4_params.supported)
13181 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013182
13183 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13184 if (!mbox)
13185 return -ENOMEM;
13186 length = (sizeof(struct lpfc_mbx_eq_create) -
13187 sizeof(struct lpfc_sli4_cfg_mhdr));
13188 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13189 LPFC_MBOX_OPCODE_EQ_CREATE,
13190 length, LPFC_SLI4_MBX_EMBED);
13191 eq_create = &mbox->u.mqe.un.eq_create;
13192 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13193 eq->page_count);
13194 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13195 LPFC_EQE_SIZE);
13196 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040013197 /* don't setup delay multiplier using EQ_CREATE */
13198 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040013199 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13200 dmult);
13201 switch (eq->entry_count) {
13202 default:
13203 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13204 "0360 Unsupported EQ count. (%d)\n",
13205 eq->entry_count);
13206 if (eq->entry_count < 256)
13207 return -EINVAL;
13208 /* otherwise default to smallest count (drop through) */
13209 case 256:
13210 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13211 LPFC_EQ_CNT_256);
13212 break;
13213 case 512:
13214 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13215 LPFC_EQ_CNT_512);
13216 break;
13217 case 1024:
13218 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13219 LPFC_EQ_CNT_1024);
13220 break;
13221 case 2048:
13222 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13223 LPFC_EQ_CNT_2048);
13224 break;
13225 case 4096:
13226 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13227 LPFC_EQ_CNT_4096);
13228 break;
13229 }
13230 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013231 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013232 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13233 putPaddrLow(dmabuf->phys);
13234 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13235 putPaddrHigh(dmabuf->phys);
13236 }
13237 mbox->vport = phba->pport;
13238 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13239 mbox->context1 = NULL;
13240 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13241 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13242 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13243 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13244 if (shdr_status || shdr_add_status || rc) {
13245 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13246 "2500 EQ_CREATE mailbox failed with "
13247 "status x%x add_status x%x, mbx status x%x\n",
13248 shdr_status, shdr_add_status, rc);
13249 status = -ENXIO;
13250 }
13251 eq->type = LPFC_EQ;
13252 eq->subtype = LPFC_NONE;
13253 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13254 if (eq->queue_id == 0xFFFF)
13255 status = -ENXIO;
13256 eq->host_index = 0;
13257 eq->hba_index = 0;
13258
James Smart8fa38512009-07-19 10:01:03 -040013259 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013260 return status;
13261}
13262
13263/**
13264 * lpfc_cq_create - Create a Completion Queue on the HBA
13265 * @phba: HBA structure that indicates port to create a queue on.
13266 * @cq: The queue structure to use to create the completion queue.
13267 * @eq: The event queue to bind this completion queue to.
13268 *
13269 * This function creates a completion queue, as detailed in @wq, on a port,
13270 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13271 *
13272 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13273 * is used to get the entry count and entry size that are necessary to
13274 * determine the number of pages to allocate and use for this queue. The @eq
13275 * is used to indicate which event queue to bind this completion queue to. This
13276 * function will send the CQ_CREATE mailbox command to the HBA to setup the
13277 * completion queue. This function is asynchronous and will wait for the mailbox
13278 * command to finish before continuing.
13279 *
13280 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013281 * memory this function will return -ENOMEM. If the queue create mailbox command
13282 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013283 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013284int
James Smart4f774512009-05-22 14:52:35 -040013285lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13286 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13287{
13288 struct lpfc_mbx_cq_create *cq_create;
13289 struct lpfc_dmabuf *dmabuf;
13290 LPFC_MBOXQ_t *mbox;
13291 int rc, length, status = 0;
13292 uint32_t shdr_status, shdr_add_status;
13293 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013294 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13295
James Smart2e90f4b2011-12-13 13:22:37 -050013296 /* sanity check on queue memory */
13297 if (!cq || !eq)
13298 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013299 if (!phba->sli4_hba.pc_sli4_params.supported)
13300 hw_page_size = SLI4_PAGE_SIZE;
13301
James Smart4f774512009-05-22 14:52:35 -040013302 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13303 if (!mbox)
13304 return -ENOMEM;
13305 length = (sizeof(struct lpfc_mbx_cq_create) -
13306 sizeof(struct lpfc_sli4_cfg_mhdr));
13307 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13308 LPFC_MBOX_OPCODE_CQ_CREATE,
13309 length, LPFC_SLI4_MBX_EMBED);
13310 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013311 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013312 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13313 cq->page_count);
13314 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13315 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013316 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13317 phba->sli4_hba.pc_sli4_params.cqv);
13318 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040013319 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13320 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013321 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13322 eq->queue_id);
13323 } else {
13324 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13325 eq->queue_id);
13326 }
James Smart4f774512009-05-22 14:52:35 -040013327 switch (cq->entry_count) {
13328 default:
13329 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13330 "0361 Unsupported CQ count. (%d)\n",
13331 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013332 if (cq->entry_count < 256) {
13333 status = -EINVAL;
13334 goto out;
13335 }
James Smart4f774512009-05-22 14:52:35 -040013336 /* otherwise default to smallest count (drop through) */
13337 case 256:
13338 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13339 LPFC_CQ_CNT_256);
13340 break;
13341 case 512:
13342 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13343 LPFC_CQ_CNT_512);
13344 break;
13345 case 1024:
13346 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13347 LPFC_CQ_CNT_1024);
13348 break;
13349 }
13350 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013351 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013352 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13353 putPaddrLow(dmabuf->phys);
13354 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13355 putPaddrHigh(dmabuf->phys);
13356 }
13357 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13358
13359 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013360 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13361 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13362 if (shdr_status || shdr_add_status || rc) {
13363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13364 "2501 CQ_CREATE mailbox failed with "
13365 "status x%x add_status x%x, mbx status x%x\n",
13366 shdr_status, shdr_add_status, rc);
13367 status = -ENXIO;
13368 goto out;
13369 }
13370 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13371 if (cq->queue_id == 0xFFFF) {
13372 status = -ENXIO;
13373 goto out;
13374 }
13375 /* link the cq onto the parent eq child list */
13376 list_add_tail(&cq->list, &eq->child_list);
13377 /* Set up completion queue's type and subtype */
13378 cq->type = type;
13379 cq->subtype = subtype;
13380 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050013381 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013382 cq->host_index = 0;
13383 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040013384
James Smart8fa38512009-07-19 10:01:03 -040013385out:
13386 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013387 return status;
13388}
13389
13390/**
James Smartb19a0612010-04-06 14:48:51 -040013391 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040013392 * @phba: HBA structure that indicates port to create a queue on.
13393 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040013394 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13395 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040013396 *
James Smartb19a0612010-04-06 14:48:51 -040013397 * This function provides failback (fb) functionality when the
13398 * mq_create_ext fails on older FW generations. It's purpose is identical
13399 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040013400 *
James Smartb19a0612010-04-06 14:48:51 -040013401 * This routine cannot fail as all attributes were previously accessed and
13402 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040013403 **/
James Smartb19a0612010-04-06 14:48:51 -040013404static void
13405lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13406 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040013407{
13408 struct lpfc_mbx_mq_create *mq_create;
13409 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040013410 int length;
James Smart04c68492009-05-22 14:52:52 -040013411
James Smart04c68492009-05-22 14:52:52 -040013412 length = (sizeof(struct lpfc_mbx_mq_create) -
13413 sizeof(struct lpfc_sli4_cfg_mhdr));
13414 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13415 LPFC_MBOX_OPCODE_MQ_CREATE,
13416 length, LPFC_SLI4_MBX_EMBED);
13417 mq_create = &mbox->u.mqe.un.mq_create;
13418 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040013419 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040013420 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040013421 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040013422 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13423 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040013424 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013425 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13426 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040013427 break;
13428 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013429 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13430 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040013431 break;
13432 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013433 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13434 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040013435 break;
13436 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013437 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13438 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040013439 break;
13440 }
13441 list_for_each_entry(dmabuf, &mq->page_list, list) {
13442 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040013443 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040013444 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040013445 putPaddrHigh(dmabuf->phys);
13446 }
13447}
13448
13449/**
13450 * lpfc_mq_create - Create a mailbox Queue on the HBA
13451 * @phba: HBA structure that indicates port to create a queue on.
13452 * @mq: The queue structure to use to create the mailbox queue.
13453 * @cq: The completion queue to associate with this cq.
13454 * @subtype: The queue's subtype.
13455 *
13456 * This function creates a mailbox queue, as detailed in @mq, on a port,
13457 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13458 *
13459 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13460 * is used to get the entry count and entry size that are necessary to
13461 * determine the number of pages to allocate and use for this queue. This
13462 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13463 * mailbox queue. This function is asynchronous and will wait for the mailbox
13464 * command to finish before continuing.
13465 *
13466 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013467 * memory this function will return -ENOMEM. If the queue create mailbox command
13468 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040013469 **/
13470int32_t
13471lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13472 struct lpfc_queue *cq, uint32_t subtype)
13473{
13474 struct lpfc_mbx_mq_create *mq_create;
13475 struct lpfc_mbx_mq_create_ext *mq_create_ext;
13476 struct lpfc_dmabuf *dmabuf;
13477 LPFC_MBOXQ_t *mbox;
13478 int rc, length, status = 0;
13479 uint32_t shdr_status, shdr_add_status;
13480 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013481 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040013482
James Smart2e90f4b2011-12-13 13:22:37 -050013483 /* sanity check on queue memory */
13484 if (!mq || !cq)
13485 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013486 if (!phba->sli4_hba.pc_sli4_params.supported)
13487 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040013488
13489 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13490 if (!mbox)
13491 return -ENOMEM;
13492 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13493 sizeof(struct lpfc_sli4_cfg_mhdr));
13494 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13495 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13496 length, LPFC_SLI4_MBX_EMBED);
13497
13498 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050013499 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050013500 bf_set(lpfc_mbx_mq_create_ext_num_pages,
13501 &mq_create_ext->u.request, mq->page_count);
13502 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13503 &mq_create_ext->u.request, 1);
13504 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040013505 &mq_create_ext->u.request, 1);
13506 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13507 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050013508 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13509 &mq_create_ext->u.request, 1);
13510 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13511 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040013512 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013513 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13514 phba->sli4_hba.pc_sli4_params.mqv);
13515 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13516 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13517 cq->queue_id);
13518 else
13519 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
13520 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040013521 switch (mq->entry_count) {
13522 default:
13523 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13524 "0362 Unsupported MQ count. (%d)\n",
13525 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013526 if (mq->entry_count < 16) {
13527 status = -EINVAL;
13528 goto out;
13529 }
James Smartb19a0612010-04-06 14:48:51 -040013530 /* otherwise default to smallest count (drop through) */
13531 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013532 bf_set(lpfc_mq_context_ring_size,
13533 &mq_create_ext->u.request.context,
13534 LPFC_MQ_RING_SIZE_16);
James Smartb19a0612010-04-06 14:48:51 -040013535 break;
13536 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013537 bf_set(lpfc_mq_context_ring_size,
13538 &mq_create_ext->u.request.context,
13539 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040013540 break;
13541 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013542 bf_set(lpfc_mq_context_ring_size,
13543 &mq_create_ext->u.request.context,
13544 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040013545 break;
13546 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013547 bf_set(lpfc_mq_context_ring_size,
13548 &mq_create_ext->u.request.context,
13549 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040013550 break;
13551 }
13552 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013553 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040013554 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
13555 putPaddrLow(dmabuf->phys);
13556 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040013557 putPaddrHigh(dmabuf->phys);
13558 }
13559 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040013560 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13561 &mq_create_ext->u.response);
13562 if (rc != MBX_SUCCESS) {
13563 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13564 "2795 MQ_CREATE_EXT failed with "
13565 "status x%x. Failback to MQ_CREATE.\n",
13566 rc);
13567 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
13568 mq_create = &mbox->u.mqe.un.mq_create;
13569 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13570 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
13571 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13572 &mq_create->u.response);
13573 }
13574
James Smart04c68492009-05-22 14:52:52 -040013575 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040013576 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13577 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13578 if (shdr_status || shdr_add_status || rc) {
13579 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13580 "2502 MQ_CREATE mailbox failed with "
13581 "status x%x add_status x%x, mbx status x%x\n",
13582 shdr_status, shdr_add_status, rc);
13583 status = -ENXIO;
13584 goto out;
13585 }
James Smart04c68492009-05-22 14:52:52 -040013586 if (mq->queue_id == 0xFFFF) {
13587 status = -ENXIO;
13588 goto out;
13589 }
13590 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050013591 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040013592 mq->subtype = subtype;
13593 mq->host_index = 0;
13594 mq->hba_index = 0;
13595
13596 /* link the mq onto the parent cq child list */
13597 list_add_tail(&mq->list, &cq->child_list);
13598out:
James Smart8fa38512009-07-19 10:01:03 -040013599 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013600 return status;
13601}
13602
13603/**
James Smart4f774512009-05-22 14:52:35 -040013604 * lpfc_wq_create - Create a Work Queue on the HBA
13605 * @phba: HBA structure that indicates port to create a queue on.
13606 * @wq: The queue structure to use to create the work queue.
13607 * @cq: The completion queue to bind this work queue to.
13608 * @subtype: The subtype of the work queue indicating its functionality.
13609 *
13610 * This function creates a work queue, as detailed in @wq, on a port, described
13611 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
13612 *
13613 * The @phba struct is used to send mailbox command to HBA. The @wq struct
13614 * is used to get the entry count and entry size that are necessary to
13615 * determine the number of pages to allocate and use for this queue. The @cq
13616 * is used to indicate which completion queue to bind this work queue to. This
13617 * function will send the WQ_CREATE mailbox command to the HBA to setup the
13618 * work queue. This function is asynchronous and will wait for the mailbox
13619 * command to finish before continuing.
13620 *
13621 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013622 * memory this function will return -ENOMEM. If the queue create mailbox command
13623 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013624 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013625int
James Smart4f774512009-05-22 14:52:35 -040013626lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
13627 struct lpfc_queue *cq, uint32_t subtype)
13628{
13629 struct lpfc_mbx_wq_create *wq_create;
13630 struct lpfc_dmabuf *dmabuf;
13631 LPFC_MBOXQ_t *mbox;
13632 int rc, length, status = 0;
13633 uint32_t shdr_status, shdr_add_status;
13634 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013635 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050013636 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050013637 void __iomem *bar_memmap_p;
13638 uint32_t db_offset;
13639 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013640
James Smart2e90f4b2011-12-13 13:22:37 -050013641 /* sanity check on queue memory */
13642 if (!wq || !cq)
13643 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013644 if (!phba->sli4_hba.pc_sli4_params.supported)
13645 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013646
13647 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13648 if (!mbox)
13649 return -ENOMEM;
13650 length = (sizeof(struct lpfc_mbx_wq_create) -
13651 sizeof(struct lpfc_sli4_cfg_mhdr));
13652 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13653 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13654 length, LPFC_SLI4_MBX_EMBED);
13655 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013656 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013657 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13658 wq->page_count);
13659 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13660 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040013661
13662 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050013663 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13664 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050013665
James Smart0c651872013-07-15 18:33:23 -040013666 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13667 case LPFC_Q_CREATE_VERSION_0:
13668 switch (wq->entry_size) {
13669 default:
13670 case 64:
13671 /* Nothing to do, version 0 ONLY supports 64 byte */
13672 page = wq_create->u.request.page;
13673 break;
13674 case 128:
13675 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13676 LPFC_WQ_SZ128_SUPPORT)) {
13677 status = -ERANGE;
13678 goto out;
13679 }
13680 /* If we get here the HBA MUST also support V1 and
13681 * we MUST use it
13682 */
13683 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13684 LPFC_Q_CREATE_VERSION_1);
13685
13686 bf_set(lpfc_mbx_wq_create_wqe_count,
13687 &wq_create->u.request_1, wq->entry_count);
13688 bf_set(lpfc_mbx_wq_create_wqe_size,
13689 &wq_create->u.request_1,
13690 LPFC_WQ_WQE_SIZE_128);
13691 bf_set(lpfc_mbx_wq_create_page_size,
13692 &wq_create->u.request_1,
James Smart27ab5412017-02-12 13:52:25 -080013693 LPFC_WQ_PAGE_SIZE_4096);
James Smart0c651872013-07-15 18:33:23 -040013694 page = wq_create->u.request_1.page;
13695 break;
13696 }
13697 break;
13698 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050013699 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13700 wq->entry_count);
James Smart730ff612017-04-21 16:04:56 -070013701 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13702 LPFC_Q_CREATE_VERSION_1);
13703
James Smart5a6f1332011-03-11 16:05:35 -050013704 switch (wq->entry_size) {
13705 default:
13706 case 64:
13707 bf_set(lpfc_mbx_wq_create_wqe_size,
13708 &wq_create->u.request_1,
13709 LPFC_WQ_WQE_SIZE_64);
13710 break;
13711 case 128:
James Smart0c651872013-07-15 18:33:23 -040013712 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13713 LPFC_WQ_SZ128_SUPPORT)) {
13714 status = -ERANGE;
13715 goto out;
13716 }
James Smart5a6f1332011-03-11 16:05:35 -050013717 bf_set(lpfc_mbx_wq_create_wqe_size,
13718 &wq_create->u.request_1,
13719 LPFC_WQ_WQE_SIZE_128);
13720 break;
13721 }
James Smart27ab5412017-02-12 13:52:25 -080013722 bf_set(lpfc_mbx_wq_create_page_size,
13723 &wq_create->u.request_1,
13724 LPFC_WQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050013725 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040013726 break;
13727 default:
13728 status = -ERANGE;
13729 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050013730 }
James Smart0c651872013-07-15 18:33:23 -040013731
James Smart4f774512009-05-22 14:52:35 -040013732 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013733 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050013734 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13735 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040013736 }
James Smart962bc512013-01-03 15:44:00 -050013737
13738 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13739 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13740
James Smart4f774512009-05-22 14:52:35 -040013741 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13742 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013743 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13744 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13745 if (shdr_status || shdr_add_status || rc) {
13746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13747 "2503 WQ_CREATE mailbox failed with "
13748 "status x%x add_status x%x, mbx status x%x\n",
13749 shdr_status, shdr_add_status, rc);
13750 status = -ENXIO;
13751 goto out;
13752 }
13753 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13754 if (wq->queue_id == 0xFFFF) {
13755 status = -ENXIO;
13756 goto out;
13757 }
James Smart962bc512013-01-03 15:44:00 -050013758 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13759 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13760 &wq_create->u.response);
13761 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13762 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13763 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13764 "3265 WQ[%d] doorbell format not "
13765 "supported: x%x\n", wq->queue_id,
13766 wq->db_format);
13767 status = -EINVAL;
13768 goto out;
13769 }
13770 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13771 &wq_create->u.response);
13772 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13773 if (!bar_memmap_p) {
13774 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13775 "3263 WQ[%d] failed to memmap pci "
13776 "barset:x%x\n", wq->queue_id,
13777 pci_barset);
13778 status = -ENOMEM;
13779 goto out;
13780 }
13781 db_offset = wq_create->u.response.doorbell_offset;
13782 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13783 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13784 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13785 "3252 WQ[%d] doorbell offset not "
13786 "supported: x%x\n", wq->queue_id,
13787 db_offset);
13788 status = -EINVAL;
13789 goto out;
13790 }
13791 wq->db_regaddr = bar_memmap_p + db_offset;
13792 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013793 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13794 "format:x%x\n", wq->queue_id, pci_barset,
13795 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013796 } else {
13797 wq->db_format = LPFC_DB_LIST_FORMAT;
13798 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13799 }
James Smart4f774512009-05-22 14:52:35 -040013800 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050013801 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013802 wq->subtype = subtype;
13803 wq->host_index = 0;
13804 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050013805 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040013806
13807 /* link the wq onto the parent cq child list */
13808 list_add_tail(&wq->list, &cq->child_list);
13809out:
James Smart8fa38512009-07-19 10:01:03 -040013810 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013811 return status;
13812}
13813
13814/**
James Smart73d91e52011-10-10 21:32:10 -040013815 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13816 * @phba: HBA structure that indicates port to create a queue on.
13817 * @rq: The queue structure to use for the receive queue.
13818 * @qno: The associated HBQ number
13819 *
13820 *
13821 * For SLI4 we need to adjust the RQ repost value based on
13822 * the number of buffers that are initially posted to the RQ.
13823 */
13824void
13825lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13826{
13827 uint32_t cnt;
13828
James Smart2e90f4b2011-12-13 13:22:37 -050013829 /* sanity check on queue memory */
13830 if (!rq)
13831 return;
James Smart73d91e52011-10-10 21:32:10 -040013832 cnt = lpfc_hbq_defs[qno]->entry_count;
13833
13834 /* Recalc repost for RQs based on buffers initially posted */
13835 cnt = (cnt >> 3);
13836 if (cnt < LPFC_QUEUE_MIN_REPOST)
13837 cnt = LPFC_QUEUE_MIN_REPOST;
13838
13839 rq->entry_repost = cnt;
13840}
13841
13842/**
James Smart4f774512009-05-22 14:52:35 -040013843 * lpfc_rq_create - Create a Receive Queue on the HBA
13844 * @phba: HBA structure that indicates port to create a queue on.
13845 * @hrq: The queue structure to use to create the header receive queue.
13846 * @drq: The queue structure to use to create the data receive queue.
13847 * @cq: The completion queue to bind this work queue to.
13848 *
13849 * This function creates a receive buffer queue pair , as detailed in @hrq and
13850 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13851 * to the HBA.
13852 *
13853 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13854 * struct is used to get the entry count that is necessary to determine the
13855 * number of pages to use for this queue. The @cq is used to indicate which
13856 * completion queue to bind received buffers that are posted to these queues to.
13857 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13858 * receive queue pair. This function is asynchronous and will wait for the
13859 * mailbox command to finish before continuing.
13860 *
13861 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013862 * memory this function will return -ENOMEM. If the queue create mailbox command
13863 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013864 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013865int
James Smart4f774512009-05-22 14:52:35 -040013866lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13867 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13868{
13869 struct lpfc_mbx_rq_create *rq_create;
13870 struct lpfc_dmabuf *dmabuf;
13871 LPFC_MBOXQ_t *mbox;
13872 int rc, length, status = 0;
13873 uint32_t shdr_status, shdr_add_status;
13874 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013875 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050013876 void __iomem *bar_memmap_p;
13877 uint32_t db_offset;
13878 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013879
James Smart2e90f4b2011-12-13 13:22:37 -050013880 /* sanity check on queue memory */
13881 if (!hrq || !drq || !cq)
13882 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013883 if (!phba->sli4_hba.pc_sli4_params.supported)
13884 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013885
13886 if (hrq->entry_count != drq->entry_count)
13887 return -EINVAL;
13888 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13889 if (!mbox)
13890 return -ENOMEM;
13891 length = (sizeof(struct lpfc_mbx_rq_create) -
13892 sizeof(struct lpfc_sli4_cfg_mhdr));
13893 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13894 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13895 length, LPFC_SLI4_MBX_EMBED);
13896 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013897 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13898 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13899 phba->sli4_hba.pc_sli4_params.rqv);
13900 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13901 bf_set(lpfc_rq_context_rqe_count_1,
13902 &rq_create->u.request.context,
13903 hrq->entry_count);
13904 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013905 bf_set(lpfc_rq_context_rqe_size,
13906 &rq_create->u.request.context,
13907 LPFC_RQE_SIZE_8);
13908 bf_set(lpfc_rq_context_page_size,
13909 &rq_create->u.request.context,
James Smart27ab5412017-02-12 13:52:25 -080013910 LPFC_RQ_PAGE_SIZE_4096);
James Smart5a6f1332011-03-11 16:05:35 -050013911 } else {
13912 switch (hrq->entry_count) {
13913 default:
13914 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13915 "2535 Unsupported RQ count. (%d)\n",
13916 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013917 if (hrq->entry_count < 512) {
13918 status = -EINVAL;
13919 goto out;
13920 }
James Smart5a6f1332011-03-11 16:05:35 -050013921 /* otherwise default to smallest count (drop through) */
13922 case 512:
13923 bf_set(lpfc_rq_context_rqe_count,
13924 &rq_create->u.request.context,
13925 LPFC_RQ_RING_SIZE_512);
13926 break;
13927 case 1024:
13928 bf_set(lpfc_rq_context_rqe_count,
13929 &rq_create->u.request.context,
13930 LPFC_RQ_RING_SIZE_1024);
13931 break;
13932 case 2048:
13933 bf_set(lpfc_rq_context_rqe_count,
13934 &rq_create->u.request.context,
13935 LPFC_RQ_RING_SIZE_2048);
13936 break;
13937 case 4096:
13938 bf_set(lpfc_rq_context_rqe_count,
13939 &rq_create->u.request.context,
13940 LPFC_RQ_RING_SIZE_4096);
13941 break;
13942 }
13943 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13944 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013945 }
13946 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13947 cq->queue_id);
13948 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13949 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013950 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013951 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013952 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13953 putPaddrLow(dmabuf->phys);
13954 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13955 putPaddrHigh(dmabuf->phys);
13956 }
James Smart962bc512013-01-03 15:44:00 -050013957 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13958 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13959
James Smart4f774512009-05-22 14:52:35 -040013960 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13961 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013962 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13963 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13964 if (shdr_status || shdr_add_status || rc) {
13965 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13966 "2504 RQ_CREATE mailbox failed with "
13967 "status x%x add_status x%x, mbx status x%x\n",
13968 shdr_status, shdr_add_status, rc);
13969 status = -ENXIO;
13970 goto out;
13971 }
13972 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13973 if (hrq->queue_id == 0xFFFF) {
13974 status = -ENXIO;
13975 goto out;
13976 }
James Smart962bc512013-01-03 15:44:00 -050013977
13978 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13979 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13980 &rq_create->u.response);
13981 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13982 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13984 "3262 RQ [%d] doorbell format not "
13985 "supported: x%x\n", hrq->queue_id,
13986 hrq->db_format);
13987 status = -EINVAL;
13988 goto out;
13989 }
13990
13991 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13992 &rq_create->u.response);
13993 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13994 if (!bar_memmap_p) {
13995 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13996 "3269 RQ[%d] failed to memmap pci "
13997 "barset:x%x\n", hrq->queue_id,
13998 pci_barset);
13999 status = -ENOMEM;
14000 goto out;
14001 }
14002
14003 db_offset = rq_create->u.response.doorbell_offset;
14004 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
14005 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
14006 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14007 "3270 RQ[%d] doorbell offset not "
14008 "supported: x%x\n", hrq->queue_id,
14009 db_offset);
14010 status = -EINVAL;
14011 goto out;
14012 }
14013 hrq->db_regaddr = bar_memmap_p + db_offset;
14014 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040014015 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
14016 "format:x%x\n", hrq->queue_id, pci_barset,
14017 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014018 } else {
14019 hrq->db_format = LPFC_DB_RING_FORMAT;
14020 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
14021 }
James Smart4f774512009-05-22 14:52:35 -040014022 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014023 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014024 hrq->subtype = subtype;
14025 hrq->host_index = 0;
14026 hrq->hba_index = 0;
14027
14028 /* now create the data queue */
14029 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14030 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14031 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050014032 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14033 phba->sli4_hba.pc_sli4_params.rqv);
14034 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14035 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040014036 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050014037 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040014038 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
14039 LPFC_RQE_SIZE_8);
14040 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
14041 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014042 } else {
14043 switch (drq->entry_count) {
14044 default:
14045 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14046 "2536 Unsupported RQ count. (%d)\n",
14047 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014048 if (drq->entry_count < 512) {
14049 status = -EINVAL;
14050 goto out;
14051 }
James Smart5a6f1332011-03-11 16:05:35 -050014052 /* otherwise default to smallest count (drop through) */
14053 case 512:
14054 bf_set(lpfc_rq_context_rqe_count,
14055 &rq_create->u.request.context,
14056 LPFC_RQ_RING_SIZE_512);
14057 break;
14058 case 1024:
14059 bf_set(lpfc_rq_context_rqe_count,
14060 &rq_create->u.request.context,
14061 LPFC_RQ_RING_SIZE_1024);
14062 break;
14063 case 2048:
14064 bf_set(lpfc_rq_context_rqe_count,
14065 &rq_create->u.request.context,
14066 LPFC_RQ_RING_SIZE_2048);
14067 break;
14068 case 4096:
14069 bf_set(lpfc_rq_context_rqe_count,
14070 &rq_create->u.request.context,
14071 LPFC_RQ_RING_SIZE_4096);
14072 break;
14073 }
14074 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14075 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040014076 }
14077 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14078 cq->queue_id);
14079 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14080 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040014081 list_for_each_entry(dmabuf, &drq->page_list, list) {
14082 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14083 putPaddrLow(dmabuf->phys);
14084 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14085 putPaddrHigh(dmabuf->phys);
14086 }
James Smart962bc512013-01-03 15:44:00 -050014087 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14088 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040014089 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14090 /* The IOCTL status is embedded in the mailbox subheader. */
14091 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14092 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14093 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14094 if (shdr_status || shdr_add_status || rc) {
14095 status = -ENXIO;
14096 goto out;
14097 }
14098 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14099 if (drq->queue_id == 0xFFFF) {
14100 status = -ENXIO;
14101 goto out;
14102 }
14103 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014104 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014105 drq->subtype = subtype;
14106 drq->host_index = 0;
14107 drq->hba_index = 0;
14108
14109 /* link the header and data RQs onto the parent cq child list */
14110 list_add_tail(&hrq->list, &cq->child_list);
14111 list_add_tail(&drq->list, &cq->child_list);
14112
14113out:
James Smart8fa38512009-07-19 10:01:03 -040014114 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014115 return status;
14116}
14117
14118/**
14119 * lpfc_eq_destroy - Destroy an event Queue on the HBA
14120 * @eq: The queue structure associated with the queue to destroy.
14121 *
14122 * This function destroys a queue, as detailed in @eq by sending an mailbox
14123 * command, specific to the type of queue, to the HBA.
14124 *
14125 * The @eq struct is used to get the queue ID of the queue to destroy.
14126 *
14127 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014128 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014129 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014130int
James Smart4f774512009-05-22 14:52:35 -040014131lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
14132{
14133 LPFC_MBOXQ_t *mbox;
14134 int rc, length, status = 0;
14135 uint32_t shdr_status, shdr_add_status;
14136 union lpfc_sli4_cfg_shdr *shdr;
14137
James Smart2e90f4b2011-12-13 13:22:37 -050014138 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014139 if (!eq)
14140 return -ENODEV;
14141 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
14142 if (!mbox)
14143 return -ENOMEM;
14144 length = (sizeof(struct lpfc_mbx_eq_destroy) -
14145 sizeof(struct lpfc_sli4_cfg_mhdr));
14146 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14147 LPFC_MBOX_OPCODE_EQ_DESTROY,
14148 length, LPFC_SLI4_MBX_EMBED);
14149 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
14150 eq->queue_id);
14151 mbox->vport = eq->phba->pport;
14152 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14153
14154 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
14155 /* The IOCTL status is embedded in the mailbox subheader. */
14156 shdr = (union lpfc_sli4_cfg_shdr *)
14157 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
14158 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14159 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14160 if (shdr_status || shdr_add_status || rc) {
14161 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14162 "2505 EQ_DESTROY mailbox failed with "
14163 "status x%x add_status x%x, mbx status x%x\n",
14164 shdr_status, shdr_add_status, rc);
14165 status = -ENXIO;
14166 }
14167
14168 /* Remove eq from any list */
14169 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040014170 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014171 return status;
14172}
14173
14174/**
14175 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
14176 * @cq: The queue structure associated with the queue to destroy.
14177 *
14178 * This function destroys a queue, as detailed in @cq by sending an mailbox
14179 * command, specific to the type of queue, to the HBA.
14180 *
14181 * The @cq struct is used to get the queue ID of the queue to destroy.
14182 *
14183 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014184 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014185 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014186int
James Smart4f774512009-05-22 14:52:35 -040014187lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14188{
14189 LPFC_MBOXQ_t *mbox;
14190 int rc, length, status = 0;
14191 uint32_t shdr_status, shdr_add_status;
14192 union lpfc_sli4_cfg_shdr *shdr;
14193
James Smart2e90f4b2011-12-13 13:22:37 -050014194 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014195 if (!cq)
14196 return -ENODEV;
14197 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14198 if (!mbox)
14199 return -ENOMEM;
14200 length = (sizeof(struct lpfc_mbx_cq_destroy) -
14201 sizeof(struct lpfc_sli4_cfg_mhdr));
14202 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14203 LPFC_MBOX_OPCODE_CQ_DESTROY,
14204 length, LPFC_SLI4_MBX_EMBED);
14205 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14206 cq->queue_id);
14207 mbox->vport = cq->phba->pport;
14208 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14209 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14210 /* The IOCTL status is embedded in the mailbox subheader. */
14211 shdr = (union lpfc_sli4_cfg_shdr *)
14212 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14213 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14214 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14215 if (shdr_status || shdr_add_status || rc) {
14216 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14217 "2506 CQ_DESTROY mailbox failed with "
14218 "status x%x add_status x%x, mbx status x%x\n",
14219 shdr_status, shdr_add_status, rc);
14220 status = -ENXIO;
14221 }
14222 /* Remove cq from any list */
14223 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040014224 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014225 return status;
14226}
14227
14228/**
James Smart04c68492009-05-22 14:52:52 -040014229 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14230 * @qm: The queue structure associated with the queue to destroy.
14231 *
14232 * This function destroys a queue, as detailed in @mq by sending an mailbox
14233 * command, specific to the type of queue, to the HBA.
14234 *
14235 * The @mq struct is used to get the queue ID of the queue to destroy.
14236 *
14237 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014238 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040014239 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014240int
James Smart04c68492009-05-22 14:52:52 -040014241lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14242{
14243 LPFC_MBOXQ_t *mbox;
14244 int rc, length, status = 0;
14245 uint32_t shdr_status, shdr_add_status;
14246 union lpfc_sli4_cfg_shdr *shdr;
14247
James Smart2e90f4b2011-12-13 13:22:37 -050014248 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040014249 if (!mq)
14250 return -ENODEV;
14251 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14252 if (!mbox)
14253 return -ENOMEM;
14254 length = (sizeof(struct lpfc_mbx_mq_destroy) -
14255 sizeof(struct lpfc_sli4_cfg_mhdr));
14256 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14257 LPFC_MBOX_OPCODE_MQ_DESTROY,
14258 length, LPFC_SLI4_MBX_EMBED);
14259 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14260 mq->queue_id);
14261 mbox->vport = mq->phba->pport;
14262 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14263 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14264 /* The IOCTL status is embedded in the mailbox subheader. */
14265 shdr = (union lpfc_sli4_cfg_shdr *)
14266 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14267 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14268 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14269 if (shdr_status || shdr_add_status || rc) {
14270 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14271 "2507 MQ_DESTROY mailbox failed with "
14272 "status x%x add_status x%x, mbx status x%x\n",
14273 shdr_status, shdr_add_status, rc);
14274 status = -ENXIO;
14275 }
14276 /* Remove mq from any list */
14277 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040014278 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014279 return status;
14280}
14281
14282/**
James Smart4f774512009-05-22 14:52:35 -040014283 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14284 * @wq: The queue structure associated with the queue to destroy.
14285 *
14286 * This function destroys a queue, as detailed in @wq by sending an mailbox
14287 * command, specific to the type of queue, to the HBA.
14288 *
14289 * The @wq struct is used to get the queue ID of the queue to destroy.
14290 *
14291 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014292 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014293 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014294int
James Smart4f774512009-05-22 14:52:35 -040014295lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14296{
14297 LPFC_MBOXQ_t *mbox;
14298 int rc, length, status = 0;
14299 uint32_t shdr_status, shdr_add_status;
14300 union lpfc_sli4_cfg_shdr *shdr;
14301
James Smart2e90f4b2011-12-13 13:22:37 -050014302 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014303 if (!wq)
14304 return -ENODEV;
14305 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14306 if (!mbox)
14307 return -ENOMEM;
14308 length = (sizeof(struct lpfc_mbx_wq_destroy) -
14309 sizeof(struct lpfc_sli4_cfg_mhdr));
14310 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14311 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14312 length, LPFC_SLI4_MBX_EMBED);
14313 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14314 wq->queue_id);
14315 mbox->vport = wq->phba->pport;
14316 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14317 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14318 shdr = (union lpfc_sli4_cfg_shdr *)
14319 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14320 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14321 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14322 if (shdr_status || shdr_add_status || rc) {
14323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14324 "2508 WQ_DESTROY mailbox failed with "
14325 "status x%x add_status x%x, mbx status x%x\n",
14326 shdr_status, shdr_add_status, rc);
14327 status = -ENXIO;
14328 }
14329 /* Remove wq from any list */
14330 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040014331 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014332 return status;
14333}
14334
14335/**
14336 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14337 * @rq: The queue structure associated with the queue to destroy.
14338 *
14339 * This function destroys a queue, as detailed in @rq by sending an mailbox
14340 * command, specific to the type of queue, to the HBA.
14341 *
14342 * The @rq struct is used to get the queue ID of the queue to destroy.
14343 *
14344 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014345 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014346 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014347int
James Smart4f774512009-05-22 14:52:35 -040014348lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14349 struct lpfc_queue *drq)
14350{
14351 LPFC_MBOXQ_t *mbox;
14352 int rc, length, status = 0;
14353 uint32_t shdr_status, shdr_add_status;
14354 union lpfc_sli4_cfg_shdr *shdr;
14355
James Smart2e90f4b2011-12-13 13:22:37 -050014356 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014357 if (!hrq || !drq)
14358 return -ENODEV;
14359 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14360 if (!mbox)
14361 return -ENOMEM;
14362 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050014363 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040014364 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14365 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14366 length, LPFC_SLI4_MBX_EMBED);
14367 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14368 hrq->queue_id);
14369 mbox->vport = hrq->phba->pport;
14370 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14371 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14372 /* The IOCTL status is embedded in the mailbox subheader. */
14373 shdr = (union lpfc_sli4_cfg_shdr *)
14374 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14375 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14376 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14377 if (shdr_status || shdr_add_status || rc) {
14378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14379 "2509 RQ_DESTROY mailbox failed with "
14380 "status x%x add_status x%x, mbx status x%x\n",
14381 shdr_status, shdr_add_status, rc);
14382 if (rc != MBX_TIMEOUT)
14383 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14384 return -ENXIO;
14385 }
14386 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14387 drq->queue_id);
14388 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14389 shdr = (union lpfc_sli4_cfg_shdr *)
14390 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14391 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14392 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14393 if (shdr_status || shdr_add_status || rc) {
14394 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14395 "2510 RQ_DESTROY mailbox failed with "
14396 "status x%x add_status x%x, mbx status x%x\n",
14397 shdr_status, shdr_add_status, rc);
14398 status = -ENXIO;
14399 }
14400 list_del_init(&hrq->list);
14401 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040014402 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014403 return status;
14404}
14405
14406/**
14407 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14408 * @phba: The virtual port for which this call being executed.
14409 * @pdma_phys_addr0: Physical address of the 1st SGL page.
14410 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14411 * @xritag: the xritag that ties this io to the SGL pages.
14412 *
14413 * This routine will post the sgl pages for the IO that has the xritag
14414 * that is in the iocbq structure. The xritag is assigned during iocbq
14415 * creation and persists for as long as the driver is loaded.
14416 * if the caller has fewer than 256 scatter gather segments to map then
14417 * pdma_phys_addr1 should be 0.
14418 * If the caller needs to map more than 256 scatter gather segment then
14419 * pdma_phys_addr1 should be a valid physical address.
14420 * physical address for SGLs must be 64 byte aligned.
14421 * If you are going to map 2 SGL's then the first one must have 256 entries
14422 * the second sgl can have between 1 and 256 entries.
14423 *
14424 * Return codes:
14425 * 0 - Success
14426 * -ENXIO, -ENOMEM - Failure
14427 **/
14428int
14429lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14430 dma_addr_t pdma_phys_addr0,
14431 dma_addr_t pdma_phys_addr1,
14432 uint16_t xritag)
14433{
14434 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14435 LPFC_MBOXQ_t *mbox;
14436 int rc;
14437 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040014438 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040014439 union lpfc_sli4_cfg_shdr *shdr;
14440
14441 if (xritag == NO_XRI) {
14442 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14443 "0364 Invalid param:\n");
14444 return -EINVAL;
14445 }
14446
14447 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14448 if (!mbox)
14449 return -ENOMEM;
14450
14451 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14452 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14453 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050014454 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040014455
14456 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14457 &mbox->u.mqe.un.post_sgl_pages;
14458 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14459 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14460
14461 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14462 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14463 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14464 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14465
14466 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14467 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14468 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14469 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14470 if (!phba->sli4_hba.intr_enable)
14471 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040014472 else {
James Smarta183a152011-10-10 21:32:43 -040014473 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040014474 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14475 }
James Smart4f774512009-05-22 14:52:35 -040014476 /* The IOCTL status is embedded in the mailbox subheader. */
14477 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14478 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14479 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14480 if (rc != MBX_TIMEOUT)
14481 mempool_free(mbox, phba->mbox_mem_pool);
14482 if (shdr_status || shdr_add_status || rc) {
14483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14484 "2511 POST_SGL mailbox failed with "
14485 "status x%x add_status x%x, mbx status x%x\n",
14486 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040014487 }
14488 return 0;
14489}
James Smart4f774512009-05-22 14:52:35 -040014490
14491/**
James Smart88a2cfb2011-07-22 18:36:33 -040014492 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040014493 * @phba: pointer to lpfc hba data structure.
14494 *
14495 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040014496 * HBA consistent with the SLI-4 interface spec. This routine
14497 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14498 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040014499 *
James Smart88a2cfb2011-07-22 18:36:33 -040014500 * Returns
14501 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14502 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14503 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014504static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014505lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14506{
14507 unsigned long xri;
14508
14509 /*
14510 * Fetch the next logical xri. Because this index is logical,
14511 * the driver starts at 0 each time.
14512 */
14513 spin_lock_irq(&phba->hbalock);
14514 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14515 phba->sli4_hba.max_cfg_param.max_xri, 0);
14516 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
14517 spin_unlock_irq(&phba->hbalock);
14518 return NO_XRI;
14519 } else {
14520 set_bit(xri, phba->sli4_hba.xri_bmask);
14521 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040014522 }
James Smart6d368e52011-05-24 11:44:12 -040014523 spin_unlock_irq(&phba->hbalock);
14524 return xri;
14525}
14526
14527/**
14528 * lpfc_sli4_free_xri - Release an xri for reuse.
14529 * @phba: pointer to lpfc hba data structure.
14530 *
14531 * This routine is invoked to release an xri to the pool of
14532 * available rpis maintained by the driver.
14533 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014534static void
James Smart6d368e52011-05-24 11:44:12 -040014535__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14536{
14537 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040014538 phba->sli4_hba.max_cfg_param.xri_used--;
14539 }
14540}
14541
14542/**
14543 * lpfc_sli4_free_xri - Release an xri for reuse.
14544 * @phba: pointer to lpfc hba data structure.
14545 *
14546 * This routine is invoked to release an xri to the pool of
14547 * available rpis maintained by the driver.
14548 **/
14549void
14550lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14551{
14552 spin_lock_irq(&phba->hbalock);
14553 __lpfc_sli4_free_xri(phba, xri);
14554 spin_unlock_irq(&phba->hbalock);
14555}
14556
14557/**
James Smart4f774512009-05-22 14:52:35 -040014558 * lpfc_sli4_next_xritag - Get an xritag for the io
14559 * @phba: Pointer to HBA context object.
14560 *
14561 * This function gets an xritag for the iocb. If there is no unused xritag
14562 * it will return 0xffff.
14563 * The function returns the allocated xritag if successful, else returns zero.
14564 * Zero is not a valid xritag.
14565 * The caller is not required to hold any lock.
14566 **/
14567uint16_t
14568lpfc_sli4_next_xritag(struct lpfc_hba *phba)
14569{
James Smart6d368e52011-05-24 11:44:12 -040014570 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040014571
James Smart6d368e52011-05-24 11:44:12 -040014572 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040014573 if (xri_index == NO_XRI)
14574 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14575 "2004 Failed to allocate XRI.last XRITAG is %d"
14576 " Max XRI is %d, Used XRI is %d\n",
14577 xri_index,
14578 phba->sli4_hba.max_cfg_param.max_xri,
14579 phba->sli4_hba.max_cfg_param.xri_used);
14580 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040014581}
14582
14583/**
James Smart6d368e52011-05-24 11:44:12 -040014584 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040014585 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040014586 * @post_sgl_list: pointer to els sgl entry list.
14587 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040014588 *
14589 * This routine is invoked to post a block of driver's sgl pages to the
14590 * HBA using non-embedded mailbox command. No Lock is held. This routine
14591 * is only called when the driver is loading and after all IO has been
14592 * stopped.
14593 **/
James Smart8a9d2e82012-05-09 21:16:12 -040014594static int
14595lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
14596 struct list_head *post_sgl_list,
14597 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040014598{
James Smart8a9d2e82012-05-09 21:16:12 -040014599 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040014600 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14601 struct sgl_page_pairs *sgl_pg_pairs;
14602 void *viraddr;
14603 LPFC_MBOXQ_t *mbox;
14604 uint32_t reqlen, alloclen, pg_pairs;
14605 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040014606 uint16_t xritag_start = 0;
14607 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040014608 uint32_t shdr_status, shdr_add_status;
14609 union lpfc_sli4_cfg_shdr *shdr;
14610
James Smart8a9d2e82012-05-09 21:16:12 -040014611 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014612 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014613 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014614 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14615 "2559 Block sgl registration required DMA "
14616 "size (%d) great than a page\n", reqlen);
14617 return -ENOMEM;
14618 }
14619 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040014620 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040014621 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040014622
14623 /* Allocate DMA memory and set up the non-embedded mailbox command */
14624 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14625 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14626 LPFC_SLI4_MBX_NEMBED);
14627
14628 if (alloclen < reqlen) {
14629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14630 "0285 Allocated DMA memory size (%d) is "
14631 "less than the requested DMA memory "
14632 "size (%d)\n", alloclen, reqlen);
14633 lpfc_sli4_mbox_cmd_free(phba, mbox);
14634 return -ENOMEM;
14635 }
James Smart4f774512009-05-22 14:52:35 -040014636 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040014637 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040014638 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14639 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14640
James Smart8a9d2e82012-05-09 21:16:12 -040014641 pg_pairs = 0;
14642 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040014643 /* Set up the sge entry */
14644 sgl_pg_pairs->sgl_pg0_addr_lo =
14645 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14646 sgl_pg_pairs->sgl_pg0_addr_hi =
14647 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14648 sgl_pg_pairs->sgl_pg1_addr_lo =
14649 cpu_to_le32(putPaddrLow(0));
14650 sgl_pg_pairs->sgl_pg1_addr_hi =
14651 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040014652
James Smart4f774512009-05-22 14:52:35 -040014653 /* Keep the first xritag on the list */
14654 if (pg_pairs == 0)
14655 xritag_start = sglq_entry->sli4_xritag;
14656 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040014657 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040014658 }
James Smart6d368e52011-05-24 11:44:12 -040014659
14660 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040014661 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040014662 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040014663 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040014664 if (!phba->sli4_hba.intr_enable)
14665 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14666 else {
James Smarta183a152011-10-10 21:32:43 -040014667 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014668 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14669 }
14670 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14671 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14672 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14673 if (rc != MBX_TIMEOUT)
14674 lpfc_sli4_mbox_cmd_free(phba, mbox);
14675 if (shdr_status || shdr_add_status || rc) {
14676 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14677 "2513 POST_SGL_BLOCK mailbox command failed "
14678 "status x%x add_status x%x mbx status x%x\n",
14679 shdr_status, shdr_add_status, rc);
14680 rc = -ENXIO;
14681 }
14682 return rc;
14683}
14684
14685/**
14686 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14687 * @phba: pointer to lpfc hba data structure.
14688 * @sblist: pointer to scsi buffer list.
14689 * @count: number of scsi buffers on the list.
14690 *
14691 * This routine is invoked to post a block of @count scsi sgl pages from a
14692 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14693 * No Lock is held.
14694 *
14695 **/
14696int
James Smart8a9d2e82012-05-09 21:16:12 -040014697lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14698 struct list_head *sblist,
14699 int count)
James Smart4f774512009-05-22 14:52:35 -040014700{
14701 struct lpfc_scsi_buf *psb;
14702 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14703 struct sgl_page_pairs *sgl_pg_pairs;
14704 void *viraddr;
14705 LPFC_MBOXQ_t *mbox;
14706 uint32_t reqlen, alloclen, pg_pairs;
14707 uint32_t mbox_tmo;
14708 uint16_t xritag_start = 0;
14709 int rc = 0;
14710 uint32_t shdr_status, shdr_add_status;
14711 dma_addr_t pdma_phys_bpl1;
14712 union lpfc_sli4_cfg_shdr *shdr;
14713
14714 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040014715 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014716 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014717 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014718 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14719 "0217 Block sgl registration required DMA "
14720 "size (%d) great than a page\n", reqlen);
14721 return -ENOMEM;
14722 }
14723 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14724 if (!mbox) {
14725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14726 "0283 Failed to allocate mbox cmd memory\n");
14727 return -ENOMEM;
14728 }
14729
14730 /* Allocate DMA memory and set up the non-embedded mailbox command */
14731 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14732 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14733 LPFC_SLI4_MBX_NEMBED);
14734
14735 if (alloclen < reqlen) {
14736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14737 "2561 Allocated DMA memory size (%d) is "
14738 "less than the requested DMA memory "
14739 "size (%d)\n", alloclen, reqlen);
14740 lpfc_sli4_mbox_cmd_free(phba, mbox);
14741 return -ENOMEM;
14742 }
James Smart6d368e52011-05-24 11:44:12 -040014743
James Smart4f774512009-05-22 14:52:35 -040014744 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040014745 viraddr = mbox->sge_array->addr[0];
14746
14747 /* Set up the SGL pages in the non-embedded DMA pages */
14748 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14749 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14750
14751 pg_pairs = 0;
14752 list_for_each_entry(psb, sblist, list) {
14753 /* Set up the sge entry */
14754 sgl_pg_pairs->sgl_pg0_addr_lo =
14755 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14756 sgl_pg_pairs->sgl_pg0_addr_hi =
14757 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14758 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14759 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14760 else
14761 pdma_phys_bpl1 = 0;
14762 sgl_pg_pairs->sgl_pg1_addr_lo =
14763 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14764 sgl_pg_pairs->sgl_pg1_addr_hi =
14765 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14766 /* Keep the first xritag on the list */
14767 if (pg_pairs == 0)
14768 xritag_start = psb->cur_iocbq.sli4_xritag;
14769 sgl_pg_pairs++;
14770 pg_pairs++;
14771 }
14772 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14773 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14774 /* Perform endian conversion if necessary */
14775 sgl->word0 = cpu_to_le32(sgl->word0);
14776
14777 if (!phba->sli4_hba.intr_enable)
14778 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14779 else {
James Smarta183a152011-10-10 21:32:43 -040014780 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014781 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14782 }
14783 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14784 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14785 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14786 if (rc != MBX_TIMEOUT)
14787 lpfc_sli4_mbox_cmd_free(phba, mbox);
14788 if (shdr_status || shdr_add_status || rc) {
14789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14790 "2564 POST_SGL_BLOCK mailbox command failed "
14791 "status x%x add_status x%x mbx status x%x\n",
14792 shdr_status, shdr_add_status, rc);
14793 rc = -ENXIO;
14794 }
14795 return rc;
14796}
14797
14798/**
14799 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14800 * @phba: pointer to lpfc_hba struct that the frame was received on
14801 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14802 *
14803 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14804 * valid type of frame that the LPFC driver will handle. This function will
14805 * return a zero if the frame is a valid frame or a non zero value when the
14806 * frame does not pass the check.
14807 **/
14808static int
14809lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14810{
Tomas Henzl474ffb72010-12-22 16:52:40 +010014811 /* make rctl_names static to save stack space */
14812 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040014813 char *type_names[] = FC_TYPE_NAMES_INIT;
14814 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050014815 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040014816
14817 switch (fc_hdr->fh_r_ctl) {
14818 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14819 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14820 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14821 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14822 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14823 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14824 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14825 case FC_RCTL_DD_CMD_STATUS: /* command status */
14826 case FC_RCTL_ELS_REQ: /* extended link services request */
14827 case FC_RCTL_ELS_REP: /* extended link services reply */
14828 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14829 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14830 case FC_RCTL_BA_NOP: /* basic link service NOP */
14831 case FC_RCTL_BA_ABTS: /* basic link service abort */
14832 case FC_RCTL_BA_RMC: /* remove connection */
14833 case FC_RCTL_BA_ACC: /* basic accept */
14834 case FC_RCTL_BA_RJT: /* basic reject */
14835 case FC_RCTL_BA_PRMT:
14836 case FC_RCTL_ACK_1: /* acknowledge_1 */
14837 case FC_RCTL_ACK_0: /* acknowledge_0 */
14838 case FC_RCTL_P_RJT: /* port reject */
14839 case FC_RCTL_F_RJT: /* fabric reject */
14840 case FC_RCTL_P_BSY: /* port busy */
14841 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14842 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14843 case FC_RCTL_LCR: /* link credit reset */
14844 case FC_RCTL_END: /* end */
14845 break;
14846 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14847 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14848 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14849 return lpfc_fc_frame_check(phba, fc_hdr);
14850 default:
14851 goto drop;
14852 }
14853 switch (fc_hdr->fh_type) {
14854 case FC_TYPE_BLS:
14855 case FC_TYPE_ELS:
14856 case FC_TYPE_FCP:
14857 case FC_TYPE_CT:
14858 break;
14859 case FC_TYPE_IP:
14860 case FC_TYPE_ILS:
14861 default:
14862 goto drop;
14863 }
James Smart546fc852011-03-11 16:06:29 -050014864
James Smart4f774512009-05-22 14:52:35 -040014865 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040014866 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14867 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14868 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14869 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050014870 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14871 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040014872 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14873 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040014874 return 0;
14875drop:
14876 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14877 "2539 Dropped frame rctl:%s type:%s\n",
14878 rctl_names[fc_hdr->fh_r_ctl],
14879 type_names[fc_hdr->fh_type]);
14880 return 1;
14881}
14882
14883/**
14884 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14885 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14886 *
14887 * This function processes the FC header to retrieve the VFI from the VF
14888 * header, if one exists. This function will return the VFI if one exists
14889 * or 0 if no VSAN Header exists.
14890 **/
14891static uint32_t
14892lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14893{
14894 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14895
14896 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14897 return 0;
14898 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14899}
14900
14901/**
14902 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14903 * @phba: Pointer to the HBA structure to search for the vport on
14904 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14905 * @fcfi: The FC Fabric ID that the frame came from
14906 *
14907 * This function searches the @phba for a vport that matches the content of the
14908 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14909 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14910 * returns the matching vport pointer or NULL if unable to match frame to a
14911 * vport.
14912 **/
14913static struct lpfc_vport *
14914lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14915 uint16_t fcfi)
14916{
14917 struct lpfc_vport **vports;
14918 struct lpfc_vport *vport = NULL;
14919 int i;
14920 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14921 fc_hdr->fh_d_id[1] << 8 |
14922 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040014923
James Smartbf086112011-08-21 21:48:13 -040014924 if (did == Fabric_DID)
14925 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040014926 if ((phba->pport->fc_flag & FC_PT2PT) &&
14927 !(phba->link_state == LPFC_HBA_READY))
14928 return phba->pport;
14929
James Smart4f774512009-05-22 14:52:35 -040014930 vports = lpfc_create_vport_work_array(phba);
14931 if (vports != NULL)
14932 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14933 if (phba->fcf.fcfi == fcfi &&
14934 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14935 vports[i]->fc_myDID == did) {
14936 vport = vports[i];
14937 break;
14938 }
14939 }
14940 lpfc_destroy_vport_work_array(phba, vports);
14941 return vport;
14942}
14943
14944/**
James Smart45ed1192009-10-02 15:17:02 -040014945 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14946 * @vport: The vport to work on.
14947 *
14948 * This function updates the receive sequence time stamp for this vport. The
14949 * receive sequence time stamp indicates the time that the last frame of the
14950 * the sequence that has been idle for the longest amount of time was received.
14951 * the driver uses this time stamp to indicate if any received sequences have
14952 * timed out.
14953 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014954static void
James Smart45ed1192009-10-02 15:17:02 -040014955lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14956{
14957 struct lpfc_dmabuf *h_buf;
14958 struct hbq_dmabuf *dmabuf = NULL;
14959
14960 /* get the oldest sequence on the rcv list */
14961 h_buf = list_get_first(&vport->rcv_buffer_list,
14962 struct lpfc_dmabuf, list);
14963 if (!h_buf)
14964 return;
14965 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14966 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14967}
14968
14969/**
14970 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14971 * @vport: The vport that the received sequences were sent to.
14972 *
14973 * This function cleans up all outstanding received sequences. This is called
14974 * by the driver when a link event or user action invalidates all the received
14975 * sequences.
14976 **/
14977void
14978lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14979{
14980 struct lpfc_dmabuf *h_buf, *hnext;
14981 struct lpfc_dmabuf *d_buf, *dnext;
14982 struct hbq_dmabuf *dmabuf = NULL;
14983
14984 /* start with the oldest sequence on the rcv list */
14985 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14986 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14987 list_del_init(&dmabuf->hbuf.list);
14988 list_for_each_entry_safe(d_buf, dnext,
14989 &dmabuf->dbuf.list, list) {
14990 list_del_init(&d_buf->list);
14991 lpfc_in_buf_free(vport->phba, d_buf);
14992 }
14993 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14994 }
14995}
14996
14997/**
14998 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14999 * @vport: The vport that the received sequences were sent to.
15000 *
15001 * This function determines whether any received sequences have timed out by
15002 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
15003 * indicates that there is at least one timed out sequence this routine will
15004 * go through the received sequences one at a time from most inactive to most
15005 * active to determine which ones need to be cleaned up. Once it has determined
15006 * that a sequence needs to be cleaned up it will simply free up the resources
15007 * without sending an abort.
15008 **/
15009void
15010lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
15011{
15012 struct lpfc_dmabuf *h_buf, *hnext;
15013 struct lpfc_dmabuf *d_buf, *dnext;
15014 struct hbq_dmabuf *dmabuf = NULL;
15015 unsigned long timeout;
15016 int abort_count = 0;
15017
15018 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15019 vport->rcv_buffer_time_stamp);
15020 if (list_empty(&vport->rcv_buffer_list) ||
15021 time_before(jiffies, timeout))
15022 return;
15023 /* start with the oldest sequence on the rcv list */
15024 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15025 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15026 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15027 dmabuf->time_stamp);
15028 if (time_before(jiffies, timeout))
15029 break;
15030 abort_count++;
15031 list_del_init(&dmabuf->hbuf.list);
15032 list_for_each_entry_safe(d_buf, dnext,
15033 &dmabuf->dbuf.list, list) {
15034 list_del_init(&d_buf->list);
15035 lpfc_in_buf_free(vport->phba, d_buf);
15036 }
15037 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15038 }
15039 if (abort_count)
15040 lpfc_update_rcv_time_stamp(vport);
15041}
15042
15043/**
James Smart4f774512009-05-22 14:52:35 -040015044 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
15045 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
15046 *
15047 * This function searches through the existing incomplete sequences that have
15048 * been sent to this @vport. If the frame matches one of the incomplete
15049 * sequences then the dbuf in the @dmabuf is added to the list of frames that
15050 * make up that sequence. If no sequence is found that matches this frame then
15051 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
15052 * This function returns a pointer to the first dmabuf in the sequence list that
15053 * the frame was linked to.
15054 **/
15055static struct hbq_dmabuf *
15056lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15057{
15058 struct fc_frame_header *new_hdr;
15059 struct fc_frame_header *temp_hdr;
15060 struct lpfc_dmabuf *d_buf;
15061 struct lpfc_dmabuf *h_buf;
15062 struct hbq_dmabuf *seq_dmabuf = NULL;
15063 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050015064 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040015065
James Smart4d9ab992009-10-02 15:16:39 -040015066 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015067 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040015068 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050015069
James Smart4f774512009-05-22 14:52:35 -040015070 /* Use the hdr_buf to find the sequence that this frame belongs to */
15071 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15072 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15073 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15074 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15075 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15076 continue;
15077 /* found a pending sequence that matches this frame */
15078 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15079 break;
15080 }
15081 if (!seq_dmabuf) {
15082 /*
15083 * This indicates first frame received for this sequence.
15084 * Queue the buffer on the vport's rcv_buffer_list.
15085 */
15086 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040015087 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015088 return dmabuf;
15089 }
15090 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050015091 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
15092 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040015093 list_del_init(&seq_dmabuf->hbuf.list);
15094 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15095 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015096 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015097 return dmabuf;
15098 }
James Smart45ed1192009-10-02 15:17:02 -040015099 /* move this sequence to the tail to indicate a young sequence */
15100 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
15101 seq_dmabuf->time_stamp = jiffies;
15102 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050015103 if (list_empty(&seq_dmabuf->dbuf.list)) {
15104 temp_hdr = dmabuf->hbuf.virt;
15105 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15106 return seq_dmabuf;
15107 }
James Smart4f774512009-05-22 14:52:35 -040015108 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050015109 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
15110 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040015111 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15112 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
15113 /*
15114 * If the frame's sequence count is greater than the frame on
15115 * the list then insert the frame right after this frame
15116 */
James Smarteeead812009-12-21 17:01:23 -050015117 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
15118 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040015119 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050015120 found = 1;
15121 break;
James Smart4f774512009-05-22 14:52:35 -040015122 }
James Smart4360ca92015-12-16 18:12:04 -050015123
15124 if (&d_buf->list == &seq_dmabuf->dbuf.list)
15125 break;
15126 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040015127 }
James Smart4360ca92015-12-16 18:12:04 -050015128
15129 if (found)
15130 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015131 return NULL;
15132}
15133
15134/**
James Smart6669f9b2009-10-02 15:16:45 -040015135 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
15136 * @vport: pointer to a vitural port
15137 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15138 *
15139 * This function tries to abort from the partially assembed sequence, described
15140 * by the information from basic abbort @dmabuf. It checks to see whether such
15141 * partially assembled sequence held by the driver. If so, it shall free up all
15142 * the frames from the partially assembled sequence.
15143 *
15144 * Return
15145 * true -- if there is matching partially assembled sequence present and all
15146 * the frames freed with the sequence;
15147 * false -- if there is no matching partially assembled sequence present so
15148 * nothing got aborted in the lower layer driver
15149 **/
15150static bool
15151lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
15152 struct hbq_dmabuf *dmabuf)
15153{
15154 struct fc_frame_header *new_hdr;
15155 struct fc_frame_header *temp_hdr;
15156 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
15157 struct hbq_dmabuf *seq_dmabuf = NULL;
15158
15159 /* Use the hdr_buf to find the sequence that matches this frame */
15160 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15161 INIT_LIST_HEAD(&dmabuf->hbuf.list);
15162 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15163 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15164 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15165 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15166 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15167 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15168 continue;
15169 /* found a pending sequence that matches this frame */
15170 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15171 break;
15172 }
15173
15174 /* Free up all the frames from the partially assembled sequence */
15175 if (seq_dmabuf) {
15176 list_for_each_entry_safe(d_buf, n_buf,
15177 &seq_dmabuf->dbuf.list, list) {
15178 list_del_init(&d_buf->list);
15179 lpfc_in_buf_free(vport->phba, d_buf);
15180 }
15181 return true;
15182 }
15183 return false;
15184}
15185
15186/**
James Smart6dd9e312013-01-03 15:43:37 -050015187 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15188 * @vport: pointer to a vitural port
15189 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15190 *
15191 * This function tries to abort from the assembed sequence from upper level
15192 * protocol, described by the information from basic abbort @dmabuf. It
15193 * checks to see whether such pending context exists at upper level protocol.
15194 * If so, it shall clean up the pending context.
15195 *
15196 * Return
15197 * true -- if there is matching pending context of the sequence cleaned
15198 * at ulp;
15199 * false -- if there is no matching pending context of the sequence present
15200 * at ulp.
15201 **/
15202static bool
15203lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15204{
15205 struct lpfc_hba *phba = vport->phba;
15206 int handled;
15207
15208 /* Accepting abort at ulp with SLI4 only */
15209 if (phba->sli_rev < LPFC_SLI_REV4)
15210 return false;
15211
15212 /* Register all caring upper level protocols to attend abort */
15213 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15214 if (handled)
15215 return true;
15216
15217 return false;
15218}
15219
15220/**
James Smart546fc852011-03-11 16:06:29 -050015221 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040015222 * @phba: Pointer to HBA context object.
15223 * @cmd_iocbq: pointer to the command iocbq structure.
15224 * @rsp_iocbq: pointer to the response iocbq structure.
15225 *
James Smart546fc852011-03-11 16:06:29 -050015226 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040015227 * event. It properly releases the memory allocated to the sequence abort
15228 * accept iocb.
15229 **/
15230static void
James Smart546fc852011-03-11 16:06:29 -050015231lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040015232 struct lpfc_iocbq *cmd_iocbq,
15233 struct lpfc_iocbq *rsp_iocbq)
15234{
James Smart6dd9e312013-01-03 15:43:37 -050015235 struct lpfc_nodelist *ndlp;
15236
15237 if (cmd_iocbq) {
15238 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15239 lpfc_nlp_put(ndlp);
15240 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015241 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050015242 }
James Smart6b5151f2012-01-18 16:24:06 -050015243
15244 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15245 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15246 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15247 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
15248 rsp_iocbq->iocb.ulpStatus,
15249 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040015250}
15251
15252/**
James Smart6d368e52011-05-24 11:44:12 -040015253 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15254 * @phba: Pointer to HBA context object.
15255 * @xri: xri id in transaction.
15256 *
15257 * This function validates the xri maps to the known range of XRIs allocated an
15258 * used by the driver.
15259 **/
James Smart7851fe22011-07-22 18:36:52 -040015260uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015261lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15262 uint16_t xri)
15263{
James Smarta2fc4aef2014-09-03 12:57:55 -040015264 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040015265
15266 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15267 if (xri == phba->sli4_hba.xri_ids[i])
15268 return i;
15269 }
15270 return NO_XRI;
15271}
15272
James Smart6d368e52011-05-24 11:44:12 -040015273/**
James Smart546fc852011-03-11 16:06:29 -050015274 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015275 * @phba: Pointer to HBA context object.
15276 * @fc_hdr: pointer to a FC frame header.
15277 *
James Smart546fc852011-03-11 16:06:29 -050015278 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015279 * event after aborting the sequence handling.
15280 **/
15281static void
James Smart6dd9e312013-01-03 15:43:37 -050015282lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15283 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040015284{
James Smart6dd9e312013-01-03 15:43:37 -050015285 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040015286 struct lpfc_iocbq *ctiocb = NULL;
15287 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040015288 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050015289 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040015290 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050015291 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040015292
15293 if (!lpfc_is_link_up(phba))
15294 return;
15295
15296 sid = sli4_sid_from_fc_hdr(fc_hdr);
15297 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050015298 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040015299
James Smart6dd9e312013-01-03 15:43:37 -050015300 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040015301 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050015302 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15303 if (!ndlp) {
15304 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15305 "1268 Failed to allocate ndlp for "
15306 "oxid:x%x SID:x%x\n", oxid, sid);
15307 return;
15308 }
15309 lpfc_nlp_init(vport, ndlp, sid);
15310 /* Put ndlp onto pport node list */
15311 lpfc_enqueue_node(vport, ndlp);
15312 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15313 /* re-setup ndlp without removing from node list */
15314 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15315 if (!ndlp) {
15316 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15317 "3275 Failed to active ndlp found "
15318 "for oxid:x%x SID:x%x\n", oxid, sid);
15319 return;
15320 }
James Smart6669f9b2009-10-02 15:16:45 -040015321 }
15322
James Smart546fc852011-03-11 16:06:29 -050015323 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040015324 ctiocb = lpfc_sli_get_iocbq(phba);
15325 if (!ctiocb)
15326 return;
15327
James Smart5ffc2662009-11-18 15:39:44 -050015328 /* Extract the F_CTL field from FC_HDR */
15329 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15330
James Smart6669f9b2009-10-02 15:16:45 -040015331 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040015332 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050015333 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040015334 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15335 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15336 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15337
15338 /* Fill in the rest of iocb fields */
15339 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15340 icmd->ulpBdeCount = 0;
15341 icmd->ulpLe = 1;
15342 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040015343 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050015344 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015345
James Smart6669f9b2009-10-02 15:16:45 -040015346 ctiocb->iocb_cmpl = NULL;
15347 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050015348 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040015349 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050015350 ctiocb->sli4_xritag = NO_XRI;
15351
James Smartee0f4fe2012-05-09 21:19:14 -040015352 if (fctl & FC_FC_EX_CTX)
15353 /* Exchange responder sent the abort so we
15354 * own the oxid.
15355 */
15356 xri = oxid;
15357 else
15358 xri = rxid;
15359 lxri = lpfc_sli4_xri_inrange(phba, xri);
15360 if (lxri != NO_XRI)
15361 lpfc_set_rrq_active(phba, ndlp, lxri,
15362 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050015363 /* For BA_ABTS from exchange responder, if the logical xri with
15364 * the oxid maps to the FCP XRI range, the port no longer has
15365 * that exchange context, send a BLS_RJT. Override the IOCB for
15366 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050015367 */
James Smart6dd9e312013-01-03 15:43:37 -050015368 if ((fctl & FC_FC_EX_CTX) &&
15369 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
15370 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15371 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15372 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15373 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15374 }
15375
15376 /* If BA_ABTS failed to abort a partially assembled receive sequence,
15377 * the driver no longer has that exchange, send a BLS_RJT. Override
15378 * the IOCB for a BA_RJT.
15379 */
15380 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050015381 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15382 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15383 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15384 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15385 }
James Smart6669f9b2009-10-02 15:16:45 -040015386
James Smart5ffc2662009-11-18 15:39:44 -050015387 if (fctl & FC_FC_EX_CTX) {
15388 /* ABTS sent by responder to CT exchange, construction
15389 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15390 * field and RX_ID from ABTS for RX_ID field.
15391 */
James Smart546fc852011-03-11 16:06:29 -050015392 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050015393 } else {
15394 /* ABTS sent by initiator to CT exchange, construction
15395 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050015396 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050015397 */
James Smart546fc852011-03-11 16:06:29 -050015398 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050015399 }
James Smartf09c3ac2012-03-01 22:33:29 -050015400 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050015401 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050015402
James Smart546fc852011-03-11 16:06:29 -050015403 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050015404 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15405 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15406 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050015407
15408 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15409 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050015410 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15411 "2925 Failed to issue CT ABTS RSP x%x on "
15412 "xri x%x, Data x%x\n",
15413 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15414 phba->link_state);
15415 lpfc_nlp_put(ndlp);
15416 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050015417 lpfc_sli_release_iocbq(phba, ctiocb);
15418 }
James Smart6669f9b2009-10-02 15:16:45 -040015419}
15420
15421/**
15422 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15423 * @vport: Pointer to the vport on which this sequence was received
15424 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15425 *
15426 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15427 * receive sequence is only partially assembed by the driver, it shall abort
15428 * the partially assembled frames for the sequence. Otherwise, if the
15429 * unsolicited receive sequence has been completely assembled and passed to
15430 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15431 * unsolicited sequence has been aborted. After that, it will issue a basic
15432 * accept to accept the abort.
15433 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015434static void
James Smart6669f9b2009-10-02 15:16:45 -040015435lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15436 struct hbq_dmabuf *dmabuf)
15437{
15438 struct lpfc_hba *phba = vport->phba;
15439 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050015440 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050015441 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040015442
James Smart6669f9b2009-10-02 15:16:45 -040015443 /* Make a copy of fc_hdr before the dmabuf being released */
15444 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050015445 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040015446
James Smart5ffc2662009-11-18 15:39:44 -050015447 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050015448 /* ABTS by responder to exchange, no cleanup needed */
15449 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050015450 } else {
James Smart6dd9e312013-01-03 15:43:37 -050015451 /* ABTS by initiator to exchange, need to do cleanup */
15452 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15453 if (aborted == false)
15454 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050015455 }
James Smart6dd9e312013-01-03 15:43:37 -050015456 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15457
15458 /* Respond with BA_ACC or BA_RJT accordingly */
15459 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040015460}
15461
15462/**
James Smart4f774512009-05-22 14:52:35 -040015463 * lpfc_seq_complete - Indicates if a sequence is complete
15464 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15465 *
15466 * This function checks the sequence, starting with the frame described by
15467 * @dmabuf, to see if all the frames associated with this sequence are present.
15468 * the frames associated with this sequence are linked to the @dmabuf using the
15469 * dbuf list. This function looks for two major things. 1) That the first frame
15470 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15471 * set. 3) That there are no holes in the sequence count. The function will
15472 * return 1 when the sequence is complete, otherwise it will return 0.
15473 **/
15474static int
15475lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15476{
15477 struct fc_frame_header *hdr;
15478 struct lpfc_dmabuf *d_buf;
15479 struct hbq_dmabuf *seq_dmabuf;
15480 uint32_t fctl;
15481 int seq_count = 0;
15482
15483 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15484 /* make sure first fame of sequence has a sequence count of zero */
15485 if (hdr->fh_seq_cnt != seq_count)
15486 return 0;
15487 fctl = (hdr->fh_f_ctl[0] << 16 |
15488 hdr->fh_f_ctl[1] << 8 |
15489 hdr->fh_f_ctl[2]);
15490 /* If last frame of sequence we can return success. */
15491 if (fctl & FC_FC_END_SEQ)
15492 return 1;
15493 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15494 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15495 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15496 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050015497 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040015498 return 0;
15499 fctl = (hdr->fh_f_ctl[0] << 16 |
15500 hdr->fh_f_ctl[1] << 8 |
15501 hdr->fh_f_ctl[2]);
15502 /* If last frame of sequence we can return success. */
15503 if (fctl & FC_FC_END_SEQ)
15504 return 1;
15505 }
15506 return 0;
15507}
15508
15509/**
15510 * lpfc_prep_seq - Prep sequence for ULP processing
15511 * @vport: Pointer to the vport on which this sequence was received
15512 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15513 *
15514 * This function takes a sequence, described by a list of frames, and creates
15515 * a list of iocbq structures to describe the sequence. This iocbq list will be
15516 * used to issue to the generic unsolicited sequence handler. This routine
15517 * returns a pointer to the first iocbq in the list. If the function is unable
15518 * to allocate an iocbq then it throw out the received frames that were not
15519 * able to be described and return a pointer to the first iocbq. If unable to
15520 * allocate any iocbqs (including the first) this function will return NULL.
15521 **/
15522static struct lpfc_iocbq *
15523lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
15524{
James Smart7851fe22011-07-22 18:36:52 -040015525 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040015526 struct lpfc_dmabuf *d_buf, *n_buf;
15527 struct lpfc_iocbq *first_iocbq, *iocbq;
15528 struct fc_frame_header *fc_hdr;
15529 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040015530 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050015531 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040015532
15533 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15534 /* remove from receive buffer list */
15535 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015536 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015537 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040015538 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040015539 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015540 /* Get an iocbq struct to fill in. */
15541 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
15542 if (first_iocbq) {
15543 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040015544 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015545 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040015546
15547 /* Check FC Header to see what TYPE of frame we are rcv'ing */
15548 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
15549 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
15550 first_iocbq->iocb.un.rcvels.parmRo =
15551 sli4_did_from_fc_hdr(fc_hdr);
15552 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
15553 } else
15554 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040015555 first_iocbq->iocb.ulpContext = NO_XRI;
15556 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
15557 be16_to_cpu(fc_hdr->fh_ox_id);
15558 /* iocbq is prepped for internal consumption. Physical vpi. */
15559 first_iocbq->iocb.unsli3.rcvsli3.vpi =
15560 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040015561 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040015562 tot_len = bf_get(lpfc_rcqe_length,
15563 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
15564
James Smart4f774512009-05-22 14:52:35 -040015565 first_iocbq->context2 = &seq_dmabuf->dbuf;
15566 first_iocbq->context3 = NULL;
15567 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040015568 if (tot_len > LPFC_DATA_BUF_SIZE)
15569 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040015570 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040015571 else
15572 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
15573
James Smart4f774512009-05-22 14:52:35 -040015574 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040015575
James Smart7851fe22011-07-22 18:36:52 -040015576 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040015577 }
15578 iocbq = first_iocbq;
15579 /*
15580 * Each IOCBq can have two Buffers assigned, so go through the list
15581 * of buffers for this sequence and save two buffers in each IOCBq
15582 */
15583 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
15584 if (!iocbq) {
15585 lpfc_in_buf_free(vport->phba, d_buf);
15586 continue;
15587 }
15588 if (!iocbq->context3) {
15589 iocbq->context3 = d_buf;
15590 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040015591 /* We need to get the size out of the right CQE */
15592 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15593 len = bf_get(lpfc_rcqe_length,
15594 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015595 pbde = (struct ulp_bde64 *)
15596 &iocbq->iocb.unsli3.sli3Words[4];
15597 if (len > LPFC_DATA_BUF_SIZE)
15598 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
15599 else
15600 pbde->tus.f.bdeSize = len;
15601
James Smart7851fe22011-07-22 18:36:52 -040015602 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
15603 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040015604 } else {
15605 iocbq = lpfc_sli_get_iocbq(vport->phba);
15606 if (!iocbq) {
15607 if (first_iocbq) {
15608 first_iocbq->iocb.ulpStatus =
15609 IOSTAT_FCP_RSP_ERROR;
15610 first_iocbq->iocb.un.ulpWord[4] =
15611 IOERR_NO_RESOURCES;
15612 }
15613 lpfc_in_buf_free(vport->phba, d_buf);
15614 continue;
15615 }
James Smart7851fe22011-07-22 18:36:52 -040015616 /* We need to get the size out of the right CQE */
15617 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15618 len = bf_get(lpfc_rcqe_length,
15619 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015620 iocbq->context2 = d_buf;
15621 iocbq->context3 = NULL;
15622 iocbq->iocb.ulpBdeCount = 1;
15623 if (len > LPFC_DATA_BUF_SIZE)
15624 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15625 LPFC_DATA_BUF_SIZE;
15626 else
15627 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
15628
James Smart7851fe22011-07-22 18:36:52 -040015629 tot_len += len;
15630 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15631
James Smart4f774512009-05-22 14:52:35 -040015632 iocbq->iocb.un.rcvels.remoteID = sid;
15633 list_add_tail(&iocbq->list, &first_iocbq->list);
15634 }
15635 }
15636 return first_iocbq;
15637}
15638
James Smart6669f9b2009-10-02 15:16:45 -040015639static void
15640lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
15641 struct hbq_dmabuf *seq_dmabuf)
15642{
15643 struct fc_frame_header *fc_hdr;
15644 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15645 struct lpfc_hba *phba = vport->phba;
15646
15647 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15648 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15649 if (!iocbq) {
15650 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15651 "2707 Ring %d handler: Failed to allocate "
15652 "iocb Rctl x%x Type x%x received\n",
15653 LPFC_ELS_RING,
15654 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15655 return;
15656 }
15657 if (!lpfc_complete_unsol_iocb(phba,
15658 &phba->sli.ring[LPFC_ELS_RING],
15659 iocbq, fc_hdr->fh_r_ctl,
15660 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040015661 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040015662 "2540 Ring %d handler: unexpected Rctl "
15663 "x%x Type x%x received\n",
15664 LPFC_ELS_RING,
15665 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15666
15667 /* Free iocb created in lpfc_prep_seq */
15668 list_for_each_entry_safe(curr_iocb, next_iocb,
15669 &iocbq->list, list) {
15670 list_del_init(&curr_iocb->list);
15671 lpfc_sli_release_iocbq(phba, curr_iocb);
15672 }
15673 lpfc_sli_release_iocbq(phba, iocbq);
15674}
15675
James Smart4f774512009-05-22 14:52:35 -040015676/**
15677 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15678 * @phba: Pointer to HBA context object.
15679 *
15680 * This function is called with no lock held. This function processes all
15681 * the received buffers and gives it to upper layers when a received buffer
15682 * indicates that it is the final frame in the sequence. The interrupt
15683 * service routine processes received buffers at interrupt contexts and adds
15684 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15685 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15686 * appropriate receive function when the final frame in a sequence is received.
15687 **/
James Smart4d9ab992009-10-02 15:16:39 -040015688void
15689lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15690 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040015691{
James Smart4d9ab992009-10-02 15:16:39 -040015692 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015693 struct fc_frame_header *fc_hdr;
15694 struct lpfc_vport *vport;
15695 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040015696 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040015697
James Smart4f774512009-05-22 14:52:35 -040015698 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040015699 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15700 /* check to see if this a valid type of frame */
15701 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15702 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15703 return;
15704 }
James Smart7851fe22011-07-22 18:36:52 -040015705 if ((bf_get(lpfc_cqe_code,
15706 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15707 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15708 &dmabuf->cq_event.cqe.rcqe_cmpl);
15709 else
15710 fcfi = bf_get(lpfc_rcqe_fcf_id,
15711 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040015712
James Smart4d9ab992009-10-02 15:16:39 -040015713 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040015714 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040015715 /* throw out the frame */
15716 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15717 return;
15718 }
James Smart939723a2012-05-09 21:19:03 -040015719
15720 /* d_id this frame is directed to */
15721 did = sli4_did_from_fc_hdr(fc_hdr);
15722
15723 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15724 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15725 (did != Fabric_DID)) {
15726 /*
15727 * Throw out the frame if we are not pt2pt.
15728 * The pt2pt protocol allows for discovery frames
15729 * to be received without a registered VPI.
15730 */
15731 if (!(vport->fc_flag & FC_PT2PT) ||
15732 (phba->link_state == LPFC_HBA_READY)) {
15733 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15734 return;
15735 }
15736 }
15737
James Smart6669f9b2009-10-02 15:16:45 -040015738 /* Handle the basic abort sequence (BA_ABTS) event */
15739 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15740 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15741 return;
15742 }
15743
James Smart4d9ab992009-10-02 15:16:39 -040015744 /* Link this frame */
15745 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15746 if (!seq_dmabuf) {
15747 /* unable to add frame to vport - throw it out */
15748 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15749 return;
15750 }
15751 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050015752 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040015753 return;
James Smartdef9c7a2009-12-21 17:02:28 -050015754
James Smart6669f9b2009-10-02 15:16:45 -040015755 /* Send the complete sequence to the upper layer protocol */
15756 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040015757}
James Smart6fb120a2009-05-22 14:52:59 -040015758
15759/**
15760 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15761 * @phba: pointer to lpfc hba data structure.
15762 *
15763 * This routine is invoked to post rpi header templates to the
15764 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015765 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15766 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015767 *
15768 * This routine does not require any locks. It's usage is expected
15769 * to be driver load or reset recovery when the driver is
15770 * sequential.
15771 *
15772 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015773 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015774 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015775 * When this error occurs, the driver is not guaranteed
15776 * to have any rpi regions posted to the device and
15777 * must either attempt to repost the regions or take a
15778 * fatal error.
15779 **/
15780int
15781lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15782{
15783 struct lpfc_rpi_hdr *rpi_page;
15784 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040015785 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015786
James Smart6d368e52011-05-24 11:44:12 -040015787 /* SLI4 ports that support extents do not require RPI headers. */
15788 if (!phba->sli4_hba.rpi_hdrs_in_use)
15789 goto exit;
15790 if (phba->sli4_hba.extents_in_use)
15791 return -EIO;
15792
James Smart6fb120a2009-05-22 14:52:59 -040015793 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040015794 /*
15795 * Assign the rpi headers a physical rpi only if the driver
15796 * has not initialized those resources. A port reset only
15797 * needs the headers posted.
15798 */
15799 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15800 LPFC_RPI_RSRC_RDY)
15801 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15802
James Smart6fb120a2009-05-22 14:52:59 -040015803 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15804 if (rc != MBX_SUCCESS) {
15805 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15806 "2008 Error %d posting all rpi "
15807 "headers\n", rc);
15808 rc = -EIO;
15809 break;
15810 }
15811 }
15812
James Smart6d368e52011-05-24 11:44:12 -040015813 exit:
15814 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15815 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040015816 return rc;
15817}
15818
15819/**
15820 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15821 * @phba: pointer to lpfc hba data structure.
15822 * @rpi_page: pointer to the rpi memory region.
15823 *
15824 * This routine is invoked to post a single rpi header to the
15825 * HBA consistent with the SLI-4 interface spec. This memory region
15826 * maps up to 64 rpi context regions.
15827 *
15828 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015829 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015830 * -ENOMEM - No available memory
15831 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015832 **/
15833int
15834lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15835{
15836 LPFC_MBOXQ_t *mboxq;
15837 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15838 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015839 uint32_t shdr_status, shdr_add_status;
15840 union lpfc_sli4_cfg_shdr *shdr;
15841
James Smart6d368e52011-05-24 11:44:12 -040015842 /* SLI4 ports that support extents do not require RPI headers. */
15843 if (!phba->sli4_hba.rpi_hdrs_in_use)
15844 return rc;
15845 if (phba->sli4_hba.extents_in_use)
15846 return -EIO;
15847
James Smart6fb120a2009-05-22 14:52:59 -040015848 /* The port is notified of the header region via a mailbox command. */
15849 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15850 if (!mboxq) {
15851 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15852 "2001 Unable to allocate memory for issuing "
15853 "SLI_CONFIG_SPECIAL mailbox command\n");
15854 return -ENOMEM;
15855 }
15856
15857 /* Post all rpi memory regions to the port. */
15858 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040015859 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15860 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15861 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050015862 sizeof(struct lpfc_sli4_cfg_mhdr),
15863 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040015864
15865
15866 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040015867 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15868 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040015869 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15870 hdr_tmpl, rpi_page->page_count);
15871
James Smart6fb120a2009-05-22 14:52:59 -040015872 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15873 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040015874 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040015875 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15876 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15877 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15878 if (rc != MBX_TIMEOUT)
15879 mempool_free(mboxq, phba->mbox_mem_pool);
15880 if (shdr_status || shdr_add_status || rc) {
15881 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15882 "2514 POST_RPI_HDR mailbox failed with "
15883 "status x%x add_status x%x, mbx status x%x\n",
15884 shdr_status, shdr_add_status, rc);
15885 rc = -ENXIO;
15886 }
15887 return rc;
15888}
15889
15890/**
15891 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15892 * @phba: pointer to lpfc hba data structure.
15893 *
15894 * This routine is invoked to post rpi header templates to the
15895 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015896 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15897 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015898 *
15899 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015900 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040015901 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15902 **/
15903int
15904lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15905{
James Smart6d368e52011-05-24 11:44:12 -040015906 unsigned long rpi;
15907 uint16_t max_rpi, rpi_limit;
15908 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015909 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040015910 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040015911
James Smart6fb120a2009-05-22 14:52:59 -040015912 /*
James Smart6d368e52011-05-24 11:44:12 -040015913 * Fetch the next logical rpi. Because this index is logical,
15914 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040015915 */
James Smart4902b382013-10-10 12:20:35 -040015916 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040015917 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
15918 rpi_limit = phba->sli4_hba.next_rpi;
15919
James Smart6d368e52011-05-24 11:44:12 -040015920 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15921 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040015922 rpi = LPFC_RPI_ALLOC_ERROR;
15923 else {
15924 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15925 phba->sli4_hba.max_cfg_param.rpi_used++;
15926 phba->sli4_hba.rpi_count++;
15927 }
James Smartbe6bb942015-04-07 15:07:22 -040015928 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15929 "0001 rpi:%x max:%x lim:%x\n",
15930 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040015931
15932 /*
15933 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040015934 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040015935 */
15936 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15937 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040015938 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015939 return rpi;
15940 }
15941
15942 /*
James Smart6d368e52011-05-24 11:44:12 -040015943 * RPI header postings are not required for SLI4 ports capable of
15944 * extents.
15945 */
15946 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040015947 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015948 return rpi;
15949 }
15950
15951 /*
James Smart6fb120a2009-05-22 14:52:59 -040015952 * If the driver is running low on rpi resources, allocate another
15953 * page now. Note that the next_rpi value is used because
15954 * it represents how many are actually in use whereas max_rpi notes
15955 * how many are supported max by the device.
15956 */
James Smart6d368e52011-05-24 11:44:12 -040015957 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040015958 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015959 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15960 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15961 if (!rpi_hdr) {
15962 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15963 "2002 Error Could not grow rpi "
15964 "count\n");
15965 } else {
James Smart6d368e52011-05-24 11:44:12 -040015966 lrpi = rpi_hdr->start_rpi;
15967 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040015968 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15969 }
15970 }
15971
15972 return rpi;
15973}
15974
15975/**
15976 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15977 * @phba: pointer to lpfc hba data structure.
15978 *
15979 * This routine is invoked to release an rpi to the pool of
15980 * available rpis maintained by the driver.
15981 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015982static void
James Smartd7c47992010-06-08 18:31:54 -040015983__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15984{
15985 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15986 phba->sli4_hba.rpi_count--;
15987 phba->sli4_hba.max_cfg_param.rpi_used--;
15988 }
15989}
15990
15991/**
15992 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15993 * @phba: pointer to lpfc hba data structure.
15994 *
15995 * This routine is invoked to release an rpi to the pool of
15996 * available rpis maintained by the driver.
15997 **/
15998void
James Smart6fb120a2009-05-22 14:52:59 -040015999lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16000{
16001 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040016002 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040016003 spin_unlock_irq(&phba->hbalock);
16004}
16005
16006/**
16007 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
16008 * @phba: pointer to lpfc hba data structure.
16009 *
16010 * This routine is invoked to remove the memory region that
16011 * provided rpi via a bitmask.
16012 **/
16013void
16014lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
16015{
16016 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040016017 kfree(phba->sli4_hba.rpi_ids);
16018 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040016019}
16020
16021/**
16022 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
16023 * @phba: pointer to lpfc hba data structure.
16024 *
16025 * This routine is invoked to remove the memory region that
16026 * provided rpi via a bitmask.
16027 **/
16028int
James Smart6b5151f2012-01-18 16:24:06 -050016029lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
16030 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040016031{
16032 LPFC_MBOXQ_t *mboxq;
16033 struct lpfc_hba *phba = ndlp->phba;
16034 int rc;
16035
16036 /* The port is notified of the header region via a mailbox command. */
16037 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16038 if (!mboxq)
16039 return -ENOMEM;
16040
16041 /* Post all rpi memory regions to the port. */
16042 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050016043 if (cmpl) {
16044 mboxq->mbox_cmpl = cmpl;
16045 mboxq->context1 = arg;
16046 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050016047 } else
16048 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050016049 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040016050 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16051 if (rc == MBX_NOT_FINISHED) {
16052 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16053 "2010 Resume RPI Mailbox failed "
16054 "status %d, mbxStatus x%x\n", rc,
16055 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16056 mempool_free(mboxq, phba->mbox_mem_pool);
16057 return -EIO;
16058 }
16059 return 0;
16060}
16061
16062/**
16063 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050016064 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040016065 *
James Smart76a95d72010-11-20 23:11:48 -050016066 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040016067 *
16068 * Returns:
16069 * 0 success
16070 * -Evalue otherwise
16071 **/
16072int
James Smart76a95d72010-11-20 23:11:48 -050016073lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040016074{
16075 LPFC_MBOXQ_t *mboxq;
16076 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040016077 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040016078 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050016079 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040016080 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16081 if (!mboxq)
16082 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050016083 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040016084 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040016085 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040016086 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050016087 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040016088 "2022 INIT VPI Mailbox failed "
16089 "status %d, mbxStatus x%x\n", rc,
16090 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040016091 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040016092 }
James Smart6a9c52c2009-10-02 15:16:51 -040016093 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050016094 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040016095
16096 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040016097}
16098
16099/**
16100 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
16101 * @phba: pointer to lpfc hba data structure.
16102 * @mboxq: Pointer to mailbox object.
16103 *
16104 * This routine is invoked to manually add a single FCF record. The caller
16105 * must pass a completely initialized FCF_Record. This routine takes
16106 * care of the nonembedded mailbox operations.
16107 **/
16108static void
16109lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
16110{
16111 void *virt_addr;
16112 union lpfc_sli4_cfg_shdr *shdr;
16113 uint32_t shdr_status, shdr_add_status;
16114
16115 virt_addr = mboxq->sge_array->addr[0];
16116 /* The IOCTL status is embedded in the mailbox subheader. */
16117 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
16118 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16119 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16120
16121 if ((shdr_status || shdr_add_status) &&
16122 (shdr_status != STATUS_FCF_IN_USE))
16123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16124 "2558 ADD_FCF_RECORD mailbox failed with "
16125 "status x%x add_status x%x\n",
16126 shdr_status, shdr_add_status);
16127
16128 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16129}
16130
16131/**
16132 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
16133 * @phba: pointer to lpfc hba data structure.
16134 * @fcf_record: pointer to the initialized fcf record to add.
16135 *
16136 * This routine is invoked to manually add a single FCF record. The caller
16137 * must pass a completely initialized FCF_Record. This routine takes
16138 * care of the nonembedded mailbox operations.
16139 **/
16140int
16141lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
16142{
16143 int rc = 0;
16144 LPFC_MBOXQ_t *mboxq;
16145 uint8_t *bytep;
16146 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040016147 struct lpfc_mbx_sge sge;
16148 uint32_t alloc_len, req_len;
16149 uint32_t fcfindex;
16150
16151 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16152 if (!mboxq) {
16153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16154 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
16155 return -ENOMEM;
16156 }
16157
16158 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
16159 sizeof(uint32_t);
16160
16161 /* Allocate DMA memory and set up the non-embedded mailbox command */
16162 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16163 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
16164 req_len, LPFC_SLI4_MBX_NEMBED);
16165 if (alloc_len < req_len) {
16166 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16167 "2523 Allocated DMA memory size (x%x) is "
16168 "less than the requested DMA memory "
16169 "size (x%x)\n", alloc_len, req_len);
16170 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16171 return -ENOMEM;
16172 }
16173
16174 /*
16175 * Get the first SGE entry from the non-embedded DMA memory. This
16176 * routine only uses a single SGE.
16177 */
16178 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040016179 virt_addr = mboxq->sge_array->addr[0];
16180 /*
16181 * Configure the FCF record for FCFI 0. This is the driver's
16182 * hardcoded default and gets used in nonFIP mode.
16183 */
16184 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16185 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16186 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16187
16188 /*
16189 * Copy the fcf_index and the FCF Record Data. The data starts after
16190 * the FCoE header plus word10. The data copy needs to be endian
16191 * correct.
16192 */
16193 bytep += sizeof(uint32_t);
16194 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16195 mboxq->vport = phba->pport;
16196 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16197 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16198 if (rc == MBX_NOT_FINISHED) {
16199 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16200 "2515 ADD_FCF_RECORD mailbox failed with "
16201 "status 0x%x\n", rc);
16202 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16203 rc = -EIO;
16204 } else
16205 rc = 0;
16206
16207 return rc;
16208}
16209
16210/**
16211 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16212 * @phba: pointer to lpfc hba data structure.
16213 * @fcf_record: pointer to the fcf record to write the default data.
16214 * @fcf_index: FCF table entry index.
16215 *
16216 * This routine is invoked to build the driver's default FCF record. The
16217 * values used are hardcoded. This routine handles memory initialization.
16218 *
16219 **/
16220void
16221lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16222 struct fcf_record *fcf_record,
16223 uint16_t fcf_index)
16224{
16225 memset(fcf_record, 0, sizeof(struct fcf_record));
16226 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16227 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16228 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16229 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16230 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16231 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16232 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16233 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16234 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16235 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16236 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16237 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16238 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040016239 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040016240 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16241 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16242 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16243 /* Set the VLAN bit map */
16244 if (phba->valid_vlan) {
16245 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16246 = 1 << (phba->vlan_id % 8);
16247 }
16248}
16249
16250/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016251 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040016252 * @phba: pointer to lpfc hba data structure.
16253 * @fcf_index: FCF table entry offset.
16254 *
James Smart0c9ab6f2010-02-26 14:15:57 -050016255 * This routine is invoked to scan the entire FCF table by reading FCF
16256 * record and processing it one at a time starting from the @fcf_index
16257 * for initial FCF discovery or fast FCF failover rediscovery.
16258 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016259 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016260 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040016261 **/
16262int
James Smart0c9ab6f2010-02-26 14:15:57 -050016263lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040016264{
16265 int rc = 0, error;
16266 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040016267
James Smart32b97932009-07-19 10:01:21 -040016268 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050016269 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040016270 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16271 if (!mboxq) {
16272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16273 "2000 Failed to allocate mbox for "
16274 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040016275 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050016276 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016277 }
James Smartecfd03c2010-02-12 14:41:27 -050016278 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050016279 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050016280 if (rc) {
16281 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050016282 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016283 }
James Smartecfd03c2010-02-12 14:41:27 -050016284 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040016285 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050016286 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040016287
16288 spin_lock_irq(&phba->hbalock);
16289 phba->hba_flag |= FCF_TS_INPROG;
16290 spin_unlock_irq(&phba->hbalock);
16291
James Smart6fb120a2009-05-22 14:52:59 -040016292 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050016293 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040016294 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050016295 else {
James Smart38b92ef2010-08-04 16:11:39 -040016296 /* Reset eligible FCF count for new scan */
16297 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040016298 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016299 error = 0;
James Smart32b97932009-07-19 10:01:21 -040016300 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016301fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040016302 if (error) {
16303 if (mboxq)
16304 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040016305 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040016306 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040016307 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040016308 spin_unlock_irq(&phba->hbalock);
16309 }
James Smart6fb120a2009-05-22 14:52:59 -040016310 return error;
16311}
James Smarta0c87cb2009-07-19 10:01:10 -040016312
16313/**
James Smarta93ff372010-10-22 11:06:08 -040016314 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050016315 * @phba: pointer to lpfc hba data structure.
16316 * @fcf_index: FCF table entry offset.
16317 *
16318 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040016319 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050016320 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016321 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016322 * otherwise.
16323 **/
16324int
16325lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16326{
16327 int rc = 0, error;
16328 LPFC_MBOXQ_t *mboxq;
16329
16330 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16331 if (!mboxq) {
16332 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16333 "2763 Failed to allocate mbox for "
16334 "READ_FCF cmd\n");
16335 error = -ENOMEM;
16336 goto fail_fcf_read;
16337 }
16338 /* Construct the read FCF record mailbox command */
16339 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16340 if (rc) {
16341 error = -EINVAL;
16342 goto fail_fcf_read;
16343 }
16344 /* Issue the mailbox command asynchronously */
16345 mboxq->vport = phba->pport;
16346 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16347 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16348 if (rc == MBX_NOT_FINISHED)
16349 error = -EIO;
16350 else
16351 error = 0;
16352
16353fail_fcf_read:
16354 if (error && mboxq)
16355 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16356 return error;
16357}
16358
16359/**
16360 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16361 * @phba: pointer to lpfc hba data structure.
16362 * @fcf_index: FCF table entry offset.
16363 *
16364 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040016365 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050016366 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016367 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016368 * otherwise.
16369 **/
16370int
16371lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16372{
16373 int rc = 0, error;
16374 LPFC_MBOXQ_t *mboxq;
16375
16376 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16377 if (!mboxq) {
16378 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16379 "2758 Failed to allocate mbox for "
16380 "READ_FCF cmd\n");
16381 error = -ENOMEM;
16382 goto fail_fcf_read;
16383 }
16384 /* Construct the read FCF record mailbox command */
16385 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16386 if (rc) {
16387 error = -EINVAL;
16388 goto fail_fcf_read;
16389 }
16390 /* Issue the mailbox command asynchronously */
16391 mboxq->vport = phba->pport;
16392 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16393 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16394 if (rc == MBX_NOT_FINISHED)
16395 error = -EIO;
16396 else
16397 error = 0;
16398
16399fail_fcf_read:
16400 if (error && mboxq)
16401 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16402 return error;
16403}
16404
16405/**
James Smartf5cb5302015-12-16 18:11:52 -050016406 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040016407 * phba pointer to the lpfc_hba struct for this port.
16408 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16409 * routine when the rr_bmask is empty. The FCF indecies are put into the
16410 * rr_bmask based on their priority level. Starting from the highest priority
16411 * to the lowest. The most likely FCF candidate will be in the highest
16412 * priority group. When this routine is called it searches the fcf_pri list for
16413 * next lowest priority group and repopulates the rr_bmask with only those
16414 * fcf_indexes.
16415 * returns:
16416 * 1=success 0=failure
16417 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016418static int
James Smart7d791df2011-07-22 18:37:52 -040016419lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16420{
16421 uint16_t next_fcf_pri;
16422 uint16_t last_index;
16423 struct lpfc_fcf_pri *fcf_pri;
16424 int rc;
16425 int ret = 0;
16426
16427 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16428 LPFC_SLI4_FCF_TBL_INDX_MAX);
16429 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16430 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050016431
16432 /* Verify the priority list has 2 or more entries */
16433 spin_lock_irq(&phba->hbalock);
16434 if (list_empty(&phba->fcf.fcf_pri_list) ||
16435 list_is_singular(&phba->fcf.fcf_pri_list)) {
16436 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040016437 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16438 "3061 Last IDX %d\n", last_index);
16439 return 0; /* Empty rr list */
16440 }
James Smart25626692013-03-01 16:36:54 -050016441 spin_unlock_irq(&phba->hbalock);
16442
James Smart7d791df2011-07-22 18:37:52 -040016443 next_fcf_pri = 0;
16444 /*
16445 * Clear the rr_bmask and set all of the bits that are at this
16446 * priority.
16447 */
16448 memset(phba->fcf.fcf_rr_bmask, 0,
16449 sizeof(*phba->fcf.fcf_rr_bmask));
16450 spin_lock_irq(&phba->hbalock);
16451 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16452 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16453 continue;
16454 /*
16455 * the 1st priority that has not FLOGI failed
16456 * will be the highest.
16457 */
16458 if (!next_fcf_pri)
16459 next_fcf_pri = fcf_pri->fcf_rec.priority;
16460 spin_unlock_irq(&phba->hbalock);
16461 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16462 rc = lpfc_sli4_fcf_rr_index_set(phba,
16463 fcf_pri->fcf_rec.fcf_index);
16464 if (rc)
16465 return 0;
16466 }
16467 spin_lock_irq(&phba->hbalock);
16468 }
16469 /*
16470 * if next_fcf_pri was not set above and the list is not empty then
16471 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070016472 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040016473 */
16474 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16475 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16476 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16477 /*
16478 * the 1st priority that has not FLOGI failed
16479 * will be the highest.
16480 */
16481 if (!next_fcf_pri)
16482 next_fcf_pri = fcf_pri->fcf_rec.priority;
16483 spin_unlock_irq(&phba->hbalock);
16484 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16485 rc = lpfc_sli4_fcf_rr_index_set(phba,
16486 fcf_pri->fcf_rec.fcf_index);
16487 if (rc)
16488 return 0;
16489 }
16490 spin_lock_irq(&phba->hbalock);
16491 }
16492 } else
16493 ret = 1;
16494 spin_unlock_irq(&phba->hbalock);
16495
16496 return ret;
16497}
16498/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016499 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16500 * @phba: pointer to lpfc hba data structure.
16501 *
16502 * This routine is to get the next eligible FCF record index in a round
16503 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040016504 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050016505 * shall be returned, otherwise, the next eligible FCF record's index
16506 * shall be returned.
16507 **/
16508uint16_t
16509lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16510{
16511 uint16_t next_fcf_index;
16512
James Smart421c6622013-01-03 15:44:16 -050016513initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040016514 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050016515 next_fcf_index = phba->fcf.current_rec.fcf_indx;
16516
James Smart7d791df2011-07-22 18:37:52 -040016517next_priority:
James Smart421c6622013-01-03 15:44:16 -050016518 /* Determine the next fcf index to check */
16519 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
James Smart0c9ab6f2010-02-26 14:15:57 -050016520 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16521 LPFC_SLI4_FCF_TBL_INDX_MAX,
James Smart3804dc82010-07-14 15:31:37 -040016522 next_fcf_index);
16523
James Smart0c9ab6f2010-02-26 14:15:57 -050016524 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040016525 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16526 /*
16527 * If we have wrapped then we need to clear the bits that
16528 * have been tested so that we can detect when we should
16529 * change the priority level.
16530 */
James Smart0c9ab6f2010-02-26 14:15:57 -050016531 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16532 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040016533 }
16534
James Smart0c9ab6f2010-02-26 14:15:57 -050016535
James Smart3804dc82010-07-14 15:31:37 -040016536 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040016537 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
16538 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
16539 /*
16540 * If next fcf index is not found check if there are lower
16541 * Priority level fcf's in the fcf_priority list.
16542 * Set up the rr_bmask with all of the avaiable fcf bits
16543 * at that level and continue the selection process.
16544 */
16545 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050016546 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040016547 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16548 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040016549 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
16550 return LPFC_FCOE_FCF_NEXT_NONE;
16551 else {
16552 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16553 "3063 Only FCF available idx %d, flag %x\n",
16554 next_fcf_index,
16555 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
16556 return next_fcf_index;
16557 }
James Smart3804dc82010-07-14 15:31:37 -040016558 }
16559
James Smart7d791df2011-07-22 18:37:52 -040016560 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
16561 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050016562 LPFC_FCF_FLOGI_FAILED) {
16563 if (list_is_singular(&phba->fcf.fcf_pri_list))
16564 return LPFC_FCOE_FCF_NEXT_NONE;
16565
James Smart7d791df2011-07-22 18:37:52 -040016566 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050016567 }
James Smart7d791df2011-07-22 18:37:52 -040016568
James Smart3804dc82010-07-14 15:31:37 -040016569 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016570 "2845 Get next roundrobin failover FCF (x%x)\n",
16571 next_fcf_index);
16572
James Smart0c9ab6f2010-02-26 14:15:57 -050016573 return next_fcf_index;
16574}
16575
16576/**
16577 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
16578 * @phba: pointer to lpfc hba data structure.
16579 *
16580 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016581 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016582 * does not go beyond the range of the driver allocated bmask dimension
16583 * before setting the bit.
16584 *
16585 * Returns 0 if the index bit successfully set, otherwise, it returns
16586 * -EINVAL.
16587 **/
16588int
16589lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
16590{
16591 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16592 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016593 "2610 FCF (x%x) reached driver's book "
16594 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016595 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16596 return -EINVAL;
16597 }
16598 /* Set the eligible FCF record index bmask */
16599 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16600
James Smart3804dc82010-07-14 15:31:37 -040016601 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016602 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040016603 "bmask\n", fcf_index);
16604
James Smart0c9ab6f2010-02-26 14:15:57 -050016605 return 0;
16606}
16607
16608/**
James Smart3804dc82010-07-14 15:31:37 -040016609 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050016610 * @phba: pointer to lpfc hba data structure.
16611 *
16612 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016613 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016614 * does not go beyond the range of the driver allocated bmask dimension
16615 * before clearing the bit.
16616 **/
16617void
16618lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
16619{
James Smart9a803a72013-09-06 12:17:56 -040016620 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050016621 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16622 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016623 "2762 FCF (x%x) reached driver's book "
16624 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016625 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16626 return;
16627 }
16628 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040016629 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040016630 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
16631 list) {
James Smart7d791df2011-07-22 18:37:52 -040016632 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
16633 list_del_init(&fcf_pri->list);
16634 break;
16635 }
16636 }
16637 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016638 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040016639
16640 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016641 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040016642 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050016643}
16644
16645/**
James Smartecfd03c2010-02-12 14:41:27 -050016646 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
16647 * @phba: pointer to lpfc hba data structure.
16648 *
16649 * This routine is the completion routine for the rediscover FCF table mailbox
16650 * command. If the mailbox command returned failure, it will try to stop the
16651 * FCF rediscover wait timer.
16652 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016653static void
James Smartecfd03c2010-02-12 14:41:27 -050016654lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16655{
16656 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16657 uint32_t shdr_status, shdr_add_status;
16658
16659 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16660
16661 shdr_status = bf_get(lpfc_mbox_hdr_status,
16662 &redisc_fcf->header.cfg_shdr.response);
16663 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16664 &redisc_fcf->header.cfg_shdr.response);
16665 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050016666 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050016667 "2746 Requesting for FCF rediscovery failed "
16668 "status x%x add_status x%x\n",
16669 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050016670 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050016671 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016672 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016673 spin_unlock_irq(&phba->hbalock);
16674 /*
16675 * CVL event triggered FCF rediscover request failed,
16676 * last resort to re-try current registered FCF entry.
16677 */
16678 lpfc_retry_pport_discovery(phba);
16679 } else {
16680 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016681 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016682 spin_unlock_irq(&phba->hbalock);
16683 /*
16684 * DEAD FCF event triggered FCF rediscover request
16685 * failed, last resort to fail over as a link down
16686 * to FCF registration.
16687 */
16688 lpfc_sli4_fcf_dead_failthrough(phba);
16689 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016690 } else {
16691 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016692 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050016693 /*
16694 * Start FCF rediscovery wait timer for pending FCF
16695 * before rescan FCF record table.
16696 */
16697 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050016698 }
James Smartecfd03c2010-02-12 14:41:27 -050016699
16700 mempool_free(mbox, phba->mbox_mem_pool);
16701}
16702
16703/**
James Smart3804dc82010-07-14 15:31:37 -040016704 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050016705 * @phba: pointer to lpfc hba data structure.
16706 *
16707 * This routine is invoked to request for rediscovery of the entire FCF table
16708 * by the port.
16709 **/
16710int
16711lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16712{
16713 LPFC_MBOXQ_t *mbox;
16714 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16715 int rc, length;
16716
James Smart0c9ab6f2010-02-26 14:15:57 -050016717 /* Cancel retry delay timers to all vports before FCF rediscover */
16718 lpfc_cancel_all_vport_retry_delay_timer(phba);
16719
James Smartecfd03c2010-02-12 14:41:27 -050016720 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16721 if (!mbox) {
16722 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16723 "2745 Failed to allocate mbox for "
16724 "requesting FCF rediscover.\n");
16725 return -ENOMEM;
16726 }
16727
16728 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16729 sizeof(struct lpfc_sli4_cfg_mhdr));
16730 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16731 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16732 length, LPFC_SLI4_MBX_EMBED);
16733
16734 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16735 /* Set count to 0 for invalidating the entire FCF database */
16736 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16737
16738 /* Issue the mailbox command asynchronously */
16739 mbox->vport = phba->pport;
16740 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16741 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16742
16743 if (rc == MBX_NOT_FINISHED) {
16744 mempool_free(mbox, phba->mbox_mem_pool);
16745 return -EIO;
16746 }
16747 return 0;
16748}
16749
16750/**
James Smartfc2b9892010-02-26 14:15:29 -050016751 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16752 * @phba: pointer to lpfc hba data structure.
16753 *
16754 * This function is the failover routine as a last resort to the FCF DEAD
16755 * event when driver failed to perform fast FCF failover.
16756 **/
16757void
16758lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16759{
16760 uint32_t link_state;
16761
16762 /*
16763 * Last resort as FCF DEAD event failover will treat this as
16764 * a link down, but save the link state because we don't want
16765 * it to be changed to Link Down unless it is already down.
16766 */
16767 link_state = phba->link_state;
16768 lpfc_linkdown(phba);
16769 phba->link_state = link_state;
16770
16771 /* Unregister FCF if no devices connected to it */
16772 lpfc_unregister_unused_fcf(phba);
16773}
16774
16775/**
James Smart026abb82011-12-13 13:20:45 -050016776 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016777 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050016778 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016779 *
James Smart026abb82011-12-13 13:20:45 -050016780 * This function gets SLI3 port configure region 23 data through memory dump
16781 * mailbox command. When it successfully retrieves data, the size of the data
16782 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040016783 **/
James Smart026abb82011-12-13 13:20:45 -050016784static uint32_t
16785lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040016786{
16787 LPFC_MBOXQ_t *pmb = NULL;
16788 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050016789 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016790 int rc;
16791
James Smart026abb82011-12-13 13:20:45 -050016792 if (!rgn23_data)
16793 return 0;
16794
James Smarta0c87cb2009-07-19 10:01:10 -040016795 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16796 if (!pmb) {
16797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016798 "2600 failed to allocate mailbox memory\n");
16799 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016800 }
16801 mb = &pmb->u.mb;
16802
James Smarta0c87cb2009-07-19 10:01:10 -040016803 do {
16804 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16805 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16806
16807 if (rc != MBX_SUCCESS) {
16808 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016809 "2601 failed to read config "
16810 "region 23, rc 0x%x Status 0x%x\n",
16811 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040016812 mb->un.varDmp.word_cnt = 0;
16813 }
16814 /*
16815 * dump mem may return a zero when finished or we got a
16816 * mailbox error, either way we are done.
16817 */
16818 if (mb->un.varDmp.word_cnt == 0)
16819 break;
16820 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16821 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16822
16823 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050016824 rgn23_data + offset,
16825 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040016826 offset += mb->un.varDmp.word_cnt;
16827 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16828
James Smart026abb82011-12-13 13:20:45 -050016829 mempool_free(pmb, phba->mbox_mem_pool);
16830 return offset;
16831}
16832
16833/**
16834 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16835 * @phba: pointer to lpfc hba data structure.
16836 * @rgn23_data: pointer to configure region 23 data.
16837 *
16838 * This function gets SLI4 port configure region 23 data through memory dump
16839 * mailbox command. When it successfully retrieves data, the size of the data
16840 * will be returned, otherwise, 0 will be returned.
16841 **/
16842static uint32_t
16843lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16844{
16845 LPFC_MBOXQ_t *mboxq = NULL;
16846 struct lpfc_dmabuf *mp = NULL;
16847 struct lpfc_mqe *mqe;
16848 uint32_t data_length = 0;
16849 int rc;
16850
16851 if (!rgn23_data)
16852 return 0;
16853
16854 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16855 if (!mboxq) {
16856 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16857 "3105 failed to allocate mailbox memory\n");
16858 return 0;
16859 }
16860
16861 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16862 goto out;
16863 mqe = &mboxq->u.mqe;
16864 mp = (struct lpfc_dmabuf *) mboxq->context1;
16865 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16866 if (rc)
16867 goto out;
16868 data_length = mqe->un.mb_words[5];
16869 if (data_length == 0)
16870 goto out;
16871 if (data_length > DMP_RGN23_SIZE) {
16872 data_length = 0;
16873 goto out;
16874 }
16875 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16876out:
16877 mempool_free(mboxq, phba->mbox_mem_pool);
16878 if (mp) {
16879 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16880 kfree(mp);
16881 }
16882 return data_length;
16883}
16884
16885/**
16886 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16887 * @phba: pointer to lpfc hba data structure.
16888 *
16889 * This function read region 23 and parse TLV for port status to
16890 * decide if the user disaled the port. If the TLV indicates the
16891 * port is disabled, the hba_flag is set accordingly.
16892 **/
16893void
16894lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16895{
16896 uint8_t *rgn23_data = NULL;
16897 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16898 uint32_t offset = 0;
16899
16900 /* Get adapter Region 23 data */
16901 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16902 if (!rgn23_data)
16903 goto out;
16904
16905 if (phba->sli_rev < LPFC_SLI_REV4)
16906 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16907 else {
16908 if_type = bf_get(lpfc_sli_intf_if_type,
16909 &phba->sli4_hba.sli_intf);
16910 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16911 goto out;
16912 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16913 }
James Smarta0c87cb2009-07-19 10:01:10 -040016914
16915 if (!data_size)
16916 goto out;
16917
16918 /* Check the region signature first */
16919 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16920 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16921 "2619 Config region 23 has bad signature\n");
16922 goto out;
16923 }
16924 offset += 4;
16925
16926 /* Check the data structure version */
16927 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16928 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16929 "2620 Config region 23 has bad version\n");
16930 goto out;
16931 }
16932 offset += 4;
16933
16934 /* Parse TLV entries in the region */
16935 while (offset < data_size) {
16936 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16937 break;
16938 /*
16939 * If the TLV is not driver specific TLV or driver id is
16940 * not linux driver id, skip the record.
16941 */
16942 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16943 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16944 (rgn23_data[offset + 3] != 0)) {
16945 offset += rgn23_data[offset + 1] * 4 + 4;
16946 continue;
16947 }
16948
16949 /* Driver found a driver specific TLV in the config region */
16950 sub_tlv_len = rgn23_data[offset + 1] * 4;
16951 offset += 4;
16952 tlv_offset = 0;
16953
16954 /*
16955 * Search for configured port state sub-TLV.
16956 */
16957 while ((offset < data_size) &&
16958 (tlv_offset < sub_tlv_len)) {
16959 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16960 offset += 4;
16961 tlv_offset += 4;
16962 break;
16963 }
16964 if (rgn23_data[offset] != PORT_STE_TYPE) {
16965 offset += rgn23_data[offset + 1] * 4 + 4;
16966 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16967 continue;
16968 }
16969
16970 /* This HBA contains PORT_STE configured */
16971 if (!rgn23_data[offset + 2])
16972 phba->hba_flag |= LINK_DISABLED;
16973
16974 goto out;
16975 }
16976 }
James Smart026abb82011-12-13 13:20:45 -050016977
James Smarta0c87cb2009-07-19 10:01:10 -040016978out:
James Smarta0c87cb2009-07-19 10:01:10 -040016979 kfree(rgn23_data);
16980 return;
16981}
James Smart695a8142010-01-26 23:08:03 -050016982
16983/**
James Smart52d52442011-05-24 11:42:45 -040016984 * lpfc_wr_object - write an object to the firmware
16985 * @phba: HBA structure that indicates port to create a queue on.
16986 * @dmabuf_list: list of dmabufs to write to the port.
16987 * @size: the total byte value of the objects to write to the port.
16988 * @offset: the current offset to be used to start the transfer.
16989 *
16990 * This routine will create a wr_object mailbox command to send to the port.
16991 * the mailbox command will be constructed using the dma buffers described in
16992 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16993 * BDEs that the imbedded mailbox can support. The @offset variable will be
16994 * used to indicate the starting offset of the transfer and will also return
16995 * the offset after the write object mailbox has completed. @size is used to
16996 * determine the end of the object and whether the eof bit should be set.
16997 *
16998 * Return 0 is successful and offset will contain the the new offset to use
16999 * for the next write.
17000 * Return negative value for error cases.
17001 **/
17002int
17003lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
17004 uint32_t size, uint32_t *offset)
17005{
17006 struct lpfc_mbx_wr_object *wr_object;
17007 LPFC_MBOXQ_t *mbox;
17008 int rc = 0, i = 0;
17009 uint32_t shdr_status, shdr_add_status;
17010 uint32_t mbox_tmo;
17011 union lpfc_sli4_cfg_shdr *shdr;
17012 struct lpfc_dmabuf *dmabuf;
17013 uint32_t written = 0;
17014
17015 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17016 if (!mbox)
17017 return -ENOMEM;
17018
17019 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17020 LPFC_MBOX_OPCODE_WRITE_OBJECT,
17021 sizeof(struct lpfc_mbx_wr_object) -
17022 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17023
17024 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
17025 wr_object->u.request.write_offset = *offset;
17026 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
17027 wr_object->u.request.object_name[0] =
17028 cpu_to_le32(wr_object->u.request.object_name[0]);
17029 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
17030 list_for_each_entry(dmabuf, dmabuf_list, list) {
17031 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
17032 break;
17033 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
17034 wr_object->u.request.bde[i].addrHigh =
17035 putPaddrHigh(dmabuf->phys);
17036 if (written + SLI4_PAGE_SIZE >= size) {
17037 wr_object->u.request.bde[i].tus.f.bdeSize =
17038 (size - written);
17039 written += (size - written);
17040 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
17041 } else {
17042 wr_object->u.request.bde[i].tus.f.bdeSize =
17043 SLI4_PAGE_SIZE;
17044 written += SLI4_PAGE_SIZE;
17045 }
17046 i++;
17047 }
17048 wr_object->u.request.bde_count = i;
17049 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
17050 if (!phba->sli4_hba.intr_enable)
17051 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17052 else {
James Smarta183a152011-10-10 21:32:43 -040017053 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040017054 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17055 }
17056 /* The IOCTL status is embedded in the mailbox subheader. */
17057 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
17058 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17059 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17060 if (rc != MBX_TIMEOUT)
17061 mempool_free(mbox, phba->mbox_mem_pool);
17062 if (shdr_status || shdr_add_status || rc) {
17063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17064 "3025 Write Object mailbox failed with "
17065 "status x%x add_status x%x, mbx status x%x\n",
17066 shdr_status, shdr_add_status, rc);
17067 rc = -ENXIO;
17068 } else
17069 *offset += wr_object->u.response.actual_write_length;
17070 return rc;
17071}
17072
17073/**
James Smart695a8142010-01-26 23:08:03 -050017074 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
17075 * @vport: pointer to vport data structure.
17076 *
17077 * This function iterate through the mailboxq and clean up all REG_LOGIN
17078 * and REG_VPI mailbox commands associated with the vport. This function
17079 * is called when driver want to restart discovery of the vport due to
17080 * a Clear Virtual Link event.
17081 **/
17082void
17083lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
17084{
17085 struct lpfc_hba *phba = vport->phba;
17086 LPFC_MBOXQ_t *mb, *nextmb;
17087 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040017088 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040017089 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040017090 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040017091 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050017092 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050017093
James Smartd439d282010-09-29 11:18:45 -040017094 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050017095 spin_lock_irq(&phba->hbalock);
17096 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
17097 if (mb->vport != vport)
17098 continue;
17099
17100 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17101 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17102 continue;
17103
James Smartd439d282010-09-29 11:18:45 -040017104 list_del(&mb->list);
17105 list_add_tail(&mb->list, &mbox_cmd_list);
17106 }
17107 /* Clean up active mailbox command with the vport */
17108 mb = phba->sli.mbox_active;
17109 if (mb && (mb->vport == vport)) {
17110 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
17111 (mb->u.mb.mbxCommand == MBX_REG_VPI))
17112 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17113 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17114 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
17115 /* Put reference count for delayed processing */
17116 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
17117 /* Unregister the RPI when mailbox complete */
17118 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17119 }
17120 }
James Smart63e801c2010-11-20 23:14:19 -050017121 /* Cleanup any mailbox completions which are not yet processed */
17122 do {
17123 restart_loop = 0;
17124 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
17125 /*
17126 * If this mailox is already processed or it is
17127 * for another vport ignore it.
17128 */
17129 if ((mb->vport != vport) ||
17130 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
17131 continue;
17132
17133 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17134 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17135 continue;
17136
17137 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17138 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17139 ndlp = (struct lpfc_nodelist *)mb->context2;
17140 /* Unregister the RPI when mailbox complete */
17141 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17142 restart_loop = 1;
17143 spin_unlock_irq(&phba->hbalock);
17144 spin_lock(shost->host_lock);
17145 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17146 spin_unlock(shost->host_lock);
17147 spin_lock_irq(&phba->hbalock);
17148 break;
17149 }
17150 }
17151 } while (restart_loop);
17152
James Smartd439d282010-09-29 11:18:45 -040017153 spin_unlock_irq(&phba->hbalock);
17154
17155 /* Release the cleaned-up mailbox commands */
17156 while (!list_empty(&mbox_cmd_list)) {
17157 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050017158 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17159 mp = (struct lpfc_dmabuf *) (mb->context1);
17160 if (mp) {
17161 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
17162 kfree(mp);
17163 }
James Smart78730cf2010-04-06 15:06:30 -040017164 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040017165 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040017166 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017167 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040017168 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017169 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040017170 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040017171 }
James Smart695a8142010-01-26 23:08:03 -050017172 }
James Smart695a8142010-01-26 23:08:03 -050017173 mempool_free(mb, phba->mbox_mem_pool);
17174 }
James Smartd439d282010-09-29 11:18:45 -040017175
17176 /* Release the ndlp with the cleaned-up active mailbox command */
17177 if (act_mbx_ndlp) {
17178 spin_lock(shost->host_lock);
17179 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17180 spin_unlock(shost->host_lock);
17181 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050017182 }
James Smart695a8142010-01-26 23:08:03 -050017183}
17184
James Smart2a9bf3d2010-06-07 15:24:45 -040017185/**
17186 * lpfc_drain_txq - Drain the txq
17187 * @phba: Pointer to HBA context object.
17188 *
17189 * This function attempt to submit IOCBs on the txq
17190 * to the adapter. For SLI4 adapters, the txq contains
17191 * ELS IOCBs that have been deferred because the there
17192 * are no SGLs. This congestion can occur with large
17193 * vport counts during node discovery.
17194 **/
17195
17196uint32_t
17197lpfc_drain_txq(struct lpfc_hba *phba)
17198{
17199 LIST_HEAD(completions);
17200 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
Daeseok Youn2e706372014-02-21 09:03:32 +090017201 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040017202 unsigned long iflags = 0;
17203 char *fail_msg = NULL;
17204 struct lpfc_sglq *sglq;
17205 union lpfc_wqe wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040017206 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040017207
James Smart398d81c2013-05-31 17:04:19 -040017208 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050017209 list_for_each_entry(piocbq, &pring->txq, list) {
17210 txq_cnt++;
17211 }
17212
17213 if (txq_cnt > pring->txq_max)
17214 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017215
James Smart398d81c2013-05-31 17:04:19 -040017216 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017217
James Smart0e9bb8d2013-03-01 16:35:12 -050017218 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040017219 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017220
James Smart19ca7602010-11-20 23:11:55 -050017221 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040017222 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040017223 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040017224 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17225 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050017226 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040017227 break;
17228 }
James Smart19ca7602010-11-20 23:11:55 -050017229 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040017230 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050017231 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040017232 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017233 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040017234 }
James Smart0e9bb8d2013-03-01 16:35:12 -050017235 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040017236
17237 /* The xri and iocb resources secured,
17238 * attempt to issue request
17239 */
James Smart6d368e52011-05-24 11:44:12 -040017240 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040017241 piocbq->sli4_xritag = sglq->sli4_xritag;
17242 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17243 fail_msg = "to convert bpl to sgl";
17244 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
17245 fail_msg = "to convert iocb to wqe";
17246 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
17247 fail_msg = " - Wq is full";
17248 else
17249 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17250
17251 if (fail_msg) {
17252 /* Failed means we can't issue and need to cancel */
17253 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17254 "2822 IOCB failed %s iotag 0x%x "
17255 "xri 0x%x\n",
17256 fail_msg,
17257 piocbq->iotag, piocbq->sli4_xritag);
17258 list_add_tail(&piocbq->list, &completions);
17259 }
James Smart398d81c2013-05-31 17:04:19 -040017260 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017261 }
17262
James Smart2a9bf3d2010-06-07 15:24:45 -040017263 /* Cancel all the IOCBs that cannot be issued */
17264 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17265 IOERR_SLI_ABORTED);
17266
James Smart0e9bb8d2013-03-01 16:35:12 -050017267 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017268}