blob: cb59f4ec46b062563789e1bb7e9746226046144b [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart50611572016-03-31 14:12:34 -07004 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010027#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050028
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040029#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050030#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040033#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040034#include <scsi/fc/fc_fs.h>
James Smart0d878412009-10-02 15:16:56 -040035#include <linux/aer.h>
dea31012005-04-17 16:05:31 -050036
James Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050038#include "lpfc_hw.h"
39#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040040#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040041#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050042#include "lpfc_disc.h"
43#include "lpfc_scsi.h"
44#include "lpfc.h"
45#include "lpfc_crtn.h"
46#include "lpfc_logmsg.h"
47#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050048#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040049#include "lpfc_vport.h"
James Smart61bda8f2016-10-13 15:06:05 -070050#include "lpfc_version.h"
dea31012005-04-17 16:05:31 -050051
52/* There are only four IOCB completion types. */
53typedef enum _lpfc_iocb_type {
54 LPFC_UNKNOWN_IOCB,
55 LPFC_UNSOL_IOCB,
56 LPFC_SOL_IOCB,
57 LPFC_ABORT_IOCB
58} lpfc_iocb_type;
59
James Smart4f774512009-05-22 14:52:35 -040060
61/* Provide function prototypes local to this module. */
62static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
63 uint32_t);
64static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
James Smart45ed1192009-10-02 15:17:02 -040065 uint8_t *, uint32_t *);
66static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
67 struct lpfc_iocbq *);
James Smart6669f9b2009-10-02 15:16:45 -040068static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
69 struct hbq_dmabuf *);
James Smart05580562011-05-24 11:40:48 -040070static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
71 struct lpfc_cqe *);
James Smart8a9d2e82012-05-09 21:16:12 -040072static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
73 int);
James Smartba20c852012-08-03 12:36:52 -040074static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
75 uint32_t);
James Smarte8d3c3b2013-10-10 12:21:30 -040076static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
77static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
James Smart05580562011-05-24 11:40:48 -040078
James Smart4f774512009-05-22 14:52:35 -040079static IOCB_t *
80lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
81{
82 return &iocbq->iocb;
83}
84
85/**
86 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
87 * @q: The Work Queue to operate on.
88 * @wqe: The work Queue Entry to put on the Work queue.
89 *
90 * This routine will copy the contents of @wqe to the next available entry on
91 * the @q. This function will then ring the Work Queue Doorbell to signal the
92 * HBA to start processing the Work Queue Entry. This function returns 0 if
93 * successful. If no entries are available on @q then this function will return
94 * -ENOMEM.
95 * The caller is expected to hold the hbalock when calling this routine.
96 **/
97static uint32_t
98lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
99{
James Smart2e90f4b2011-12-13 13:22:37 -0500100 union lpfc_wqe *temp_wqe;
James Smart4f774512009-05-22 14:52:35 -0400101 struct lpfc_register doorbell;
102 uint32_t host_index;
James Smart027140e2012-08-03 12:35:44 -0400103 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400104
James Smart2e90f4b2011-12-13 13:22:37 -0500105 /* sanity check on queue memory */
106 if (unlikely(!q))
107 return -ENOMEM;
108 temp_wqe = q->qe[q->host_index].wqe;
109
James Smart4f774512009-05-22 14:52:35 -0400110 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400111 idx = ((q->host_index + 1) % q->entry_count);
112 if (idx == q->hba_index) {
James Smartb84daac2012-08-03 12:35:13 -0400113 q->WQ_overflow++;
James Smart4f774512009-05-22 14:52:35 -0400114 return -ENOMEM;
James Smartb84daac2012-08-03 12:35:13 -0400115 }
116 q->WQ_posted++;
James Smart4f774512009-05-22 14:52:35 -0400117 /* set consumption flag every once in a while */
James Smartff78d8f2011-12-13 13:21:35 -0500118 if (!((q->host_index + 1) % q->entry_repost))
James Smartf0d9bcc2010-10-22 11:07:09 -0400119 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
James Smartfedd3b72011-02-16 12:39:24 -0500120 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
121 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400122 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
123
124 /* Update the host index before invoking device */
125 host_index = q->host_index;
James Smart027140e2012-08-03 12:35:44 -0400126
127 q->host_index = idx;
James Smart4f774512009-05-22 14:52:35 -0400128
129 /* Ring Doorbell */
130 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500131 if (q->db_format == LPFC_DB_LIST_FORMAT) {
132 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
133 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
134 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
135 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
136 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
137 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
138 } else {
139 return -EINVAL;
140 }
141 writel(doorbell.word0, q->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400142
143 return 0;
144}
145
146/**
147 * lpfc_sli4_wq_release - Updates internal hba index for WQ
148 * @q: The Work Queue to operate on.
149 * @index: The index to advance the hba index to.
150 *
151 * This routine will update the HBA index of a queue to reflect consumption of
152 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
153 * an entry the host calls this function to update the queue's internal
154 * pointers. This routine returns the number of entries that were consumed by
155 * the HBA.
156 **/
157static uint32_t
158lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
159{
160 uint32_t released = 0;
161
James Smart2e90f4b2011-12-13 13:22:37 -0500162 /* sanity check on queue memory */
163 if (unlikely(!q))
164 return 0;
165
James Smart4f774512009-05-22 14:52:35 -0400166 if (q->hba_index == index)
167 return 0;
168 do {
169 q->hba_index = ((q->hba_index + 1) % q->entry_count);
170 released++;
171 } while (q->hba_index != index);
172 return released;
173}
174
175/**
176 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
177 * @q: The Mailbox Queue to operate on.
178 * @wqe: The Mailbox Queue Entry to put on the Work queue.
179 *
180 * This routine will copy the contents of @mqe to the next available entry on
181 * the @q. This function will then ring the Work Queue Doorbell to signal the
182 * HBA to start processing the Work Queue Entry. This function returns 0 if
183 * successful. If no entries are available on @q then this function will return
184 * -ENOMEM.
185 * The caller is expected to hold the hbalock when calling this routine.
186 **/
187static uint32_t
188lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
189{
James Smart2e90f4b2011-12-13 13:22:37 -0500190 struct lpfc_mqe *temp_mqe;
James Smart4f774512009-05-22 14:52:35 -0400191 struct lpfc_register doorbell;
James Smart4f774512009-05-22 14:52:35 -0400192
James Smart2e90f4b2011-12-13 13:22:37 -0500193 /* sanity check on queue memory */
194 if (unlikely(!q))
195 return -ENOMEM;
196 temp_mqe = q->qe[q->host_index].mqe;
197
James Smart4f774512009-05-22 14:52:35 -0400198 /* If the host has not yet processed the next entry then we are done */
199 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
200 return -ENOMEM;
201 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
202 /* Save off the mailbox pointer for completion */
203 q->phba->mbox = (MAILBOX_t *)temp_mqe;
204
205 /* Update the host index before invoking device */
James Smart4f774512009-05-22 14:52:35 -0400206 q->host_index = ((q->host_index + 1) % q->entry_count);
207
208 /* Ring Doorbell */
209 doorbell.word0 = 0;
210 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
211 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
212 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400213 return 0;
214}
215
216/**
217 * lpfc_sli4_mq_release - Updates internal hba index for MQ
218 * @q: The Mailbox Queue to operate on.
219 *
220 * This routine will update the HBA index of a queue to reflect consumption of
221 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
222 * an entry the host calls this function to update the queue's internal
223 * pointers. This routine returns the number of entries that were consumed by
224 * the HBA.
225 **/
226static uint32_t
227lpfc_sli4_mq_release(struct lpfc_queue *q)
228{
James Smart2e90f4b2011-12-13 13:22:37 -0500229 /* sanity check on queue memory */
230 if (unlikely(!q))
231 return 0;
232
James Smart4f774512009-05-22 14:52:35 -0400233 /* Clear the mailbox pointer for completion */
234 q->phba->mbox = NULL;
235 q->hba_index = ((q->hba_index + 1) % q->entry_count);
236 return 1;
237}
238
239/**
240 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
241 * @q: The Event Queue to get the first valid EQE from
242 *
243 * This routine will get the first valid Event Queue Entry from @q, update
244 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
245 * the Queue (no more work to do), or the Queue is full of EQEs that have been
246 * processed, but not popped back to the HBA then this routine will return NULL.
247 **/
248static struct lpfc_eqe *
249lpfc_sli4_eq_get(struct lpfc_queue *q)
250{
James Smart2e90f4b2011-12-13 13:22:37 -0500251 struct lpfc_eqe *eqe;
James Smart027140e2012-08-03 12:35:44 -0400252 uint32_t idx;
James Smart2e90f4b2011-12-13 13:22:37 -0500253
254 /* sanity check on queue memory */
255 if (unlikely(!q))
256 return NULL;
257 eqe = q->qe[q->hba_index].eqe;
James Smart4f774512009-05-22 14:52:35 -0400258
259 /* If the next EQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400260 if (!bf_get_le32(lpfc_eqe_valid, eqe))
James Smart4f774512009-05-22 14:52:35 -0400261 return NULL;
262 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400263 idx = ((q->hba_index + 1) % q->entry_count);
264 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400265 return NULL;
266
James Smart027140e2012-08-03 12:35:44 -0400267 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400268
269 /*
270 * insert barrier for instruction interlock : data from the hardware
271 * must have the valid bit checked before it can be copied and acted
272 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
273 * instructions allowing action on content before valid bit checked,
274 * add barrier here as well. May not be needed as "content" is a
275 * single 32-bit entity here (vs multi word structure for cq's).
276 */
277 mb();
James Smart4f774512009-05-22 14:52:35 -0400278 return eqe;
279}
280
281/**
James Smartba20c852012-08-03 12:36:52 -0400282 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
283 * @q: The Event Queue to disable interrupts
284 *
285 **/
286static inline void
287lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
288{
289 struct lpfc_register doorbell;
290
291 doorbell.word0 = 0;
292 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
293 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
294 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
295 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
296 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
297 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
298}
299
300/**
James Smart4f774512009-05-22 14:52:35 -0400301 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
302 * @q: The Event Queue that the host has completed processing for.
303 * @arm: Indicates whether the host wants to arms this CQ.
304 *
305 * This routine will mark all Event Queue Entries on @q, from the last
306 * known completed entry to the last entry that was processed, as completed
307 * by clearing the valid bit for each completion queue entry. Then it will
308 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
309 * The internal host index in the @q will be updated by this routine to indicate
310 * that the host has finished processing the entries. The @arm parameter
311 * indicates that the queue should be rearmed when ringing the doorbell.
312 *
313 * This function will return the number of EQEs that were popped.
314 **/
315uint32_t
316lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
317{
318 uint32_t released = 0;
319 struct lpfc_eqe *temp_eqe;
320 struct lpfc_register doorbell;
321
James Smart2e90f4b2011-12-13 13:22:37 -0500322 /* sanity check on queue memory */
323 if (unlikely(!q))
324 return 0;
325
James Smart4f774512009-05-22 14:52:35 -0400326 /* while there are valid entries */
327 while (q->hba_index != q->host_index) {
328 temp_eqe = q->qe[q->host_index].eqe;
James Smartcb5172e2010-03-15 11:25:07 -0400329 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
James Smart4f774512009-05-22 14:52:35 -0400330 released++;
331 q->host_index = ((q->host_index + 1) % q->entry_count);
332 }
333 if (unlikely(released == 0 && !arm))
334 return 0;
335
336 /* ring doorbell for number popped */
337 doorbell.word0 = 0;
338 if (arm) {
339 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
340 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
341 }
342 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
343 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
James Smart6b5151f2012-01-18 16:24:06 -0500344 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
345 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
346 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400347 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
James Smarta747c9c2009-11-18 15:41:10 -0500348 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
349 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
350 readl(q->phba->sli4_hba.EQCQDBregaddr);
James Smart4f774512009-05-22 14:52:35 -0400351 return released;
352}
353
354/**
355 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
356 * @q: The Completion Queue to get the first valid CQE from
357 *
358 * This routine will get the first valid Completion Queue Entry from @q, update
359 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
360 * the Queue (no more work to do), or the Queue is full of CQEs that have been
361 * processed, but not popped back to the HBA then this routine will return NULL.
362 **/
363static struct lpfc_cqe *
364lpfc_sli4_cq_get(struct lpfc_queue *q)
365{
366 struct lpfc_cqe *cqe;
James Smart027140e2012-08-03 12:35:44 -0400367 uint32_t idx;
James Smart4f774512009-05-22 14:52:35 -0400368
James Smart2e90f4b2011-12-13 13:22:37 -0500369 /* sanity check on queue memory */
370 if (unlikely(!q))
371 return NULL;
372
James Smart4f774512009-05-22 14:52:35 -0400373 /* If the next CQE is not valid then we are done */
James Smartcb5172e2010-03-15 11:25:07 -0400374 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
James Smart4f774512009-05-22 14:52:35 -0400375 return NULL;
376 /* If the host has not yet processed the next entry then we are done */
James Smart027140e2012-08-03 12:35:44 -0400377 idx = ((q->hba_index + 1) % q->entry_count);
378 if (idx == q->host_index)
James Smart4f774512009-05-22 14:52:35 -0400379 return NULL;
380
381 cqe = q->qe[q->hba_index].cqe;
James Smart027140e2012-08-03 12:35:44 -0400382 q->hba_index = idx;
James Smart27f344e2014-05-07 17:16:46 -0400383
384 /*
385 * insert barrier for instruction interlock : data from the hardware
386 * must have the valid bit checked before it can be copied and acted
387 * upon. Speculative instructions were allowing a bcopy at the start
388 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
389 * after our return, to copy data before the valid bit check above
390 * was done. As such, some of the copied data was stale. The barrier
391 * ensures the check is before any data is copied.
392 */
393 mb();
James Smart4f774512009-05-22 14:52:35 -0400394 return cqe;
395}
396
397/**
398 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
399 * @q: The Completion Queue that the host has completed processing for.
400 * @arm: Indicates whether the host wants to arms this CQ.
401 *
402 * This routine will mark all Completion queue entries on @q, from the last
403 * known completed entry to the last entry that was processed, as completed
404 * by clearing the valid bit for each completion queue entry. Then it will
405 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
406 * The internal host index in the @q will be updated by this routine to indicate
407 * that the host has finished processing the entries. The @arm parameter
408 * indicates that the queue should be rearmed when ringing the doorbell.
409 *
410 * This function will return the number of CQEs that were released.
411 **/
412uint32_t
413lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
414{
415 uint32_t released = 0;
416 struct lpfc_cqe *temp_qe;
417 struct lpfc_register doorbell;
418
James Smart2e90f4b2011-12-13 13:22:37 -0500419 /* sanity check on queue memory */
420 if (unlikely(!q))
421 return 0;
James Smart4f774512009-05-22 14:52:35 -0400422 /* while there are valid entries */
423 while (q->hba_index != q->host_index) {
424 temp_qe = q->qe[q->host_index].cqe;
James Smartcb5172e2010-03-15 11:25:07 -0400425 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
James Smart4f774512009-05-22 14:52:35 -0400426 released++;
427 q->host_index = ((q->host_index + 1) % q->entry_count);
428 }
429 if (unlikely(released == 0 && !arm))
430 return 0;
431
432 /* ring doorbell for number popped */
433 doorbell.word0 = 0;
434 if (arm)
435 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
436 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
437 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
James Smart6b5151f2012-01-18 16:24:06 -0500438 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
439 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
440 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
James Smart4f774512009-05-22 14:52:35 -0400441 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
442 return released;
443}
444
445/**
446 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
447 * @q: The Header Receive Queue to operate on.
448 * @wqe: The Receive Queue Entry to put on the Receive queue.
449 *
450 * This routine will copy the contents of @wqe to the next available entry on
451 * the @q. This function will then ring the Receive Queue Doorbell to signal the
452 * HBA to start processing the Receive Queue Entry. This function returns the
453 * index that the rqe was copied to if successful. If no entries are available
454 * on @q then this function will return -ENOMEM.
455 * The caller is expected to hold the hbalock when calling this routine.
456 **/
457static int
458lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
459 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
460{
James Smart2e90f4b2011-12-13 13:22:37 -0500461 struct lpfc_rqe *temp_hrqe;
462 struct lpfc_rqe *temp_drqe;
James Smart4f774512009-05-22 14:52:35 -0400463 struct lpfc_register doorbell;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500464 int put_index;
James Smart4f774512009-05-22 14:52:35 -0400465
James Smart2e90f4b2011-12-13 13:22:37 -0500466 /* sanity check on queue memory */
467 if (unlikely(!hq) || unlikely(!dq))
468 return -ENOMEM;
Wei Yongjun5a25bf32012-12-02 08:33:24 -0500469 put_index = hq->host_index;
James Smart2e90f4b2011-12-13 13:22:37 -0500470 temp_hrqe = hq->qe[hq->host_index].rqe;
471 temp_drqe = dq->qe[dq->host_index].rqe;
472
James Smart4f774512009-05-22 14:52:35 -0400473 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
474 return -EINVAL;
475 if (hq->host_index != dq->host_index)
476 return -EINVAL;
477 /* If the host has not yet processed the next entry then we are done */
478 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
479 return -EBUSY;
480 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
481 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
482
483 /* Update the host index to point to the next slot */
484 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
485 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
486
487 /* Ring The Header Receive Queue Doorbell */
James Smart73d91e52011-10-10 21:32:10 -0400488 if (!(hq->host_index % hq->entry_repost)) {
James Smart4f774512009-05-22 14:52:35 -0400489 doorbell.word0 = 0;
James Smart962bc512013-01-03 15:44:00 -0500490 if (hq->db_format == LPFC_DB_RING_FORMAT) {
491 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
492 hq->entry_repost);
493 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
494 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
495 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
496 hq->entry_repost);
497 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
498 hq->host_index);
499 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
500 } else {
501 return -EINVAL;
502 }
503 writel(doorbell.word0, hq->db_regaddr);
James Smart4f774512009-05-22 14:52:35 -0400504 }
505 return put_index;
506}
507
508/**
509 * lpfc_sli4_rq_release - Updates internal hba index for RQ
510 * @q: The Header Receive Queue to operate on.
511 *
512 * This routine will update the HBA index of a queue to reflect consumption of
513 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
514 * consumed an entry the host calls this function to update the queue's
515 * internal pointers. This routine returns the number of entries that were
516 * consumed by the HBA.
517 **/
518static uint32_t
519lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
520{
James Smart2e90f4b2011-12-13 13:22:37 -0500521 /* sanity check on queue memory */
522 if (unlikely(!hq) || unlikely(!dq))
523 return 0;
524
James Smart4f774512009-05-22 14:52:35 -0400525 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
526 return 0;
527 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
528 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
529 return 1;
530}
531
James Smarte59058c2008-08-24 21:49:00 -0400532/**
James Smart3621a712009-04-06 18:47:14 -0400533 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400534 * @phba: Pointer to HBA context object.
535 * @pring: Pointer to driver SLI ring object.
536 *
537 * This function returns pointer to next command iocb entry
538 * in the command ring. The caller must hold hbalock to prevent
539 * other threads consume the next command iocb.
540 * SLI-2/SLI-3 provide different sized iocbs.
541 **/
James Smarted957682007-06-17 19:56:37 -0500542static inline IOCB_t *
543lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
544{
James Smart7e56aa22012-08-03 12:35:34 -0400545 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
546 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
James Smarted957682007-06-17 19:56:37 -0500547}
548
James Smarte59058c2008-08-24 21:49:00 -0400549/**
James Smart3621a712009-04-06 18:47:14 -0400550 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400551 * @phba: Pointer to HBA context object.
552 * @pring: Pointer to driver SLI ring object.
553 *
554 * This function returns pointer to next response iocb entry
555 * in the response ring. The caller must hold hbalock to make sure
556 * that no other thread consume the next response iocb.
557 * SLI-2/SLI-3 provide different sized iocbs.
558 **/
James Smarted957682007-06-17 19:56:37 -0500559static inline IOCB_t *
560lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
561{
James Smart7e56aa22012-08-03 12:35:34 -0400562 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
563 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
James Smarted957682007-06-17 19:56:37 -0500564}
565
James Smarte59058c2008-08-24 21:49:00 -0400566/**
James Smart3621a712009-04-06 18:47:14 -0400567 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400568 * @phba: Pointer to HBA context object.
569 *
570 * This function is called with hbalock held. This function
571 * allocates a new driver iocb object from the iocb pool. If the
572 * allocation is successful, it returns pointer to the newly
573 * allocated iocb object else it returns NULL.
574 **/
James Smart4f2e66c2012-05-09 21:17:07 -0400575struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500576__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400577{
578 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
579 struct lpfc_iocbq * iocbq = NULL;
580
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100581 lockdep_assert_held(&phba->hbalock);
582
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400583 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
James Smart2a9bf3d2010-06-07 15:24:45 -0400584 if (iocbq)
585 phba->iocb_cnt++;
586 if (phba->iocb_cnt > phba->iocb_max)
587 phba->iocb_max = phba->iocb_cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400588 return iocbq;
589}
590
James Smarte59058c2008-08-24 21:49:00 -0400591/**
James Smartda0436e2009-05-22 14:51:39 -0400592 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
593 * @phba: Pointer to HBA context object.
594 * @xritag: XRI value.
595 *
596 * This function clears the sglq pointer from the array of acive
597 * sglq's. The xritag that is passed in is used to index into the
598 * array. Before the xritag can be used it needs to be adjusted
599 * by subtracting the xribase.
600 *
601 * Returns sglq ponter = success, NULL = Failure.
602 **/
603static struct lpfc_sglq *
604__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
605{
James Smartda0436e2009-05-22 14:51:39 -0400606 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400607
608 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
609 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
James Smartda0436e2009-05-22 14:51:39 -0400610 return sglq;
611}
612
613/**
614 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
615 * @phba: Pointer to HBA context object.
616 * @xritag: XRI value.
617 *
618 * This function returns the sglq pointer from the array of acive
619 * sglq's. The xritag that is passed in is used to index into the
620 * array. Before the xritag can be used it needs to be adjusted
621 * by subtracting the xribase.
622 *
623 * Returns sglq ponter = success, NULL = Failure.
624 **/
James Smart0f65ff62010-02-26 14:14:23 -0500625struct lpfc_sglq *
James Smartda0436e2009-05-22 14:51:39 -0400626__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
627{
James Smartda0436e2009-05-22 14:51:39 -0400628 struct lpfc_sglq *sglq;
James Smart6d368e52011-05-24 11:44:12 -0400629
630 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
James Smartda0436e2009-05-22 14:51:39 -0400631 return sglq;
632}
633
634/**
James Smart1151e3e2011-02-16 12:39:35 -0500635 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500636 * @phba: Pointer to HBA context object.
637 * @xritag: xri used in this exchange.
638 * @rrq: The RRQ to be cleared.
639 *
James Smart19ca7602010-11-20 23:11:55 -0500640 **/
James Smart1151e3e2011-02-16 12:39:35 -0500641void
642lpfc_clr_rrq_active(struct lpfc_hba *phba,
643 uint16_t xritag,
644 struct lpfc_node_rrq *rrq)
James Smart19ca7602010-11-20 23:11:55 -0500645{
James Smart1151e3e2011-02-16 12:39:35 -0500646 struct lpfc_nodelist *ndlp = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500647
James Smart1151e3e2011-02-16 12:39:35 -0500648 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
649 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
James Smart19ca7602010-11-20 23:11:55 -0500650
651 /* The target DID could have been swapped (cable swap)
652 * we should use the ndlp from the findnode if it is
653 * available.
654 */
James Smart1151e3e2011-02-16 12:39:35 -0500655 if ((!ndlp) && rrq->ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500656 ndlp = rrq->ndlp;
657
James Smart1151e3e2011-02-16 12:39:35 -0500658 if (!ndlp)
659 goto out;
660
James Smartcff261f2013-12-17 20:29:47 -0500661 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
James Smart19ca7602010-11-20 23:11:55 -0500662 rrq->send_rrq = 0;
663 rrq->xritag = 0;
664 rrq->rrq_stop_time = 0;
665 }
James Smart1151e3e2011-02-16 12:39:35 -0500666out:
James Smart19ca7602010-11-20 23:11:55 -0500667 mempool_free(rrq, phba->rrq_pool);
668}
669
670/**
671 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
672 * @phba: Pointer to HBA context object.
673 *
674 * This function is called with hbalock held. This function
675 * Checks if stop_time (ratov from setting rrq active) has
676 * been reached, if it has and the send_rrq flag is set then
677 * it will call lpfc_send_rrq. If the send_rrq flag is not set
678 * then it will just call the routine to clear the rrq and
679 * free the rrq resource.
680 * The timer is set to the next rrq that is going to expire before
681 * leaving the routine.
682 *
683 **/
684void
685lpfc_handle_rrq_active(struct lpfc_hba *phba)
686{
687 struct lpfc_node_rrq *rrq;
688 struct lpfc_node_rrq *nextrrq;
689 unsigned long next_time;
690 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500691 LIST_HEAD(send_rrq);
James Smart19ca7602010-11-20 23:11:55 -0500692
693 spin_lock_irqsave(&phba->hbalock, iflags);
694 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
James Smart256ec0d2013-04-17 20:14:58 -0400695 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smart19ca7602010-11-20 23:11:55 -0500696 list_for_each_entry_safe(rrq, nextrrq,
James Smart1151e3e2011-02-16 12:39:35 -0500697 &phba->active_rrq_list, list) {
698 if (time_after(jiffies, rrq->rrq_stop_time))
699 list_move(&rrq->list, &send_rrq);
700 else if (time_before(rrq->rrq_stop_time, next_time))
James Smart19ca7602010-11-20 23:11:55 -0500701 next_time = rrq->rrq_stop_time;
702 }
703 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart06918ac2014-02-20 09:57:57 -0500704 if ((!list_empty(&phba->active_rrq_list)) &&
705 (!(phba->pport->load_flag & FC_UNLOADING)))
James Smart19ca7602010-11-20 23:11:55 -0500706 mod_timer(&phba->rrq_tmr, next_time);
James Smart1151e3e2011-02-16 12:39:35 -0500707 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
708 list_del(&rrq->list);
709 if (!rrq->send_rrq)
710 /* this call will free the rrq */
711 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
712 else if (lpfc_send_rrq(phba, rrq)) {
713 /* if we send the rrq then the completion handler
714 * will clear the bit in the xribitmap.
715 */
716 lpfc_clr_rrq_active(phba, rrq->xritag,
717 rrq);
718 }
719 }
James Smart19ca7602010-11-20 23:11:55 -0500720}
721
722/**
723 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
724 * @vport: Pointer to vport context object.
725 * @xri: The xri used in the exchange.
726 * @did: The targets DID for this exchange.
727 *
728 * returns NULL = rrq not found in the phba->active_rrq_list.
729 * rrq = rrq for this xri and target.
730 **/
731struct lpfc_node_rrq *
732lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
733{
734 struct lpfc_hba *phba = vport->phba;
735 struct lpfc_node_rrq *rrq;
736 struct lpfc_node_rrq *nextrrq;
737 unsigned long iflags;
738
739 if (phba->sli_rev != LPFC_SLI_REV4)
740 return NULL;
741 spin_lock_irqsave(&phba->hbalock, iflags);
742 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
743 if (rrq->vport == vport && rrq->xritag == xri &&
744 rrq->nlp_DID == did){
745 list_del(&rrq->list);
746 spin_unlock_irqrestore(&phba->hbalock, iflags);
747 return rrq;
748 }
749 }
750 spin_unlock_irqrestore(&phba->hbalock, iflags);
751 return NULL;
752}
753
754/**
755 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
756 * @vport: Pointer to vport context object.
James Smart1151e3e2011-02-16 12:39:35 -0500757 * @ndlp: Pointer to the lpfc_node_list structure.
758 * If ndlp is NULL Remove all active RRQs for this vport from the
759 * phba->active_rrq_list and clear the rrq.
760 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
James Smart19ca7602010-11-20 23:11:55 -0500761 **/
762void
James Smart1151e3e2011-02-16 12:39:35 -0500763lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smart19ca7602010-11-20 23:11:55 -0500764
765{
766 struct lpfc_hba *phba = vport->phba;
767 struct lpfc_node_rrq *rrq;
768 struct lpfc_node_rrq *nextrrq;
769 unsigned long iflags;
James Smart1151e3e2011-02-16 12:39:35 -0500770 LIST_HEAD(rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500771
772 if (phba->sli_rev != LPFC_SLI_REV4)
773 return;
James Smart1151e3e2011-02-16 12:39:35 -0500774 if (!ndlp) {
775 lpfc_sli4_vport_delete_els_xri_aborted(vport);
776 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
James Smart19ca7602010-11-20 23:11:55 -0500777 }
James Smart1151e3e2011-02-16 12:39:35 -0500778 spin_lock_irqsave(&phba->hbalock, iflags);
779 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
780 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
781 list_move(&rrq->list, &rrq_list);
James Smart19ca7602010-11-20 23:11:55 -0500782 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart1151e3e2011-02-16 12:39:35 -0500783
784 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
785 list_del(&rrq->list);
786 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
787 }
James Smart19ca7602010-11-20 23:11:55 -0500788}
789
790/**
James Smart1151e3e2011-02-16 12:39:35 -0500791 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
James Smart19ca7602010-11-20 23:11:55 -0500792 * @phba: Pointer to HBA context object.
793 * @ndlp: Targets nodelist pointer for this exchange.
794 * @xritag the xri in the bitmap to test.
795 *
796 * This function is called with hbalock held. This function
797 * returns 0 = rrq not active for this xri
798 * 1 = rrq is valid for this xri.
799 **/
James Smart1151e3e2011-02-16 12:39:35 -0500800int
801lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smart19ca7602010-11-20 23:11:55 -0500802 uint16_t xritag)
803{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100804 lockdep_assert_held(&phba->hbalock);
James Smart19ca7602010-11-20 23:11:55 -0500805 if (!ndlp)
806 return 0;
James Smartcff261f2013-12-17 20:29:47 -0500807 if (!ndlp->active_rrqs_xri_bitmap)
808 return 0;
809 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smart19ca7602010-11-20 23:11:55 -0500810 return 1;
811 else
812 return 0;
813}
814
815/**
816 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
817 * @phba: Pointer to HBA context object.
818 * @ndlp: nodelist pointer for this target.
819 * @xritag: xri used in this exchange.
820 * @rxid: Remote Exchange ID.
821 * @send_rrq: Flag used to determine if we should send rrq els cmd.
822 *
823 * This function takes the hbalock.
824 * The active bit is always set in the active rrq xri_bitmap even
825 * if there is no slot avaiable for the other rrq information.
826 *
827 * returns 0 rrq actived for this xri
828 * < 0 No memory or invalid ndlp.
829 **/
830int
831lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
James Smartb42c07c2012-01-18 16:25:55 -0500832 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
James Smart19ca7602010-11-20 23:11:55 -0500833{
James Smart19ca7602010-11-20 23:11:55 -0500834 unsigned long iflags;
James Smartb42c07c2012-01-18 16:25:55 -0500835 struct lpfc_node_rrq *rrq;
836 int empty;
837
838 if (!ndlp)
839 return -EINVAL;
840
841 if (!phba->cfg_enable_rrq)
842 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500843
844 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500845 if (phba->pport->load_flag & FC_UNLOADING) {
846 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
847 goto out;
848 }
849
850 /*
851 * set the active bit even if there is no mem available.
852 */
853 if (NLP_CHK_FREE_REQ(ndlp))
854 goto out;
855
856 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
857 goto out;
858
James Smartcff261f2013-12-17 20:29:47 -0500859 if (!ndlp->active_rrqs_xri_bitmap)
860 goto out;
861
862 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
James Smartb42c07c2012-01-18 16:25:55 -0500863 goto out;
864
James Smart19ca7602010-11-20 23:11:55 -0500865 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartb42c07c2012-01-18 16:25:55 -0500866 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
867 if (!rrq) {
868 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
869 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
870 " DID:0x%x Send:%d\n",
871 xritag, rxid, ndlp->nlp_DID, send_rrq);
872 return -EINVAL;
873 }
James Smarte5771b42013-03-01 16:37:14 -0500874 if (phba->cfg_enable_rrq == 1)
875 rrq->send_rrq = send_rrq;
876 else
877 rrq->send_rrq = 0;
James Smartb42c07c2012-01-18 16:25:55 -0500878 rrq->xritag = xritag;
James Smart256ec0d2013-04-17 20:14:58 -0400879 rrq->rrq_stop_time = jiffies +
880 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
James Smartb42c07c2012-01-18 16:25:55 -0500881 rrq->ndlp = ndlp;
882 rrq->nlp_DID = ndlp->nlp_DID;
883 rrq->vport = ndlp->vport;
884 rrq->rxid = rxid;
James Smartb42c07c2012-01-18 16:25:55 -0500885 spin_lock_irqsave(&phba->hbalock, iflags);
886 empty = list_empty(&phba->active_rrq_list);
887 list_add_tail(&rrq->list, &phba->active_rrq_list);
888 phba->hba_flag |= HBA_RRQ_ACTIVE;
889 if (empty)
890 lpfc_worker_wake_up(phba);
891 spin_unlock_irqrestore(&phba->hbalock, iflags);
892 return 0;
893out:
894 spin_unlock_irqrestore(&phba->hbalock, iflags);
895 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
896 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
897 " DID:0x%x Send:%d\n",
898 xritag, rxid, ndlp->nlp_DID, send_rrq);
899 return -EINVAL;
James Smart19ca7602010-11-20 23:11:55 -0500900}
901
902/**
James Smartda0436e2009-05-22 14:51:39 -0400903 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
904 * @phba: Pointer to HBA context object.
James Smart19ca7602010-11-20 23:11:55 -0500905 * @piocb: Pointer to the iocbq.
James Smartda0436e2009-05-22 14:51:39 -0400906 *
James Smartdafe8ce2014-09-03 12:56:40 -0400907 * This function is called with the ring lock held. This function
James Smart6d368e52011-05-24 11:44:12 -0400908 * gets a new driver sglq object from the sglq list. If the
James Smartda0436e2009-05-22 14:51:39 -0400909 * list is not empty then it is successful, it returns pointer to the newly
910 * allocated sglq object else it returns NULL.
911 **/
912static struct lpfc_sglq *
James Smart19ca7602010-11-20 23:11:55 -0500913__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
James Smartda0436e2009-05-22 14:51:39 -0400914{
915 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
916 struct lpfc_sglq *sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500917 struct lpfc_sglq *start_sglq = NULL;
James Smart19ca7602010-11-20 23:11:55 -0500918 struct lpfc_scsi_buf *lpfc_cmd;
919 struct lpfc_nodelist *ndlp;
920 int found = 0;
921
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +0100922 lockdep_assert_held(&phba->hbalock);
923
James Smart19ca7602010-11-20 23:11:55 -0500924 if (piocbq->iocb_flag & LPFC_IO_FCP) {
925 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
926 ndlp = lpfc_cmd->rdata->pnode;
James Smartbe858b62010-12-15 17:57:20 -0500927 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
James Smart6c7cf482015-04-07 15:07:25 -0400928 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
James Smart19ca7602010-11-20 23:11:55 -0500929 ndlp = piocbq->context_un.ndlp;
James Smart6c7cf482015-04-07 15:07:25 -0400930 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
931 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
932 ndlp = NULL;
933 else
934 ndlp = piocbq->context_un.ndlp;
935 } else {
James Smart19ca7602010-11-20 23:11:55 -0500936 ndlp = piocbq->context1;
James Smart6c7cf482015-04-07 15:07:25 -0400937 }
James Smart19ca7602010-11-20 23:11:55 -0500938
James Smartda0436e2009-05-22 14:51:39 -0400939 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
James Smart19ca7602010-11-20 23:11:55 -0500940 start_sglq = sglq;
941 while (!found) {
942 if (!sglq)
943 return NULL;
James Smartee0f4fe2012-05-09 21:19:14 -0400944 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
James Smart19ca7602010-11-20 23:11:55 -0500945 /* This xri has an rrq outstanding for this DID.
946 * put it back in the list and get another xri.
947 */
948 list_add_tail(&sglq->list, lpfc_sgl_list);
949 sglq = NULL;
950 list_remove_head(lpfc_sgl_list, sglq,
951 struct lpfc_sglq, list);
952 if (sglq == start_sglq) {
953 sglq = NULL;
954 break;
955 } else
956 continue;
957 }
958 sglq->ndlp = ndlp;
959 found = 1;
James Smart6d368e52011-05-24 11:44:12 -0400960 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
James Smart19ca7602010-11-20 23:11:55 -0500961 sglq->state = SGL_ALLOCATED;
962 }
James Smartda0436e2009-05-22 14:51:39 -0400963 return sglq;
964}
965
966/**
James Smart3621a712009-04-06 18:47:14 -0400967 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400968 * @phba: Pointer to HBA context object.
969 *
970 * This function is called with no lock held. This function
971 * allocates a new driver iocb object from the iocb pool. If the
972 * allocation is successful, it returns pointer to the newly
973 * allocated iocb object else it returns NULL.
974 **/
James Smart2e0fef82007-06-17 19:56:36 -0500975struct lpfc_iocbq *
976lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500977{
James Smart2e0fef82007-06-17 19:56:36 -0500978 struct lpfc_iocbq * iocbq = NULL;
979 unsigned long iflags;
980
981 spin_lock_irqsave(&phba->hbalock, iflags);
982 iocbq = __lpfc_sli_get_iocbq(phba);
983 spin_unlock_irqrestore(&phba->hbalock, iflags);
984 return iocbq;
985}
986
James Smarte59058c2008-08-24 21:49:00 -0400987/**
James Smart4f774512009-05-22 14:52:35 -0400988 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
989 * @phba: Pointer to HBA context object.
990 * @iocbq: Pointer to driver iocb object.
991 *
992 * This function is called with hbalock held to release driver
993 * iocb object to the iocb pool. The iotag in the iocb object
994 * does not change for each use of the iocb object. This function
995 * clears all other fields of the iocb object when it is freed.
996 * The sqlq structure that holds the xritag and phys and virtual
997 * mappings for the scatter gather list is retrieved from the
998 * active array of sglq. The get of the sglq pointer also clears
999 * the entry in the array. If the status of the IO indiactes that
1000 * this IO was aborted then the sglq entry it put on the
1001 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1002 * IO has good status or fails for any other reason then the sglq
1003 * entry is added to the free list (lpfc_sgl_list).
1004 **/
1005static void
1006__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1007{
1008 struct lpfc_sglq *sglq;
1009 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04001010 unsigned long iflag = 0;
1011 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart4f774512009-05-22 14:52:35 -04001012
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001013 lockdep_assert_held(&phba->hbalock);
1014
James Smart4f774512009-05-22 14:52:35 -04001015 if (iocbq->sli4_xritag == NO_XRI)
1016 sglq = NULL;
1017 else
James Smart6d368e52011-05-24 11:44:12 -04001018 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1019
James Smart0e9bb8d2013-03-01 16:35:12 -05001020
James Smart4f774512009-05-22 14:52:35 -04001021 if (sglq) {
James Smart0f65ff62010-02-26 14:14:23 -05001022 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1023 (sglq->state != SGL_XRI_ABORTED)) {
James Smart4f774512009-05-22 14:52:35 -04001024 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1025 iflag);
1026 list_add(&sglq->list,
1027 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1028 spin_unlock_irqrestore(
1029 &phba->sli4_hba.abts_sgl_list_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001030 } else {
James Smartdafe8ce2014-09-03 12:56:40 -04001031 spin_lock_irqsave(&pring->ring_lock, iflag);
James Smart0f65ff62010-02-26 14:14:23 -05001032 sglq->state = SGL_FREED;
James Smart19ca7602010-11-20 23:11:55 -05001033 sglq->ndlp = NULL;
James Smartfedd3b72011-02-16 12:39:24 -05001034 list_add_tail(&sglq->list,
1035 &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04001036 spin_unlock_irqrestore(&pring->ring_lock, iflag);
James Smart2a9bf3d2010-06-07 15:24:45 -04001037
1038 /* Check if TXQ queue needs to be serviced */
James Smart0e9bb8d2013-03-01 16:35:12 -05001039 if (!list_empty(&pring->txq))
James Smart2a9bf3d2010-06-07 15:24:45 -04001040 lpfc_worker_wake_up(phba);
James Smart0f65ff62010-02-26 14:14:23 -05001041 }
James Smart4f774512009-05-22 14:52:35 -04001042 }
1043
1044
1045 /*
1046 * Clean all volatile data fields, preserve iotag and node struct.
1047 */
1048 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
James Smart6d368e52011-05-24 11:44:12 -04001049 iocbq->sli4_lxritag = NO_XRI;
James Smart4f774512009-05-22 14:52:35 -04001050 iocbq->sli4_xritag = NO_XRI;
1051 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1052}
1053
James Smart2a9bf3d2010-06-07 15:24:45 -04001054
James Smart4f774512009-05-22 14:52:35 -04001055/**
James Smart3772a992009-05-22 14:50:54 -04001056 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1057 * @phba: Pointer to HBA context object.
1058 * @iocbq: Pointer to driver iocb object.
1059 *
1060 * This function is called with hbalock held to release driver
1061 * iocb object to the iocb pool. The iotag in the iocb object
1062 * does not change for each use of the iocb object. This function
1063 * clears all other fields of the iocb object when it is freed.
1064 **/
1065static void
1066__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1067{
1068 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1069
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001070 lockdep_assert_held(&phba->hbalock);
James Smart0e9bb8d2013-03-01 16:35:12 -05001071
1072 /*
James Smart3772a992009-05-22 14:50:54 -04001073 * Clean all volatile data fields, preserve iotag and node struct.
1074 */
1075 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1076 iocbq->sli4_xritag = NO_XRI;
1077 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1078}
1079
1080/**
James Smart3621a712009-04-06 18:47:14 -04001081 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001082 * @phba: Pointer to HBA context object.
1083 * @iocbq: Pointer to driver iocb object.
1084 *
1085 * This function is called with hbalock held to release driver
1086 * iocb object to the iocb pool. The iotag in the iocb object
1087 * does not change for each use of the iocb object. This function
1088 * clears all other fields of the iocb object when it is freed.
1089 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001090static void
James Smart2e0fef82007-06-17 19:56:36 -05001091__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1092{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001093 lockdep_assert_held(&phba->hbalock);
1094
James Smart3772a992009-05-22 14:50:54 -04001095 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -04001096 phba->iocb_cnt--;
James Bottomley604a3e32005-10-29 10:28:33 -05001097}
1098
James Smarte59058c2008-08-24 21:49:00 -04001099/**
James Smart3621a712009-04-06 18:47:14 -04001100 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -04001101 * @phba: Pointer to HBA context object.
1102 * @iocbq: Pointer to driver iocb object.
1103 *
1104 * This function is called with no lock held to release the iocb to
1105 * iocb pool.
1106 **/
James Smart2e0fef82007-06-17 19:56:36 -05001107void
1108lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1109{
1110 unsigned long iflags;
1111
1112 /*
1113 * Clean all volatile data fields, preserve iotag and node struct.
1114 */
1115 spin_lock_irqsave(&phba->hbalock, iflags);
1116 __lpfc_sli_release_iocbq(phba, iocbq);
1117 spin_unlock_irqrestore(&phba->hbalock, iflags);
1118}
1119
James Smarte59058c2008-08-24 21:49:00 -04001120/**
James Smarta257bf92009-04-06 18:48:10 -04001121 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1122 * @phba: Pointer to HBA context object.
1123 * @iocblist: List of IOCBs.
1124 * @ulpstatus: ULP status in IOCB command field.
1125 * @ulpWord4: ULP word-4 in IOCB command field.
1126 *
1127 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1128 * on the list by invoking the complete callback function associated with the
1129 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1130 * fields.
1131 **/
1132void
1133lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1134 uint32_t ulpstatus, uint32_t ulpWord4)
1135{
1136 struct lpfc_iocbq *piocb;
1137
1138 while (!list_empty(iocblist)) {
1139 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
James Smarta257bf92009-04-06 18:48:10 -04001140 if (!piocb->iocb_cmpl)
1141 lpfc_sli_release_iocbq(phba, piocb);
1142 else {
1143 piocb->iocb.ulpStatus = ulpstatus;
1144 piocb->iocb.un.ulpWord[4] = ulpWord4;
1145 (piocb->iocb_cmpl) (phba, piocb, piocb);
1146 }
1147 }
1148 return;
1149}
1150
1151/**
James Smart3621a712009-04-06 18:47:14 -04001152 * lpfc_sli_iocb_cmd_type - Get the iocb type
1153 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -04001154 *
1155 * This function is called by ring event handler function to get the iocb type.
1156 * This function translates the iocb command to an iocb command type used to
1157 * decide the final disposition of each completed IOCB.
1158 * The function returns
1159 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1160 * LPFC_SOL_IOCB if it is a solicited iocb completion
1161 * LPFC_ABORT_IOCB if it is an abort iocb
1162 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1163 *
1164 * The caller is not required to hold any lock.
1165 **/
dea31012005-04-17 16:05:31 -05001166static lpfc_iocb_type
1167lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1168{
1169 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1170
1171 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1172 return 0;
1173
1174 switch (iocb_cmnd) {
1175 case CMD_XMIT_SEQUENCE_CR:
1176 case CMD_XMIT_SEQUENCE_CX:
1177 case CMD_XMIT_BCAST_CN:
1178 case CMD_XMIT_BCAST_CX:
1179 case CMD_ELS_REQUEST_CR:
1180 case CMD_ELS_REQUEST_CX:
1181 case CMD_CREATE_XRI_CR:
1182 case CMD_CREATE_XRI_CX:
1183 case CMD_GET_RPI_CN:
1184 case CMD_XMIT_ELS_RSP_CX:
1185 case CMD_GET_RPI_CR:
1186 case CMD_FCP_IWRITE_CR:
1187 case CMD_FCP_IWRITE_CX:
1188 case CMD_FCP_IREAD_CR:
1189 case CMD_FCP_IREAD_CX:
1190 case CMD_FCP_ICMND_CR:
1191 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -05001192 case CMD_FCP_TSEND_CX:
1193 case CMD_FCP_TRSP_CX:
1194 case CMD_FCP_TRECEIVE_CX:
1195 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -05001196 case CMD_ADAPTER_MSG:
1197 case CMD_ADAPTER_DUMP:
1198 case CMD_XMIT_SEQUENCE64_CR:
1199 case CMD_XMIT_SEQUENCE64_CX:
1200 case CMD_XMIT_BCAST64_CN:
1201 case CMD_XMIT_BCAST64_CX:
1202 case CMD_ELS_REQUEST64_CR:
1203 case CMD_ELS_REQUEST64_CX:
1204 case CMD_FCP_IWRITE64_CR:
1205 case CMD_FCP_IWRITE64_CX:
1206 case CMD_FCP_IREAD64_CR:
1207 case CMD_FCP_IREAD64_CX:
1208 case CMD_FCP_ICMND64_CR:
1209 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -05001210 case CMD_FCP_TSEND64_CX:
1211 case CMD_FCP_TRSP64_CX:
1212 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -05001213 case CMD_GEN_REQUEST64_CR:
1214 case CMD_GEN_REQUEST64_CX:
1215 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -04001216 case DSSCMD_IWRITE64_CR:
1217 case DSSCMD_IWRITE64_CX:
1218 case DSSCMD_IREAD64_CR:
1219 case DSSCMD_IREAD64_CX:
dea31012005-04-17 16:05:31 -05001220 type = LPFC_SOL_IOCB;
1221 break;
1222 case CMD_ABORT_XRI_CN:
1223 case CMD_ABORT_XRI_CX:
1224 case CMD_CLOSE_XRI_CN:
1225 case CMD_CLOSE_XRI_CX:
1226 case CMD_XRI_ABORTED_CX:
1227 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -04001228 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -05001229 type = LPFC_ABORT_IOCB;
1230 break;
1231 case CMD_RCV_SEQUENCE_CX:
1232 case CMD_RCV_ELS_REQ_CX:
1233 case CMD_RCV_SEQUENCE64_CX:
1234 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -04001235 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -05001236 case CMD_IOCB_RCV_SEQ64_CX:
1237 case CMD_IOCB_RCV_ELS64_CX:
1238 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -05001239 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -05001240 type = LPFC_UNSOL_IOCB;
1241 break;
James Smart3163f722008-02-08 18:50:25 -05001242 case CMD_IOCB_XMIT_MSEQ64_CR:
1243 case CMD_IOCB_XMIT_MSEQ64_CX:
1244 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1245 case CMD_IOCB_RCV_ELS_LIST64_CX:
1246 case CMD_IOCB_CLOSE_EXTENDED_CN:
1247 case CMD_IOCB_ABORT_EXTENDED_CN:
1248 case CMD_IOCB_RET_HBQE64_CN:
1249 case CMD_IOCB_FCP_IBIDIR64_CR:
1250 case CMD_IOCB_FCP_IBIDIR64_CX:
1251 case CMD_IOCB_FCP_ITASKMGT64_CX:
1252 case CMD_IOCB_LOGENTRY_CN:
1253 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1254 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07001255 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -05001256 type = LPFC_UNKNOWN_IOCB;
1257 break;
dea31012005-04-17 16:05:31 -05001258 default:
1259 type = LPFC_UNKNOWN_IOCB;
1260 break;
1261 }
1262
1263 return type;
1264}
1265
James Smarte59058c2008-08-24 21:49:00 -04001266/**
James Smart3621a712009-04-06 18:47:14 -04001267 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -04001268 * @phba: Pointer to HBA context object.
1269 *
1270 * This function is called from SLI initialization code
1271 * to configure every ring of the HBA's SLI interface. The
1272 * caller is not required to hold any lock. This function issues
1273 * a config_ring mailbox command for each ring.
1274 * This function returns zero if successful else returns a negative
1275 * error code.
1276 **/
dea31012005-04-17 16:05:31 -05001277static int
James Smarted957682007-06-17 19:56:37 -05001278lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001279{
1280 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -05001281 LPFC_MBOXQ_t *pmb;
1282 MAILBOX_t *pmbox;
1283 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -05001284
James Smarted957682007-06-17 19:56:37 -05001285 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1286 if (!pmb)
1287 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -04001288 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05001289 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05001290 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -05001291 lpfc_config_ring(phba, i, pmb);
1292 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1293 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -05001294 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04001295 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -05001296 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1297 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001298 rc, pmbox->mbxCommand,
1299 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -05001300 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05001301 ret = -ENXIO;
1302 break;
dea31012005-04-17 16:05:31 -05001303 }
1304 }
James Smarted957682007-06-17 19:56:37 -05001305 mempool_free(pmb, phba->mbox_mem_pool);
1306 return ret;
dea31012005-04-17 16:05:31 -05001307}
1308
James Smarte59058c2008-08-24 21:49:00 -04001309/**
James Smart3621a712009-04-06 18:47:14 -04001310 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -04001311 * @phba: Pointer to HBA context object.
1312 * @pring: Pointer to driver SLI ring object.
1313 * @piocb: Pointer to the driver iocb object.
1314 *
1315 * This function is called with hbalock held. The function adds the
1316 * new iocb to txcmplq of the given ring. This function always returns
1317 * 0. If this function is called for ELS ring, this function checks if
1318 * there is a vport associated with the ELS command. This function also
1319 * starts els_tmofunc timer if this is an ELS command.
1320 **/
dea31012005-04-17 16:05:31 -05001321static int
James Smart2e0fef82007-06-17 19:56:36 -05001322lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1323 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05001324{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001325 lockdep_assert_held(&phba->hbalock);
1326
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001327 BUG_ON(!piocb || !piocb->vport);
1328
dea31012005-04-17 16:05:31 -05001329 list_add_tail(&piocb->list, &pring->txcmplq);
James Smart4f2e66c2012-05-09 21:17:07 -04001330 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04001331
James Smart92d7f7b2007-06-17 19:56:38 -05001332 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1333 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
James Smart06918ac2014-02-20 09:57:57 -05001334 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN) &&
Johannes Thumshirn22466da2016-07-29 15:30:56 +02001335 (!(piocb->vport->load_flag & FC_UNLOADING)))
1336 mod_timer(&piocb->vport->els_tmofunc,
1337 jiffies +
1338 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
dea31012005-04-17 16:05:31 -05001339
James Smart2e0fef82007-06-17 19:56:36 -05001340 return 0;
dea31012005-04-17 16:05:31 -05001341}
1342
James Smarte59058c2008-08-24 21:49:00 -04001343/**
James Smart3621a712009-04-06 18:47:14 -04001344 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -04001345 * @phba: Pointer to HBA context object.
1346 * @pring: Pointer to driver SLI ring object.
1347 *
1348 * This function is called with hbalock held to get next
1349 * iocb in txq of the given ring. If there is any iocb in
1350 * the txq, the function returns first iocb in the list after
1351 * removing the iocb from the list, else it returns NULL.
1352 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04001353struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05001354lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001355{
dea31012005-04-17 16:05:31 -05001356 struct lpfc_iocbq *cmd_iocb;
1357
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001358 lockdep_assert_held(&phba->hbalock);
1359
James Smart858c9f62007-06-17 19:56:39 -05001360 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
James Smart2e0fef82007-06-17 19:56:36 -05001361 return cmd_iocb;
dea31012005-04-17 16:05:31 -05001362}
1363
James Smarte59058c2008-08-24 21:49:00 -04001364/**
James Smart3621a712009-04-06 18:47:14 -04001365 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -04001366 * @phba: Pointer to HBA context object.
1367 * @pring: Pointer to driver SLI ring object.
1368 *
1369 * This function is called with hbalock held and the caller must post the
1370 * iocb without releasing the lock. If the caller releases the lock,
1371 * iocb slot returned by the function is not guaranteed to be available.
1372 * The function returns pointer to the next available iocb slot if there
1373 * is available slot in the ring, else it returns NULL.
1374 * If the get index of the ring is ahead of the put index, the function
1375 * will post an error attention event to the worker thread to take the
1376 * HBA to offline state.
1377 **/
dea31012005-04-17 16:05:31 -05001378static IOCB_t *
1379lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1380{
James Smart34b02dc2008-08-24 21:49:55 -04001381 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James Smart7e56aa22012-08-03 12:35:34 -04001382 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001383
1384 lockdep_assert_held(&phba->hbalock);
1385
James Smart7e56aa22012-08-03 12:35:34 -04001386 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1387 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1388 pring->sli.sli3.next_cmdidx = 0;
dea31012005-04-17 16:05:31 -05001389
James Smart7e56aa22012-08-03 12:35:34 -04001390 if (unlikely(pring->sli.sli3.local_getidx ==
1391 pring->sli.sli3.next_cmdidx)) {
dea31012005-04-17 16:05:31 -05001392
James Smart7e56aa22012-08-03 12:35:34 -04001393 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05001394
James Smart7e56aa22012-08-03 12:35:34 -04001395 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
dea31012005-04-17 16:05:31 -05001396 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001397 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02001398 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04001399 pring->ringno,
James Smart7e56aa22012-08-03 12:35:34 -04001400 pring->sli.sli3.local_getidx,
1401 max_cmd_idx);
dea31012005-04-17 16:05:31 -05001402
James Smart2e0fef82007-06-17 19:56:36 -05001403 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001404 /*
1405 * All error attention handlers are posted to
1406 * worker thread
1407 */
1408 phba->work_ha |= HA_ERATT;
1409 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05001410
James Smart5e9d9b82008-06-14 22:52:53 -04001411 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05001412
1413 return NULL;
1414 }
1415
James Smart7e56aa22012-08-03 12:35:34 -04001416 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
dea31012005-04-17 16:05:31 -05001417 return NULL;
1418 }
1419
James Smarted957682007-06-17 19:56:37 -05001420 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -05001421}
1422
James Smarte59058c2008-08-24 21:49:00 -04001423/**
James Smart3621a712009-04-06 18:47:14 -04001424 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -04001425 * @phba: Pointer to HBA context object.
1426 * @iocbq: Pointer to driver iocb object.
1427 *
1428 * This function gets an iotag for the iocb. If there is no unused iotag and
1429 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1430 * array and assigns a new iotag.
1431 * The function returns the allocated iotag if successful, else returns zero.
1432 * Zero is not a valid iotag.
1433 * The caller is not required to hold any lock.
1434 **/
James Bottomley604a3e32005-10-29 10:28:33 -05001435uint16_t
James Smart2e0fef82007-06-17 19:56:36 -05001436lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -05001437{
James Smart2e0fef82007-06-17 19:56:36 -05001438 struct lpfc_iocbq **new_arr;
1439 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -05001440 size_t new_len;
1441 struct lpfc_sli *psli = &phba->sli;
1442 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001443
James Smart2e0fef82007-06-17 19:56:36 -05001444 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001445 iotag = psli->last_iotag;
1446 if(++iotag < psli->iocbq_lookup_len) {
1447 psli->last_iotag = iotag;
1448 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001449 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001450 iocbq->iotag = iotag;
1451 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -05001452 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -05001453 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1454 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -05001455 spin_unlock_irq(&phba->hbalock);
1456 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -05001457 GFP_KERNEL);
1458 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -05001459 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001460 old_arr = psli->iocbq_lookup;
1461 if (new_len <= psli->iocbq_lookup_len) {
1462 /* highly unprobable case */
1463 kfree(new_arr);
1464 iotag = psli->last_iotag;
1465 if(++iotag < psli->iocbq_lookup_len) {
1466 psli->last_iotag = iotag;
1467 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001468 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001469 iocbq->iotag = iotag;
1470 return iotag;
1471 }
James Smart2e0fef82007-06-17 19:56:36 -05001472 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001473 return 0;
1474 }
1475 if (psli->iocbq_lookup)
1476 memcpy(new_arr, old_arr,
1477 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001478 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001479 psli->iocbq_lookup = new_arr;
1480 psli->iocbq_lookup_len = new_len;
1481 psli->last_iotag = iotag;
1482 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001483 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001484 iocbq->iotag = iotag;
1485 kfree(old_arr);
1486 return iotag;
1487 }
James Smart8f6d98d2006-08-01 07:34:00 -04001488 } else
James Smart2e0fef82007-06-17 19:56:36 -05001489 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001490
James Smartbc739052010-08-04 16:11:18 -04001491 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001492 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1493 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001494
James Bottomley604a3e32005-10-29 10:28:33 -05001495 return 0;
dea31012005-04-17 16:05:31 -05001496}
1497
James Smarte59058c2008-08-24 21:49:00 -04001498/**
James Smart3621a712009-04-06 18:47:14 -04001499 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001500 * @phba: Pointer to HBA context object.
1501 * @pring: Pointer to driver SLI ring object.
1502 * @iocb: Pointer to iocb slot in the ring.
1503 * @nextiocb: Pointer to driver iocb object which need to be
1504 * posted to firmware.
1505 *
1506 * This function is called with hbalock held to post a new iocb to
1507 * the firmware. This function copies the new iocb to ring iocb slot and
1508 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1509 * a completion call back for this iocb else the function will free the
1510 * iocb object.
1511 **/
dea31012005-04-17 16:05:31 -05001512static void
1513lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1514 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1515{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001516 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001517 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001518 * Set up an iotag
dea31012005-04-17 16:05:31 -05001519 */
James Bottomley604a3e32005-10-29 10:28:33 -05001520 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001521
James Smarte2a0a9d2008-12-04 22:40:02 -05001522
James Smarta58cbd52007-08-02 11:09:43 -04001523 if (pring->ringno == LPFC_ELS_RING) {
1524 lpfc_debugfs_slow_ring_trc(phba,
1525 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1526 *(((uint32_t *) &nextiocb->iocb) + 4),
1527 *(((uint32_t *) &nextiocb->iocb) + 6),
1528 *(((uint32_t *) &nextiocb->iocb) + 7));
1529 }
1530
dea31012005-04-17 16:05:31 -05001531 /*
1532 * Issue iocb command to adapter
1533 */
James Smart92d7f7b2007-06-17 19:56:38 -05001534 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001535 wmb();
1536 pring->stats.iocb_cmd++;
1537
1538 /*
1539 * If there is no completion routine to call, we can release the
1540 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1541 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1542 */
1543 if (nextiocb->iocb_cmpl)
1544 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001545 else
James Smart2e0fef82007-06-17 19:56:36 -05001546 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001547
1548 /*
1549 * Let the HBA know what IOCB slot will be the next one the
1550 * driver will put a command into.
1551 */
James Smart7e56aa22012-08-03 12:35:34 -04001552 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1553 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001554}
1555
James Smarte59058c2008-08-24 21:49:00 -04001556/**
James Smart3621a712009-04-06 18:47:14 -04001557 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001558 * @phba: Pointer to HBA context object.
1559 * @pring: Pointer to driver SLI ring object.
1560 *
1561 * The caller is not required to hold any lock for calling this function.
1562 * This function updates the chip attention bits for the ring to inform firmware
1563 * that there are pending work to be done for this ring and requests an
1564 * interrupt when there is space available in the ring. This function is
1565 * called when the driver is unable to post more iocbs to the ring due
1566 * to unavailability of space in the ring.
1567 **/
dea31012005-04-17 16:05:31 -05001568static void
James Smart2e0fef82007-06-17 19:56:36 -05001569lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001570{
1571 int ringno = pring->ringno;
1572
1573 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1574
1575 wmb();
1576
1577 /*
1578 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1579 * The HBA will tell us when an IOCB entry is available.
1580 */
1581 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1582 readl(phba->CAregaddr); /* flush */
1583
1584 pring->stats.iocb_cmd_full++;
1585}
1586
James Smarte59058c2008-08-24 21:49:00 -04001587/**
James Smart3621a712009-04-06 18:47:14 -04001588 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001589 * @phba: Pointer to HBA context object.
1590 * @pring: Pointer to driver SLI ring object.
1591 *
1592 * This function updates the chip attention register bit for the
1593 * given ring to inform HBA that there is more work to be done
1594 * in this ring. The caller is not required to hold any lock.
1595 **/
dea31012005-04-17 16:05:31 -05001596static void
James Smart2e0fef82007-06-17 19:56:36 -05001597lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001598{
1599 int ringno = pring->ringno;
1600
1601 /*
1602 * Tell the HBA that there is work to do in this ring.
1603 */
James Smart34b02dc2008-08-24 21:49:55 -04001604 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1605 wmb();
1606 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1607 readl(phba->CAregaddr); /* flush */
1608 }
dea31012005-04-17 16:05:31 -05001609}
1610
James Smarte59058c2008-08-24 21:49:00 -04001611/**
James Smart3621a712009-04-06 18:47:14 -04001612 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001613 * @phba: Pointer to HBA context object.
1614 * @pring: Pointer to driver SLI ring object.
1615 *
1616 * This function is called with hbalock held to post pending iocbs
1617 * in the txq to the firmware. This function is called when driver
1618 * detects space available in the ring.
1619 **/
dea31012005-04-17 16:05:31 -05001620static void
James Smart2e0fef82007-06-17 19:56:36 -05001621lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001622{
1623 IOCB_t *iocb;
1624 struct lpfc_iocbq *nextiocb;
1625
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001626 lockdep_assert_held(&phba->hbalock);
1627
dea31012005-04-17 16:05:31 -05001628 /*
1629 * Check to see if:
1630 * (a) there is anything on the txq to send
1631 * (b) link is up
1632 * (c) link attention events can be processed (fcp ring only)
1633 * (d) IOCB processing is not blocked by the outstanding mbox command.
1634 */
James Smart0e9bb8d2013-03-01 16:35:12 -05001635
1636 if (lpfc_is_link_up(phba) &&
1637 (!list_empty(&pring->txq)) &&
dea31012005-04-17 16:05:31 -05001638 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001639 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001640
1641 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1642 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1643 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1644
1645 if (iocb)
1646 lpfc_sli_update_ring(phba, pring);
1647 else
1648 lpfc_sli_update_full_ring(phba, pring);
1649 }
1650
1651 return;
1652}
1653
James Smarte59058c2008-08-24 21:49:00 -04001654/**
James Smart3621a712009-04-06 18:47:14 -04001655 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001656 * @phba: Pointer to HBA context object.
1657 * @hbqno: HBQ number.
1658 *
1659 * This function is called with hbalock held to get the next
1660 * available slot for the given HBQ. If there is free slot
1661 * available for the HBQ it will return pointer to the next available
1662 * HBQ entry else it will return NULL.
1663 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001664static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001665lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1666{
1667 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1668
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001669 lockdep_assert_held(&phba->hbalock);
1670
James Smarted957682007-06-17 19:56:37 -05001671 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1672 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1673 hbqp->next_hbqPutIdx = 0;
1674
1675 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001676 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001677 uint32_t getidx = le32_to_cpu(raw_index);
1678
1679 hbqp->local_hbqGetIdx = getidx;
1680
1681 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1682 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001683 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001684 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001685 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001686 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001687 hbqp->entry_count);
1688
1689 phba->link_state = LPFC_HBA_ERROR;
1690 return NULL;
1691 }
1692
1693 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1694 return NULL;
1695 }
1696
James Smart51ef4c22007-08-02 11:10:31 -04001697 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1698 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001699}
1700
James Smarte59058c2008-08-24 21:49:00 -04001701/**
James Smart3621a712009-04-06 18:47:14 -04001702 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001703 * @phba: Pointer to HBA context object.
1704 *
1705 * This function is called with no lock held to free all the
1706 * hbq buffers while uninitializing the SLI interface. It also
1707 * frees the HBQ buffers returned by the firmware but not yet
1708 * processed by the upper layers.
1709 **/
James Smarted957682007-06-17 19:56:37 -05001710void
1711lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1712{
James Smart92d7f7b2007-06-17 19:56:38 -05001713 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1714 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001715 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001716 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001717 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001718
James Smart51ef4c22007-08-02 11:10:31 -04001719 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001720 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001721 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001722 for (i = 0; i < hbq_count; ++i) {
1723 list_for_each_entry_safe(dmabuf, next_dmabuf,
1724 &phba->hbqs[i].hbq_buffer_list, list) {
1725 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1726 list_del(&hbq_buf->dbuf.list);
1727 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1728 }
James Smarta8adb832007-10-27 13:37:53 -04001729 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001730 }
James Smart3163f722008-02-08 18:50:25 -05001731 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001732 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1733 list) {
James Smart3163f722008-02-08 18:50:25 -05001734 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1735 list_del(&hbq_buf->dbuf.list);
1736 if (hbq_buf->tag == -1) {
1737 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1738 (phba, hbq_buf);
1739 } else {
1740 hbqno = hbq_buf->tag >> 16;
1741 if (hbqno >= LPFC_MAX_HBQS)
1742 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1743 (phba, hbq_buf);
1744 else
1745 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1746 hbq_buf);
1747 }
1748 }
1749
1750 /* Mark the HBQs not in use */
1751 phba->hbq_in_use = 0;
1752 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001753}
1754
James Smarte59058c2008-08-24 21:49:00 -04001755/**
James Smart3621a712009-04-06 18:47:14 -04001756 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001757 * @phba: Pointer to HBA context object.
1758 * @hbqno: HBQ number.
1759 * @hbq_buf: Pointer to HBQ buffer.
1760 *
1761 * This function is called with the hbalock held to post a
1762 * hbq buffer to the firmware. If the function finds an empty
1763 * slot in the HBQ, it will post the buffer. The function will return
1764 * pointer to the hbq entry if it successfully post the buffer
1765 * else it will return NULL.
1766 **/
James Smart3772a992009-05-22 14:50:54 -04001767static int
James Smarted957682007-06-17 19:56:37 -05001768lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001769 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001770{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001771 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04001772 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1773}
1774
1775/**
1776 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1777 * @phba: Pointer to HBA context object.
1778 * @hbqno: HBQ number.
1779 * @hbq_buf: Pointer to HBQ buffer.
1780 *
1781 * This function is called with the hbalock held to post a hbq buffer to the
1782 * firmware. If the function finds an empty slot in the HBQ, it will post the
1783 * buffer and place it on the hbq_buffer_list. The function will return zero if
1784 * it successfully post the buffer else it will return an error.
1785 **/
1786static int
1787lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1788 struct hbq_dmabuf *hbq_buf)
1789{
James Smarted957682007-06-17 19:56:37 -05001790 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001791 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001792
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001793 lockdep_assert_held(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05001794 /* Get next HBQ entry slot to use */
1795 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1796 if (hbqe) {
1797 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1798
James Smart92d7f7b2007-06-17 19:56:38 -05001799 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1800 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001801 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001802 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001803 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1804 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1805 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001806 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1807 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001808 /* flush */
James Smarted957682007-06-17 19:56:37 -05001809 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001810 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001811 return 0;
1812 } else
1813 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001814}
1815
James Smart4f774512009-05-22 14:52:35 -04001816/**
1817 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1818 * @phba: Pointer to HBA context object.
1819 * @hbqno: HBQ number.
1820 * @hbq_buf: Pointer to HBQ buffer.
1821 *
1822 * This function is called with the hbalock held to post an RQE to the SLI4
1823 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1824 * the hbq_buffer_list and return zero, otherwise it will return an error.
1825 **/
1826static int
1827lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1828 struct hbq_dmabuf *hbq_buf)
1829{
1830 int rc;
1831 struct lpfc_rqe hrqe;
1832 struct lpfc_rqe drqe;
1833
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001834 lockdep_assert_held(&phba->hbalock);
James Smart4f774512009-05-22 14:52:35 -04001835 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1836 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1837 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1838 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1839 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1840 &hrqe, &drqe);
1841 if (rc < 0)
1842 return rc;
1843 hbq_buf->tag = rc;
1844 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1845 return 0;
1846}
1847
James Smarte59058c2008-08-24 21:49:00 -04001848/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001849static struct lpfc_hbq_init lpfc_els_hbq = {
1850 .rn = 1,
James Smartdef9c7a2009-12-21 17:02:28 -05001851 .entry_count = 256,
James Smart92d7f7b2007-06-17 19:56:38 -05001852 .mask_count = 0,
1853 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001854 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001855 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001856 .init_count = 40,
1857 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001858};
James Smarted957682007-06-17 19:56:37 -05001859
James Smarte59058c2008-08-24 21:49:00 -04001860/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001861static struct lpfc_hbq_init lpfc_extra_hbq = {
1862 .rn = 1,
1863 .entry_count = 200,
1864 .mask_count = 0,
1865 .profile = 0,
1866 .ring_mask = (1 << LPFC_EXTRA_RING),
1867 .buffer_count = 0,
1868 .init_count = 0,
1869 .add_count = 5,
1870};
1871
James Smarte59058c2008-08-24 21:49:00 -04001872/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001873struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001874 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001875 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001876};
1877
James Smarte59058c2008-08-24 21:49:00 -04001878/**
James Smart3621a712009-04-06 18:47:14 -04001879 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001880 * @phba: Pointer to HBA context object.
1881 * @hbqno: HBQ number.
1882 * @count: Number of HBQ buffers to be posted.
1883 *
James Smartd7c255b2008-08-24 21:50:00 -04001884 * This function is called with no lock held to post more hbq buffers to the
1885 * given HBQ. The function returns the number of HBQ buffers successfully
1886 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001887 **/
James Smart311464e2007-08-02 11:10:37 -04001888static int
James Smart92d7f7b2007-06-17 19:56:38 -05001889lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1890{
James Smartd7c255b2008-08-24 21:50:00 -04001891 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001892 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001893 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001894 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001895 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001896 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001897
James Smartd7c255b2008-08-24 21:50:00 -04001898 if ((phba->hbqs[hbqno].buffer_count + count) >
1899 lpfc_hbq_defs[hbqno]->entry_count)
1900 count = lpfc_hbq_defs[hbqno]->entry_count -
1901 phba->hbqs[hbqno].buffer_count;
1902 if (!count)
1903 return 0;
1904 /* Allocate HBQ entries */
1905 for (i = 0; i < count; i++) {
1906 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1907 if (!hbq_buffer)
1908 break;
1909 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1910 }
James Smart3163f722008-02-08 18:50:25 -05001911 /* Check whether HBQ is still in use */
1912 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001913 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001914 goto err;
1915 while (!list_empty(&hbq_buf_list)) {
1916 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1917 dbuf.list);
1918 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1919 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001920 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001921 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001922 posted++;
1923 } else
James Smart51ef4c22007-08-02 11:10:31 -04001924 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001925 }
James Smart3163f722008-02-08 18:50:25 -05001926 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001927 return posted;
1928err:
1929 spin_unlock_irqrestore(&phba->hbalock, flags);
1930 while (!list_empty(&hbq_buf_list)) {
1931 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1932 dbuf.list);
1933 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1934 }
James Smart92d7f7b2007-06-17 19:56:38 -05001935 return 0;
James Smarted957682007-06-17 19:56:37 -05001936}
1937
James Smarte59058c2008-08-24 21:49:00 -04001938/**
James Smart3621a712009-04-06 18:47:14 -04001939 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001940 * @phba: Pointer to HBA context object.
1941 * @qno: HBQ number.
1942 *
1943 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001944 * is called with no lock held. The function returns the number of HBQ entries
1945 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001946 **/
James Smarted957682007-06-17 19:56:37 -05001947int
James Smart92d7f7b2007-06-17 19:56:38 -05001948lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001949{
James Smartdef9c7a2009-12-21 17:02:28 -05001950 if (phba->sli_rev == LPFC_SLI_REV4)
1951 return 0;
1952 else
1953 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1954 lpfc_hbq_defs[qno]->add_count);
James Smarted957682007-06-17 19:56:37 -05001955}
1956
James Smarte59058c2008-08-24 21:49:00 -04001957/**
James Smart3621a712009-04-06 18:47:14 -04001958 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001959 * @phba: Pointer to HBA context object.
1960 * @qno: HBQ queue number.
1961 *
1962 * This function is called from SLI initialization code path with
1963 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001964 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001965 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001966static int
James Smart92d7f7b2007-06-17 19:56:38 -05001967lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001968{
James Smartdef9c7a2009-12-21 17:02:28 -05001969 if (phba->sli_rev == LPFC_SLI_REV4)
1970 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
James Smart73d91e52011-10-10 21:32:10 -04001971 lpfc_hbq_defs[qno]->entry_count);
James Smartdef9c7a2009-12-21 17:02:28 -05001972 else
1973 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1974 lpfc_hbq_defs[qno]->init_count);
James Smarted957682007-06-17 19:56:37 -05001975}
1976
James Smarte59058c2008-08-24 21:49:00 -04001977/**
James Smart3772a992009-05-22 14:50:54 -04001978 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1979 * @phba: Pointer to HBA context object.
1980 * @hbqno: HBQ number.
1981 *
1982 * This function removes the first hbq buffer on an hbq list and returns a
1983 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1984 **/
1985static struct hbq_dmabuf *
1986lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1987{
1988 struct lpfc_dmabuf *d_buf;
1989
1990 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1991 if (!d_buf)
1992 return NULL;
1993 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1994}
1995
1996/**
James Smart3621a712009-04-06 18:47:14 -04001997 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001998 * @phba: Pointer to HBA context object.
1999 * @tag: Tag of the hbq buffer.
2000 *
Sebastian Herbszt71892412016-04-17 13:27:27 +02002001 * This function searches for the hbq buffer associated with the given tag in
2002 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2003 * otherwise it returns NULL.
James Smarte59058c2008-08-24 21:49:00 -04002004 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01002005static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05002006lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2007{
James Smart92d7f7b2007-06-17 19:56:38 -05002008 struct lpfc_dmabuf *d_buf;
2009 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04002010 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05002011
James Smart51ef4c22007-08-02 11:10:31 -04002012 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02002013 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04002014 return NULL;
2015
James Smart3772a992009-05-22 14:50:54 -04002016 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04002017 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05002018 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04002019 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04002020 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002021 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05002022 }
2023 }
James Smart3772a992009-05-22 14:50:54 -04002024 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05002025 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04002026 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04002027 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05002028 return NULL;
James Smarted957682007-06-17 19:56:37 -05002029}
2030
James Smarte59058c2008-08-24 21:49:00 -04002031/**
James Smart3621a712009-04-06 18:47:14 -04002032 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04002033 * @phba: Pointer to HBA context object.
2034 * @hbq_buffer: Pointer to HBQ buffer.
2035 *
2036 * This function is called with hbalock. This function gives back
2037 * the hbq buffer to firmware. If the HBQ does not have space to
2038 * post the buffer, it will free the buffer.
2039 **/
James Smarted957682007-06-17 19:56:37 -05002040void
James Smart51ef4c22007-08-02 11:10:31 -04002041lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05002042{
2043 uint32_t hbqno;
2044
James Smart51ef4c22007-08-02 11:10:31 -04002045 if (hbq_buffer) {
2046 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04002047 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04002048 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05002049 }
2050}
2051
James Smarte59058c2008-08-24 21:49:00 -04002052/**
James Smart3621a712009-04-06 18:47:14 -04002053 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04002054 * @mbxCommand: mailbox command code.
2055 *
2056 * This function is called by the mailbox event handler function to verify
2057 * that the completed mailbox command is a legitimate mailbox command. If the
2058 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2059 * and the mailbox event handler will take the HBA offline.
2060 **/
dea31012005-04-17 16:05:31 -05002061static int
2062lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2063{
2064 uint8_t ret;
2065
2066 switch (mbxCommand) {
2067 case MBX_LOAD_SM:
2068 case MBX_READ_NV:
2069 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04002070 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05002071 case MBX_RUN_BIU_DIAG:
2072 case MBX_INIT_LINK:
2073 case MBX_DOWN_LINK:
2074 case MBX_CONFIG_LINK:
2075 case MBX_CONFIG_RING:
2076 case MBX_RESET_RING:
2077 case MBX_READ_CONFIG:
2078 case MBX_READ_RCONFIG:
2079 case MBX_READ_SPARM:
2080 case MBX_READ_STATUS:
2081 case MBX_READ_RPI:
2082 case MBX_READ_XRI:
2083 case MBX_READ_REV:
2084 case MBX_READ_LNK_STAT:
2085 case MBX_REG_LOGIN:
2086 case MBX_UNREG_LOGIN:
dea31012005-04-17 16:05:31 -05002087 case MBX_CLEAR_LA:
2088 case MBX_DUMP_MEMORY:
2089 case MBX_DUMP_CONTEXT:
2090 case MBX_RUN_DIAGS:
2091 case MBX_RESTART:
2092 case MBX_UPDATE_CFG:
2093 case MBX_DOWN_LOAD:
2094 case MBX_DEL_LD_ENTRY:
2095 case MBX_RUN_PROGRAM:
2096 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05002097 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05002098 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002099 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05002100 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05002101 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05002102 case MBX_LOAD_AREA:
2103 case MBX_RUN_BIU_DIAG64:
2104 case MBX_CONFIG_PORT:
2105 case MBX_READ_SPARM64:
2106 case MBX_READ_RPI64:
2107 case MBX_REG_LOGIN64:
James Smart76a95d72010-11-20 23:11:48 -05002108 case MBX_READ_TOPOLOGY:
James Smart09372822008-01-11 01:52:54 -05002109 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05002110 case MBX_SET_DEBUG:
2111 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04002112 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05002113 case MBX_REG_VPI:
2114 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05002115 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04002116 case MBX_PORT_CAPABILITIES:
2117 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04002118 case MBX_SLI4_CONFIG:
2119 case MBX_SLI4_REQ_FTRS:
2120 case MBX_REG_FCFI:
2121 case MBX_UNREG_FCFI:
2122 case MBX_REG_VFI:
2123 case MBX_UNREG_VFI:
2124 case MBX_INIT_VPI:
2125 case MBX_INIT_VFI:
2126 case MBX_RESUME_RPI:
James Smartc7495932010-04-06 15:05:28 -04002127 case MBX_READ_EVENT_LOG_STATUS:
2128 case MBX_READ_EVENT_LOG:
James Smartdcf2a4e2010-09-29 11:18:53 -04002129 case MBX_SECURITY_MGMT:
2130 case MBX_AUTH_PORT:
James Smart940eb682012-08-03 12:37:08 -04002131 case MBX_ACCESS_VDATA:
dea31012005-04-17 16:05:31 -05002132 ret = mbxCommand;
2133 break;
2134 default:
2135 ret = MBX_SHUTDOWN;
2136 break;
2137 }
James Smart2e0fef82007-06-17 19:56:36 -05002138 return ret;
dea31012005-04-17 16:05:31 -05002139}
James Smarte59058c2008-08-24 21:49:00 -04002140
2141/**
James Smart3621a712009-04-06 18:47:14 -04002142 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002143 * @phba: Pointer to HBA context object.
2144 * @pmboxq: Pointer to mailbox command.
2145 *
2146 * This is completion handler function for mailbox commands issued from
2147 * lpfc_sli_issue_mbox_wait function. This function is called by the
2148 * mailbox event handler function with no lock held. This function
2149 * will wake up thread waiting on the wait queue pointed by context1
2150 * of the mailbox.
2151 **/
James Smart04c68492009-05-22 14:52:52 -04002152void
James Smart2e0fef82007-06-17 19:56:36 -05002153lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05002154{
2155 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05002156 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05002157
2158 /*
2159 * If pdone_q is empty, the driver thread gave up waiting and
2160 * continued running.
2161 */
James Smart7054a602007-04-25 09:52:34 -04002162 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05002163 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002164 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2165 if (pdone_q)
2166 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05002167 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05002168 return;
2169}
2170
James Smarte59058c2008-08-24 21:49:00 -04002171
2172/**
James Smart3621a712009-04-06 18:47:14 -04002173 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04002174 * @phba: Pointer to HBA context object.
2175 * @pmb: Pointer to mailbox object.
2176 *
2177 * This function is the default mailbox completion handler. It
2178 * frees the memory resources associated with the completed mailbox
2179 * command. If the completed command is a REG_LOGIN mailbox command,
2180 * this function will issue a UREG_LOGIN to re-claim the RPI.
2181 **/
dea31012005-04-17 16:05:31 -05002182void
James Smart2e0fef82007-06-17 19:56:36 -05002183lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002184{
James Smartd439d282010-09-29 11:18:45 -04002185 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05002186 struct lpfc_dmabuf *mp;
James Smartd439d282010-09-29 11:18:45 -04002187 struct lpfc_nodelist *ndlp;
James Smart5af5eee2010-10-22 11:06:38 -04002188 struct Scsi_Host *shost;
James Smart04c68492009-05-22 14:52:52 -04002189 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04002190 int rc;
2191
dea31012005-04-17 16:05:31 -05002192 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04002193
dea31012005-04-17 16:05:31 -05002194 if (mp) {
2195 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2196 kfree(mp);
2197 }
James Smart7054a602007-04-25 09:52:34 -04002198
2199 /*
2200 * If a REG_LOGIN succeeded after node is destroyed or node
2201 * is in re-discovery driver need to cleanup the RPI.
2202 */
James Smart2e0fef82007-06-17 19:56:36 -05002203 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04002204 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2205 !pmb->u.mb.mbxStatus) {
2206 rpi = pmb->u.mb.un.varWords[0];
James Smart6d368e52011-05-24 11:44:12 -04002207 vpi = pmb->u.mb.un.varRegLogin.vpi;
James Smart04c68492009-05-22 14:52:52 -04002208 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smartde96e9c2016-03-31 14:12:27 -07002209 pmb->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05002210 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04002211 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2212 if (rc != MBX_NOT_FINISHED)
2213 return;
2214 }
2215
James Smart695a8142010-01-26 23:08:03 -05002216 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2217 !(phba->pport->load_flag & FC_UNLOADING) &&
2218 !pmb->u.mb.mbxStatus) {
James Smart5af5eee2010-10-22 11:06:38 -04002219 shost = lpfc_shost_from_vport(vport);
2220 spin_lock_irq(shost->host_lock);
2221 vport->vpi_state |= LPFC_VPI_REGISTERED;
2222 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2223 spin_unlock_irq(shost->host_lock);
James Smart695a8142010-01-26 23:08:03 -05002224 }
2225
James Smartd439d282010-09-29 11:18:45 -04002226 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2227 ndlp = (struct lpfc_nodelist *)pmb->context2;
2228 lpfc_nlp_put(ndlp);
2229 pmb->context2 = NULL;
2230 }
2231
James Smartdcf2a4e2010-09-29 11:18:53 -04002232 /* Check security permission status on INIT_LINK mailbox command */
2233 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2234 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2235 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2236 "2860 SLI authentication is required "
2237 "for INIT_LINK but has not done yet\n");
2238
James Smart04c68492009-05-22 14:52:52 -04002239 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2240 lpfc_sli4_mbox_cmd_free(phba, pmb);
2241 else
2242 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05002243}
James Smartbe6bb942015-04-07 15:07:22 -04002244 /**
2245 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2246 * @phba: Pointer to HBA context object.
2247 * @pmb: Pointer to mailbox object.
2248 *
2249 * This function is the unreg rpi mailbox completion handler. It
2250 * frees the memory resources associated with the completed mailbox
2251 * command. An additional refrenece is put on the ndlp to prevent
2252 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2253 * the unreg mailbox command completes, this routine puts the
2254 * reference back.
2255 *
2256 **/
2257void
2258lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2259{
2260 struct lpfc_vport *vport = pmb->vport;
2261 struct lpfc_nodelist *ndlp;
2262
2263 ndlp = pmb->context1;
2264 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2265 if (phba->sli_rev == LPFC_SLI_REV4 &&
2266 (bf_get(lpfc_sli_intf_if_type,
2267 &phba->sli4_hba.sli_intf) ==
2268 LPFC_SLI_INTF_IF_TYPE_2)) {
2269 if (ndlp) {
2270 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2271 "0010 UNREG_LOGIN vpi:%x "
2272 "rpi:%x DID:%x map:%x %p\n",
2273 vport->vpi, ndlp->nlp_rpi,
2274 ndlp->nlp_DID,
2275 ndlp->nlp_usg_map, ndlp);
James Smart7c5e5182015-05-22 10:42:43 -04002276 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
James Smartbe6bb942015-04-07 15:07:22 -04002277 lpfc_nlp_put(ndlp);
2278 }
2279 }
2280 }
2281
2282 mempool_free(pmb, phba->mbox_mem_pool);
2283}
dea31012005-04-17 16:05:31 -05002284
James Smarte59058c2008-08-24 21:49:00 -04002285/**
James Smart3621a712009-04-06 18:47:14 -04002286 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04002287 * @phba: Pointer to HBA context object.
2288 *
2289 * This function is called with no lock held. This function processes all
2290 * the completed mailbox commands and gives it to upper layers. The interrupt
2291 * service routine processes mailbox completion interrupt and adds completed
2292 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2293 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2294 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2295 * function returns the mailbox commands to the upper layer by calling the
2296 * completion handler function of each mailbox.
2297 **/
dea31012005-04-17 16:05:31 -05002298int
James Smart2e0fef82007-06-17 19:56:36 -05002299lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05002300{
James Smart92d7f7b2007-06-17 19:56:38 -05002301 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05002302 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05002303 int rc;
2304 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05002305
2306 phba->sli.slistat.mbox_event++;
2307
James Smart92d7f7b2007-06-17 19:56:38 -05002308 /* Get all completed mailboxe buffers into the cmplq */
2309 spin_lock_irq(&phba->hbalock);
2310 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2311 spin_unlock_irq(&phba->hbalock);
2312
dea31012005-04-17 16:05:31 -05002313 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05002314 do {
2315 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2316 if (pmb == NULL)
2317 break;
2318
James Smart04c68492009-05-22 14:52:52 -04002319 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002320
James Smart858c9f62007-06-17 19:56:39 -05002321 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2322 if (pmb->vport) {
2323 lpfc_debugfs_disc_trc(pmb->vport,
2324 LPFC_DISC_TRC_MBOX_VPORT,
2325 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2326 (uint32_t)pmbox->mbxCommand,
2327 pmbox->un.varWords[0],
2328 pmbox->un.varWords[1]);
2329 }
2330 else {
2331 lpfc_debugfs_disc_trc(phba->pport,
2332 LPFC_DISC_TRC_MBOX,
2333 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2334 (uint32_t)pmbox->mbxCommand,
2335 pmbox->un.varWords[0],
2336 pmbox->un.varWords[1]);
2337 }
2338 }
2339
dea31012005-04-17 16:05:31 -05002340 /*
2341 * It is a fatal error if unknown mbox command completion.
2342 */
2343 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2344 MBX_SHUTDOWN) {
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002345 /* Unknown mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05002346 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002347 "(%d):0323 Unknown Mailbox command "
James Smarta183a152011-10-10 21:32:43 -04002348 "x%x (x%x/x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002349 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04002350 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002351 lpfc_sli_config_mbox_subsys_get(phba,
2352 pmb),
2353 lpfc_sli_config_mbox_opcode_get(phba,
2354 pmb));
James Smart2e0fef82007-06-17 19:56:36 -05002355 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05002356 phba->work_hs = HS_FFER3;
2357 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002358 continue;
dea31012005-04-17 16:05:31 -05002359 }
2360
dea31012005-04-17 16:05:31 -05002361 if (pmbox->mbxStatus) {
2362 phba->sli.slistat.mbox_stat_err++;
2363 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2364 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05002365 lpfc_printf_log(phba, KERN_INFO,
James Smarta183a152011-10-10 21:32:43 -04002366 LOG_MBOX | LOG_SLI,
2367 "(%d):0305 Mbox cmd cmpl "
2368 "error - RETRYing Data: x%x "
2369 "(x%x/x%x) x%x x%x x%x\n",
2370 pmb->vport ? pmb->vport->vpi : 0,
2371 pmbox->mbxCommand,
2372 lpfc_sli_config_mbox_subsys_get(phba,
2373 pmb),
2374 lpfc_sli_config_mbox_opcode_get(phba,
2375 pmb),
2376 pmbox->mbxStatus,
2377 pmbox->un.varWords[0],
2378 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05002379 pmbox->mbxStatus = 0;
2380 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05002381 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04002382 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05002383 continue;
dea31012005-04-17 16:05:31 -05002384 }
2385 }
2386
2387 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05002388 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04002389 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
James Smarte74c03c2013-04-17 20:15:19 -04002390 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2391 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05002392 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05002393 pmbox->mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04002394 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2395 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05002396 pmb->mbox_cmpl,
2397 *((uint32_t *) pmbox),
2398 pmbox->un.varWords[0],
2399 pmbox->un.varWords[1],
2400 pmbox->un.varWords[2],
2401 pmbox->un.varWords[3],
2402 pmbox->un.varWords[4],
2403 pmbox->un.varWords[5],
2404 pmbox->un.varWords[6],
James Smarte74c03c2013-04-17 20:15:19 -04002405 pmbox->un.varWords[7],
2406 pmbox->un.varWords[8],
2407 pmbox->un.varWords[9],
2408 pmbox->un.varWords[10]);
dea31012005-04-17 16:05:31 -05002409
James Smart92d7f7b2007-06-17 19:56:38 -05002410 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05002411 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05002412 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05002413 return 0;
dea31012005-04-17 16:05:31 -05002414}
James Smart92d7f7b2007-06-17 19:56:38 -05002415
James Smarte59058c2008-08-24 21:49:00 -04002416/**
James Smart3621a712009-04-06 18:47:14 -04002417 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04002418 * @phba: Pointer to HBA context object.
2419 * @pring: Pointer to driver SLI ring object.
2420 * @tag: buffer tag.
2421 *
2422 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2423 * is set in the tag the buffer is posted for a particular exchange,
2424 * the function will return the buffer without replacing the buffer.
2425 * If the buffer is for unsolicited ELS or CT traffic, this function
2426 * returns the buffer and also posts another buffer to the firmware.
2427 **/
James Smart76bb24e2007-10-27 13:38:00 -04002428static struct lpfc_dmabuf *
2429lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05002430 struct lpfc_sli_ring *pring,
2431 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04002432{
James Smart9f1e1b52008-12-04 22:39:40 -05002433 struct hbq_dmabuf *hbq_entry;
2434
James Smart76bb24e2007-10-27 13:38:00 -04002435 if (tag & QUE_BUFTAG_BIT)
2436 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05002437 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2438 if (!hbq_entry)
2439 return NULL;
2440 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04002441}
James Smart57127f12007-10-27 13:37:05 -04002442
James Smart3772a992009-05-22 14:50:54 -04002443/**
2444 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2445 * @phba: Pointer to HBA context object.
2446 * @pring: Pointer to driver SLI ring object.
2447 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2448 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2449 * @fch_type: the type for the first frame of the sequence.
2450 *
2451 * This function is called with no lock held. This function uses the r_ctl and
2452 * type of the received sequence to find the correct callback function to call
2453 * to process the sequence.
2454 **/
2455static int
2456lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2457 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2458 uint32_t fch_type)
2459{
2460 int i;
2461
2462 /* unSolicited Responses */
2463 if (pring->prt[0].profile) {
2464 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2465 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2466 saveq);
2467 return 1;
2468 }
2469 /* We must search, based on rctl / type
2470 for the right routine */
2471 for (i = 0; i < pring->num_mask; i++) {
2472 if ((pring->prt[i].rctl == fch_r_ctl) &&
2473 (pring->prt[i].type == fch_type)) {
2474 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2475 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2476 (phba, pring, saveq);
2477 return 1;
2478 }
2479 }
2480 return 0;
2481}
James Smarte59058c2008-08-24 21:49:00 -04002482
2483/**
James Smart3621a712009-04-06 18:47:14 -04002484 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04002485 * @phba: Pointer to HBA context object.
2486 * @pring: Pointer to driver SLI ring object.
2487 * @saveq: Pointer to the unsolicited iocb.
2488 *
2489 * This function is called with no lock held by the ring event handler
2490 * when there is an unsolicited iocb posted to the response ring by the
2491 * firmware. This function gets the buffer associated with the iocbs
2492 * and calls the event handler for the ring. This function handles both
2493 * qring buffers and hbq buffers.
2494 * When the function returns 1 the caller can free the iocb object otherwise
2495 * upper layer functions will free the iocb objects.
2496 **/
dea31012005-04-17 16:05:31 -05002497static int
2498lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2499 struct lpfc_iocbq *saveq)
2500{
2501 IOCB_t * irsp;
2502 WORD5 * w5p;
2503 uint32_t Rctl, Type;
James Smart76bb24e2007-10-27 13:38:00 -04002504 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05002505 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05002506
dea31012005-04-17 16:05:31 -05002507 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04002508
2509 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2510 if (pring->lpfc_sli_rcv_async_status)
2511 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2512 else
2513 lpfc_printf_log(phba,
2514 KERN_WARNING,
2515 LOG_SLI,
2516 "0316 Ring %d handler: unexpected "
2517 "ASYNC_STATUS iocb received evt_code "
2518 "0x%x\n",
2519 pring->ringno,
2520 irsp->un.asyncstat.evt_code);
2521 return 1;
2522 }
2523
James Smart3163f722008-02-08 18:50:25 -05002524 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2525 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2526 if (irsp->ulpBdeCount > 0) {
2527 dmzbuf = lpfc_sli_get_buff(phba, pring,
2528 irsp->un.ulpWord[3]);
2529 lpfc_in_buf_free(phba, dmzbuf);
2530 }
2531
2532 if (irsp->ulpBdeCount > 1) {
2533 dmzbuf = lpfc_sli_get_buff(phba, pring,
2534 irsp->unsli3.sli3Words[3]);
2535 lpfc_in_buf_free(phba, dmzbuf);
2536 }
2537
2538 if (irsp->ulpBdeCount > 2) {
2539 dmzbuf = lpfc_sli_get_buff(phba, pring,
2540 irsp->unsli3.sli3Words[7]);
2541 lpfc_in_buf_free(phba, dmzbuf);
2542 }
2543
2544 return 1;
2545 }
2546
James Smart92d7f7b2007-06-17 19:56:38 -05002547 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04002548 if (irsp->ulpBdeCount != 0) {
2549 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05002550 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002551 if (!saveq->context2)
2552 lpfc_printf_log(phba,
2553 KERN_ERR,
2554 LOG_SLI,
2555 "0341 Ring %d Cannot find buffer for "
2556 "an unsolicited iocb. tag 0x%x\n",
2557 pring->ringno,
2558 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002559 }
2560 if (irsp->ulpBdeCount == 2) {
2561 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002562 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002563 if (!saveq->context3)
2564 lpfc_printf_log(phba,
2565 KERN_ERR,
2566 LOG_SLI,
2567 "0342 Ring %d Cannot find buffer for an"
2568 " unsolicited iocb. tag 0x%x\n",
2569 pring->ringno,
2570 irsp->unsli3.sli3Words[7]);
2571 }
2572 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002573 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002574 if (irsp->ulpBdeCount != 0) {
2575 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2576 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002577 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002578 lpfc_printf_log(phba,
2579 KERN_ERR,
2580 LOG_SLI,
2581 "0343 Ring %d Cannot find "
2582 "buffer for an unsolicited iocb"
2583 ". tag 0x%x\n", pring->ringno,
2584 irsp->un.ulpWord[3]);
2585 }
2586 if (irsp->ulpBdeCount == 2) {
2587 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2588 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002589 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002590 lpfc_printf_log(phba,
2591 KERN_ERR,
2592 LOG_SLI,
2593 "0344 Ring %d Cannot find "
2594 "buffer for an unsolicited "
2595 "iocb. tag 0x%x\n",
2596 pring->ringno,
2597 irsp->unsli3.sli3Words[7]);
2598 }
2599 }
James Smart92d7f7b2007-06-17 19:56:38 -05002600 }
James Smart9c2face2008-01-11 01:53:18 -05002601 if (irsp->ulpBdeCount != 0 &&
2602 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2603 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2604 int found = 0;
2605
2606 /* search continue save q for same XRI */
2607 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
James Smart7851fe22011-07-22 18:36:52 -04002608 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2609 saveq->iocb.unsli3.rcvsli3.ox_id) {
James Smart9c2face2008-01-11 01:53:18 -05002610 list_add_tail(&saveq->list, &iocbq->list);
2611 found = 1;
2612 break;
2613 }
2614 }
2615 if (!found)
2616 list_add_tail(&saveq->clist,
2617 &pring->iocb_continue_saveq);
2618 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2619 list_del_init(&iocbq->clist);
2620 saveq = iocbq;
2621 irsp = &(saveq->iocb);
2622 } else
2623 return 0;
2624 }
2625 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2626 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2627 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002628 Rctl = FC_RCTL_ELS_REQ;
2629 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002630 } else {
2631 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2632 Rctl = w5p->hcsw.Rctl;
2633 Type = w5p->hcsw.Type;
2634
2635 /* Firmware Workaround */
2636 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2637 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2638 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
James Smart6a9c52c2009-10-02 15:16:51 -04002639 Rctl = FC_RCTL_ELS_REQ;
2640 Type = FC_TYPE_ELS;
James Smart9c2face2008-01-11 01:53:18 -05002641 w5p->hcsw.Rctl = Rctl;
2642 w5p->hcsw.Type = Type;
2643 }
2644 }
James Smart92d7f7b2007-06-17 19:56:38 -05002645
James Smart3772a992009-05-22 14:50:54 -04002646 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002647 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002648 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002649 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002650 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002651
James Smart92d7f7b2007-06-17 19:56:38 -05002652 return 1;
dea31012005-04-17 16:05:31 -05002653}
2654
James Smarte59058c2008-08-24 21:49:00 -04002655/**
James Smart3621a712009-04-06 18:47:14 -04002656 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002657 * @phba: Pointer to HBA context object.
2658 * @pring: Pointer to driver SLI ring object.
2659 * @prspiocb: Pointer to response iocb object.
2660 *
2661 * This function looks up the iocb_lookup table to get the command iocb
2662 * corresponding to the given response iocb using the iotag of the
2663 * response iocb. This function is called with the hbalock held.
2664 * This function returns the command iocb object if it finds the command
2665 * iocb else returns NULL.
2666 **/
dea31012005-04-17 16:05:31 -05002667static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002668lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2669 struct lpfc_sli_ring *pring,
2670 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002671{
dea31012005-04-17 16:05:31 -05002672 struct lpfc_iocbq *cmd_iocb = NULL;
2673 uint16_t iotag;
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002674 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05002675
James Bottomley604a3e32005-10-29 10:28:33 -05002676 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002677
James Bottomley604a3e32005-10-29 10:28:33 -05002678 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2679 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002680 list_del_init(&cmd_iocb->list);
James Smart4f2e66c2012-05-09 21:17:07 -04002681 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
James Smart4f2e66c2012-05-09 21:17:07 -04002682 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart2a9bf3d2010-06-07 15:24:45 -04002683 }
James Bottomley604a3e32005-10-29 10:28:33 -05002684 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002685 }
2686
dea31012005-04-17 16:05:31 -05002687 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002688 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002689 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002690 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002691 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002692 return NULL;
2693}
2694
James Smarte59058c2008-08-24 21:49:00 -04002695/**
James Smart3772a992009-05-22 14:50:54 -04002696 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2697 * @phba: Pointer to HBA context object.
2698 * @pring: Pointer to driver SLI ring object.
2699 * @iotag: IOCB tag.
2700 *
2701 * This function looks up the iocb_lookup table to get the command iocb
2702 * corresponding to the given iotag. This function is called with the
2703 * hbalock held.
2704 * This function returns the command iocb object if it finds the command
2705 * iocb else returns NULL.
2706 **/
2707static struct lpfc_iocbq *
2708lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2709 struct lpfc_sli_ring *pring, uint16_t iotag)
2710{
2711 struct lpfc_iocbq *cmd_iocb;
2712
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01002713 lockdep_assert_held(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04002714 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2715 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart4f2e66c2012-05-09 21:17:07 -04002716 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2717 /* remove from txcmpl queue list */
2718 list_del_init(&cmd_iocb->list);
2719 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
James Smart4f2e66c2012-05-09 21:17:07 -04002720 return cmd_iocb;
James Smart2a9bf3d2010-06-07 15:24:45 -04002721 }
James Smart3772a992009-05-22 14:50:54 -04002722 }
James Smart3772a992009-05-22 14:50:54 -04002723 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2724 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2725 iotag, phba->sli.last_iotag);
2726 return NULL;
2727}
2728
2729/**
James Smart3621a712009-04-06 18:47:14 -04002730 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002731 * @phba: Pointer to HBA context object.
2732 * @pring: Pointer to driver SLI ring object.
2733 * @saveq: Pointer to the response iocb to be processed.
2734 *
2735 * This function is called by the ring event handler for non-fcp
2736 * rings when there is a new response iocb in the response ring.
2737 * The caller is not required to hold any locks. This function
2738 * gets the command iocb associated with the response iocb and
2739 * calls the completion handler for the command iocb. If there
2740 * is no completion handler, the function will free the resources
2741 * associated with command iocb. If the response iocb is for
2742 * an already aborted command iocb, the status of the completion
2743 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2744 * This function always returns 1.
2745 **/
dea31012005-04-17 16:05:31 -05002746static int
James Smart2e0fef82007-06-17 19:56:36 -05002747lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002748 struct lpfc_iocbq *saveq)
2749{
James Smart2e0fef82007-06-17 19:56:36 -05002750 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002751 int rc = 1;
2752 unsigned long iflag;
2753
2754 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002755 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002756 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002757 spin_unlock_irqrestore(&phba->hbalock, iflag);
2758
dea31012005-04-17 16:05:31 -05002759 if (cmdiocbp) {
2760 if (cmdiocbp->iocb_cmpl) {
2761 /*
James Smartea2151b2008-09-07 11:52:10 -04002762 * If an ELS command failed send an event to mgmt
2763 * application.
2764 */
2765 if (saveq->iocb.ulpStatus &&
2766 (pring->ringno == LPFC_ELS_RING) &&
2767 (cmdiocbp->iocb.ulpCommand ==
2768 CMD_ELS_REQUEST64_CR))
2769 lpfc_send_els_failure_event(phba,
2770 cmdiocbp, saveq);
2771
2772 /*
dea31012005-04-17 16:05:31 -05002773 * Post all ELS completions to the worker thread.
2774 * All other are passed to the completion callback.
2775 */
2776 if (pring->ringno == LPFC_ELS_RING) {
James Smart341af102010-01-26 23:07:37 -05002777 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2778 (cmdiocbp->iocb_flag &
2779 LPFC_DRIVER_ABORTED)) {
2780 spin_lock_irqsave(&phba->hbalock,
2781 iflag);
James Smart07951072007-04-25 09:51:38 -04002782 cmdiocbp->iocb_flag &=
2783 ~LPFC_DRIVER_ABORTED;
James Smart341af102010-01-26 23:07:37 -05002784 spin_unlock_irqrestore(&phba->hbalock,
2785 iflag);
James Smart07951072007-04-25 09:51:38 -04002786 saveq->iocb.ulpStatus =
2787 IOSTAT_LOCAL_REJECT;
2788 saveq->iocb.un.ulpWord[4] =
2789 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002790
2791 /* Firmware could still be in progress
2792 * of DMAing payload, so don't free data
2793 * buffer till after a hbeat.
2794 */
James Smart341af102010-01-26 23:07:37 -05002795 spin_lock_irqsave(&phba->hbalock,
2796 iflag);
James Smart0ff10d42008-01-11 01:52:36 -05002797 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart341af102010-01-26 23:07:37 -05002798 spin_unlock_irqrestore(&phba->hbalock,
2799 iflag);
2800 }
James Smart0f65ff62010-02-26 14:14:23 -05002801 if (phba->sli_rev == LPFC_SLI_REV4) {
2802 if (saveq->iocb_flag &
2803 LPFC_EXCHANGE_BUSY) {
2804 /* Set cmdiocb flag for the
2805 * exchange busy so sgl (xri)
2806 * will not be released until
2807 * the abort xri is received
2808 * from hba.
2809 */
2810 spin_lock_irqsave(
2811 &phba->hbalock, iflag);
2812 cmdiocbp->iocb_flag |=
2813 LPFC_EXCHANGE_BUSY;
2814 spin_unlock_irqrestore(
2815 &phba->hbalock, iflag);
2816 }
2817 if (cmdiocbp->iocb_flag &
2818 LPFC_DRIVER_ABORTED) {
2819 /*
2820 * Clear LPFC_DRIVER_ABORTED
2821 * bit in case it was driver
2822 * initiated abort.
2823 */
2824 spin_lock_irqsave(
2825 &phba->hbalock, iflag);
2826 cmdiocbp->iocb_flag &=
2827 ~LPFC_DRIVER_ABORTED;
2828 spin_unlock_irqrestore(
2829 &phba->hbalock, iflag);
2830 cmdiocbp->iocb.ulpStatus =
2831 IOSTAT_LOCAL_REJECT;
2832 cmdiocbp->iocb.un.ulpWord[4] =
2833 IOERR_ABORT_REQUESTED;
2834 /*
2835 * For SLI4, irsiocb contains
2836 * NO_XRI in sli_xritag, it
2837 * shall not affect releasing
2838 * sgl (xri) process.
2839 */
2840 saveq->iocb.ulpStatus =
2841 IOSTAT_LOCAL_REJECT;
2842 saveq->iocb.un.ulpWord[4] =
2843 IOERR_SLI_ABORTED;
2844 spin_lock_irqsave(
2845 &phba->hbalock, iflag);
2846 saveq->iocb_flag |=
2847 LPFC_DELAY_MEM_FREE;
2848 spin_unlock_irqrestore(
2849 &phba->hbalock, iflag);
2850 }
James Smart07951072007-04-25 09:51:38 -04002851 }
dea31012005-04-17 16:05:31 -05002852 }
James Smart2e0fef82007-06-17 19:56:36 -05002853 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002854 } else
2855 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002856 } else {
2857 /*
2858 * Unknown initiating command based on the response iotag.
2859 * This could be the case on the ELS ring because of
2860 * lpfc_els_abort().
2861 */
2862 if (pring->ringno != LPFC_ELS_RING) {
2863 /*
2864 * Ring <ringno> handler: unexpected completion IoTag
2865 * <IoTag>
2866 */
James Smarta257bf92009-04-06 18:48:10 -04002867 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002868 "0322 Ring %d handler: "
2869 "unexpected completion IoTag x%x "
2870 "Data: x%x x%x x%x x%x\n",
2871 pring->ringno,
2872 saveq->iocb.ulpIoTag,
2873 saveq->iocb.ulpStatus,
2874 saveq->iocb.un.ulpWord[4],
2875 saveq->iocb.ulpCommand,
2876 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002877 }
2878 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002879
dea31012005-04-17 16:05:31 -05002880 return rc;
2881}
2882
James Smarte59058c2008-08-24 21:49:00 -04002883/**
James Smart3621a712009-04-06 18:47:14 -04002884 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002885 * @phba: Pointer to HBA context object.
2886 * @pring: Pointer to driver SLI ring object.
2887 *
2888 * This function is called from the iocb ring event handlers when
2889 * put pointer is ahead of the get pointer for a ring. This function signal
2890 * an error attention condition to the worker thread and the worker
2891 * thread will transition the HBA to offline state.
2892 **/
James Smart2e0fef82007-06-17 19:56:36 -05002893static void
2894lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002895{
James Smart34b02dc2008-08-24 21:49:55 -04002896 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002897 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002898 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002899 * rsp ring <portRspMax>
2900 */
2901 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002902 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002903 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002904 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James Smart7e56aa22012-08-03 12:35:34 -04002905 pring->sli.sli3.numRiocb);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002906
James Smart2e0fef82007-06-17 19:56:36 -05002907 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002908
2909 /*
2910 * All error attention handlers are posted to
2911 * worker thread
2912 */
2913 phba->work_ha |= HA_ERATT;
2914 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002915
James Smart5e9d9b82008-06-14 22:52:53 -04002916 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002917
2918 return;
2919}
2920
James Smarte59058c2008-08-24 21:49:00 -04002921/**
James Smart3621a712009-04-06 18:47:14 -04002922 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002923 * @ptr: Pointer to address of HBA context object.
2924 *
2925 * This function is invoked by the Error Attention polling timer when the
2926 * timer times out. It will check the SLI Error Attention register for
2927 * possible attention events. If so, it will post an Error Attention event
2928 * and wake up worker thread to process it. Otherwise, it will set up the
2929 * Error Attention polling timer for the next poll.
2930 **/
2931void lpfc_poll_eratt(unsigned long ptr)
2932{
2933 struct lpfc_hba *phba;
James Smarteb016562014-09-03 12:58:06 -04002934 uint32_t eratt = 0;
James Smartaa6fbb72012-08-03 12:36:03 -04002935 uint64_t sli_intr, cnt;
James Smart93996272008-08-24 21:50:30 -04002936
2937 phba = (struct lpfc_hba *)ptr;
2938
James Smartaa6fbb72012-08-03 12:36:03 -04002939 /* Here we will also keep track of interrupts per sec of the hba */
2940 sli_intr = phba->sli.slistat.sli_intr;
2941
2942 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2943 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2944 sli_intr);
2945 else
2946 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2947
James Smart65791f12016-07-06 12:35:56 -07002948 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
2949 do_div(cnt, phba->eratt_poll_interval);
James Smartaa6fbb72012-08-03 12:36:03 -04002950 phba->sli.slistat.sli_ips = cnt;
2951
2952 phba->sli.slistat.sli_prev_intr = sli_intr;
2953
James Smart93996272008-08-24 21:50:30 -04002954 /* Check chip HA register for error event */
2955 eratt = lpfc_sli_check_eratt(phba);
2956
2957 if (eratt)
2958 /* Tell the worker thread there is work to do */
2959 lpfc_worker_wake_up(phba);
2960 else
2961 /* Restart the timer for next eratt poll */
James Smart256ec0d2013-04-17 20:14:58 -04002962 mod_timer(&phba->eratt_poll,
2963 jiffies +
James Smart65791f12016-07-06 12:35:56 -07002964 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smart93996272008-08-24 21:50:30 -04002965 return;
2966}
2967
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002968
James Smarte59058c2008-08-24 21:49:00 -04002969/**
James Smart3621a712009-04-06 18:47:14 -04002970 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002971 * @phba: Pointer to HBA context object.
2972 * @pring: Pointer to driver SLI ring object.
2973 * @mask: Host attention register mask for this ring.
2974 *
2975 * This function is called from the interrupt context when there is a ring
2976 * event for the fcp ring. The caller does not hold any lock.
2977 * The function processes each response iocb in the response ring until it
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002978 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
James Smarte59058c2008-08-24 21:49:00 -04002979 * LE bit set. The function will call the completion handler of the command iocb
2980 * if the response iocb indicates a completion for a command iocb or it is
2981 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2982 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002983 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smart45ed1192009-10-02 15:17:02 -04002984 * to check it explicitly.
2985 */
2986int
James Smart2e0fef82007-06-17 19:56:36 -05002987lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2988 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002989{
James Smart34b02dc2008-08-24 21:49:55 -04002990 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002991 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002992 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002993 struct lpfc_iocbq *cmdiocbq = NULL;
2994 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002995 uint32_t status;
2996 uint32_t portRspPut, portRspMax;
2997 int rc = 1;
2998 lpfc_iocb_type type;
2999 unsigned long iflag;
3000 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05003001
James Smart2e0fef82007-06-17 19:56:36 -05003002 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003003 pring->stats.iocb_event++;
3004
dea31012005-04-17 16:05:31 -05003005 /*
3006 * The next available response entry should never exceed the maximum
3007 * entries. If it does, treat it as an adapter hardware error.
3008 */
James Smart7e56aa22012-08-03 12:35:34 -04003009 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003010 portRspPut = le32_to_cpu(pgp->rspPutInx);
3011 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003012 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05003013 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003014 return 1;
3015 }
James Smart45ed1192009-10-02 15:17:02 -04003016 if (phba->fcp_ring_in_use) {
3017 spin_unlock_irqrestore(&phba->hbalock, iflag);
3018 return 1;
3019 } else
3020 phba->fcp_ring_in_use = 1;
dea31012005-04-17 16:05:31 -05003021
3022 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003023 while (pring->sli.sli3.rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003024 /*
3025 * Fetch an entry off the ring and copy it into a local data
3026 * structure. The copy involves a byte-swap since the
3027 * network byte order and pci byte orders are different.
3028 */
James Smarted957682007-06-17 19:56:37 -05003029 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05003030 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003031
James Smart7e56aa22012-08-03 12:35:34 -04003032 if (++pring->sli.sli3.rspidx >= portRspMax)
3033 pring->sli.sli3.rspidx = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05003034
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003035 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3036 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05003037 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05003038 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04003039 irsp = &rspiocbq.iocb;
3040
dea31012005-04-17 16:05:31 -05003041 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3042 pring->stats.iocb_rsp++;
3043 rsp_cmpl++;
3044
3045 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003046 /*
3047 * If resource errors reported from HBA, reduce
3048 * queuedepths of the SCSI device.
3049 */
3050 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003051 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3052 IOERR_NO_RESOURCES)) {
James Smart92d7f7b2007-06-17 19:56:38 -05003053 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003054 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003055 spin_lock_irqsave(&phba->hbalock, iflag);
3056 }
3057
dea31012005-04-17 16:05:31 -05003058 /* Rsp ring <ringno> error: IOCB */
3059 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003060 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05003061 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003062 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05003063 irsp->un.ulpWord[0],
3064 irsp->un.ulpWord[1],
3065 irsp->un.ulpWord[2],
3066 irsp->un.ulpWord[3],
3067 irsp->un.ulpWord[4],
3068 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04003069 *(uint32_t *)&irsp->un1,
3070 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05003071 }
3072
3073 switch (type) {
3074 case LPFC_ABORT_IOCB:
3075 case LPFC_SOL_IOCB:
3076 /*
3077 * Idle exchange closed via ABTS from port. No iocb
3078 * resources need to be recovered.
3079 */
3080 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04003081 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003082 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04003083 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05003084 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04003085 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05003086 break;
3087 }
3088
James Bottomley604a3e32005-10-29 10:28:33 -05003089 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3090 &rspiocbq);
James Smart0f65ff62010-02-26 14:14:23 -05003091 if (unlikely(!cmdiocbq))
3092 break;
3093 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3094 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3095 if (cmdiocbq->iocb_cmpl) {
3096 spin_unlock_irqrestore(&phba->hbalock, iflag);
3097 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3098 &rspiocbq);
3099 spin_lock_irqsave(&phba->hbalock, iflag);
3100 }
dea31012005-04-17 16:05:31 -05003101 break;
James Smarta4bc3372006-12-02 13:34:16 -05003102 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05003103 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003104 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05003105 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05003106 break;
dea31012005-04-17 16:05:31 -05003107 default:
3108 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3109 char adaptermsg[LPFC_MAX_ADPTMSG];
3110 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3111 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3112 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07003113 dev_warn(&((phba->pcidev)->dev),
3114 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05003115 phba->brd_no, adaptermsg);
3116 } else {
3117 /* Unknown IOCB command */
3118 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003119 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05003120 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003121 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05003122 irsp->ulpStatus,
3123 irsp->ulpIoTag,
3124 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05003125 }
3126 break;
3127 }
3128
3129 /*
3130 * The response IOCB has been processed. Update the ring
3131 * pointer in SLIM. If the port response put pointer has not
3132 * been updated, sync the pgp->rspPutInx and fetch the new port
3133 * response put pointer.
3134 */
James Smart7e56aa22012-08-03 12:35:34 -04003135 writel(pring->sli.sli3.rspidx,
3136 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003137
James Smart7e56aa22012-08-03 12:35:34 -04003138 if (pring->sli.sli3.rspidx == portRspPut)
dea31012005-04-17 16:05:31 -05003139 portRspPut = le32_to_cpu(pgp->rspPutInx);
3140 }
3141
3142 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3143 pring->stats.iocb_rsp_full++;
3144 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3145 writel(status, phba->CAregaddr);
3146 readl(phba->CAregaddr);
3147 }
3148 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3149 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3150 pring->stats.iocb_cmd_empty++;
3151
3152 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003153 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003154 lpfc_sli_resume_iocb(phba, pring);
3155
3156 if ((pring->lpfc_sli_cmd_available))
3157 (pring->lpfc_sli_cmd_available) (phba, pring);
3158
3159 }
3160
James Smart45ed1192009-10-02 15:17:02 -04003161 phba->fcp_ring_in_use = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003162 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003163 return rc;
3164}
3165
James Smarte59058c2008-08-24 21:49:00 -04003166/**
James Smart3772a992009-05-22 14:50:54 -04003167 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3168 * @phba: Pointer to HBA context object.
3169 * @pring: Pointer to driver SLI ring object.
3170 * @rspiocbp: Pointer to driver response IOCB object.
3171 *
3172 * This function is called from the worker thread when there is a slow-path
3173 * response IOCB to process. This function chains all the response iocbs until
3174 * seeing the iocb with the LE bit set. The function will call
3175 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3176 * completion of a command iocb. The function will call the
3177 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3178 * The function frees the resources or calls the completion handler if this
3179 * iocb is an abort completion. The function returns NULL when the response
3180 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3181 * this function shall chain the iocb on to the iocb_continueq and return the
3182 * response iocb passed in.
3183 **/
3184static struct lpfc_iocbq *
3185lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3186 struct lpfc_iocbq *rspiocbp)
3187{
3188 struct lpfc_iocbq *saveq;
3189 struct lpfc_iocbq *cmdiocbp;
3190 struct lpfc_iocbq *next_iocb;
3191 IOCB_t *irsp = NULL;
3192 uint32_t free_saveq;
3193 uint8_t iocb_cmd_type;
3194 lpfc_iocb_type type;
3195 unsigned long iflag;
3196 int rc;
3197
3198 spin_lock_irqsave(&phba->hbalock, iflag);
3199 /* First add the response iocb to the countinueq list */
3200 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3201 pring->iocb_continueq_cnt++;
3202
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02003203 /* Now, determine whether the list is completed for processing */
James Smart3772a992009-05-22 14:50:54 -04003204 irsp = &rspiocbp->iocb;
3205 if (irsp->ulpLe) {
3206 /*
3207 * By default, the driver expects to free all resources
3208 * associated with this iocb completion.
3209 */
3210 free_saveq = 1;
3211 saveq = list_get_first(&pring->iocb_continueq,
3212 struct lpfc_iocbq, list);
3213 irsp = &(saveq->iocb);
3214 list_del_init(&pring->iocb_continueq);
3215 pring->iocb_continueq_cnt = 0;
3216
3217 pring->stats.iocb_rsp++;
3218
3219 /*
3220 * If resource errors reported from HBA, reduce
3221 * queuedepths of the SCSI device.
3222 */
3223 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
James Smarte3d2b802012-08-14 14:25:43 -04003224 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3225 IOERR_NO_RESOURCES)) {
James Smart3772a992009-05-22 14:50:54 -04003226 spin_unlock_irqrestore(&phba->hbalock, iflag);
3227 phba->lpfc_rampdown_queue_depth(phba);
3228 spin_lock_irqsave(&phba->hbalock, iflag);
3229 }
3230
3231 if (irsp->ulpStatus) {
3232 /* Rsp ring <ringno> error: IOCB */
3233 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3234 "0328 Rsp Ring %d error: "
3235 "IOCB Data: "
3236 "x%x x%x x%x x%x "
3237 "x%x x%x x%x x%x "
3238 "x%x x%x x%x x%x "
3239 "x%x x%x x%x x%x\n",
3240 pring->ringno,
3241 irsp->un.ulpWord[0],
3242 irsp->un.ulpWord[1],
3243 irsp->un.ulpWord[2],
3244 irsp->un.ulpWord[3],
3245 irsp->un.ulpWord[4],
3246 irsp->un.ulpWord[5],
3247 *(((uint32_t *) irsp) + 6),
3248 *(((uint32_t *) irsp) + 7),
3249 *(((uint32_t *) irsp) + 8),
3250 *(((uint32_t *) irsp) + 9),
3251 *(((uint32_t *) irsp) + 10),
3252 *(((uint32_t *) irsp) + 11),
3253 *(((uint32_t *) irsp) + 12),
3254 *(((uint32_t *) irsp) + 13),
3255 *(((uint32_t *) irsp) + 14),
3256 *(((uint32_t *) irsp) + 15));
3257 }
3258
3259 /*
3260 * Fetch the IOCB command type and call the correct completion
3261 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3262 * get freed back to the lpfc_iocb_list by the discovery
3263 * kernel thread.
3264 */
3265 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3266 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3267 switch (type) {
3268 case LPFC_SOL_IOCB:
3269 spin_unlock_irqrestore(&phba->hbalock, iflag);
3270 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3271 spin_lock_irqsave(&phba->hbalock, iflag);
3272 break;
3273
3274 case LPFC_UNSOL_IOCB:
3275 spin_unlock_irqrestore(&phba->hbalock, iflag);
3276 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3277 spin_lock_irqsave(&phba->hbalock, iflag);
3278 if (!rc)
3279 free_saveq = 0;
3280 break;
3281
3282 case LPFC_ABORT_IOCB:
3283 cmdiocbp = NULL;
3284 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3285 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3286 saveq);
3287 if (cmdiocbp) {
3288 /* Call the specified completion routine */
3289 if (cmdiocbp->iocb_cmpl) {
3290 spin_unlock_irqrestore(&phba->hbalock,
3291 iflag);
3292 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3293 saveq);
3294 spin_lock_irqsave(&phba->hbalock,
3295 iflag);
3296 } else
3297 __lpfc_sli_release_iocbq(phba,
3298 cmdiocbp);
3299 }
3300 break;
3301
3302 case LPFC_UNKNOWN_IOCB:
3303 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3304 char adaptermsg[LPFC_MAX_ADPTMSG];
3305 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3306 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3307 MAX_MSG_DATA);
3308 dev_warn(&((phba->pcidev)->dev),
3309 "lpfc%d: %s\n",
3310 phba->brd_no, adaptermsg);
3311 } else {
3312 /* Unknown IOCB command */
3313 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3314 "0335 Unknown IOCB "
3315 "command Data: x%x "
3316 "x%x x%x x%x\n",
3317 irsp->ulpCommand,
3318 irsp->ulpStatus,
3319 irsp->ulpIoTag,
3320 irsp->ulpContext);
3321 }
3322 break;
3323 }
3324
3325 if (free_saveq) {
3326 list_for_each_entry_safe(rspiocbp, next_iocb,
3327 &saveq->list, list) {
James Smart61f35bf2013-05-31 17:03:48 -04003328 list_del_init(&rspiocbp->list);
James Smart3772a992009-05-22 14:50:54 -04003329 __lpfc_sli_release_iocbq(phba, rspiocbp);
3330 }
3331 __lpfc_sli_release_iocbq(phba, saveq);
3332 }
3333 rspiocbp = NULL;
3334 }
3335 spin_unlock_irqrestore(&phba->hbalock, iflag);
3336 return rspiocbp;
3337}
3338
3339/**
3340 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04003341 * @phba: Pointer to HBA context object.
3342 * @pring: Pointer to driver SLI ring object.
3343 * @mask: Host attention register mask for this ring.
3344 *
James Smart3772a992009-05-22 14:50:54 -04003345 * This routine wraps the actual slow_ring event process routine from the
3346 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04003347 **/
James Smart3772a992009-05-22 14:50:54 -04003348void
James Smart2e0fef82007-06-17 19:56:36 -05003349lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3350 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05003351{
James Smart3772a992009-05-22 14:50:54 -04003352 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3353}
3354
3355/**
3356 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3357 * @phba: Pointer to HBA context object.
3358 * @pring: Pointer to driver SLI ring object.
3359 * @mask: Host attention register mask for this ring.
3360 *
3361 * This function is called from the worker thread when there is a ring event
3362 * for non-fcp rings. The caller does not hold any lock. The function will
3363 * remove each response iocb in the response ring and calls the handle
3364 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3365 **/
3366static void
3367lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3368 struct lpfc_sli_ring *pring, uint32_t mask)
3369{
James Smart34b02dc2008-08-24 21:49:55 -04003370 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05003371 IOCB_t *entry;
3372 IOCB_t *irsp = NULL;
3373 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05003374 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05003375 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04003376 uint32_t status;
dea31012005-04-17 16:05:31 -05003377
James Smart34b02dc2008-08-24 21:49:55 -04003378 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05003379 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003380 pring->stats.iocb_event++;
3381
dea31012005-04-17 16:05:31 -05003382 /*
3383 * The next available response entry should never exceed the maximum
3384 * entries. If it does, treat it as an adapter hardware error.
3385 */
James Smart7e56aa22012-08-03 12:35:34 -04003386 portRspMax = pring->sli.sli3.numRiocb;
dea31012005-04-17 16:05:31 -05003387 portRspPut = le32_to_cpu(pgp->rspPutInx);
3388 if (portRspPut >= portRspMax) {
3389 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003390 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05003391 * rsp ring <portRspMax>
3392 */
James Smarted957682007-06-17 19:56:37 -05003393 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003394 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02003395 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003396 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05003397
James Smart2e0fef82007-06-17 19:56:36 -05003398 phba->link_state = LPFC_HBA_ERROR;
3399 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003400
3401 phba->work_hs = HS_FFER3;
3402 lpfc_handle_eratt(phba);
3403
James Smart3772a992009-05-22 14:50:54 -04003404 return;
dea31012005-04-17 16:05:31 -05003405 }
3406
3407 rmb();
James Smart7e56aa22012-08-03 12:35:34 -04003408 while (pring->sli.sli3.rspidx != portRspPut) {
dea31012005-04-17 16:05:31 -05003409 /*
3410 * Build a completion list and call the appropriate handler.
3411 * The process is to get the next available response iocb, get
3412 * a free iocb from the list, copy the response data into the
3413 * free iocb, insert to the continuation list, and update the
3414 * next response index to slim. This process makes response
3415 * iocb's in the ring available to DMA as fast as possible but
3416 * pays a penalty for a copy operation. Since the iocb is
3417 * only 32 bytes, this penalty is considered small relative to
3418 * the PCI reads for register values and a slim write. When
3419 * the ulpLe field is set, the entire Command has been
3420 * received.
3421 */
James Smarted957682007-06-17 19:56:37 -05003422 entry = lpfc_resp_iocb(phba, pring);
3423
James Smart858c9f62007-06-17 19:56:39 -05003424 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05003425 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05003426 if (rspiocbp == NULL) {
3427 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07003428 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05003429 break;
3430 }
3431
James Smarted957682007-06-17 19:56:37 -05003432 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3433 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05003434 irsp = &rspiocbp->iocb;
3435
James Smart7e56aa22012-08-03 12:35:34 -04003436 if (++pring->sli.sli3.rspidx >= portRspMax)
3437 pring->sli.sli3.rspidx = 0;
dea31012005-04-17 16:05:31 -05003438
James Smarta58cbd52007-08-02 11:09:43 -04003439 if (pring->ringno == LPFC_ELS_RING) {
3440 lpfc_debugfs_slow_ring_trc(phba,
3441 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3442 *(((uint32_t *) irsp) + 4),
3443 *(((uint32_t *) irsp) + 6),
3444 *(((uint32_t *) irsp) + 7));
3445 }
3446
James Smart7e56aa22012-08-03 12:35:34 -04003447 writel(pring->sli.sli3.rspidx,
3448 &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05003449
James Smart3772a992009-05-22 14:50:54 -04003450 spin_unlock_irqrestore(&phba->hbalock, iflag);
3451 /* Handle the response IOCB */
3452 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3453 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05003454
3455 /*
3456 * If the port response put pointer has not been updated, sync
3457 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3458 * response put pointer.
3459 */
James Smart7e56aa22012-08-03 12:35:34 -04003460 if (pring->sli.sli3.rspidx == portRspPut) {
dea31012005-04-17 16:05:31 -05003461 portRspPut = le32_to_cpu(pgp->rspPutInx);
3462 }
James Smart7e56aa22012-08-03 12:35:34 -04003463 } /* while (pring->sli.sli3.rspidx != portRspPut) */
dea31012005-04-17 16:05:31 -05003464
James Smart92d7f7b2007-06-17 19:56:38 -05003465 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05003466 /* At least one response entry has been freed */
3467 pring->stats.iocb_rsp_full++;
3468 /* SET RxRE_RSP in Chip Att register */
3469 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3470 writel(status, phba->CAregaddr);
3471 readl(phba->CAregaddr); /* flush */
3472 }
3473 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3474 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3475 pring->stats.iocb_cmd_empty++;
3476
3477 /* Force update of the local copy of cmdGetInx */
James Smart7e56aa22012-08-03 12:35:34 -04003478 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
dea31012005-04-17 16:05:31 -05003479 lpfc_sli_resume_iocb(phba, pring);
3480
3481 if ((pring->lpfc_sli_cmd_available))
3482 (pring->lpfc_sli_cmd_available) (phba, pring);
3483
3484 }
3485
James Smart2e0fef82007-06-17 19:56:36 -05003486 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003487 return;
dea31012005-04-17 16:05:31 -05003488}
3489
James Smarte59058c2008-08-24 21:49:00 -04003490/**
James Smart4f774512009-05-22 14:52:35 -04003491 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3492 * @phba: Pointer to HBA context object.
3493 * @pring: Pointer to driver SLI ring object.
3494 * @mask: Host attention register mask for this ring.
3495 *
3496 * This function is called from the worker thread when there is a pending
3497 * ELS response iocb on the driver internal slow-path response iocb worker
3498 * queue. The caller does not hold any lock. The function will remove each
3499 * response iocb from the response worker queue and calls the handle
3500 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3501 **/
3502static void
3503lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3504 struct lpfc_sli_ring *pring, uint32_t mask)
3505{
3506 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003507 struct hbq_dmabuf *dmabuf;
3508 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003509 unsigned long iflag;
3510
James Smart45ed1192009-10-02 15:17:02 -04003511 spin_lock_irqsave(&phba->hbalock, iflag);
3512 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3513 spin_unlock_irqrestore(&phba->hbalock, iflag);
3514 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
James Smart4f774512009-05-22 14:52:35 -04003515 /* Get the response iocb from the head of work queue */
3516 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart45ed1192009-10-02 15:17:02 -04003517 list_remove_head(&phba->sli4_hba.sp_queue_event,
James Smart4d9ab992009-10-02 15:16:39 -04003518 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003519 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003520
3521 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3522 case CQE_CODE_COMPL_WQE:
3523 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3524 cq_event);
James Smart45ed1192009-10-02 15:17:02 -04003525 /* Translate ELS WCQE to response IOCBQ */
3526 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3527 irspiocbq);
3528 if (irspiocbq)
3529 lpfc_sli_sp_handle_rspiocb(phba, pring,
3530 irspiocbq);
James Smart4d9ab992009-10-02 15:16:39 -04003531 break;
3532 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -04003533 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -04003534 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3535 cq_event);
3536 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3537 break;
3538 default:
3539 break;
3540 }
James Smart4f774512009-05-22 14:52:35 -04003541 }
3542}
3543
3544/**
James Smart3621a712009-04-06 18:47:14 -04003545 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003546 * @phba: Pointer to HBA context object.
3547 * @pring: Pointer to driver SLI ring object.
3548 *
3549 * This function aborts all iocbs in the given ring and frees all the iocb
3550 * objects in txq. This function issues an abort iocb for all the iocb commands
3551 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3552 * the return of this function. The caller is not required to hold any locks.
3553 **/
James Smart2e0fef82007-06-17 19:56:36 -05003554void
dea31012005-04-17 16:05:31 -05003555lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3556{
James Smart2534ba72007-04-25 09:52:20 -04003557 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003558 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003559
James Smart92d7f7b2007-06-17 19:56:38 -05003560 if (pring->ringno == LPFC_ELS_RING) {
3561 lpfc_fabric_abort_hba(phba);
3562 }
3563
dea31012005-04-17 16:05:31 -05003564 /* Error everything on txq and txcmplq
3565 * First do the txq.
3566 */
James Smartdb55fba2014-04-04 13:52:02 -04003567 if (phba->sli_rev >= LPFC_SLI_REV4) {
3568 spin_lock_irq(&pring->ring_lock);
3569 list_splice_init(&pring->txq, &completions);
3570 pring->txq_cnt = 0;
3571 spin_unlock_irq(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05003572
James Smartdb55fba2014-04-04 13:52:02 -04003573 spin_lock_irq(&phba->hbalock);
3574 /* Next issue ABTS for everything on the txcmplq */
3575 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3576 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3577 spin_unlock_irq(&phba->hbalock);
3578 } else {
3579 spin_lock_irq(&phba->hbalock);
3580 list_splice_init(&pring->txq, &completions);
3581 pring->txq_cnt = 0;
James Smart2534ba72007-04-25 09:52:20 -04003582
James Smartdb55fba2014-04-04 13:52:02 -04003583 /* Next issue ABTS for everything on the txcmplq */
3584 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3585 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3586 spin_unlock_irq(&phba->hbalock);
3587 }
James Smart2534ba72007-04-25 09:52:20 -04003588
James Smarta257bf92009-04-06 18:48:10 -04003589 /* Cancel all the IOCBs from the completions list */
3590 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3591 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003592}
3593
James Smarte59058c2008-08-24 21:49:00 -04003594/**
James Smartdb55fba2014-04-04 13:52:02 -04003595 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3596 * @phba: Pointer to HBA context object.
3597 * @pring: Pointer to driver SLI ring object.
3598 *
3599 * This function aborts all iocbs in FCP rings and frees all the iocb
3600 * objects in txq. This function issues an abort iocb for all the iocb commands
3601 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3602 * the return of this function. The caller is not required to hold any locks.
3603 **/
3604void
3605lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3606{
3607 struct lpfc_sli *psli = &phba->sli;
3608 struct lpfc_sli_ring *pring;
3609 uint32_t i;
3610
3611 /* Look on all the FCP Rings for the iotag */
3612 if (phba->sli_rev >= LPFC_SLI_REV4) {
3613 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3614 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3615 lpfc_sli_abort_iocb_ring(phba, pring);
3616 }
3617 } else {
3618 pring = &psli->ring[psli->fcp_ring];
3619 lpfc_sli_abort_iocb_ring(phba, pring);
3620 }
3621}
3622
3623
3624/**
James Smart3621a712009-04-06 18:47:14 -04003625 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003626 * @phba: Pointer to HBA context object.
3627 *
3628 * This function flushes all iocbs in the fcp ring and frees all the iocb
3629 * objects in txq and txcmplq. This function will not issue abort iocbs
3630 * for all the iocb commands in txcmplq, they will just be returned with
3631 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3632 * slot has been permanently disabled.
3633 **/
3634void
3635lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3636{
3637 LIST_HEAD(txq);
3638 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003639 struct lpfc_sli *psli = &phba->sli;
3640 struct lpfc_sli_ring *pring;
James Smartdb55fba2014-04-04 13:52:02 -04003641 uint32_t i;
James Smarta8e497d2008-08-24 21:50:11 -04003642
3643 spin_lock_irq(&phba->hbalock);
James Smart4f2e66c2012-05-09 21:17:07 -04003644 /* Indicate the I/O queues are flushed */
3645 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
James Smarta8e497d2008-08-24 21:50:11 -04003646 spin_unlock_irq(&phba->hbalock);
3647
James Smartdb55fba2014-04-04 13:52:02 -04003648 /* Look on all the FCP Rings for the iotag */
3649 if (phba->sli_rev >= LPFC_SLI_REV4) {
3650 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3651 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
James Smarta8e497d2008-08-24 21:50:11 -04003652
James Smartdb55fba2014-04-04 13:52:02 -04003653 spin_lock_irq(&pring->ring_lock);
3654 /* Retrieve everything on txq */
3655 list_splice_init(&pring->txq, &txq);
3656 /* Retrieve everything on the txcmplq */
3657 list_splice_init(&pring->txcmplq, &txcmplq);
3658 pring->txq_cnt = 0;
3659 pring->txcmplq_cnt = 0;
3660 spin_unlock_irq(&pring->ring_lock);
3661
3662 /* Flush the txq */
3663 lpfc_sli_cancel_iocbs(phba, &txq,
3664 IOSTAT_LOCAL_REJECT,
3665 IOERR_SLI_DOWN);
3666 /* Flush the txcmpq */
3667 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3668 IOSTAT_LOCAL_REJECT,
3669 IOERR_SLI_DOWN);
3670 }
3671 } else {
3672 pring = &psli->ring[psli->fcp_ring];
3673
3674 spin_lock_irq(&phba->hbalock);
3675 /* Retrieve everything on txq */
3676 list_splice_init(&pring->txq, &txq);
3677 /* Retrieve everything on the txcmplq */
3678 list_splice_init(&pring->txcmplq, &txcmplq);
3679 pring->txq_cnt = 0;
3680 pring->txcmplq_cnt = 0;
3681 spin_unlock_irq(&phba->hbalock);
3682
3683 /* Flush the txq */
3684 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3685 IOERR_SLI_DOWN);
3686 /* Flush the txcmpq */
3687 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3688 IOERR_SLI_DOWN);
3689 }
James Smarta8e497d2008-08-24 21:50:11 -04003690}
3691
3692/**
James Smart3772a992009-05-22 14:50:54 -04003693 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003694 * @phba: Pointer to HBA context object.
3695 * @mask: Bit mask to be checked.
3696 *
3697 * This function reads the host status register and compares
3698 * with the provided bit mask to check if HBA completed
3699 * the restart. This function will wait in a loop for the
3700 * HBA to complete restart. If the HBA does not restart within
3701 * 15 iterations, the function will reset the HBA again. The
3702 * function returns 1 when HBA fail to restart otherwise returns
3703 * zero.
3704 **/
James Smart3772a992009-05-22 14:50:54 -04003705static int
3706lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003707{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003708 uint32_t status;
3709 int i = 0;
3710 int retval = 0;
dea31012005-04-17 16:05:31 -05003711
Jamie Wellnitz41415862006-02-28 19:25:27 -05003712 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003713 if (lpfc_readl(phba->HSregaddr, &status))
3714 return 1;
dea31012005-04-17 16:05:31 -05003715
Jamie Wellnitz41415862006-02-28 19:25:27 -05003716 /*
3717 * Check status register every 100ms for 5 retries, then every
3718 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3719 * every 2.5 sec for 4.
3720 * Break our of the loop if errors occurred during init.
3721 */
3722 while (((status & mask) != mask) &&
3723 !(status & HS_FFERM) &&
3724 i++ < 20) {
dea31012005-04-17 16:05:31 -05003725
Jamie Wellnitz41415862006-02-28 19:25:27 -05003726 if (i <= 5)
3727 msleep(10);
3728 else if (i <= 10)
3729 msleep(500);
3730 else
3731 msleep(2500);
dea31012005-04-17 16:05:31 -05003732
Jamie Wellnitz41415862006-02-28 19:25:27 -05003733 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003734 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003735 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003736 lpfc_sli_brdrestart(phba);
3737 }
3738 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05003739 if (lpfc_readl(phba->HSregaddr, &status)) {
3740 retval = 1;
3741 break;
3742 }
dea31012005-04-17 16:05:31 -05003743 }
dea31012005-04-17 16:05:31 -05003744
Jamie Wellnitz41415862006-02-28 19:25:27 -05003745 /* Check to see if any errors occurred during init */
3746 if ((status & HS_FFERM) || (i >= 20)) {
James Smarte40a02c2010-02-26 14:13:54 -05003747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3748 "2751 Adapter failed to restart, "
3749 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3750 status,
3751 readl(phba->MBslimaddr + 0xa8),
3752 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003753 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003754 retval = 1;
3755 }
dea31012005-04-17 16:05:31 -05003756
Jamie Wellnitz41415862006-02-28 19:25:27 -05003757 return retval;
dea31012005-04-17 16:05:31 -05003758}
3759
James Smartda0436e2009-05-22 14:51:39 -04003760/**
3761 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3762 * @phba: Pointer to HBA context object.
3763 * @mask: Bit mask to be checked.
3764 *
3765 * This function checks the host status register to check if HBA is
3766 * ready. This function will wait in a loop for the HBA to be ready
3767 * If the HBA is not ready , the function will will reset the HBA PCI
3768 * function again. The function returns 1 when HBA fail to be ready
3769 * otherwise returns zero.
3770 **/
3771static int
3772lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3773{
3774 uint32_t status;
3775 int retval = 0;
3776
3777 /* Read the HBA Host Status Register */
3778 status = lpfc_sli4_post_status_check(phba);
3779
3780 if (status) {
3781 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3782 lpfc_sli_brdrestart(phba);
3783 status = lpfc_sli4_post_status_check(phba);
3784 }
3785
3786 /* Check to see if any errors occurred during init */
3787 if (status) {
3788 phba->link_state = LPFC_HBA_ERROR;
3789 retval = 1;
3790 } else
3791 phba->sli4_hba.intr_enable = 0;
3792
3793 return retval;
3794}
3795
3796/**
3797 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3798 * @phba: Pointer to HBA context object.
3799 * @mask: Bit mask to be checked.
3800 *
3801 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3802 * from the API jump table function pointer from the lpfc_hba struct.
3803 **/
3804int
3805lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3806{
3807 return phba->lpfc_sli_brdready(phba, mask);
3808}
3809
James Smart92908312006-03-07 15:04:13 -05003810#define BARRIER_TEST_PATTERN (0xdeadbeef)
3811
James Smarte59058c2008-08-24 21:49:00 -04003812/**
James Smart3621a712009-04-06 18:47:14 -04003813 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003814 * @phba: Pointer to HBA context object.
3815 *
James Smart1b511972011-12-13 13:23:09 -05003816 * This function is called before resetting an HBA. This function is called
3817 * with hbalock held and requests HBA to quiesce DMAs before a reset.
James Smarte59058c2008-08-24 21:49:00 -04003818 **/
James Smart2e0fef82007-06-17 19:56:36 -05003819void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003820{
James Smart65a29c12006-07-06 15:50:50 -04003821 uint32_t __iomem *resp_buf;
3822 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003823 volatile uint32_t mbox;
James Smart9940b972011-03-11 16:06:12 -05003824 uint32_t hc_copy, ha_copy, resp_data;
James Smart92908312006-03-07 15:04:13 -05003825 int i;
3826 uint8_t hdrtype;
3827
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01003828 lockdep_assert_held(&phba->hbalock);
3829
James Smart92908312006-03-07 15:04:13 -05003830 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3831 if (hdrtype != 0x80 ||
3832 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3833 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3834 return;
3835
3836 /*
3837 * Tell the other part of the chip to suspend temporarily all
3838 * its DMA activity.
3839 */
James Smart65a29c12006-07-06 15:50:50 -04003840 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003841
3842 /* Disable the error attention */
James Smart9940b972011-03-11 16:06:12 -05003843 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3844 return;
James Smart92908312006-03-07 15:04:13 -05003845 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3846 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003847 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003848
James Smart9940b972011-03-11 16:06:12 -05003849 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3850 return;
3851 if (ha_copy & HA_ERATT) {
James Smart92908312006-03-07 15:04:13 -05003852 /* Clear Chip error bit */
3853 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003854 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003855 }
3856
3857 mbox = 0;
3858 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3859 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3860
3861 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003862 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003863 writel(mbox, mbox_buf);
3864
James Smart9940b972011-03-11 16:06:12 -05003865 for (i = 0; i < 50; i++) {
3866 if (lpfc_readl((resp_buf + 1), &resp_data))
3867 return;
3868 if (resp_data != ~(BARRIER_TEST_PATTERN))
3869 mdelay(1);
3870 else
3871 break;
3872 }
3873 resp_data = 0;
3874 if (lpfc_readl((resp_buf + 1), &resp_data))
3875 return;
3876 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003877 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003878 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003879 goto restore_hc;
3880 else
3881 goto clear_errat;
3882 }
3883
3884 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
James Smart9940b972011-03-11 16:06:12 -05003885 resp_data = 0;
3886 for (i = 0; i < 500; i++) {
3887 if (lpfc_readl(resp_buf, &resp_data))
3888 return;
3889 if (resp_data != mbox)
3890 mdelay(1);
3891 else
3892 break;
3893 }
James Smart92908312006-03-07 15:04:13 -05003894
3895clear_errat:
3896
James Smart9940b972011-03-11 16:06:12 -05003897 while (++i < 500) {
3898 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3899 return;
3900 if (!(ha_copy & HA_ERATT))
3901 mdelay(1);
3902 else
3903 break;
3904 }
James Smart92908312006-03-07 15:04:13 -05003905
3906 if (readl(phba->HAregaddr) & HA_ERATT) {
3907 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003908 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003909 }
3910
3911restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003912 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003913 writel(hc_copy, phba->HCregaddr);
3914 readl(phba->HCregaddr); /* flush */
3915}
3916
James Smarte59058c2008-08-24 21:49:00 -04003917/**
James Smart3621a712009-04-06 18:47:14 -04003918 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003919 * @phba: Pointer to HBA context object.
3920 *
3921 * This function issues a kill_board mailbox command and waits for
3922 * the error attention interrupt. This function is called for stopping
3923 * the firmware processing. The caller is not required to hold any
3924 * locks. This function calls lpfc_hba_down_post function to free
3925 * any pending commands after the kill. The function will return 1 when it
3926 * fails to kill the board else will return 0.
3927 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003928int
James Smart2e0fef82007-06-17 19:56:36 -05003929lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003930{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003931 struct lpfc_sli *psli;
3932 LPFC_MBOXQ_t *pmb;
3933 uint32_t status;
3934 uint32_t ha_copy;
3935 int retval;
3936 int i = 0;
3937
3938 psli = &phba->sli;
3939
3940 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003941 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003942 "0329 Kill HBA Data: x%x x%x\n",
3943 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003944
James Smart98c9ea52007-10-27 13:37:33 -04003945 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3946 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003947 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003948
3949 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003950 spin_lock_irq(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -05003951 if (lpfc_readl(phba->HCregaddr, &status)) {
3952 spin_unlock_irq(&phba->hbalock);
3953 mempool_free(pmb, phba->mbox_mem_pool);
3954 return 1;
3955 }
Jamie Wellnitz41415862006-02-28 19:25:27 -05003956 status &= ~HC_ERINT_ENA;
3957 writel(status, phba->HCregaddr);
3958 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003959 phba->link_flag |= LS_IGNORE_ERATT;
3960 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003961
3962 lpfc_kill_board(phba, pmb);
3963 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3964 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3965
3966 if (retval != MBX_SUCCESS) {
3967 if (retval != MBX_BUSY)
3968 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte40a02c2010-02-26 14:13:54 -05003969 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3970 "2752 KILL_BOARD command failed retval %d\n",
3971 retval);
James Smart2e0fef82007-06-17 19:56:36 -05003972 spin_lock_irq(&phba->hbalock);
3973 phba->link_flag &= ~LS_IGNORE_ERATT;
3974 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003975 return 1;
3976 }
3977
James Smartf4b4c682009-05-22 14:53:12 -04003978 spin_lock_irq(&phba->hbalock);
3979 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3980 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003981
Jamie Wellnitz41415862006-02-28 19:25:27 -05003982 mempool_free(pmb, phba->mbox_mem_pool);
3983
3984 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3985 * attention every 100ms for 3 seconds. If we don't get ERATT after
3986 * 3 seconds we still set HBA_ERROR state because the status of the
3987 * board is now undefined.
3988 */
James Smart9940b972011-03-11 16:06:12 -05003989 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3990 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003991 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3992 mdelay(100);
James Smart9940b972011-03-11 16:06:12 -05003993 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3994 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003995 }
3996
3997 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003998 if (ha_copy & HA_ERATT) {
3999 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05004000 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05004001 }
James Smart2e0fef82007-06-17 19:56:36 -05004002 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004003 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04004004 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05004005 phba->link_flag &= ~LS_IGNORE_ERATT;
4006 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004007
Jamie Wellnitz41415862006-02-28 19:25:27 -05004008 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004009 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004010
James Smart2e0fef82007-06-17 19:56:36 -05004011 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004012}
4013
James Smarte59058c2008-08-24 21:49:00 -04004014/**
James Smart3772a992009-05-22 14:50:54 -04004015 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04004016 * @phba: Pointer to HBA context object.
4017 *
4018 * This function resets the HBA by writing HC_INITFF to the control
4019 * register. After the HBA resets, this function resets all the iocb ring
4020 * indices. This function disables PCI layer parity checking during
4021 * the reset.
4022 * This function returns 0 always.
4023 * The caller is not required to hold any locks.
4024 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05004025int
James Smart2e0fef82007-06-17 19:56:36 -05004026lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004027{
4028 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05004029 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004030 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05004031 int i;
dea31012005-04-17 16:05:31 -05004032
Jamie Wellnitz41415862006-02-28 19:25:27 -05004033 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004034
Jamie Wellnitz41415862006-02-28 19:25:27 -05004035 /* Reset HBA */
4036 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004037 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004038 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05004039
4040 /* perform board reset */
4041 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004042 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004043 phba->pport->fc_myDID = 0;
4044 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05004045
Jamie Wellnitz41415862006-02-28 19:25:27 -05004046 /* Turn off parity checking and serr during the physical reset */
4047 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4048 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4049 (cfg_value &
4050 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4051
James Smart3772a992009-05-22 14:50:54 -04004052 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4053
Jamie Wellnitz41415862006-02-28 19:25:27 -05004054 /* Now toggle INITFF bit in the Host Control Register */
4055 writel(HC_INITFF, phba->HCregaddr);
4056 mdelay(1);
4057 readl(phba->HCregaddr); /* flush */
4058 writel(0, phba->HCregaddr);
4059 readl(phba->HCregaddr); /* flush */
4060
4061 /* Restore PCI cmd register */
4062 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05004063
4064 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004065 for (i = 0; i < psli->num_rings; i++) {
4066 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05004067 pring->flag = 0;
James Smart7e56aa22012-08-03 12:35:34 -04004068 pring->sli.sli3.rspidx = 0;
4069 pring->sli.sli3.next_cmdidx = 0;
4070 pring->sli.sli3.local_getidx = 0;
4071 pring->sli.sli3.cmdidx = 0;
dea31012005-04-17 16:05:31 -05004072 pring->missbufcnt = 0;
4073 }
dea31012005-04-17 16:05:31 -05004074
James Smart2e0fef82007-06-17 19:56:36 -05004075 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004076 return 0;
4077}
4078
James Smarte59058c2008-08-24 21:49:00 -04004079/**
James Smartda0436e2009-05-22 14:51:39 -04004080 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4081 * @phba: Pointer to HBA context object.
4082 *
4083 * This function resets a SLI4 HBA. This function disables PCI layer parity
4084 * checking during resets the device. The caller is not required to hold
4085 * any locks.
4086 *
4087 * This function returns 0 always.
4088 **/
4089int
4090lpfc_sli4_brdreset(struct lpfc_hba *phba)
4091{
4092 struct lpfc_sli *psli = &phba->sli;
4093 uint16_t cfg_value;
James Smart02936352014-04-04 13:52:12 -04004094 int rc = 0;
James Smartda0436e2009-05-22 14:51:39 -04004095
4096 /* Reset HBA */
4097 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart02936352014-04-04 13:52:12 -04004098 "0295 Reset HBA Data: x%x x%x x%x\n",
4099 phba->pport->port_state, psli->sli_flag,
4100 phba->hba_flag);
James Smartda0436e2009-05-22 14:51:39 -04004101
4102 /* perform board reset */
4103 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04004104 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04004105 phba->pport->fc_myDID = 0;
4106 phba->pport->fc_prevDID = 0;
4107
James Smartda0436e2009-05-22 14:51:39 -04004108 spin_lock_irq(&phba->hbalock);
4109 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4110 phba->fcf.fcf_flag = 0;
James Smartda0436e2009-05-22 14:51:39 -04004111 spin_unlock_irq(&phba->hbalock);
4112
James Smart02936352014-04-04 13:52:12 -04004113 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4114 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4115 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4116 return rc;
4117 }
4118
James Smartda0436e2009-05-22 14:51:39 -04004119 /* Now physically reset the device */
4120 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4121 "0389 Performing PCI function reset!\n");
James Smartbe858b62010-12-15 17:57:20 -05004122
4123 /* Turn off parity checking and serr during the physical reset */
4124 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4125 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4126 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4127
James Smart88318812012-09-29 11:29:29 -04004128 /* Perform FCoE PCI function reset before freeing queue memory */
James Smart27b01b82012-05-09 21:19:44 -04004129 rc = lpfc_pci_function_reset(phba);
James Smart88318812012-09-29 11:29:29 -04004130 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04004131
James Smartbe858b62010-12-15 17:57:20 -05004132 /* Restore PCI cmd register */
4133 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4134
James Smart27b01b82012-05-09 21:19:44 -04004135 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004136}
4137
4138/**
4139 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04004140 * @phba: Pointer to HBA context object.
4141 *
4142 * This function is called in the SLI initialization code path to
4143 * restart the HBA. The caller is not required to hold any lock.
4144 * This function writes MBX_RESTART mailbox command to the SLIM and
4145 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4146 * function to free any pending commands. The function enables
4147 * POST only during the first initialization. The function returns zero.
4148 * The function does not guarantee completion of MBX_RESTART mailbox
4149 * command before the return of this function.
4150 **/
James Smartda0436e2009-05-22 14:51:39 -04004151static int
4152lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004153{
4154 MAILBOX_t *mb;
4155 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004156 volatile uint32_t word0;
4157 void __iomem *to_slim;
James Smart0d878412009-10-02 15:16:56 -04004158 uint32_t hba_aer_enabled;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004159
James Smart2e0fef82007-06-17 19:56:36 -05004160 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004161
James Smart0d878412009-10-02 15:16:56 -04004162 /* Take PCIe device Advanced Error Reporting (AER) state */
4163 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4164
Jamie Wellnitz41415862006-02-28 19:25:27 -05004165 psli = &phba->sli;
4166
4167 /* Restart HBA */
4168 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004169 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05004170 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004171
4172 word0 = 0;
4173 mb = (MAILBOX_t *) &word0;
4174 mb->mbxCommand = MBX_RESTART;
4175 mb->mbxHc = 1;
4176
James Smart92908312006-03-07 15:04:13 -05004177 lpfc_reset_barrier(phba);
4178
Jamie Wellnitz41415862006-02-28 19:25:27 -05004179 to_slim = phba->MBslimaddr;
4180 writel(*(uint32_t *) mb, to_slim);
4181 readl(to_slim); /* flush */
4182
4183 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05004184 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05004185 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05004186 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05004187 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04004188 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004189 writel(*(uint32_t *) mb, to_slim);
4190 readl(to_slim); /* flush */
4191
4192 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05004193 phba->pport->stopped = 0;
4194 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04004195 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05004196 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004197
James Smart64ba8812006-08-02 15:24:34 -04004198 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4199 psli->stats_start = get_seconds();
4200
James Smarteaf15d52008-12-04 22:39:29 -05004201 /* Give the INITFF and Post time to settle. */
4202 mdelay(100);
dea31012005-04-17 16:05:31 -05004203
James Smart0d878412009-10-02 15:16:56 -04004204 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4205 if (hba_aer_enabled)
4206 pci_disable_pcie_error_reporting(phba->pcidev);
4207
Jamie Wellnitz41415862006-02-28 19:25:27 -05004208 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05004209
4210 return 0;
4211}
4212
James Smarte59058c2008-08-24 21:49:00 -04004213/**
James Smartda0436e2009-05-22 14:51:39 -04004214 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4215 * @phba: Pointer to HBA context object.
4216 *
4217 * This function is called in the SLI initialization code path to restart
4218 * a SLI4 HBA. The caller is not required to hold any lock.
4219 * At the end of the function, it calls lpfc_hba_down_post function to
4220 * free any pending commands.
4221 **/
4222static int
4223lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4224{
4225 struct lpfc_sli *psli = &phba->sli;
James Smart75baf692010-06-08 18:31:21 -04004226 uint32_t hba_aer_enabled;
James Smart27b01b82012-05-09 21:19:44 -04004227 int rc;
James Smartda0436e2009-05-22 14:51:39 -04004228
4229 /* Restart HBA */
4230 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4231 "0296 Restart HBA Data: x%x x%x\n",
4232 phba->pport->port_state, psli->sli_flag);
4233
James Smart75baf692010-06-08 18:31:21 -04004234 /* Take PCIe device Advanced Error Reporting (AER) state */
4235 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4236
James Smart27b01b82012-05-09 21:19:44 -04004237 rc = lpfc_sli4_brdreset(phba);
James Smartda0436e2009-05-22 14:51:39 -04004238
4239 spin_lock_irq(&phba->hbalock);
4240 phba->pport->stopped = 0;
4241 phba->link_state = LPFC_INIT_START;
4242 phba->hba_flag = 0;
4243 spin_unlock_irq(&phba->hbalock);
4244
4245 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4246 psli->stats_start = get_seconds();
4247
James Smart75baf692010-06-08 18:31:21 -04004248 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4249 if (hba_aer_enabled)
4250 pci_disable_pcie_error_reporting(phba->pcidev);
4251
James Smartda0436e2009-05-22 14:51:39 -04004252 lpfc_hba_down_post(phba);
4253
James Smart27b01b82012-05-09 21:19:44 -04004254 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004255}
4256
4257/**
4258 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4259 * @phba: Pointer to HBA context object.
4260 *
4261 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4262 * API jump table function pointer from the lpfc_hba struct.
4263**/
4264int
4265lpfc_sli_brdrestart(struct lpfc_hba *phba)
4266{
4267 return phba->lpfc_sli_brdrestart(phba);
4268}
4269
4270/**
James Smart3621a712009-04-06 18:47:14 -04004271 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04004272 * @phba: Pointer to HBA context object.
4273 *
4274 * This function is called after a HBA restart to wait for successful
4275 * restart of the HBA. Successful restart of the HBA is indicated by
4276 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4277 * iteration, the function will restart the HBA again. The function returns
4278 * zero if HBA successfully restarted else returns negative error code.
4279 **/
dea31012005-04-17 16:05:31 -05004280static int
4281lpfc_sli_chipset_init(struct lpfc_hba *phba)
4282{
4283 uint32_t status, i = 0;
4284
4285 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004286 if (lpfc_readl(phba->HSregaddr, &status))
4287 return -EIO;
dea31012005-04-17 16:05:31 -05004288
4289 /* Check status register to see what current state is */
4290 i = 0;
4291 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4292
James Smartdcf2a4e2010-09-29 11:18:53 -04004293 /* Check every 10ms for 10 retries, then every 100ms for 90
4294 * retries, then every 1 sec for 50 retires for a total of
4295 * ~60 seconds before reset the board again and check every
4296 * 1 sec for 50 retries. The up to 60 seconds before the
4297 * board ready is required by the Falcon FIPS zeroization
4298 * complete, and any reset the board in between shall cause
4299 * restart of zeroization, further delay the board ready.
dea31012005-04-17 16:05:31 -05004300 */
James Smartdcf2a4e2010-09-29 11:18:53 -04004301 if (i++ >= 200) {
dea31012005-04-17 16:05:31 -05004302 /* Adapter failed to init, timeout, status reg
4303 <status> */
James Smarted957682007-06-17 19:56:37 -05004304 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004305 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004306 "timeout, status reg x%x, "
4307 "FW Data: A8 x%x AC x%x\n", status,
4308 readl(phba->MBslimaddr + 0xa8),
4309 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004310 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004311 return -ETIMEDOUT;
4312 }
4313
4314 /* Check to see if any errors occurred during init */
4315 if (status & HS_FFERM) {
4316 /* ERROR: During chipset initialization */
4317 /* Adapter failed to init, chipset, status reg
4318 <status> */
James Smarted957682007-06-17 19:56:37 -05004319 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004320 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05004321 "chipset, status reg x%x, "
4322 "FW Data: A8 x%x AC x%x\n", status,
4323 readl(phba->MBslimaddr + 0xa8),
4324 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004325 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004326 return -EIO;
4327 }
4328
James Smartdcf2a4e2010-09-29 11:18:53 -04004329 if (i <= 10)
dea31012005-04-17 16:05:31 -05004330 msleep(10);
James Smartdcf2a4e2010-09-29 11:18:53 -04004331 else if (i <= 100)
4332 msleep(100);
4333 else
4334 msleep(1000);
dea31012005-04-17 16:05:31 -05004335
James Smartdcf2a4e2010-09-29 11:18:53 -04004336 if (i == 150) {
4337 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05004338 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004339 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004340 }
4341 /* Read the HBA Host Status Register */
James Smart9940b972011-03-11 16:06:12 -05004342 if (lpfc_readl(phba->HSregaddr, &status))
4343 return -EIO;
dea31012005-04-17 16:05:31 -05004344 }
4345
4346 /* Check to see if any errors occurred during init */
4347 if (status & HS_FFERM) {
4348 /* ERROR: During chipset initialization */
4349 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05004350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004351 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05004352 "status reg x%x, "
4353 "FW Data: A8 x%x AC x%x\n", status,
4354 readl(phba->MBslimaddr + 0xa8),
4355 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05004356 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004357 return -EIO;
4358 }
4359
4360 /* Clear all interrupt enable conditions */
4361 writel(0, phba->HCregaddr);
4362 readl(phba->HCregaddr); /* flush */
4363
4364 /* setup host attn register */
4365 writel(0xffffffff, phba->HAregaddr);
4366 readl(phba->HAregaddr); /* flush */
4367 return 0;
4368}
4369
James Smarte59058c2008-08-24 21:49:00 -04004370/**
James Smart3621a712009-04-06 18:47:14 -04004371 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04004372 *
4373 * This function calculates and returns the number of HBQs required to be
4374 * configured.
4375 **/
James Smart78b2d852007-08-02 11:10:21 -04004376int
James Smarted957682007-06-17 19:56:37 -05004377lpfc_sli_hbq_count(void)
4378{
James Smart92d7f7b2007-06-17 19:56:38 -05004379 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05004380}
4381
James Smarte59058c2008-08-24 21:49:00 -04004382/**
James Smart3621a712009-04-06 18:47:14 -04004383 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004384 *
4385 * This function adds the number of hbq entries in every HBQ to get
4386 * the total number of hbq entries required for the HBA and returns
4387 * the total count.
4388 **/
James Smarted957682007-06-17 19:56:37 -05004389static int
4390lpfc_sli_hbq_entry_count(void)
4391{
4392 int hbq_count = lpfc_sli_hbq_count();
4393 int count = 0;
4394 int i;
4395
4396 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05004397 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05004398 return count;
4399}
4400
James Smarte59058c2008-08-24 21:49:00 -04004401/**
James Smart3621a712009-04-06 18:47:14 -04004402 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04004403 *
4404 * This function calculates amount of memory required for all hbq entries
4405 * to be configured and returns the total memory required.
4406 **/
dea31012005-04-17 16:05:31 -05004407int
James Smarted957682007-06-17 19:56:37 -05004408lpfc_sli_hbq_size(void)
4409{
4410 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4411}
4412
James Smarte59058c2008-08-24 21:49:00 -04004413/**
James Smart3621a712009-04-06 18:47:14 -04004414 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04004415 * @phba: Pointer to HBA context object.
4416 *
4417 * This function is called during the SLI initialization to configure
4418 * all the HBQs and post buffers to the HBQ. The caller is not
4419 * required to hold any locks. This function will return zero if successful
4420 * else it will return negative error code.
4421 **/
James Smarted957682007-06-17 19:56:37 -05004422static int
4423lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4424{
4425 int hbq_count = lpfc_sli_hbq_count();
4426 LPFC_MBOXQ_t *pmb;
4427 MAILBOX_t *pmbox;
4428 uint32_t hbqno;
4429 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05004430
James Smart92d7f7b2007-06-17 19:56:38 -05004431 /* Get a Mailbox buffer to setup mailbox
4432 * commands for HBA initialization
4433 */
James Smarted957682007-06-17 19:56:37 -05004434 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4435
4436 if (!pmb)
4437 return -ENOMEM;
4438
James Smart04c68492009-05-22 14:52:52 -04004439 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05004440
4441 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4442 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05004443 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05004444
4445 hbq_entry_index = 0;
4446 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4447 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4448 phba->hbqs[hbqno].hbqPutIdx = 0;
4449 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4450 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05004451 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04004452 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4453 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05004454 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4455
4456 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4457 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4458 mbxStatus <status>, ring <num> */
4459
4460 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004461 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004462 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05004463 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004464 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05004465 pmbox->mbxStatus, hbqno);
4466
4467 phba->link_state = LPFC_HBA_ERROR;
4468 mempool_free(pmb, phba->mbox_mem_pool);
James Smart6e7288d2010-06-07 15:23:35 -04004469 return -ENXIO;
James Smarted957682007-06-17 19:56:37 -05004470 }
4471 }
4472 phba->hbq_count = hbq_count;
4473
James Smarted957682007-06-17 19:56:37 -05004474 mempool_free(pmb, phba->mbox_mem_pool);
4475
James Smart92d7f7b2007-06-17 19:56:38 -05004476 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04004477 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4478 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05004479 return 0;
4480}
4481
James Smarte59058c2008-08-24 21:49:00 -04004482/**
James Smart4f774512009-05-22 14:52:35 -04004483 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4484 * @phba: Pointer to HBA context object.
4485 *
4486 * This function is called during the SLI initialization to configure
4487 * all the HBQs and post buffers to the HBQ. The caller is not
4488 * required to hold any locks. This function will return zero if successful
4489 * else it will return negative error code.
4490 **/
4491static int
4492lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4493{
4494 phba->hbq_in_use = 1;
4495 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4496 phba->hbq_count = 1;
4497 /* Initially populate or replenish the HBQs */
4498 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4499 return 0;
4500}
4501
4502/**
James Smart3621a712009-04-06 18:47:14 -04004503 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04004504 * @phba: Pointer to HBA context object.
4505 * @sli_mode: sli mode - 2/3
4506 *
4507 * This function is called by the sli intialization code path
4508 * to issue config_port mailbox command. This function restarts the
4509 * HBA firmware and issues a config_port mailbox command to configure
4510 * the SLI interface in the sli mode specified by sli_mode
4511 * variable. The caller is not required to hold any locks.
4512 * The function returns 0 if successful, else returns negative error
4513 * code.
4514 **/
James Smart93996272008-08-24 21:50:30 -04004515int
4516lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05004517{
4518 LPFC_MBOXQ_t *pmb;
4519 uint32_t resetcount = 0, rc = 0, done = 0;
4520
4521 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4522 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05004523 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05004524 return -ENOMEM;
4525 }
4526
James Smarted957682007-06-17 19:56:37 -05004527 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05004528 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05004529 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004530 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004531 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05004532 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004533 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05004534 rc = lpfc_sli_chipset_init(phba);
4535 if (rc)
4536 break;
4537
James Smart2e0fef82007-06-17 19:56:36 -05004538 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04004539 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004540 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004541 resetcount++;
4542
James Smarted957682007-06-17 19:56:37 -05004543 /* Call pre CONFIG_PORT mailbox command initialization. A
4544 * value of 0 means the call was successful. Any other
4545 * nonzero value is a failure, but if ERESTART is returned,
4546 * the driver may reset the HBA and try again.
4547 */
dea31012005-04-17 16:05:31 -05004548 rc = lpfc_config_port_prep(phba);
4549 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05004550 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05004551 continue;
James Smart34b02dc2008-08-24 21:49:55 -04004552 } else if (rc)
dea31012005-04-17 16:05:31 -05004553 break;
James Smart6d368e52011-05-24 11:44:12 -04004554
James Smart2e0fef82007-06-17 19:56:36 -05004555 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05004556 lpfc_config_port(phba, pmb);
4557 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04004558 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4559 LPFC_SLI3_HBQ_ENABLED |
4560 LPFC_SLI3_CRP_ENABLED |
James Smartbc739052010-08-04 16:11:18 -04004561 LPFC_SLI3_BG_ENABLED |
4562 LPFC_SLI3_DSS_ENABLED);
James Smarted957682007-06-17 19:56:37 -05004563 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05004564 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004565 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004566 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04004567 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05004568 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04004569 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004570 spin_unlock_irq(&phba->hbalock);
4571 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04004572 } else {
4573 /* Allow asynchronous mailbox command to go through */
4574 spin_lock_irq(&phba->hbalock);
4575 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4576 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05004577 done = 1;
James Smartcb69f7d2011-12-13 13:21:57 -05004578
4579 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4580 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4581 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4582 "3110 Port did not grant ASABT\n");
James Smart04c68492009-05-22 14:52:52 -04004583 }
dea31012005-04-17 16:05:31 -05004584 }
James Smarted957682007-06-17 19:56:37 -05004585 if (!done) {
4586 rc = -EINVAL;
4587 goto do_prep_failed;
4588 }
James Smart04c68492009-05-22 14:52:52 -04004589 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4590 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04004591 rc = -ENXIO;
4592 goto do_prep_failed;
4593 }
James Smart04c68492009-05-22 14:52:52 -04004594 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04004595 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004596 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4597 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4598 phba->max_vpi : phba->max_vports;
4599
James Smart34b02dc2008-08-24 21:49:55 -04004600 } else
4601 phba->max_vpi = 0;
James Smartbc739052010-08-04 16:11:18 -04004602 phba->fips_level = 0;
4603 phba->fips_spec_rev = 0;
4604 if (pmb->u.mb.un.varCfgPort.gdss) {
James Smart04c68492009-05-22 14:52:52 -04004605 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
James Smartbc739052010-08-04 16:11:18 -04004606 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4607 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4608 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4609 "2850 Security Crypto Active. FIPS x%d "
4610 "(Spec Rev: x%d)",
4611 phba->fips_level, phba->fips_spec_rev);
4612 }
4613 if (pmb->u.mb.un.varCfgPort.sec_err) {
4614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4615 "2856 Config Port Security Crypto "
4616 "Error: x%x ",
4617 pmb->u.mb.un.varCfgPort.sec_err);
4618 }
James Smart04c68492009-05-22 14:52:52 -04004619 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04004620 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04004621 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04004622 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart6e7288d2010-06-07 15:23:35 -04004623
4624 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4625 phba->port_gp = phba->mbox->us.s3_pgp.port;
James Smarte2a0a9d2008-12-04 22:40:02 -05004626
4627 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04004628 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05004629 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4630 else
4631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4632 "0443 Adapter did not grant "
4633 "BlockGuard\n");
4634 }
James Smart34b02dc2008-08-24 21:49:55 -04004635 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05004636 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004637 phba->port_gp = phba->mbox->us.s2.port;
James Smartd7c255b2008-08-24 21:50:00 -04004638 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004639 }
James Smart92d7f7b2007-06-17 19:56:38 -05004640do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004641 mempool_free(pmb, phba->mbox_mem_pool);
4642 return rc;
4643}
4644
James Smarte59058c2008-08-24 21:49:00 -04004645
4646/**
James Smart3621a712009-04-06 18:47:14 -04004647 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004648 * @phba: Pointer to HBA context object.
4649 *
4650 * This function is the main SLI intialization function. This function
4651 * is called by the HBA intialization code, HBA reset code and HBA
4652 * error attention handler code. Caller is not required to hold any
4653 * locks. This function issues config_port mailbox command to configure
4654 * the SLI, setup iocb rings and HBQ rings. In the end the function
4655 * calls the config_port_post function to issue init_link mailbox
4656 * command and to start the discovery. The function will return zero
4657 * if successful, else it will return negative error code.
4658 **/
James Smarted957682007-06-17 19:56:37 -05004659int
4660lpfc_sli_hba_setup(struct lpfc_hba *phba)
4661{
4662 uint32_t rc;
James Smart6d368e52011-05-24 11:44:12 -04004663 int mode = 3, i;
4664 int longs;
James Smarted957682007-06-17 19:56:37 -05004665
James Smart12247e82016-07-06 12:36:09 -07004666 switch (phba->cfg_sli_mode) {
James Smarted957682007-06-17 19:56:37 -05004667 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004668 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004670 "1824 NPIV enabled: Override sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004671 "parameter (%d) to auto (0).\n",
James Smart12247e82016-07-06 12:36:09 -07004672 phba->cfg_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004673 break;
4674 }
James Smarted957682007-06-17 19:56:37 -05004675 mode = 2;
4676 break;
4677 case 0:
4678 case 3:
4679 break;
4680 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004681 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smart12247e82016-07-06 12:36:09 -07004682 "1819 Unrecognized sli_mode parameter: %d.\n",
4683 phba->cfg_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004684
4685 break;
4686 }
James Smartb5c53952016-03-31 14:12:30 -07004687 phba->fcp_embed_io = 0; /* SLI4 FC support only */
James Smarted957682007-06-17 19:56:37 -05004688
James Smart93996272008-08-24 21:50:30 -04004689 rc = lpfc_sli_config_port(phba, mode);
4690
James Smart12247e82016-07-06 12:36:09 -07004691 if (rc && phba->cfg_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004692 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004693 "1820 Unable to select SLI-3. "
4694 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004695 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004696 rc = lpfc_sli_config_port(phba, 2);
James Smart4597663f2016-07-06 12:36:01 -07004697 else if (rc && mode == 2)
4698 rc = lpfc_sli_config_port(phba, 3);
James Smarted957682007-06-17 19:56:37 -05004699 if (rc)
dea31012005-04-17 16:05:31 -05004700 goto lpfc_sli_hba_setup_error;
4701
James Smart0d878412009-10-02 15:16:56 -04004702 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4703 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4704 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4705 if (!rc) {
4706 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4707 "2709 This device supports "
4708 "Advanced Error Reporting (AER)\n");
4709 spin_lock_irq(&phba->hbalock);
4710 phba->hba_flag |= HBA_AER_ENABLED;
4711 spin_unlock_irq(&phba->hbalock);
4712 } else {
4713 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4714 "2708 This device does not support "
James Smartb069d7e2013-05-31 17:04:36 -04004715 "Advanced Error Reporting (AER): %d\n",
4716 rc);
James Smart0d878412009-10-02 15:16:56 -04004717 phba->cfg_aer_support = 0;
4718 }
4719 }
4720
James Smarted957682007-06-17 19:56:37 -05004721 if (phba->sli_rev == 3) {
4722 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4723 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004724 } else {
4725 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4726 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004727 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004728 }
4729
4730 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004731 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4732 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004733 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004734
4735 if (rc)
4736 goto lpfc_sli_hba_setup_error;
4737
James Smart6d368e52011-05-24 11:44:12 -04004738 /* Initialize VPIs. */
4739 if (phba->sli_rev == LPFC_SLI_REV3) {
4740 /*
4741 * The VPI bitmask and physical ID array are allocated
4742 * and initialized once only - at driver load. A port
4743 * reset doesn't need to reinitialize this memory.
4744 */
4745 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4746 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4747 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4748 GFP_KERNEL);
4749 if (!phba->vpi_bmask) {
4750 rc = -ENOMEM;
4751 goto lpfc_sli_hba_setup_error;
4752 }
4753
4754 phba->vpi_ids = kzalloc(
4755 (phba->max_vpi+1) * sizeof(uint16_t),
4756 GFP_KERNEL);
4757 if (!phba->vpi_ids) {
4758 kfree(phba->vpi_bmask);
4759 rc = -ENOMEM;
4760 goto lpfc_sli_hba_setup_error;
4761 }
4762 for (i = 0; i < phba->max_vpi; i++)
4763 phba->vpi_ids[i] = i;
4764 }
4765 }
4766
James Smart93996272008-08-24 21:50:30 -04004767 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004768 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4769 rc = lpfc_sli_hbq_setup(phba);
4770 if (rc)
4771 goto lpfc_sli_hba_setup_error;
4772 }
James Smart04c68492009-05-22 14:52:52 -04004773 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004774 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004775 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004776
4777 rc = lpfc_config_port_post(phba);
4778 if (rc)
4779 goto lpfc_sli_hba_setup_error;
4780
James Smarted957682007-06-17 19:56:37 -05004781 return rc;
4782
James Smart92d7f7b2007-06-17 19:56:38 -05004783lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004784 phba->link_state = LPFC_HBA_ERROR;
James Smarte40a02c2010-02-26 14:13:54 -05004785 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004786 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004787 return rc;
4788}
4789
James Smartda0436e2009-05-22 14:51:39 -04004790/**
4791 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4792 * @phba: Pointer to HBA context object.
4793 * @mboxq: mailbox pointer.
4794 * This function issue a dump mailbox command to read config region
4795 * 23 and parse the records in the region and populate driver
4796 * data structure.
4797 **/
4798static int
James Smartff78d8f2011-12-13 13:21:35 -05004799lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
James Smartda0436e2009-05-22 14:51:39 -04004800{
James Smartff78d8f2011-12-13 13:21:35 -05004801 LPFC_MBOXQ_t *mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004802 struct lpfc_dmabuf *mp;
4803 struct lpfc_mqe *mqe;
4804 uint32_t data_length;
4805 int rc;
4806
4807 /* Program the default value of vlan_id and fc_map */
4808 phba->valid_vlan = 0;
4809 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4810 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4811 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4812
James Smartff78d8f2011-12-13 13:21:35 -05004813 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4814 if (!mboxq)
James Smartda0436e2009-05-22 14:51:39 -04004815 return -ENOMEM;
4816
James Smartff78d8f2011-12-13 13:21:35 -05004817 mqe = &mboxq->u.mqe;
4818 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4819 rc = -ENOMEM;
4820 goto out_free_mboxq;
4821 }
4822
James Smartda0436e2009-05-22 14:51:39 -04004823 mp = (struct lpfc_dmabuf *) mboxq->context1;
4824 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4825
4826 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4827 "(%d):2571 Mailbox cmd x%x Status x%x "
4828 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4829 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4830 "CQ: x%x x%x x%x x%x\n",
4831 mboxq->vport ? mboxq->vport->vpi : 0,
4832 bf_get(lpfc_mqe_command, mqe),
4833 bf_get(lpfc_mqe_status, mqe),
4834 mqe->un.mb_words[0], mqe->un.mb_words[1],
4835 mqe->un.mb_words[2], mqe->un.mb_words[3],
4836 mqe->un.mb_words[4], mqe->un.mb_words[5],
4837 mqe->un.mb_words[6], mqe->un.mb_words[7],
4838 mqe->un.mb_words[8], mqe->un.mb_words[9],
4839 mqe->un.mb_words[10], mqe->un.mb_words[11],
4840 mqe->un.mb_words[12], mqe->un.mb_words[13],
4841 mqe->un.mb_words[14], mqe->un.mb_words[15],
4842 mqe->un.mb_words[16], mqe->un.mb_words[50],
4843 mboxq->mcqe.word0,
4844 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4845 mboxq->mcqe.trailer);
4846
4847 if (rc) {
4848 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4849 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004850 rc = -EIO;
4851 goto out_free_mboxq;
James Smartda0436e2009-05-22 14:51:39 -04004852 }
4853 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004854 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004855 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4856 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004857 rc = -EIO;
4858 goto out_free_mboxq;
James Smartd11e31d2009-06-10 17:23:06 -04004859 }
James Smartda0436e2009-05-22 14:51:39 -04004860
4861 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4862 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4863 kfree(mp);
James Smartff78d8f2011-12-13 13:21:35 -05004864 rc = 0;
4865
4866out_free_mboxq:
4867 mempool_free(mboxq, phba->mbox_mem_pool);
4868 return rc;
James Smartda0436e2009-05-22 14:51:39 -04004869}
4870
4871/**
4872 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4873 * @phba: pointer to lpfc hba data structure.
4874 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4875 * @vpd: pointer to the memory to hold resulting port vpd data.
4876 * @vpd_size: On input, the number of bytes allocated to @vpd.
4877 * On output, the number of data bytes in @vpd.
4878 *
4879 * This routine executes a READ_REV SLI4 mailbox command. In
4880 * addition, this routine gets the port vpd data.
4881 *
4882 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004883 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -04004884 * -ENOMEM - could not allocated memory.
James Smartda0436e2009-05-22 14:51:39 -04004885 **/
4886static int
4887lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4888 uint8_t *vpd, uint32_t *vpd_size)
4889{
4890 int rc = 0;
4891 uint32_t dma_size;
4892 struct lpfc_dmabuf *dmabuf;
4893 struct lpfc_mqe *mqe;
4894
4895 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4896 if (!dmabuf)
4897 return -ENOMEM;
4898
4899 /*
4900 * Get a DMA buffer for the vpd data resulting from the READ_REV
4901 * mailbox command.
4902 */
4903 dma_size = *vpd_size;
Joe Perches1aee3832014-09-03 12:56:12 -04004904 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
4905 &dmabuf->phys, GFP_KERNEL);
James Smartda0436e2009-05-22 14:51:39 -04004906 if (!dmabuf->virt) {
4907 kfree(dmabuf);
4908 return -ENOMEM;
4909 }
James Smartda0436e2009-05-22 14:51:39 -04004910
4911 /*
4912 * The SLI4 implementation of READ_REV conflicts at word1,
4913 * bits 31:16 and SLI4 adds vpd functionality not present
4914 * in SLI3. This code corrects the conflicts.
4915 */
4916 lpfc_read_rev(phba, mboxq);
4917 mqe = &mboxq->u.mqe;
4918 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4919 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4920 mqe->un.read_rev.word1 &= 0x0000FFFF;
4921 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4922 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4923
4924 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4925 if (rc) {
4926 dma_free_coherent(&phba->pcidev->dev, dma_size,
4927 dmabuf->virt, dmabuf->phys);
James Smartdef9c7a2009-12-21 17:02:28 -05004928 kfree(dmabuf);
James Smartda0436e2009-05-22 14:51:39 -04004929 return -EIO;
4930 }
4931
James Smartda0436e2009-05-22 14:51:39 -04004932 /*
4933 * The available vpd length cannot be bigger than the
4934 * DMA buffer passed to the port. Catch the less than
4935 * case and update the caller's size.
4936 */
4937 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4938 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4939
James Smartd7c47992010-06-08 18:31:54 -04004940 memcpy(vpd, dmabuf->virt, *vpd_size);
4941
James Smartda0436e2009-05-22 14:51:39 -04004942 dma_free_coherent(&phba->pcidev->dev, dma_size,
4943 dmabuf->virt, dmabuf->phys);
4944 kfree(dmabuf);
4945 return 0;
4946}
4947
4948/**
James Smartcd1c8302011-10-10 21:33:25 -04004949 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4950 * @phba: pointer to lpfc hba data structure.
4951 *
4952 * This routine retrieves SLI4 device physical port name this PCI function
4953 * is attached to.
4954 *
4955 * Return codes
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004956 * 0 - successful
James Smartcd1c8302011-10-10 21:33:25 -04004957 * otherwise - failed to retrieve physical port name
4958 **/
4959static int
4960lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4961{
4962 LPFC_MBOXQ_t *mboxq;
James Smartcd1c8302011-10-10 21:33:25 -04004963 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4964 struct lpfc_controller_attribute *cntl_attr;
4965 struct lpfc_mbx_get_port_name *get_port_name;
4966 void *virtaddr = NULL;
4967 uint32_t alloclen, reqlen;
4968 uint32_t shdr_status, shdr_add_status;
4969 union lpfc_sli4_cfg_shdr *shdr;
4970 char cport_name = 0;
4971 int rc;
4972
4973 /* We assume nothing at this point */
4974 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4975 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4976
4977 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4978 if (!mboxq)
4979 return -ENOMEM;
James Smartcd1c8302011-10-10 21:33:25 -04004980 /* obtain link type and link number via READ_CONFIG */
James Smartff78d8f2011-12-13 13:21:35 -05004981 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4982 lpfc_sli4_read_config(phba);
4983 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4984 goto retrieve_ppname;
James Smartcd1c8302011-10-10 21:33:25 -04004985
4986 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4987 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4988 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4989 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4990 LPFC_SLI4_MBX_NEMBED);
4991 if (alloclen < reqlen) {
4992 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4993 "3084 Allocated DMA memory size (%d) is "
4994 "less than the requested DMA memory size "
4995 "(%d)\n", alloclen, reqlen);
4996 rc = -ENOMEM;
4997 goto out_free_mboxq;
4998 }
4999 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5000 virtaddr = mboxq->sge_array->addr[0];
5001 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5002 shdr = &mbx_cntl_attr->cfg_shdr;
5003 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5004 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5005 if (shdr_status || shdr_add_status || rc) {
5006 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5007 "3085 Mailbox x%x (x%x/x%x) failed, "
5008 "rc:x%x, status:x%x, add_status:x%x\n",
5009 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5010 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5011 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5012 rc, shdr_status, shdr_add_status);
5013 rc = -ENXIO;
5014 goto out_free_mboxq;
5015 }
5016 cntl_attr = &mbx_cntl_attr->cntl_attr;
5017 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5018 phba->sli4_hba.lnk_info.lnk_tp =
5019 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5020 phba->sli4_hba.lnk_info.lnk_no =
5021 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5022 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5023 "3086 lnk_type:%d, lnk_numb:%d\n",
5024 phba->sli4_hba.lnk_info.lnk_tp,
5025 phba->sli4_hba.lnk_info.lnk_no);
5026
5027retrieve_ppname:
5028 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5029 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5030 sizeof(struct lpfc_mbx_get_port_name) -
5031 sizeof(struct lpfc_sli4_cfg_mhdr),
5032 LPFC_SLI4_MBX_EMBED);
5033 get_port_name = &mboxq->u.mqe.un.get_port_name;
5034 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5035 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5036 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5037 phba->sli4_hba.lnk_info.lnk_tp);
5038 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5039 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5040 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5041 if (shdr_status || shdr_add_status || rc) {
5042 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5043 "3087 Mailbox x%x (x%x/x%x) failed: "
5044 "rc:x%x, status:x%x, add_status:x%x\n",
5045 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5046 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5047 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5048 rc, shdr_status, shdr_add_status);
5049 rc = -ENXIO;
5050 goto out_free_mboxq;
5051 }
5052 switch (phba->sli4_hba.lnk_info.lnk_no) {
5053 case LPFC_LINK_NUMBER_0:
5054 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5055 &get_port_name->u.response);
5056 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5057 break;
5058 case LPFC_LINK_NUMBER_1:
5059 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5060 &get_port_name->u.response);
5061 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5062 break;
5063 case LPFC_LINK_NUMBER_2:
5064 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5065 &get_port_name->u.response);
5066 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5067 break;
5068 case LPFC_LINK_NUMBER_3:
5069 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5070 &get_port_name->u.response);
5071 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5072 break;
5073 default:
5074 break;
5075 }
5076
5077 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5078 phba->Port[0] = cport_name;
5079 phba->Port[1] = '\0';
5080 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5081 "3091 SLI get port name: %s\n", phba->Port);
5082 }
5083
5084out_free_mboxq:
5085 if (rc != MBX_TIMEOUT) {
5086 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5087 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5088 else
5089 mempool_free(mboxq, phba->mbox_mem_pool);
5090 }
5091 return rc;
5092}
5093
5094/**
James Smartda0436e2009-05-22 14:51:39 -04005095 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5096 * @phba: pointer to lpfc hba data structure.
5097 *
5098 * This routine is called to explicitly arm the SLI4 device's completion and
5099 * event queues
5100 **/
5101static void
5102lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5103{
James Smart962bc512013-01-03 15:44:00 -05005104 int fcp_eqidx;
James Smartda0436e2009-05-22 14:51:39 -04005105
5106 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5107 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smart05580562011-05-24 11:40:48 -04005108 fcp_eqidx = 0;
James Smart2e90f4b2011-12-13 13:22:37 -05005109 if (phba->sli4_hba.fcp_cq) {
James Smart67d12732012-08-03 12:36:13 -04005110 do {
James Smart2e90f4b2011-12-13 13:22:37 -05005111 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
5112 LPFC_QUEUE_REARM);
James Smart67d12732012-08-03 12:36:13 -04005113 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
James Smart2e90f4b2011-12-13 13:22:37 -05005114 }
James Smart1ba981f2014-02-20 09:56:45 -05005115
James Smartf38fa0b2014-04-04 13:52:21 -04005116 if (phba->cfg_fof)
James Smart1ba981f2014-02-20 09:56:45 -05005117 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5118
James Smart67d12732012-08-03 12:36:13 -04005119 if (phba->sli4_hba.hba_eq) {
5120 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart2e90f4b2011-12-13 13:22:37 -05005121 fcp_eqidx++)
James Smart67d12732012-08-03 12:36:13 -04005122 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
James Smart2e90f4b2011-12-13 13:22:37 -05005123 LPFC_QUEUE_REARM);
5124 }
James Smart1ba981f2014-02-20 09:56:45 -05005125
5126 if (phba->cfg_fof)
5127 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04005128}
5129
5130/**
James Smart6d368e52011-05-24 11:44:12 -04005131 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5132 * @phba: Pointer to HBA context object.
5133 * @type: The resource extent type.
James Smartb76f2dc2011-07-22 18:37:42 -04005134 * @extnt_count: buffer to hold port available extent count.
5135 * @extnt_size: buffer to hold element count per extent.
James Smart6d368e52011-05-24 11:44:12 -04005136 *
James Smartb76f2dc2011-07-22 18:37:42 -04005137 * This function calls the port and retrievs the number of available
5138 * extents and their size for a particular extent type.
5139 *
5140 * Returns: 0 if successful. Nonzero otherwise.
James Smart6d368e52011-05-24 11:44:12 -04005141 **/
James Smartb76f2dc2011-07-22 18:37:42 -04005142int
James Smart6d368e52011-05-24 11:44:12 -04005143lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5144 uint16_t *extnt_count, uint16_t *extnt_size)
5145{
5146 int rc = 0;
5147 uint32_t length;
5148 uint32_t mbox_tmo;
5149 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5150 LPFC_MBOXQ_t *mbox;
5151
5152 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5153 if (!mbox)
5154 return -ENOMEM;
5155
5156 /* Find out how many extents are available for this resource type */
5157 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5158 sizeof(struct lpfc_sli4_cfg_mhdr));
5159 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5160 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5161 length, LPFC_SLI4_MBX_EMBED);
5162
5163 /* Send an extents count of 0 - the GET doesn't use it. */
5164 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5165 LPFC_SLI4_MBX_EMBED);
5166 if (unlikely(rc)) {
5167 rc = -EIO;
5168 goto err_exit;
5169 }
5170
5171 if (!phba->sli4_hba.intr_enable)
5172 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5173 else {
James Smarta183a152011-10-10 21:32:43 -04005174 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005175 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5176 }
5177 if (unlikely(rc)) {
5178 rc = -EIO;
5179 goto err_exit;
5180 }
5181
5182 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5183 if (bf_get(lpfc_mbox_hdr_status,
5184 &rsrc_info->header.cfg_shdr.response)) {
5185 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5186 "2930 Failed to get resource extents "
5187 "Status 0x%x Add'l Status 0x%x\n",
5188 bf_get(lpfc_mbox_hdr_status,
5189 &rsrc_info->header.cfg_shdr.response),
5190 bf_get(lpfc_mbox_hdr_add_status,
5191 &rsrc_info->header.cfg_shdr.response));
5192 rc = -EIO;
5193 goto err_exit;
5194 }
5195
5196 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5197 &rsrc_info->u.rsp);
5198 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5199 &rsrc_info->u.rsp);
James Smart8a9d2e82012-05-09 21:16:12 -04005200
5201 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5202 "3162 Retrieved extents type-%d from port: count:%d, "
5203 "size:%d\n", type, *extnt_count, *extnt_size);
5204
5205err_exit:
James Smart6d368e52011-05-24 11:44:12 -04005206 mempool_free(mbox, phba->mbox_mem_pool);
5207 return rc;
5208}
5209
5210/**
5211 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5212 * @phba: Pointer to HBA context object.
5213 * @type: The extent type to check.
5214 *
5215 * This function reads the current available extents from the port and checks
5216 * if the extent count or extent size has changed since the last access.
5217 * Callers use this routine post port reset to understand if there is a
5218 * extent reprovisioning requirement.
5219 *
5220 * Returns:
5221 * -Error: error indicates problem.
5222 * 1: Extent count or size has changed.
5223 * 0: No changes.
5224 **/
5225static int
5226lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5227{
5228 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5229 uint16_t size_diff, rsrc_ext_size;
5230 int rc = 0;
5231 struct lpfc_rsrc_blks *rsrc_entry;
5232 struct list_head *rsrc_blk_list = NULL;
5233
5234 size_diff = 0;
5235 curr_ext_cnt = 0;
5236 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5237 &rsrc_ext_cnt,
5238 &rsrc_ext_size);
5239 if (unlikely(rc))
5240 return -EIO;
5241
5242 switch (type) {
5243 case LPFC_RSC_TYPE_FCOE_RPI:
5244 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5245 break;
5246 case LPFC_RSC_TYPE_FCOE_VPI:
5247 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5248 break;
5249 case LPFC_RSC_TYPE_FCOE_XRI:
5250 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5251 break;
5252 case LPFC_RSC_TYPE_FCOE_VFI:
5253 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5254 break;
5255 default:
5256 break;
5257 }
5258
5259 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5260 curr_ext_cnt++;
5261 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5262 size_diff++;
5263 }
5264
5265 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5266 rc = 1;
5267
5268 return rc;
5269}
5270
5271/**
5272 * lpfc_sli4_cfg_post_extnts -
5273 * @phba: Pointer to HBA context object.
5274 * @extnt_cnt - number of available extents.
5275 * @type - the extent type (rpi, xri, vfi, vpi).
5276 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5277 * @mbox - pointer to the caller's allocated mailbox structure.
5278 *
5279 * This function executes the extents allocation request. It also
5280 * takes care of the amount of memory needed to allocate or get the
5281 * allocated extents. It is the caller's responsibility to evaluate
5282 * the response.
5283 *
5284 * Returns:
5285 * -Error: Error value describes the condition found.
5286 * 0: if successful
5287 **/
5288static int
James Smart8a9d2e82012-05-09 21:16:12 -04005289lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
James Smart6d368e52011-05-24 11:44:12 -04005290 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5291{
5292 int rc = 0;
5293 uint32_t req_len;
5294 uint32_t emb_len;
5295 uint32_t alloc_len, mbox_tmo;
5296
5297 /* Calculate the total requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -04005298 req_len = extnt_cnt * sizeof(uint16_t);
James Smart6d368e52011-05-24 11:44:12 -04005299
5300 /*
5301 * Calculate the size of an embedded mailbox. The uint32_t
5302 * accounts for extents-specific word.
5303 */
5304 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5305 sizeof(uint32_t);
5306
5307 /*
5308 * Presume the allocation and response will fit into an embedded
5309 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5310 */
5311 *emb = LPFC_SLI4_MBX_EMBED;
5312 if (req_len > emb_len) {
James Smart8a9d2e82012-05-09 21:16:12 -04005313 req_len = extnt_cnt * sizeof(uint16_t) +
James Smart6d368e52011-05-24 11:44:12 -04005314 sizeof(union lpfc_sli4_cfg_shdr) +
5315 sizeof(uint32_t);
5316 *emb = LPFC_SLI4_MBX_NEMBED;
5317 }
5318
5319 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5320 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5321 req_len, *emb);
5322 if (alloc_len < req_len) {
5323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartb76f2dc2011-07-22 18:37:42 -04005324 "2982 Allocated DMA memory size (x%x) is "
James Smart6d368e52011-05-24 11:44:12 -04005325 "less than the requested DMA memory "
5326 "size (x%x)\n", alloc_len, req_len);
5327 return -ENOMEM;
5328 }
James Smart8a9d2e82012-05-09 21:16:12 -04005329 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
James Smart6d368e52011-05-24 11:44:12 -04005330 if (unlikely(rc))
5331 return -EIO;
5332
5333 if (!phba->sli4_hba.intr_enable)
5334 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5335 else {
James Smarta183a152011-10-10 21:32:43 -04005336 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005337 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5338 }
5339
5340 if (unlikely(rc))
5341 rc = -EIO;
5342 return rc;
5343}
5344
5345/**
5346 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5347 * @phba: Pointer to HBA context object.
5348 * @type: The resource extent type to allocate.
5349 *
5350 * This function allocates the number of elements for the specified
5351 * resource type.
5352 **/
5353static int
5354lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5355{
5356 bool emb = false;
5357 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5358 uint16_t rsrc_id, rsrc_start, j, k;
5359 uint16_t *ids;
5360 int i, rc;
5361 unsigned long longs;
5362 unsigned long *bmask;
5363 struct lpfc_rsrc_blks *rsrc_blks;
5364 LPFC_MBOXQ_t *mbox;
5365 uint32_t length;
5366 struct lpfc_id_range *id_array = NULL;
5367 void *virtaddr = NULL;
5368 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5369 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5370 struct list_head *ext_blk_list;
5371
5372 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5373 &rsrc_cnt,
5374 &rsrc_size);
5375 if (unlikely(rc))
5376 return -EIO;
5377
5378 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5379 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5380 "3009 No available Resource Extents "
5381 "for resource type 0x%x: Count: 0x%x, "
5382 "Size 0x%x\n", type, rsrc_cnt,
5383 rsrc_size);
5384 return -ENOMEM;
5385 }
5386
James Smart8a9d2e82012-05-09 21:16:12 -04005387 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5388 "2903 Post resource extents type-0x%x: "
5389 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
James Smart6d368e52011-05-24 11:44:12 -04005390
5391 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5392 if (!mbox)
5393 return -ENOMEM;
5394
James Smart8a9d2e82012-05-09 21:16:12 -04005395 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005396 if (unlikely(rc)) {
5397 rc = -EIO;
5398 goto err_exit;
5399 }
5400
5401 /*
5402 * Figure out where the response is located. Then get local pointers
5403 * to the response data. The port does not guarantee to respond to
5404 * all extents counts request so update the local variable with the
5405 * allocated count from the port.
5406 */
5407 if (emb == LPFC_SLI4_MBX_EMBED) {
5408 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5409 id_array = &rsrc_ext->u.rsp.id[0];
5410 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5411 } else {
5412 virtaddr = mbox->sge_array->addr[0];
5413 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5414 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5415 id_array = &n_rsrc->id;
5416 }
5417
5418 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5419 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5420
5421 /*
5422 * Based on the resource size and count, correct the base and max
5423 * resource values.
5424 */
5425 length = sizeof(struct lpfc_rsrc_blks);
5426 switch (type) {
5427 case LPFC_RSC_TYPE_FCOE_RPI:
5428 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5429 sizeof(unsigned long),
5430 GFP_KERNEL);
5431 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5432 rc = -ENOMEM;
5433 goto err_exit;
5434 }
5435 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5436 sizeof(uint16_t),
5437 GFP_KERNEL);
5438 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5439 kfree(phba->sli4_hba.rpi_bmask);
5440 rc = -ENOMEM;
5441 goto err_exit;
5442 }
5443
5444 /*
5445 * The next_rpi was initialized with the maximum available
5446 * count but the port may allocate a smaller number. Catch
5447 * that case and update the next_rpi.
5448 */
5449 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5450
5451 /* Initialize local ptrs for common extent processing later. */
5452 bmask = phba->sli4_hba.rpi_bmask;
5453 ids = phba->sli4_hba.rpi_ids;
5454 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5455 break;
5456 case LPFC_RSC_TYPE_FCOE_VPI:
5457 phba->vpi_bmask = kzalloc(longs *
5458 sizeof(unsigned long),
5459 GFP_KERNEL);
5460 if (unlikely(!phba->vpi_bmask)) {
5461 rc = -ENOMEM;
5462 goto err_exit;
5463 }
5464 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5465 sizeof(uint16_t),
5466 GFP_KERNEL);
5467 if (unlikely(!phba->vpi_ids)) {
5468 kfree(phba->vpi_bmask);
5469 rc = -ENOMEM;
5470 goto err_exit;
5471 }
5472
5473 /* Initialize local ptrs for common extent processing later. */
5474 bmask = phba->vpi_bmask;
5475 ids = phba->vpi_ids;
5476 ext_blk_list = &phba->lpfc_vpi_blk_list;
5477 break;
5478 case LPFC_RSC_TYPE_FCOE_XRI:
5479 phba->sli4_hba.xri_bmask = kzalloc(longs *
5480 sizeof(unsigned long),
5481 GFP_KERNEL);
5482 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5483 rc = -ENOMEM;
5484 goto err_exit;
5485 }
James Smart8a9d2e82012-05-09 21:16:12 -04005486 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005487 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5488 sizeof(uint16_t),
5489 GFP_KERNEL);
5490 if (unlikely(!phba->sli4_hba.xri_ids)) {
5491 kfree(phba->sli4_hba.xri_bmask);
5492 rc = -ENOMEM;
5493 goto err_exit;
5494 }
5495
5496 /* Initialize local ptrs for common extent processing later. */
5497 bmask = phba->sli4_hba.xri_bmask;
5498 ids = phba->sli4_hba.xri_ids;
5499 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5500 break;
5501 case LPFC_RSC_TYPE_FCOE_VFI:
5502 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5503 sizeof(unsigned long),
5504 GFP_KERNEL);
5505 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5506 rc = -ENOMEM;
5507 goto err_exit;
5508 }
5509 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5510 sizeof(uint16_t),
5511 GFP_KERNEL);
5512 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5513 kfree(phba->sli4_hba.vfi_bmask);
5514 rc = -ENOMEM;
5515 goto err_exit;
5516 }
5517
5518 /* Initialize local ptrs for common extent processing later. */
5519 bmask = phba->sli4_hba.vfi_bmask;
5520 ids = phba->sli4_hba.vfi_ids;
5521 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5522 break;
5523 default:
5524 /* Unsupported Opcode. Fail call. */
5525 id_array = NULL;
5526 bmask = NULL;
5527 ids = NULL;
5528 ext_blk_list = NULL;
5529 goto err_exit;
5530 }
5531
5532 /*
5533 * Complete initializing the extent configuration with the
5534 * allocated ids assigned to this function. The bitmask serves
5535 * as an index into the array and manages the available ids. The
5536 * array just stores the ids communicated to the port via the wqes.
5537 */
5538 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5539 if ((i % 2) == 0)
5540 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5541 &id_array[k]);
5542 else
5543 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5544 &id_array[k]);
5545
5546 rsrc_blks = kzalloc(length, GFP_KERNEL);
5547 if (unlikely(!rsrc_blks)) {
5548 rc = -ENOMEM;
5549 kfree(bmask);
5550 kfree(ids);
5551 goto err_exit;
5552 }
5553 rsrc_blks->rsrc_start = rsrc_id;
5554 rsrc_blks->rsrc_size = rsrc_size;
5555 list_add_tail(&rsrc_blks->list, ext_blk_list);
5556 rsrc_start = rsrc_id;
5557 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5558 phba->sli4_hba.scsi_xri_start = rsrc_start +
5559 lpfc_sli4_get_els_iocb_cnt(phba);
5560
5561 while (rsrc_id < (rsrc_start + rsrc_size)) {
5562 ids[j] = rsrc_id;
5563 rsrc_id++;
5564 j++;
5565 }
5566 /* Entire word processed. Get next word.*/
5567 if ((i % 2) == 1)
5568 k++;
5569 }
5570 err_exit:
5571 lpfc_sli4_mbox_cmd_free(phba, mbox);
5572 return rc;
5573}
5574
5575/**
5576 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5577 * @phba: Pointer to HBA context object.
5578 * @type: the extent's type.
5579 *
5580 * This function deallocates all extents of a particular resource type.
5581 * SLI4 does not allow for deallocating a particular extent range. It
5582 * is the caller's responsibility to release all kernel memory resources.
5583 **/
5584static int
5585lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5586{
5587 int rc;
5588 uint32_t length, mbox_tmo = 0;
5589 LPFC_MBOXQ_t *mbox;
5590 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5591 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5592
5593 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5594 if (!mbox)
5595 return -ENOMEM;
5596
5597 /*
5598 * This function sends an embedded mailbox because it only sends the
5599 * the resource type. All extents of this type are released by the
5600 * port.
5601 */
5602 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5603 sizeof(struct lpfc_sli4_cfg_mhdr));
5604 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5605 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5606 length, LPFC_SLI4_MBX_EMBED);
5607
5608 /* Send an extents count of 0 - the dealloc doesn't use it. */
5609 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5610 LPFC_SLI4_MBX_EMBED);
5611 if (unlikely(rc)) {
5612 rc = -EIO;
5613 goto out_free_mbox;
5614 }
5615 if (!phba->sli4_hba.intr_enable)
5616 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5617 else {
James Smarta183a152011-10-10 21:32:43 -04005618 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -04005619 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5620 }
5621 if (unlikely(rc)) {
5622 rc = -EIO;
5623 goto out_free_mbox;
5624 }
5625
5626 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5627 if (bf_get(lpfc_mbox_hdr_status,
5628 &dealloc_rsrc->header.cfg_shdr.response)) {
5629 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5630 "2919 Failed to release resource extents "
5631 "for type %d - Status 0x%x Add'l Status 0x%x. "
5632 "Resource memory not released.\n",
5633 type,
5634 bf_get(lpfc_mbox_hdr_status,
5635 &dealloc_rsrc->header.cfg_shdr.response),
5636 bf_get(lpfc_mbox_hdr_add_status,
5637 &dealloc_rsrc->header.cfg_shdr.response));
5638 rc = -EIO;
5639 goto out_free_mbox;
5640 }
5641
5642 /* Release kernel memory resources for the specific type. */
5643 switch (type) {
5644 case LPFC_RSC_TYPE_FCOE_VPI:
5645 kfree(phba->vpi_bmask);
5646 kfree(phba->vpi_ids);
5647 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5648 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5649 &phba->lpfc_vpi_blk_list, list) {
5650 list_del_init(&rsrc_blk->list);
5651 kfree(rsrc_blk);
5652 }
James Smart16a3a202013-04-17 20:14:38 -04005653 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005654 break;
5655 case LPFC_RSC_TYPE_FCOE_XRI:
5656 kfree(phba->sli4_hba.xri_bmask);
5657 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005658 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5659 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5660 list_del_init(&rsrc_blk->list);
5661 kfree(rsrc_blk);
5662 }
5663 break;
5664 case LPFC_RSC_TYPE_FCOE_VFI:
5665 kfree(phba->sli4_hba.vfi_bmask);
5666 kfree(phba->sli4_hba.vfi_ids);
5667 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5668 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5669 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5670 list_del_init(&rsrc_blk->list);
5671 kfree(rsrc_blk);
5672 }
5673 break;
5674 case LPFC_RSC_TYPE_FCOE_RPI:
5675 /* RPI bitmask and physical id array are cleaned up earlier. */
5676 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5677 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5678 list_del_init(&rsrc_blk->list);
5679 kfree(rsrc_blk);
5680 }
5681 break;
5682 default:
5683 break;
5684 }
5685
5686 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5687
5688 out_free_mbox:
5689 mempool_free(mbox, phba->mbox_mem_pool);
5690 return rc;
5691}
5692
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08005693static void
James Smart7bdedb32016-07-06 12:36:00 -07005694lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5695 uint32_t feature)
James Smart65791f12016-07-06 12:35:56 -07005696{
James Smart65791f12016-07-06 12:35:56 -07005697 uint32_t len;
James Smart65791f12016-07-06 12:35:56 -07005698
James Smart65791f12016-07-06 12:35:56 -07005699 len = sizeof(struct lpfc_mbx_set_feature) -
5700 sizeof(struct lpfc_sli4_cfg_mhdr);
5701 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5702 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5703 LPFC_SLI4_MBX_EMBED);
James Smart65791f12016-07-06 12:35:56 -07005704
James Smart7bdedb32016-07-06 12:36:00 -07005705 switch (feature) {
5706 case LPFC_SET_UE_RECOVERY:
5707 bf_set(lpfc_mbx_set_feature_UER,
5708 &mbox->u.mqe.un.set_feature, 1);
5709 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5710 mbox->u.mqe.un.set_feature.param_len = 8;
5711 break;
5712 case LPFC_SET_MDS_DIAGS:
5713 bf_set(lpfc_mbx_set_feature_mds,
5714 &mbox->u.mqe.un.set_feature, 1);
5715 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5716 &mbox->u.mqe.un.set_feature, 0);
5717 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5718 mbox->u.mqe.un.set_feature.param_len = 8;
5719 break;
James Smart65791f12016-07-06 12:35:56 -07005720 }
James Smart7bdedb32016-07-06 12:36:00 -07005721
5722 return;
James Smart65791f12016-07-06 12:35:56 -07005723}
5724
James Smart6d368e52011-05-24 11:44:12 -04005725/**
5726 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5727 * @phba: Pointer to HBA context object.
5728 *
5729 * This function allocates all SLI4 resource identifiers.
5730 **/
5731int
5732lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5733{
5734 int i, rc, error = 0;
5735 uint16_t count, base;
5736 unsigned long longs;
5737
James Smartff78d8f2011-12-13 13:21:35 -05005738 if (!phba->sli4_hba.rpi_hdrs_in_use)
5739 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart6d368e52011-05-24 11:44:12 -04005740 if (phba->sli4_hba.extents_in_use) {
5741 /*
5742 * The port supports resource extents. The XRI, VPI, VFI, RPI
5743 * resource extent count must be read and allocated before
5744 * provisioning the resource id arrays.
5745 */
5746 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5747 LPFC_IDX_RSRC_RDY) {
5748 /*
5749 * Extent-based resources are set - the driver could
5750 * be in a port reset. Figure out if any corrective
5751 * actions need to be taken.
5752 */
5753 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5754 LPFC_RSC_TYPE_FCOE_VFI);
5755 if (rc != 0)
5756 error++;
5757 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5758 LPFC_RSC_TYPE_FCOE_VPI);
5759 if (rc != 0)
5760 error++;
5761 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5762 LPFC_RSC_TYPE_FCOE_XRI);
5763 if (rc != 0)
5764 error++;
5765 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5766 LPFC_RSC_TYPE_FCOE_RPI);
5767 if (rc != 0)
5768 error++;
5769
5770 /*
5771 * It's possible that the number of resources
5772 * provided to this port instance changed between
5773 * resets. Detect this condition and reallocate
5774 * resources. Otherwise, there is no action.
5775 */
5776 if (error) {
5777 lpfc_printf_log(phba, KERN_INFO,
5778 LOG_MBOX | LOG_INIT,
5779 "2931 Detected extent resource "
5780 "change. Reallocating all "
5781 "extents.\n");
5782 rc = lpfc_sli4_dealloc_extent(phba,
5783 LPFC_RSC_TYPE_FCOE_VFI);
5784 rc = lpfc_sli4_dealloc_extent(phba,
5785 LPFC_RSC_TYPE_FCOE_VPI);
5786 rc = lpfc_sli4_dealloc_extent(phba,
5787 LPFC_RSC_TYPE_FCOE_XRI);
5788 rc = lpfc_sli4_dealloc_extent(phba,
5789 LPFC_RSC_TYPE_FCOE_RPI);
5790 } else
5791 return 0;
5792 }
5793
5794 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5795 if (unlikely(rc))
5796 goto err_exit;
5797
5798 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5799 if (unlikely(rc))
5800 goto err_exit;
5801
5802 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5803 if (unlikely(rc))
5804 goto err_exit;
5805
5806 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5807 if (unlikely(rc))
5808 goto err_exit;
5809 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5810 LPFC_IDX_RSRC_RDY);
5811 return rc;
5812 } else {
5813 /*
5814 * The port does not support resource extents. The XRI, VPI,
5815 * VFI, RPI resource ids were determined from READ_CONFIG.
5816 * Just allocate the bitmasks and provision the resource id
5817 * arrays. If a port reset is active, the resources don't
5818 * need any action - just exit.
5819 */
5820 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
James Smartff78d8f2011-12-13 13:21:35 -05005821 LPFC_IDX_RSRC_RDY) {
5822 lpfc_sli4_dealloc_resource_identifiers(phba);
5823 lpfc_sli4_remove_rpis(phba);
5824 }
James Smart6d368e52011-05-24 11:44:12 -04005825 /* RPIs. */
5826 count = phba->sli4_hba.max_cfg_param.max_rpi;
James Smart0a630c22013-01-03 15:44:09 -05005827 if (count <= 0) {
5828 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5829 "3279 Invalid provisioning of "
5830 "rpi:%d\n", count);
5831 rc = -EINVAL;
5832 goto err_exit;
5833 }
James Smart6d368e52011-05-24 11:44:12 -04005834 base = phba->sli4_hba.max_cfg_param.rpi_base;
5835 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5836 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5837 sizeof(unsigned long),
5838 GFP_KERNEL);
5839 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5840 rc = -ENOMEM;
5841 goto err_exit;
5842 }
5843 phba->sli4_hba.rpi_ids = kzalloc(count *
5844 sizeof(uint16_t),
5845 GFP_KERNEL);
5846 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5847 rc = -ENOMEM;
5848 goto free_rpi_bmask;
5849 }
5850
5851 for (i = 0; i < count; i++)
5852 phba->sli4_hba.rpi_ids[i] = base + i;
5853
5854 /* VPIs. */
5855 count = phba->sli4_hba.max_cfg_param.max_vpi;
James Smart0a630c22013-01-03 15:44:09 -05005856 if (count <= 0) {
5857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5858 "3280 Invalid provisioning of "
5859 "vpi:%d\n", count);
5860 rc = -EINVAL;
5861 goto free_rpi_ids;
5862 }
James Smart6d368e52011-05-24 11:44:12 -04005863 base = phba->sli4_hba.max_cfg_param.vpi_base;
5864 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5865 phba->vpi_bmask = kzalloc(longs *
5866 sizeof(unsigned long),
5867 GFP_KERNEL);
5868 if (unlikely(!phba->vpi_bmask)) {
5869 rc = -ENOMEM;
5870 goto free_rpi_ids;
5871 }
5872 phba->vpi_ids = kzalloc(count *
5873 sizeof(uint16_t),
5874 GFP_KERNEL);
5875 if (unlikely(!phba->vpi_ids)) {
5876 rc = -ENOMEM;
5877 goto free_vpi_bmask;
5878 }
5879
5880 for (i = 0; i < count; i++)
5881 phba->vpi_ids[i] = base + i;
5882
5883 /* XRIs. */
5884 count = phba->sli4_hba.max_cfg_param.max_xri;
James Smart0a630c22013-01-03 15:44:09 -05005885 if (count <= 0) {
5886 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5887 "3281 Invalid provisioning of "
5888 "xri:%d\n", count);
5889 rc = -EINVAL;
5890 goto free_vpi_ids;
5891 }
James Smart6d368e52011-05-24 11:44:12 -04005892 base = phba->sli4_hba.max_cfg_param.xri_base;
5893 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5894 phba->sli4_hba.xri_bmask = kzalloc(longs *
5895 sizeof(unsigned long),
5896 GFP_KERNEL);
5897 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5898 rc = -ENOMEM;
5899 goto free_vpi_ids;
5900 }
James Smart41899be2012-03-01 22:34:19 -05005901 phba->sli4_hba.max_cfg_param.xri_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005902 phba->sli4_hba.xri_ids = kzalloc(count *
5903 sizeof(uint16_t),
5904 GFP_KERNEL);
5905 if (unlikely(!phba->sli4_hba.xri_ids)) {
5906 rc = -ENOMEM;
5907 goto free_xri_bmask;
5908 }
5909
5910 for (i = 0; i < count; i++)
5911 phba->sli4_hba.xri_ids[i] = base + i;
5912
5913 /* VFIs. */
5914 count = phba->sli4_hba.max_cfg_param.max_vfi;
James Smart0a630c22013-01-03 15:44:09 -05005915 if (count <= 0) {
5916 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5917 "3282 Invalid provisioning of "
5918 "vfi:%d\n", count);
5919 rc = -EINVAL;
5920 goto free_xri_ids;
5921 }
James Smart6d368e52011-05-24 11:44:12 -04005922 base = phba->sli4_hba.max_cfg_param.vfi_base;
5923 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5924 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5925 sizeof(unsigned long),
5926 GFP_KERNEL);
5927 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5928 rc = -ENOMEM;
5929 goto free_xri_ids;
5930 }
5931 phba->sli4_hba.vfi_ids = kzalloc(count *
5932 sizeof(uint16_t),
5933 GFP_KERNEL);
5934 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5935 rc = -ENOMEM;
5936 goto free_vfi_bmask;
5937 }
5938
5939 for (i = 0; i < count; i++)
5940 phba->sli4_hba.vfi_ids[i] = base + i;
5941
5942 /*
5943 * Mark all resources ready. An HBA reset doesn't need
5944 * to reset the initialization.
5945 */
5946 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5947 LPFC_IDX_RSRC_RDY);
5948 return 0;
5949 }
5950
5951 free_vfi_bmask:
5952 kfree(phba->sli4_hba.vfi_bmask);
5953 free_xri_ids:
5954 kfree(phba->sli4_hba.xri_ids);
5955 free_xri_bmask:
5956 kfree(phba->sli4_hba.xri_bmask);
5957 free_vpi_ids:
5958 kfree(phba->vpi_ids);
5959 free_vpi_bmask:
5960 kfree(phba->vpi_bmask);
5961 free_rpi_ids:
5962 kfree(phba->sli4_hba.rpi_ids);
5963 free_rpi_bmask:
5964 kfree(phba->sli4_hba.rpi_bmask);
5965 err_exit:
5966 return rc;
5967}
5968
5969/**
5970 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5971 * @phba: Pointer to HBA context object.
5972 *
5973 * This function allocates the number of elements for the specified
5974 * resource type.
5975 **/
5976int
5977lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5978{
5979 if (phba->sli4_hba.extents_in_use) {
5980 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5981 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5982 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5983 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5984 } else {
5985 kfree(phba->vpi_bmask);
James Smart16a3a202013-04-17 20:14:38 -04005986 phba->sli4_hba.max_cfg_param.vpi_used = 0;
James Smart6d368e52011-05-24 11:44:12 -04005987 kfree(phba->vpi_ids);
5988 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5989 kfree(phba->sli4_hba.xri_bmask);
5990 kfree(phba->sli4_hba.xri_ids);
James Smart6d368e52011-05-24 11:44:12 -04005991 kfree(phba->sli4_hba.vfi_bmask);
5992 kfree(phba->sli4_hba.vfi_ids);
5993 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5994 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5995 }
5996
5997 return 0;
5998}
5999
6000/**
James Smartb76f2dc2011-07-22 18:37:42 -04006001 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6002 * @phba: Pointer to HBA context object.
6003 * @type: The resource extent type.
6004 * @extnt_count: buffer to hold port extent count response
6005 * @extnt_size: buffer to hold port extent size response.
6006 *
6007 * This function calls the port to read the host allocated extents
6008 * for a particular type.
6009 **/
6010int
6011lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6012 uint16_t *extnt_cnt, uint16_t *extnt_size)
6013{
6014 bool emb;
6015 int rc = 0;
6016 uint16_t curr_blks = 0;
6017 uint32_t req_len, emb_len;
6018 uint32_t alloc_len, mbox_tmo;
6019 struct list_head *blk_list_head;
6020 struct lpfc_rsrc_blks *rsrc_blk;
6021 LPFC_MBOXQ_t *mbox;
6022 void *virtaddr = NULL;
6023 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6024 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6025 union lpfc_sli4_cfg_shdr *shdr;
6026
6027 switch (type) {
6028 case LPFC_RSC_TYPE_FCOE_VPI:
6029 blk_list_head = &phba->lpfc_vpi_blk_list;
6030 break;
6031 case LPFC_RSC_TYPE_FCOE_XRI:
6032 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6033 break;
6034 case LPFC_RSC_TYPE_FCOE_VFI:
6035 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6036 break;
6037 case LPFC_RSC_TYPE_FCOE_RPI:
6038 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6039 break;
6040 default:
6041 return -EIO;
6042 }
6043
6044 /* Count the number of extents currently allocatd for this type. */
6045 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6046 if (curr_blks == 0) {
6047 /*
6048 * The GET_ALLOCATED mailbox does not return the size,
6049 * just the count. The size should be just the size
6050 * stored in the current allocated block and all sizes
6051 * for an extent type are the same so set the return
6052 * value now.
6053 */
6054 *extnt_size = rsrc_blk->rsrc_size;
6055 }
6056 curr_blks++;
6057 }
6058
James Smartb76f2dc2011-07-22 18:37:42 -04006059 /*
6060 * Calculate the size of an embedded mailbox. The uint32_t
6061 * accounts for extents-specific word.
6062 */
6063 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6064 sizeof(uint32_t);
6065
6066 /*
6067 * Presume the allocation and response will fit into an embedded
6068 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6069 */
6070 emb = LPFC_SLI4_MBX_EMBED;
6071 req_len = emb_len;
6072 if (req_len > emb_len) {
6073 req_len = curr_blks * sizeof(uint16_t) +
6074 sizeof(union lpfc_sli4_cfg_shdr) +
6075 sizeof(uint32_t);
6076 emb = LPFC_SLI4_MBX_NEMBED;
6077 }
6078
6079 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6080 if (!mbox)
6081 return -ENOMEM;
6082 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6083
6084 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6085 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6086 req_len, emb);
6087 if (alloc_len < req_len) {
6088 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6089 "2983 Allocated DMA memory size (x%x) is "
6090 "less than the requested DMA memory "
6091 "size (x%x)\n", alloc_len, req_len);
6092 rc = -ENOMEM;
6093 goto err_exit;
6094 }
6095 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6096 if (unlikely(rc)) {
6097 rc = -EIO;
6098 goto err_exit;
6099 }
6100
6101 if (!phba->sli4_hba.intr_enable)
6102 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6103 else {
James Smarta183a152011-10-10 21:32:43 -04006104 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smartb76f2dc2011-07-22 18:37:42 -04006105 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6106 }
6107
6108 if (unlikely(rc)) {
6109 rc = -EIO;
6110 goto err_exit;
6111 }
6112
6113 /*
6114 * Figure out where the response is located. Then get local pointers
6115 * to the response data. The port does not guarantee to respond to
6116 * all extents counts request so update the local variable with the
6117 * allocated count from the port.
6118 */
6119 if (emb == LPFC_SLI4_MBX_EMBED) {
6120 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6121 shdr = &rsrc_ext->header.cfg_shdr;
6122 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6123 } else {
6124 virtaddr = mbox->sge_array->addr[0];
6125 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6126 shdr = &n_rsrc->cfg_shdr;
6127 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6128 }
6129
6130 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6131 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6132 "2984 Failed to read allocated resources "
6133 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6134 type,
6135 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6136 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6137 rc = -EIO;
6138 goto err_exit;
6139 }
6140 err_exit:
6141 lpfc_sli4_mbox_cmd_free(phba, mbox);
6142 return rc;
6143}
6144
6145/**
James Smart8a9d2e82012-05-09 21:16:12 -04006146 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
6147 * @phba: pointer to lpfc hba data structure.
6148 *
6149 * This routine walks the list of els buffers that have been allocated and
6150 * repost them to the port by using SGL block post. This is needed after a
6151 * pci_function_reset/warm_start or start. It attempts to construct blocks
6152 * of els buffer sgls which contains contiguous xris and uses the non-embedded
6153 * SGL block post mailbox commands to post them to the port. For single els
6154 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6155 * mailbox command for posting.
6156 *
6157 * Returns: 0 = success, non-zero failure.
6158 **/
6159static int
6160lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
6161{
6162 struct lpfc_sglq *sglq_entry = NULL;
6163 struct lpfc_sglq *sglq_entry_next = NULL;
6164 struct lpfc_sglq *sglq_entry_first = NULL;
James Smart711ea882013-04-17 20:18:29 -04006165 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
James Smart8a9d2e82012-05-09 21:16:12 -04006166 int last_xritag = NO_XRI;
James Smartdafe8ce2014-09-03 12:56:40 -04006167 struct lpfc_sli_ring *pring;
James Smart8a9d2e82012-05-09 21:16:12 -04006168 LIST_HEAD(prep_sgl_list);
6169 LIST_HEAD(blck_sgl_list);
6170 LIST_HEAD(allc_sgl_list);
6171 LIST_HEAD(post_sgl_list);
6172 LIST_HEAD(free_sgl_list);
6173
James Smartdafe8ce2014-09-03 12:56:40 -04006174 pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart38c20672013-03-01 16:37:44 -05006175 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04006176 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04006177 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04006178 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05006179 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006180
James Smart711ea882013-04-17 20:18:29 -04006181 total_cnt = phba->sli4_hba.els_xri_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006182 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6183 &allc_sgl_list, list) {
6184 list_del_init(&sglq_entry->list);
6185 block_cnt++;
6186 if ((last_xritag != NO_XRI) &&
6187 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6188 /* a hole in xri block, form a sgl posting block */
6189 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6190 post_cnt = block_cnt - 1;
6191 /* prepare list for next posting block */
6192 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6193 block_cnt = 1;
6194 } else {
6195 /* prepare list for next posting block */
6196 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6197 /* enough sgls for non-embed sgl mbox command */
6198 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6199 list_splice_init(&prep_sgl_list,
6200 &blck_sgl_list);
6201 post_cnt = block_cnt;
6202 block_cnt = 0;
6203 }
6204 }
6205 num_posted++;
6206
6207 /* keep track of last sgl's xritag */
6208 last_xritag = sglq_entry->sli4_xritag;
6209
6210 /* end of repost sgl list condition for els buffers */
6211 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6212 if (post_cnt == 0) {
6213 list_splice_init(&prep_sgl_list,
6214 &blck_sgl_list);
6215 post_cnt = block_cnt;
6216 } else if (block_cnt == 1) {
6217 status = lpfc_sli4_post_sgl(phba,
6218 sglq_entry->phys, 0,
6219 sglq_entry->sli4_xritag);
6220 if (!status) {
6221 /* successful, put sgl to posted list */
6222 list_add_tail(&sglq_entry->list,
6223 &post_sgl_list);
6224 } else {
6225 /* Failure, put sgl to free list */
6226 lpfc_printf_log(phba, KERN_WARNING,
6227 LOG_SLI,
6228 "3159 Failed to post els "
6229 "sgl, xritag:x%x\n",
6230 sglq_entry->sli4_xritag);
6231 list_add_tail(&sglq_entry->list,
6232 &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006233 total_cnt--;
James Smart8a9d2e82012-05-09 21:16:12 -04006234 }
6235 }
6236 }
6237
6238 /* continue until a nembed page worth of sgls */
6239 if (post_cnt == 0)
6240 continue;
6241
6242 /* post the els buffer list sgls as a block */
6243 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6244 post_cnt);
6245
6246 if (!status) {
6247 /* success, put sgl list to posted sgl list */
6248 list_splice_init(&blck_sgl_list, &post_sgl_list);
6249 } else {
6250 /* Failure, put sgl list to free sgl list */
6251 sglq_entry_first = list_first_entry(&blck_sgl_list,
6252 struct lpfc_sglq,
6253 list);
6254 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6255 "3160 Failed to post els sgl-list, "
6256 "xritag:x%x-x%x\n",
6257 sglq_entry_first->sli4_xritag,
6258 (sglq_entry_first->sli4_xritag +
6259 post_cnt - 1));
6260 list_splice_init(&blck_sgl_list, &free_sgl_list);
James Smart711ea882013-04-17 20:18:29 -04006261 total_cnt -= post_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006262 }
6263
6264 /* don't reset xirtag due to hole in xri block */
6265 if (block_cnt == 0)
6266 last_xritag = NO_XRI;
6267
6268 /* reset els sgl post count for next round of posting */
6269 post_cnt = 0;
6270 }
James Smart711ea882013-04-17 20:18:29 -04006271 /* update the number of XRIs posted for ELS */
6272 phba->sli4_hba.els_xri_cnt = total_cnt;
James Smart8a9d2e82012-05-09 21:16:12 -04006273
6274 /* free the els sgls failed to post */
6275 lpfc_free_sgl_list(phba, &free_sgl_list);
6276
6277 /* push els sgls posted to the availble list */
6278 if (!list_empty(&post_sgl_list)) {
James Smart38c20672013-03-01 16:37:44 -05006279 spin_lock_irq(&phba->hbalock);
James Smartdafe8ce2014-09-03 12:56:40 -04006280 spin_lock(&pring->ring_lock);
James Smart8a9d2e82012-05-09 21:16:12 -04006281 list_splice_init(&post_sgl_list,
6282 &phba->sli4_hba.lpfc_sgl_list);
James Smartdafe8ce2014-09-03 12:56:40 -04006283 spin_unlock(&pring->ring_lock);
James Smart38c20672013-03-01 16:37:44 -05006284 spin_unlock_irq(&phba->hbalock);
James Smart8a9d2e82012-05-09 21:16:12 -04006285 } else {
6286 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6287 "3161 Failure to post els sgl to port.\n");
6288 return -EIO;
6289 }
6290 return 0;
6291}
6292
James Smart61bda8f2016-10-13 15:06:05 -07006293void
6294lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
6295{
6296 uint32_t len;
6297
6298 len = sizeof(struct lpfc_mbx_set_host_data) -
6299 sizeof(struct lpfc_sli4_cfg_mhdr);
6300 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6301 LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
6302 LPFC_SLI4_MBX_EMBED);
6303
6304 mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
6305 mbox->u.mqe.un.set_host_data.param_len = 8;
6306 snprintf(mbox->u.mqe.un.set_host_data.data,
6307 LPFC_HOST_OS_DRIVER_VERSION_SIZE,
6308 "Linux %s v"LPFC_DRIVER_VERSION,
6309 (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
6310}
6311
James Smart8a9d2e82012-05-09 21:16:12 -04006312/**
James Smartda0436e2009-05-22 14:51:39 -04006313 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6314 * @phba: Pointer to HBA context object.
6315 *
6316 * This function is the main SLI4 device intialization PCI function. This
6317 * function is called by the HBA intialization code, HBA reset code and
6318 * HBA error attention handler code. Caller is not required to hold any
6319 * locks.
6320 **/
6321int
6322lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6323{
6324 int rc;
6325 LPFC_MBOXQ_t *mboxq;
6326 struct lpfc_mqe *mqe;
6327 uint8_t *vpd;
6328 uint32_t vpd_size;
6329 uint32_t ftr_rsp = 0;
6330 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6331 struct lpfc_vport *vport = phba->pport;
6332 struct lpfc_dmabuf *mp;
6333
6334 /* Perform a PCI function reset to start from clean */
6335 rc = lpfc_pci_function_reset(phba);
6336 if (unlikely(rc))
6337 return -ENODEV;
6338
6339 /* Check the HBA Host Status Register for readyness */
6340 rc = lpfc_sli4_post_status_check(phba);
6341 if (unlikely(rc))
6342 return -ENODEV;
6343 else {
6344 spin_lock_irq(&phba->hbalock);
6345 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6346 spin_unlock_irq(&phba->hbalock);
6347 }
6348
6349 /*
6350 * Allocate a single mailbox container for initializing the
6351 * port.
6352 */
6353 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6354 if (!mboxq)
6355 return -ENOMEM;
6356
James Smartda0436e2009-05-22 14:51:39 -04006357 /* Issue READ_REV to collect vpd and FW information. */
James Smart49198b32010-04-06 15:04:33 -04006358 vpd_size = SLI4_PAGE_SIZE;
James Smartda0436e2009-05-22 14:51:39 -04006359 vpd = kzalloc(vpd_size, GFP_KERNEL);
6360 if (!vpd) {
6361 rc = -ENOMEM;
6362 goto out_free_mbox;
6363 }
6364
6365 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
James Smart76a95d72010-11-20 23:11:48 -05006366 if (unlikely(rc)) {
6367 kfree(vpd);
6368 goto out_free_mbox;
6369 }
James Smart572709e2013-07-15 18:32:43 -04006370
James Smartda0436e2009-05-22 14:51:39 -04006371 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04006372 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
James Smartb5c53952016-03-31 14:12:30 -07006373 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
James Smart76a95d72010-11-20 23:11:48 -05006374 phba->hba_flag |= HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006375 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6376 } else {
James Smart76a95d72010-11-20 23:11:48 -05006377 phba->hba_flag &= ~HBA_FCOE_MODE;
James Smartb5c53952016-03-31 14:12:30 -07006378 }
James Smart45ed1192009-10-02 15:17:02 -04006379
6380 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6381 LPFC_DCBX_CEE_MODE)
6382 phba->hba_flag |= HBA_FIP_SUPPORT;
6383 else
6384 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6385
James Smart4f2e66c2012-05-09 21:17:07 -04006386 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6387
James Smartc31098c2011-04-16 11:03:33 -04006388 if (phba->sli_rev != LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -04006389 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6390 "0376 READ_REV Error. SLI Level %d "
6391 "FCoE enabled %d\n",
James Smart76a95d72010-11-20 23:11:48 -05006392 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
James Smartda0436e2009-05-22 14:51:39 -04006393 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006394 kfree(vpd);
6395 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006396 }
James Smartcd1c8302011-10-10 21:33:25 -04006397
6398 /*
James Smartff78d8f2011-12-13 13:21:35 -05006399 * Continue initialization with default values even if driver failed
6400 * to read FCoE param config regions, only read parameters if the
6401 * board is FCoE
6402 */
6403 if (phba->hba_flag & HBA_FCOE_MODE &&
6404 lpfc_sli4_read_fcoe_params(phba))
6405 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6406 "2570 Failed to read FCoE parameters\n");
6407
6408 /*
James Smartcd1c8302011-10-10 21:33:25 -04006409 * Retrieve sli4 device physical port name, failure of doing it
6410 * is considered as non-fatal.
6411 */
6412 rc = lpfc_sli4_retrieve_pport_name(phba);
6413 if (!rc)
6414 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6415 "3080 Successful retrieving SLI4 device "
6416 "physical port name: %s.\n", phba->Port);
6417
James Smartda0436e2009-05-22 14:51:39 -04006418 /*
6419 * Evaluate the read rev and vpd data. Populate the driver
6420 * state with the results. If this routine fails, the failure
6421 * is not fatal as the driver will use generic values.
6422 */
6423 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6424 if (unlikely(!rc)) {
6425 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6426 "0377 Error %d parsing vpd. "
6427 "Using defaults.\n", rc);
6428 rc = 0;
6429 }
James Smart76a95d72010-11-20 23:11:48 -05006430 kfree(vpd);
James Smartda0436e2009-05-22 14:51:39 -04006431
James Smartf1126682009-06-10 17:22:44 -04006432 /* Save information as VPD data */
6433 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6434 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6435 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6436 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6437 &mqe->un.read_rev);
6438 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6439 &mqe->un.read_rev);
6440 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6441 &mqe->un.read_rev);
6442 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6443 &mqe->un.read_rev);
6444 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6445 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6446 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6447 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6448 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6449 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6450 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6451 "(%d):0380 READ_REV Status x%x "
6452 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6453 mboxq->vport ? mboxq->vport->vpi : 0,
6454 bf_get(lpfc_mqe_status, mqe),
6455 phba->vpd.rev.opFwName,
6456 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6457 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04006458
James Smart572709e2013-07-15 18:32:43 -04006459 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6460 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6461 if (phba->pport->cfg_lun_queue_depth > rc) {
6462 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6463 "3362 LUN queue depth changed from %d to %d\n",
6464 phba->pport->cfg_lun_queue_depth, rc);
6465 phba->pport->cfg_lun_queue_depth = rc;
6466 }
6467
James Smart65791f12016-07-06 12:35:56 -07006468 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
James Smart7bdedb32016-07-06 12:36:00 -07006469 LPFC_SLI_INTF_IF_TYPE_0) {
6470 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6471 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6472 if (rc == MBX_SUCCESS) {
6473 phba->hba_flag |= HBA_RECOVERABLE_UE;
6474 /* Set 1Sec interval to detect UE */
6475 phba->eratt_poll_interval = 1;
6476 phba->sli4_hba.ue_to_sr = bf_get(
6477 lpfc_mbx_set_feature_UESR,
6478 &mboxq->u.mqe.un.set_feature);
6479 phba->sli4_hba.ue_to_rp = bf_get(
6480 lpfc_mbx_set_feature_UERP,
6481 &mboxq->u.mqe.un.set_feature);
6482 }
6483 }
6484
6485 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6486 /* Enable MDS Diagnostics only if the SLI Port supports it */
6487 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6488 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6489 if (rc != MBX_SUCCESS)
6490 phba->mds_diags_support = 0;
6491 }
James Smart572709e2013-07-15 18:32:43 -04006492
James Smartda0436e2009-05-22 14:51:39 -04006493 /*
6494 * Discover the port's supported feature set and match it against the
6495 * hosts requests.
6496 */
6497 lpfc_request_features(phba, mboxq);
6498 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6499 if (unlikely(rc)) {
6500 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006501 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006502 }
6503
6504 /*
6505 * The port must support FCP initiator mode as this is the
6506 * only mode running in the host.
6507 */
6508 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6509 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6510 "0378 No support for fcpi mode.\n");
6511 ftr_rsp++;
6512 }
James Smartfedd3b72011-02-16 12:39:24 -05006513 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6514 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6515 else
6516 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
James Smartda0436e2009-05-22 14:51:39 -04006517 /*
6518 * If the port cannot support the host's requested features
6519 * then turn off the global config parameters to disable the
6520 * feature in the driver. This is not a fatal error.
6521 */
James Smartbf086112011-08-21 21:48:13 -04006522 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6523 if (phba->cfg_enable_bg) {
6524 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6525 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6526 else
6527 ftr_rsp++;
6528 }
James Smartda0436e2009-05-22 14:51:39 -04006529
6530 if (phba->max_vpi && phba->cfg_enable_npiv &&
6531 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6532 ftr_rsp++;
6533
6534 if (ftr_rsp) {
6535 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6536 "0379 Feature Mismatch Data: x%08x %08x "
6537 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6538 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6539 phba->cfg_enable_npiv, phba->max_vpi);
6540 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6541 phba->cfg_enable_bg = 0;
6542 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6543 phba->cfg_enable_npiv = 0;
6544 }
6545
6546 /* These SLI3 features are assumed in SLI4 */
6547 spin_lock_irq(&phba->hbalock);
6548 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6549 spin_unlock_irq(&phba->hbalock);
6550
James Smart6d368e52011-05-24 11:44:12 -04006551 /*
6552 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6553 * calls depends on these resources to complete port setup.
6554 */
6555 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6556 if (rc) {
6557 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6558 "2920 Failed to alloc Resource IDs "
6559 "rc = x%x\n", rc);
6560 goto out_free_mbox;
6561 }
6562
James Smart61bda8f2016-10-13 15:06:05 -07006563 lpfc_set_host_data(phba, mboxq);
6564
6565 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6566 if (rc) {
6567 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6568 "2134 Failed to set host os driver version %x",
6569 rc);
6570 }
6571
James Smartda0436e2009-05-22 14:51:39 -04006572 /* Read the port's service parameters. */
James Smart9f1177a2010-02-26 14:12:57 -05006573 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6574 if (rc) {
6575 phba->link_state = LPFC_HBA_ERROR;
6576 rc = -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -05006577 goto out_free_mbox;
James Smart9f1177a2010-02-26 14:12:57 -05006578 }
6579
James Smartda0436e2009-05-22 14:51:39 -04006580 mboxq->vport = vport;
6581 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6582 mp = (struct lpfc_dmabuf *) mboxq->context1;
6583 if (rc == MBX_SUCCESS) {
6584 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6585 rc = 0;
6586 }
6587
6588 /*
6589 * This memory was allocated by the lpfc_read_sparam routine. Release
6590 * it to the mbuf pool.
6591 */
6592 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6593 kfree(mp);
6594 mboxq->context1 = NULL;
6595 if (unlikely(rc)) {
6596 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6597 "0382 READ_SPARAM command failed "
6598 "status %d, mbxStatus x%x\n",
6599 rc, bf_get(lpfc_mqe_status, mqe));
6600 phba->link_state = LPFC_HBA_ERROR;
6601 rc = -EIO;
James Smart76a95d72010-11-20 23:11:48 -05006602 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006603 }
6604
James Smart05580562011-05-24 11:40:48 -04006605 lpfc_update_vport_wwn(vport);
James Smartda0436e2009-05-22 14:51:39 -04006606
6607 /* Update the fc_host data structures with new wwn. */
6608 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6609 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6610
James Smart8a9d2e82012-05-09 21:16:12 -04006611 /* update host els and scsi xri-sgl sizes and mappings */
6612 rc = lpfc_sli4_xri_sgl_update(phba);
6613 if (unlikely(rc)) {
6614 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6615 "1400 Failed to update xri-sgl size and "
6616 "mapping: %d\n", rc);
6617 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006618 }
6619
James Smart8a9d2e82012-05-09 21:16:12 -04006620 /* register the els sgl pool to the port */
6621 rc = lpfc_sli4_repost_els_sgl_list(phba);
6622 if (unlikely(rc)) {
6623 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6624 "0582 Error %d during els sgl post "
6625 "operation\n", rc);
6626 rc = -ENODEV;
6627 goto out_free_mbox;
6628 }
6629
6630 /* register the allocated scsi sgl pool to the port */
James Smartda0436e2009-05-22 14:51:39 -04006631 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6632 if (unlikely(rc)) {
James Smart6d368e52011-05-24 11:44:12 -04006633 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart6a9c52c2009-10-02 15:16:51 -04006634 "0383 Error %d during scsi sgl post "
6635 "operation\n", rc);
James Smartda0436e2009-05-22 14:51:39 -04006636 /* Some Scsi buffers were moved to the abort scsi list */
6637 /* A pci function reset will repost them */
6638 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006639 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006640 }
6641
6642 /* Post the rpi header region to the device. */
6643 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6644 if (unlikely(rc)) {
6645 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6646 "0393 Error %d during rpi post operation\n",
6647 rc);
6648 rc = -ENODEV;
James Smart76a95d72010-11-20 23:11:48 -05006649 goto out_free_mbox;
James Smartda0436e2009-05-22 14:51:39 -04006650 }
James Smart97f2ecf2012-03-01 22:35:23 -05006651 lpfc_sli4_node_prep(phba);
James Smartda0436e2009-05-22 14:51:39 -04006652
James Smart5350d872011-10-10 21:33:49 -04006653 /* Create all the SLI4 queues */
6654 rc = lpfc_sli4_queue_create(phba);
6655 if (rc) {
6656 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6657 "3089 Failed to allocate queues\n");
6658 rc = -ENODEV;
6659 goto out_stop_timers;
6660 }
James Smartda0436e2009-05-22 14:51:39 -04006661 /* Set up all the queues to the device */
6662 rc = lpfc_sli4_queue_setup(phba);
6663 if (unlikely(rc)) {
6664 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6665 "0381 Error %d during queue setup.\n ", rc);
James Smart5350d872011-10-10 21:33:49 -04006666 goto out_destroy_queue;
James Smartda0436e2009-05-22 14:51:39 -04006667 }
6668
6669 /* Arm the CQs and then EQs on device */
6670 lpfc_sli4_arm_cqeq_intr(phba);
6671
6672 /* Indicate device interrupt mode */
6673 phba->sli4_hba.intr_enable = 1;
6674
6675 /* Allow asynchronous mailbox command to go through */
6676 spin_lock_irq(&phba->hbalock);
6677 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6678 spin_unlock_irq(&phba->hbalock);
6679
6680 /* Post receive buffers to the device */
6681 lpfc_sli4_rb_setup(phba);
6682
James Smartfc2b9892010-02-26 14:15:29 -05006683 /* Reset HBA FCF states after HBA reset */
6684 phba->fcf.fcf_flag = 0;
6685 phba->fcf.current_rec.flag = 0;
6686
James Smartda0436e2009-05-22 14:51:39 -04006687 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04006688 mod_timer(&vport->els_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006689 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
James Smartda0436e2009-05-22 14:51:39 -04006690
6691 /* Start heart beat timer */
6692 mod_timer(&phba->hb_tmofunc,
James Smart256ec0d2013-04-17 20:14:58 -04006693 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
James Smartda0436e2009-05-22 14:51:39 -04006694 phba->hb_outstanding = 0;
6695 phba->last_completion_time = jiffies;
6696
6697 /* Start error attention (ERATT) polling timer */
James Smart256ec0d2013-04-17 20:14:58 -04006698 mod_timer(&phba->eratt_poll,
James Smart65791f12016-07-06 12:35:56 -07006699 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
James Smartda0436e2009-05-22 14:51:39 -04006700
James Smart75baf692010-06-08 18:31:21 -04006701 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6702 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6703 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6704 if (!rc) {
6705 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6706 "2829 This device supports "
6707 "Advanced Error Reporting (AER)\n");
6708 spin_lock_irq(&phba->hbalock);
6709 phba->hba_flag |= HBA_AER_ENABLED;
6710 spin_unlock_irq(&phba->hbalock);
6711 } else {
6712 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6713 "2830 This device does not support "
6714 "Advanced Error Reporting (AER)\n");
6715 phba->cfg_aer_support = 0;
6716 }
James Smart0a96e972011-07-22 18:37:28 -04006717 rc = 0;
James Smart75baf692010-06-08 18:31:21 -04006718 }
6719
James Smart76a95d72010-11-20 23:11:48 -05006720 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6721 /*
6722 * The FC Port needs to register FCFI (index 0)
6723 */
6724 lpfc_reg_fcfi(phba, mboxq);
6725 mboxq->vport = phba->pport;
6726 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart9589b0622011-04-16 11:03:17 -04006727 if (rc != MBX_SUCCESS)
James Smart76a95d72010-11-20 23:11:48 -05006728 goto out_unset_queue;
James Smart9589b0622011-04-16 11:03:17 -04006729 rc = 0;
6730 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6731 &mboxq->u.mqe.un.reg_fcfi);
James Smart026abb82011-12-13 13:20:45 -05006732
6733 /* Check if the port is configured to be disabled */
6734 lpfc_sli_read_link_ste(phba);
James Smart76a95d72010-11-20 23:11:48 -05006735 }
James Smart026abb82011-12-13 13:20:45 -05006736
James Smartda0436e2009-05-22 14:51:39 -04006737 /*
6738 * The port is ready, set the host's link state to LINK_DOWN
6739 * in preparation for link interrupts.
6740 */
James Smartda0436e2009-05-22 14:51:39 -04006741 spin_lock_irq(&phba->hbalock);
6742 phba->link_state = LPFC_LINK_DOWN;
6743 spin_unlock_irq(&phba->hbalock);
James Smart026abb82011-12-13 13:20:45 -05006744 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6745 (phba->hba_flag & LINK_DISABLED)) {
6746 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6747 "3103 Adapter Link is disabled.\n");
6748 lpfc_down_link(phba, mboxq);
6749 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6750 if (rc != MBX_SUCCESS) {
6751 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6752 "3104 Adapter failed to issue "
6753 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6754 goto out_unset_queue;
6755 }
6756 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
James Smart1b511972011-12-13 13:23:09 -05006757 /* don't perform init_link on SLI4 FC port loopback test */
6758 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6759 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6760 if (rc)
6761 goto out_unset_queue;
6762 }
James Smart5350d872011-10-10 21:33:49 -04006763 }
6764 mempool_free(mboxq, phba->mbox_mem_pool);
6765 return rc;
James Smart76a95d72010-11-20 23:11:48 -05006766out_unset_queue:
James Smartda0436e2009-05-22 14:51:39 -04006767 /* Unset all the queues set up in this routine when error out */
James Smart5350d872011-10-10 21:33:49 -04006768 lpfc_sli4_queue_unset(phba);
6769out_destroy_queue:
6770 lpfc_sli4_queue_destroy(phba);
James Smartda0436e2009-05-22 14:51:39 -04006771out_stop_timers:
James Smart5350d872011-10-10 21:33:49 -04006772 lpfc_stop_hba_timers(phba);
James Smartda0436e2009-05-22 14:51:39 -04006773out_free_mbox:
6774 mempool_free(mboxq, phba->mbox_mem_pool);
6775 return rc;
6776}
James Smarte59058c2008-08-24 21:49:00 -04006777
6778/**
James Smart3621a712009-04-06 18:47:14 -04006779 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04006780 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05006781 *
James Smarte59058c2008-08-24 21:49:00 -04006782 * This is the callback function for mailbox timer. The mailbox
6783 * timer is armed when a new mailbox command is issued and the timer
6784 * is deleted when the mailbox complete. The function is called by
6785 * the kernel timer code when a mailbox does not complete within
6786 * expected time. This function wakes up the worker thread to
6787 * process the mailbox timeout and returns. All the processing is
6788 * done by the worker thread function lpfc_mbox_timeout_handler.
6789 **/
dea31012005-04-17 16:05:31 -05006790void
6791lpfc_mbox_timeout(unsigned long ptr)
6792{
James Smart92d7f7b2007-06-17 19:56:38 -05006793 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05006794 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05006795 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05006796
James Smart2e0fef82007-06-17 19:56:36 -05006797 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05006798 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05006799 if (!tmo_posted)
6800 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6801 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6802
James Smart5e9d9b82008-06-14 22:52:53 -04006803 if (!tmo_posted)
6804 lpfc_worker_wake_up(phba);
6805 return;
dea31012005-04-17 16:05:31 -05006806}
6807
James Smarte8d3c3b2013-10-10 12:21:30 -04006808/**
6809 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6810 * are pending
6811 * @phba: Pointer to HBA context object.
6812 *
6813 * This function checks if any mailbox completions are present on the mailbox
6814 * completion queue.
6815 **/
Nicholas Krause3bb11fc2015-08-31 16:48:13 -04006816static bool
James Smarte8d3c3b2013-10-10 12:21:30 -04006817lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6818{
6819
6820 uint32_t idx;
6821 struct lpfc_queue *mcq;
6822 struct lpfc_mcqe *mcqe;
6823 bool pending_completions = false;
6824
6825 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6826 return false;
6827
6828 /* Check for completions on mailbox completion queue */
6829
6830 mcq = phba->sli4_hba.mbx_cq;
6831 idx = mcq->hba_index;
6832 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6833 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6834 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6835 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6836 pending_completions = true;
6837 break;
6838 }
6839 idx = (idx + 1) % mcq->entry_count;
6840 if (mcq->hba_index == idx)
6841 break;
6842 }
6843 return pending_completions;
6844
6845}
6846
6847/**
6848 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6849 * that were missed.
6850 * @phba: Pointer to HBA context object.
6851 *
6852 * For sli4, it is possible to miss an interrupt. As such mbox completions
6853 * maybe missed causing erroneous mailbox timeouts to occur. This function
6854 * checks to see if mbox completions are on the mailbox completion queue
6855 * and will process all the completions associated with the eq for the
6856 * mailbox completion queue.
6857 **/
6858bool
6859lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6860{
6861
6862 uint32_t eqidx;
6863 struct lpfc_queue *fpeq = NULL;
6864 struct lpfc_eqe *eqe;
6865 bool mbox_pending;
6866
6867 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6868 return false;
6869
6870 /* Find the eq associated with the mcq */
6871
6872 if (phba->sli4_hba.hba_eq)
6873 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6874 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6875 phba->sli4_hba.mbx_cq->assoc_qid) {
6876 fpeq = phba->sli4_hba.hba_eq[eqidx];
6877 break;
6878 }
6879 if (!fpeq)
6880 return false;
6881
6882 /* Turn off interrupts from this EQ */
6883
6884 lpfc_sli4_eq_clr_intr(fpeq);
6885
6886 /* Check to see if a mbox completion is pending */
6887
6888 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6889
6890 /*
6891 * If a mbox completion is pending, process all the events on EQ
6892 * associated with the mbox completion queue (this could include
6893 * mailbox commands, async events, els commands, receive queue data
6894 * and fcp commands)
6895 */
6896
6897 if (mbox_pending)
6898 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6899 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6900 fpeq->EQ_processed++;
6901 }
6902
6903 /* Always clear and re-arm the EQ */
6904
6905 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6906
6907 return mbox_pending;
6908
6909}
James Smarte59058c2008-08-24 21:49:00 -04006910
6911/**
James Smart3621a712009-04-06 18:47:14 -04006912 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04006913 * @phba: Pointer to HBA context object.
6914 *
6915 * This function is called from worker thread when a mailbox command times out.
6916 * The caller is not required to hold any locks. This function will reset the
6917 * HBA and recover all the pending commands.
6918 **/
dea31012005-04-17 16:05:31 -05006919void
6920lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6921{
James Smart2e0fef82007-06-17 19:56:36 -05006922 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smarteb016562014-09-03 12:58:06 -04006923 MAILBOX_t *mb = NULL;
6924
James Smart1dcb58e2007-04-25 09:51:30 -04006925 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006926
James Smarte8d3c3b2013-10-10 12:21:30 -04006927 /* If the mailbox completed, process the completion and return */
6928 if (lpfc_sli4_process_missed_mbox_completions(phba))
6929 return;
6930
James Smarteb016562014-09-03 12:58:06 -04006931 if (pmbox != NULL)
6932 mb = &pmbox->u.mb;
James Smarta257bf92009-04-06 18:48:10 -04006933 /* Check the pmbox pointer first. There is a race condition
6934 * between the mbox timeout handler getting executed in the
6935 * worklist and the mailbox actually completing. When this
6936 * race condition occurs, the mbox_active will be NULL.
6937 */
6938 spin_lock_irq(&phba->hbalock);
6939 if (pmbox == NULL) {
6940 lpfc_printf_log(phba, KERN_WARNING,
6941 LOG_MBOX | LOG_SLI,
6942 "0353 Active Mailbox cleared - mailbox timeout "
6943 "exiting\n");
6944 spin_unlock_irq(&phba->hbalock);
6945 return;
6946 }
6947
dea31012005-04-17 16:05:31 -05006948 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05006949 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006950 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05006951 mb->mbxCommand,
6952 phba->pport->port_state,
6953 phba->sli.sli_flag,
6954 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04006955 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006956
James Smart1dcb58e2007-04-25 09:51:30 -04006957 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6958 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006959 * it to fail all outstanding SCSI IO.
James Smart1dcb58e2007-04-25 09:51:30 -04006960 */
James Smart2e0fef82007-06-17 19:56:36 -05006961 spin_lock_irq(&phba->pport->work_port_lock);
6962 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6963 spin_unlock_irq(&phba->pport->work_port_lock);
6964 spin_lock_irq(&phba->hbalock);
6965 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04006966 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05006967 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04006968
James Smartdb55fba2014-04-04 13:52:02 -04006969 lpfc_sli_abort_fcp_rings(phba);
James Smart1dcb58e2007-04-25 09:51:30 -04006970
6971 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006972 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04006973
6974 /* Reset the HBA device */
6975 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05006976}
6977
James Smarte59058c2008-08-24 21:49:00 -04006978/**
James Smart3772a992009-05-22 14:50:54 -04006979 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04006980 * @phba: Pointer to HBA context object.
6981 * @pmbox: Pointer to mailbox object.
6982 * @flag: Flag indicating how the mailbox need to be processed.
6983 *
6984 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04006985 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6986 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04006987 * The mailbox command can be submitted in polling mode, in which case
6988 * this function will wait in a polling loop for the completion of the
6989 * mailbox.
6990 * If the mailbox is submitted in no_wait mode (not polling) the
6991 * function will submit the command and returns immediately without waiting
6992 * for the mailbox completion. The no_wait is supported only when HBA
6993 * is in SLI2/SLI3 mode - interrupts are enabled.
6994 * The SLI interface allows only one mailbox pending at a time. If the
6995 * mailbox is issued in polling mode and there is already a mailbox
6996 * pending, then the function will return an error. If the mailbox is issued
6997 * in NO_WAIT mode and there is a mailbox pending already, the function
6998 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6999 * The sli layer owns the mailbox object until the completion of mailbox
7000 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
7001 * return codes the caller owns the mailbox command after the return of
7002 * the function.
7003 **/
James Smart3772a992009-05-22 14:50:54 -04007004static int
7005lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
7006 uint32_t flag)
dea31012005-04-17 16:05:31 -05007007{
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007008 MAILBOX_t *mbx;
James Smart2e0fef82007-06-17 19:56:36 -05007009 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05007010 uint32_t status, evtctr;
James Smart9940b972011-03-11 16:06:12 -05007011 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -05007012 int i;
James Smart09372822008-01-11 01:52:54 -05007013 unsigned long timeout;
dea31012005-04-17 16:05:31 -05007014 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04007015 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05007016 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04007017 int processing_queue = 0;
7018
7019 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7020 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04007021 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04007022 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04007023 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7024 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7025 return MBX_SUCCESS;
7026 }
James Smart58da1ff2008-04-07 10:15:56 -04007027 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04007028 pmbox = lpfc_mbox_get(phba);
7029 if (!pmbox) {
7030 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7031 return MBX_SUCCESS;
7032 }
7033 }
dea31012005-04-17 16:05:31 -05007034
James Smarted957682007-06-17 19:56:37 -05007035 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05007036 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05007037 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04007038 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05007039 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05007040 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04007041 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04007042 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05007043 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04007044 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05007045 }
7046 }
7047
Linas Vepstas8d63f372007-02-14 14:28:36 -06007048 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04007049 if (unlikely(pci_channel_offline(phba->pcidev))) {
7050 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7051 goto out_not_finished;
7052 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06007053
James Smarta257bf92009-04-06 18:48:10 -04007054 /* If HBA has a deferred error attention, fail the iocb. */
7055 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7056 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7057 goto out_not_finished;
7058 }
7059
dea31012005-04-17 16:05:31 -05007060 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05007061
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007062 mbx = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05007063 status = MBX_SUCCESS;
7064
James Smart2e0fef82007-06-17 19:56:36 -05007065 if (phba->link_state == LPFC_HBA_ERROR) {
7066 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05007067
7068 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007069 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7070 "(%d):0311 Mailbox command x%x cannot "
7071 "issue Data: x%x x%x\n",
7072 pmbox->vport ? pmbox->vport->vpi : 0,
7073 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007074 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05007075 }
7076
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007077 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
James Smart9940b972011-03-11 16:06:12 -05007078 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7079 !(hc_copy & HC_MBINT_ENA)) {
7080 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7081 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart3772a992009-05-22 14:50:54 -04007082 "(%d):2528 Mailbox command x%x cannot "
7083 "issue Data: x%x x%x\n",
7084 pmbox->vport ? pmbox->vport->vpi : 0,
7085 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart9940b972011-03-11 16:06:12 -05007086 goto out_not_finished;
7087 }
James Smart92908312006-03-07 15:04:13 -05007088 }
7089
dea31012005-04-17 16:05:31 -05007090 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7091 /* Polling for a mbox command when another one is already active
7092 * is not allowed in SLI. Also, the driver must have established
7093 * SLI2 mode to queue and process multiple mbox commands.
7094 */
7095
7096 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05007097 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007098
7099 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007100 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7101 "(%d):2529 Mailbox command x%x "
7102 "cannot issue Data: x%x x%x\n",
7103 pmbox->vport ? pmbox->vport->vpi : 0,
7104 pmbox->u.mb.mbxCommand,
7105 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007106 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007107 }
7108
James Smart3772a992009-05-22 14:50:54 -04007109 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05007110 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007111 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007112 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7113 "(%d):2530 Mailbox command x%x "
7114 "cannot issue Data: x%x x%x\n",
7115 pmbox->vport ? pmbox->vport->vpi : 0,
7116 pmbox->u.mb.mbxCommand,
7117 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007118 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007119 }
7120
dea31012005-04-17 16:05:31 -05007121 /* Another mailbox command is still being processed, queue this
7122 * command to be processed later.
7123 */
7124 lpfc_mbox_put(phba, pmbox);
7125
7126 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05007127 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007128 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007129 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05007130 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007131 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007132 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007133
7134 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05007135 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007136
James Smart858c9f62007-06-17 19:56:39 -05007137 if (pmbox->vport) {
7138 lpfc_debugfs_disc_trc(pmbox->vport,
7139 LPFC_DISC_TRC_MBOX_VPORT,
7140 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007141 (uint32_t)mbx->mbxCommand,
7142 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007143 }
7144 else {
7145 lpfc_debugfs_disc_trc(phba->pport,
7146 LPFC_DISC_TRC_MBOX,
7147 "MBOX Bsy: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007148 (uint32_t)mbx->mbxCommand,
7149 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007150 }
7151
James Smart2e0fef82007-06-17 19:56:36 -05007152 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05007153 }
7154
dea31012005-04-17 16:05:31 -05007155 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7156
7157 /* If we are not polling, we MUST be in SLI2 mode */
7158 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04007159 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007160 (mbx->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05007161 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007162 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05007163 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04007164 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7165 "(%d):2531 Mailbox command x%x "
7166 "cannot issue Data: x%x x%x\n",
7167 pmbox->vport ? pmbox->vport->vpi : 0,
7168 pmbox->u.mb.mbxCommand,
7169 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04007170 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007171 }
7172 /* timeout active mbox command */
James Smart256ec0d2013-04-17 20:14:58 -04007173 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7174 1000);
7175 mod_timer(&psli->mbox_tmo, jiffies + timeout);
dea31012005-04-17 16:05:31 -05007176 }
7177
7178 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05007179 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007180 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05007181 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007182 pmbox->vport ? pmbox->vport->vpi : 0,
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007183 mbx->mbxCommand, phba->pport->port_state,
James Smart92d7f7b2007-06-17 19:56:38 -05007184 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05007185
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007186 if (mbx->mbxCommand != MBX_HEARTBEAT) {
James Smart858c9f62007-06-17 19:56:39 -05007187 if (pmbox->vport) {
7188 lpfc_debugfs_disc_trc(pmbox->vport,
7189 LPFC_DISC_TRC_MBOX_VPORT,
7190 "MBOX Send vport: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007191 (uint32_t)mbx->mbxCommand,
7192 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007193 }
7194 else {
7195 lpfc_debugfs_disc_trc(phba->pport,
7196 LPFC_DISC_TRC_MBOX,
7197 "MBOX Send: cmd:x%x mb:x%x x%x",
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007198 (uint32_t)mbx->mbxCommand,
7199 mbx->un.varWords[0], mbx->un.varWords[1]);
James Smart858c9f62007-06-17 19:56:39 -05007200 }
7201 }
7202
dea31012005-04-17 16:05:31 -05007203 psli->slistat.mbox_cmd++;
7204 evtctr = psli->slistat.mbox_event;
7205
7206 /* next set own bit for the adapter and copy over command word */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007207 mbx->mbxOwner = OWN_CHIP;
dea31012005-04-17 16:05:31 -05007208
James Smart3772a992009-05-22 14:50:54 -04007209 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
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 = (uint8_t *)phba->mbox_ext
7214 - (uint8_t *)phba->mbox;
7215 }
7216
7217 /* Copy the mailbox extension data */
7218 if (pmbox->in_ext_byte_len && pmbox->context2) {
7219 lpfc_sli_pcimem_bcopy(pmbox->context2,
7220 (uint8_t *)phba->mbox_ext,
7221 pmbox->in_ext_byte_len);
7222 }
7223 /* Copy command data to host SLIM area */
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 } else {
James Smart7a470272010-03-15 11:25:20 -04007226 /* Populate mbox extension offset word. */
7227 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007228 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
James Smart7a470272010-03-15 11:25:20 -04007229 = MAILBOX_HBA_EXT_OFFSET;
7230
7231 /* Copy the mailbox extension data */
7232 if (pmbox->in_ext_byte_len && pmbox->context2) {
7233 lpfc_memcpy_to_slim(phba->MBslimaddr +
7234 MAILBOX_HBA_EXT_OFFSET,
7235 pmbox->context2, pmbox->in_ext_byte_len);
7236
7237 }
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007238 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007239 /* copy command data into host mbox for cmpl */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007240 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05007241 }
7242
7243 /* First copy mbox command data to HBA SLIM, skip past first
7244 word */
7245 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007246 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
dea31012005-04-17 16:05:31 -05007247 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7248
7249 /* Next copy over first word, with mbxOwner set */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007250 ldata = *((uint32_t *)mbx);
dea31012005-04-17 16:05:31 -05007251 to_slim = phba->MBslimaddr;
7252 writel(ldata, to_slim);
7253 readl(to_slim); /* flush */
7254
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007255 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007256 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04007257 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007258 }
7259 }
7260
7261 wmb();
dea31012005-04-17 16:05:31 -05007262
7263 switch (flag) {
7264 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05007265 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05007266 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05007267 /* Interrupt board to do it */
7268 writel(CA_MBATT, phba->CAregaddr);
7269 readl(phba->CAregaddr); /* flush */
7270 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05007271 break;
7272
7273 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05007274 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05007275 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05007276 /* Interrupt board to do it */
7277 writel(CA_MBATT, phba->CAregaddr);
7278 readl(phba->CAregaddr); /* flush */
7279
James Smart3772a992009-05-22 14:50:54 -04007280 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007281 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04007282 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007283 word0 = le32_to_cpu(word0);
7284 } else {
7285 /* First read mbox status word */
James Smart9940b972011-03-11 16:06:12 -05007286 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7287 spin_unlock_irqrestore(&phba->hbalock,
7288 drvr_flag);
7289 goto out_not_finished;
7290 }
dea31012005-04-17 16:05:31 -05007291 }
7292
7293 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007294 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7295 spin_unlock_irqrestore(&phba->hbalock,
7296 drvr_flag);
7297 goto out_not_finished;
7298 }
James Smarta183a152011-10-10 21:32:43 -04007299 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7300 1000) + jiffies;
James Smart09372822008-01-11 01:52:54 -05007301 i = 0;
dea31012005-04-17 16:05:31 -05007302 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05007303 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7304 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05007305 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05007306 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05007307 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05007308 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05007309 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04007310 goto out_not_finished;
dea31012005-04-17 16:05:31 -05007311 }
7312
7313 /* Check if we took a mbox interrupt while we were
7314 polling */
7315 if (((word0 & OWN_CHIP) != OWN_CHIP)
7316 && (evtctr != psli->slistat.mbox_event))
7317 break;
7318
James Smart09372822008-01-11 01:52:54 -05007319 if (i++ > 10) {
7320 spin_unlock_irqrestore(&phba->hbalock,
7321 drvr_flag);
7322 msleep(1);
7323 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7324 }
dea31012005-04-17 16:05:31 -05007325
James Smart3772a992009-05-22 14:50:54 -04007326 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007327 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04007328 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05007329 word0 = le32_to_cpu(word0);
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007330 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05007331 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04007332 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05007333 /* Check real SLIM for any errors */
7334 slimword0 = readl(phba->MBslimaddr);
7335 slimmb = (MAILBOX_t *) & slimword0;
7336 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7337 && slimmb->mbxStatus) {
7338 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04007339 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05007340 word0 = slimword0;
7341 }
7342 }
7343 } else {
7344 /* First copy command data */
7345 word0 = readl(phba->MBslimaddr);
7346 }
7347 /* Read the HBA Host Attention Register */
James Smart9940b972011-03-11 16:06:12 -05007348 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7349 spin_unlock_irqrestore(&phba->hbalock,
7350 drvr_flag);
7351 goto out_not_finished;
7352 }
dea31012005-04-17 16:05:31 -05007353 }
7354
James Smart3772a992009-05-22 14:50:54 -04007355 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05007356 /* copy results back to user */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007357 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007358 /* Copy the mailbox extension data */
7359 if (pmbox->out_ext_byte_len && pmbox->context2) {
7360 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7361 pmbox->context2,
7362 pmbox->out_ext_byte_len);
7363 }
dea31012005-04-17 16:05:31 -05007364 } else {
7365 /* First copy command data */
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007366 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
dea31012005-04-17 16:05:31 -05007367 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -04007368 /* Copy the mailbox extension data */
7369 if (pmbox->out_ext_byte_len && pmbox->context2) {
7370 lpfc_memcpy_from_slim(pmbox->context2,
7371 phba->MBslimaddr +
7372 MAILBOX_HBA_EXT_OFFSET,
7373 pmbox->out_ext_byte_len);
dea31012005-04-17 16:05:31 -05007374 }
7375 }
7376
7377 writel(HA_MBATT, phba->HAregaddr);
7378 readl(phba->HAregaddr); /* flush */
7379
7380 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
Randy Dunlapbf07bde2013-02-22 10:23:54 -08007381 status = mbx->mbxStatus;
dea31012005-04-17 16:05:31 -05007382 }
7383
James Smart2e0fef82007-06-17 19:56:36 -05007384 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7385 return status;
James Smart58da1ff2008-04-07 10:15:56 -04007386
7387out_not_finished:
7388 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04007389 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04007390 lpfc_mbox_cmpl_put(phba, pmbox);
7391 }
7392 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007393}
7394
James Smarte59058c2008-08-24 21:49:00 -04007395/**
James Smartf1126682009-06-10 17:22:44 -04007396 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7397 * @phba: Pointer to HBA context object.
7398 *
7399 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7400 * the driver internal pending mailbox queue. It will then try to wait out the
7401 * possible outstanding mailbox command before return.
7402 *
7403 * Returns:
7404 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7405 * the outstanding mailbox command timed out.
7406 **/
7407static int
7408lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7409{
7410 struct lpfc_sli *psli = &phba->sli;
James Smartf1126682009-06-10 17:22:44 -04007411 int rc = 0;
James Smarta183a152011-10-10 21:32:43 -04007412 unsigned long timeout = 0;
James Smartf1126682009-06-10 17:22:44 -04007413
7414 /* Mark the asynchronous mailbox command posting as blocked */
7415 spin_lock_irq(&phba->hbalock);
7416 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smartf1126682009-06-10 17:22:44 -04007417 /* Determine how long we might wait for the active mailbox
7418 * command to be gracefully completed by firmware.
7419 */
James Smarta183a152011-10-10 21:32:43 -04007420 if (phba->sli.mbox_active)
7421 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7422 phba->sli.mbox_active) *
7423 1000) + jiffies;
7424 spin_unlock_irq(&phba->hbalock);
7425
James Smarte8d3c3b2013-10-10 12:21:30 -04007426 /* Make sure the mailbox is really active */
7427 if (timeout)
7428 lpfc_sli4_process_missed_mbox_completions(phba);
7429
James Smartf1126682009-06-10 17:22:44 -04007430 /* Wait for the outstnading mailbox command to complete */
7431 while (phba->sli.mbox_active) {
7432 /* Check active mailbox complete status every 2ms */
7433 msleep(2);
7434 if (time_after(jiffies, timeout)) {
7435 /* Timeout, marked the outstanding cmd not complete */
7436 rc = 1;
7437 break;
7438 }
7439 }
7440
7441 /* Can not cleanly block async mailbox command, fails it */
7442 if (rc) {
7443 spin_lock_irq(&phba->hbalock);
7444 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7445 spin_unlock_irq(&phba->hbalock);
7446 }
7447 return rc;
7448}
7449
7450/**
7451 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7452 * @phba: Pointer to HBA context object.
7453 *
7454 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7455 * commands from the driver internal pending mailbox queue. It makes sure
7456 * that there is no outstanding mailbox command before resuming posting
7457 * asynchronous mailbox commands. If, for any reason, there is outstanding
7458 * mailbox command, it will try to wait it out before resuming asynchronous
7459 * mailbox command posting.
7460 **/
7461static void
7462lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7463{
7464 struct lpfc_sli *psli = &phba->sli;
7465
7466 spin_lock_irq(&phba->hbalock);
7467 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7468 /* Asynchronous mailbox posting is not blocked, do nothing */
7469 spin_unlock_irq(&phba->hbalock);
7470 return;
7471 }
7472
7473 /* Outstanding synchronous mailbox command is guaranteed to be done,
7474 * successful or timeout, after timing-out the outstanding mailbox
7475 * command shall always be removed, so just unblock posting async
7476 * mailbox command and resume
7477 */
7478 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7479 spin_unlock_irq(&phba->hbalock);
7480
7481 /* wake up worker thread to post asynchronlous mailbox command */
7482 lpfc_worker_wake_up(phba);
7483}
7484
7485/**
James Smart2d843ed2012-09-29 11:29:06 -04007486 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7487 * @phba: Pointer to HBA context object.
7488 * @mboxq: Pointer to mailbox object.
7489 *
7490 * The function waits for the bootstrap mailbox register ready bit from
7491 * port for twice the regular mailbox command timeout value.
7492 *
7493 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7494 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7495 **/
7496static int
7497lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7498{
7499 uint32_t db_ready;
7500 unsigned long timeout;
7501 struct lpfc_register bmbx_reg;
7502
7503 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7504 * 1000) + jiffies;
7505
7506 do {
7507 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7508 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7509 if (!db_ready)
7510 msleep(2);
7511
7512 if (time_after(jiffies, timeout))
7513 return MBXERR_ERROR;
7514 } while (!db_ready);
7515
7516 return 0;
7517}
7518
7519/**
James Smartda0436e2009-05-22 14:51:39 -04007520 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7521 * @phba: Pointer to HBA context object.
7522 * @mboxq: Pointer to mailbox object.
7523 *
7524 * The function posts a mailbox to the port. The mailbox is expected
7525 * to be comletely filled in and ready for the port to operate on it.
7526 * This routine executes a synchronous completion operation on the
7527 * mailbox by polling for its completion.
7528 *
7529 * The caller must not be holding any locks when calling this routine.
7530 *
7531 * Returns:
7532 * MBX_SUCCESS - mailbox posted successfully
7533 * Any of the MBX error values.
7534 **/
7535static int
7536lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7537{
7538 int rc = MBX_SUCCESS;
7539 unsigned long iflag;
James Smartda0436e2009-05-22 14:51:39 -04007540 uint32_t mcqe_status;
7541 uint32_t mbx_cmnd;
James Smartda0436e2009-05-22 14:51:39 -04007542 struct lpfc_sli *psli = &phba->sli;
7543 struct lpfc_mqe *mb = &mboxq->u.mqe;
7544 struct lpfc_bmbx_create *mbox_rgn;
7545 struct dma_address *dma_address;
James Smartda0436e2009-05-22 14:51:39 -04007546
7547 /*
7548 * Only one mailbox can be active to the bootstrap mailbox region
7549 * at a time and there is no queueing provided.
7550 */
7551 spin_lock_irqsave(&phba->hbalock, iflag);
7552 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7553 spin_unlock_irqrestore(&phba->hbalock, iflag);
7554 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007555 "(%d):2532 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007556 "cannot issue Data: x%x x%x\n",
7557 mboxq->vport ? mboxq->vport->vpi : 0,
7558 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007559 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7560 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007561 psli->sli_flag, MBX_POLL);
7562 return MBXERR_ERROR;
7563 }
7564 /* The server grabs the token and owns it until release */
7565 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7566 phba->sli.mbox_active = mboxq;
7567 spin_unlock_irqrestore(&phba->hbalock, iflag);
7568
James Smart2d843ed2012-09-29 11:29:06 -04007569 /* wait for bootstrap mbox register for readyness */
7570 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7571 if (rc)
7572 goto exit;
7573
James Smartda0436e2009-05-22 14:51:39 -04007574 /*
7575 * Initialize the bootstrap memory region to avoid stale data areas
7576 * in the mailbox post. Then copy the caller's mailbox contents to
7577 * the bmbx mailbox region.
7578 */
7579 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7580 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7581 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7582 sizeof(struct lpfc_mqe));
7583
7584 /* Post the high mailbox dma address to the port and wait for ready. */
7585 dma_address = &phba->sli4_hba.bmbx.dma_address;
7586 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7587
James Smart2d843ed2012-09-29 11:29:06 -04007588 /* wait for bootstrap mbox register for hi-address write done */
7589 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7590 if (rc)
7591 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007592
7593 /* Post the low mailbox dma address to the port. */
7594 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
James Smartda0436e2009-05-22 14:51:39 -04007595
James Smart2d843ed2012-09-29 11:29:06 -04007596 /* wait for bootstrap mbox register for low address write done */
7597 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7598 if (rc)
7599 goto exit;
James Smartda0436e2009-05-22 14:51:39 -04007600
7601 /*
7602 * Read the CQ to ensure the mailbox has completed.
7603 * If so, update the mailbox status so that the upper layers
7604 * can complete the request normally.
7605 */
7606 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7607 sizeof(struct lpfc_mqe));
7608 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7609 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7610 sizeof(struct lpfc_mcqe));
7611 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
James Smart05580562011-05-24 11:40:48 -04007612 /*
7613 * When the CQE status indicates a failure and the mailbox status
7614 * indicates success then copy the CQE status into the mailbox status
7615 * (and prefix it with x4000).
7616 */
James Smartda0436e2009-05-22 14:51:39 -04007617 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
James Smart05580562011-05-24 11:40:48 -04007618 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7619 bf_set(lpfc_mqe_status, mb,
7620 (LPFC_MBX_ERROR_RANGE | mcqe_status));
James Smartda0436e2009-05-22 14:51:39 -04007621 rc = MBXERR_ERROR;
James Smartd7c47992010-06-08 18:31:54 -04007622 } else
7623 lpfc_sli4_swap_str(phba, mboxq);
James Smartda0436e2009-05-22 14:51:39 -04007624
7625 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007626 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
James Smartda0436e2009-05-22 14:51:39 -04007627 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7628 " x%x x%x CQ: x%x x%x x%x x%x\n",
James Smarta183a152011-10-10 21:32:43 -04007629 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7630 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7631 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007632 bf_get(lpfc_mqe_status, mb),
7633 mb->un.mb_words[0], mb->un.mb_words[1],
7634 mb->un.mb_words[2], mb->un.mb_words[3],
7635 mb->un.mb_words[4], mb->un.mb_words[5],
7636 mb->un.mb_words[6], mb->un.mb_words[7],
7637 mb->un.mb_words[8], mb->un.mb_words[9],
7638 mb->un.mb_words[10], mb->un.mb_words[11],
7639 mb->un.mb_words[12], mboxq->mcqe.word0,
7640 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7641 mboxq->mcqe.trailer);
7642exit:
7643 /* We are holding the token, no needed for lock when release */
7644 spin_lock_irqsave(&phba->hbalock, iflag);
7645 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7646 phba->sli.mbox_active = NULL;
7647 spin_unlock_irqrestore(&phba->hbalock, iflag);
7648 return rc;
7649}
7650
7651/**
7652 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7653 * @phba: Pointer to HBA context object.
7654 * @pmbox: Pointer to mailbox object.
7655 * @flag: Flag indicating how the mailbox need to be processed.
7656 *
7657 * This function is called by discovery code and HBA management code to submit
7658 * a mailbox command to firmware with SLI-4 interface spec.
7659 *
7660 * Return codes the caller owns the mailbox command after the return of the
7661 * function.
7662 **/
7663static int
7664lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7665 uint32_t flag)
7666{
7667 struct lpfc_sli *psli = &phba->sli;
7668 unsigned long iflags;
7669 int rc;
7670
James Smartb76f2dc2011-07-22 18:37:42 -04007671 /* dump from issue mailbox command if setup */
7672 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7673
James Smart8fa38512009-07-19 10:01:03 -04007674 rc = lpfc_mbox_dev_check(phba);
7675 if (unlikely(rc)) {
7676 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007677 "(%d):2544 Mailbox command x%x (x%x/x%x) "
James Smart8fa38512009-07-19 10:01:03 -04007678 "cannot issue Data: x%x x%x\n",
7679 mboxq->vport ? mboxq->vport->vpi : 0,
7680 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007681 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7682 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smart8fa38512009-07-19 10:01:03 -04007683 psli->sli_flag, flag);
7684 goto out_not_finished;
7685 }
7686
James Smartda0436e2009-05-22 14:51:39 -04007687 /* Detect polling mode and jump to a handler */
7688 if (!phba->sli4_hba.intr_enable) {
7689 if (flag == MBX_POLL)
7690 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7691 else
7692 rc = -EIO;
7693 if (rc != MBX_SUCCESS)
James Smart05580562011-05-24 11:40:48 -04007694 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
James Smartda0436e2009-05-22 14:51:39 -04007695 "(%d):2541 Mailbox command x%x "
James Smartcc459f12012-05-09 21:18:30 -04007696 "(x%x/x%x) failure: "
7697 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7698 "Data: x%x x%x\n,",
James Smartda0436e2009-05-22 14:51:39 -04007699 mboxq->vport ? mboxq->vport->vpi : 0,
7700 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007701 lpfc_sli_config_mbox_subsys_get(phba,
7702 mboxq),
7703 lpfc_sli_config_mbox_opcode_get(phba,
7704 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007705 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7706 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7707 bf_get(lpfc_mcqe_ext_status,
7708 &mboxq->mcqe),
James Smartda0436e2009-05-22 14:51:39 -04007709 psli->sli_flag, flag);
7710 return rc;
7711 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04007712 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7713 "(%d):2542 Try to issue mailbox command "
James Smarta183a152011-10-10 21:32:43 -04007714 "x%x (x%x/x%x) synchronously ahead of async"
James Smartf1126682009-06-10 17:22:44 -04007715 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007716 mboxq->vport ? mboxq->vport->vpi : 0,
7717 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007718 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7719 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007720 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007721 /* Try to block the asynchronous mailbox posting */
7722 rc = lpfc_sli4_async_mbox_block(phba);
7723 if (!rc) {
7724 /* Successfully blocked, now issue sync mbox cmd */
7725 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7726 if (rc != MBX_SUCCESS)
James Smartcc459f12012-05-09 21:18:30 -04007727 lpfc_printf_log(phba, KERN_WARNING,
James Smarta183a152011-10-10 21:32:43 -04007728 LOG_MBOX | LOG_SLI,
James Smartcc459f12012-05-09 21:18:30 -04007729 "(%d):2597 Sync Mailbox command "
7730 "x%x (x%x/x%x) failure: "
7731 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7732 "Data: x%x x%x\n,",
7733 mboxq->vport ? mboxq->vport->vpi : 0,
James Smarta183a152011-10-10 21:32:43 -04007734 mboxq->u.mb.mbxCommand,
7735 lpfc_sli_config_mbox_subsys_get(phba,
7736 mboxq),
7737 lpfc_sli_config_mbox_opcode_get(phba,
7738 mboxq),
James Smartcc459f12012-05-09 21:18:30 -04007739 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7740 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7741 bf_get(lpfc_mcqe_ext_status,
7742 &mboxq->mcqe),
James Smarta183a152011-10-10 21:32:43 -04007743 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04007744 /* Unblock the async mailbox posting afterward */
7745 lpfc_sli4_async_mbox_unblock(phba);
7746 }
7747 return rc;
James Smartda0436e2009-05-22 14:51:39 -04007748 }
7749
7750 /* Now, interrupt mode asynchrous mailbox command */
7751 rc = lpfc_mbox_cmd_check(phba, mboxq);
7752 if (rc) {
7753 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007754 "(%d):2543 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007755 "cannot issue Data: x%x x%x\n",
7756 mboxq->vport ? mboxq->vport->vpi : 0,
7757 mboxq->u.mb.mbxCommand,
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 psli->sli_flag, flag);
7761 goto out_not_finished;
7762 }
James Smartda0436e2009-05-22 14:51:39 -04007763
7764 /* Put the mailbox command to the driver internal FIFO */
7765 psli->slistat.mbox_busy++;
7766 spin_lock_irqsave(&phba->hbalock, iflags);
7767 lpfc_mbox_put(phba, mboxq);
7768 spin_unlock_irqrestore(&phba->hbalock, iflags);
7769 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7770 "(%d):0354 Mbox cmd issue - Enqueue Data: "
James Smarta183a152011-10-10 21:32:43 -04007771 "x%x (x%x/x%x) x%x x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04007772 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7773 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
James Smarta183a152011-10-10 21:32:43 -04007774 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7775 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007776 phba->pport->port_state,
7777 psli->sli_flag, MBX_NOWAIT);
7778 /* Wake up worker thread to transport mailbox command from head */
7779 lpfc_worker_wake_up(phba);
7780
7781 return MBX_BUSY;
7782
7783out_not_finished:
7784 return MBX_NOT_FINISHED;
7785}
7786
7787/**
7788 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7789 * @phba: Pointer to HBA context object.
7790 *
7791 * This function is called by worker thread to send a mailbox command to
7792 * SLI4 HBA firmware.
7793 *
7794 **/
7795int
7796lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7797{
7798 struct lpfc_sli *psli = &phba->sli;
7799 LPFC_MBOXQ_t *mboxq;
7800 int rc = MBX_SUCCESS;
7801 unsigned long iflags;
7802 struct lpfc_mqe *mqe;
7803 uint32_t mbx_cmnd;
7804
7805 /* Check interrupt mode before post async mailbox command */
7806 if (unlikely(!phba->sli4_hba.intr_enable))
7807 return MBX_NOT_FINISHED;
7808
7809 /* Check for mailbox command service token */
7810 spin_lock_irqsave(&phba->hbalock, iflags);
7811 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7812 spin_unlock_irqrestore(&phba->hbalock, iflags);
7813 return MBX_NOT_FINISHED;
7814 }
7815 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7816 spin_unlock_irqrestore(&phba->hbalock, iflags);
7817 return MBX_NOT_FINISHED;
7818 }
7819 if (unlikely(phba->sli.mbox_active)) {
7820 spin_unlock_irqrestore(&phba->hbalock, iflags);
7821 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7822 "0384 There is pending active mailbox cmd\n");
7823 return MBX_NOT_FINISHED;
7824 }
7825 /* Take the mailbox command service token */
7826 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7827
7828 /* Get the next mailbox command from head of queue */
7829 mboxq = lpfc_mbox_get(phba);
7830
7831 /* If no more mailbox command waiting for post, we're done */
7832 if (!mboxq) {
7833 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7834 spin_unlock_irqrestore(&phba->hbalock, iflags);
7835 return MBX_SUCCESS;
7836 }
7837 phba->sli.mbox_active = mboxq;
7838 spin_unlock_irqrestore(&phba->hbalock, iflags);
7839
7840 /* Check device readiness for posting mailbox command */
7841 rc = lpfc_mbox_dev_check(phba);
7842 if (unlikely(rc))
7843 /* Driver clean routine will clean up pending mailbox */
7844 goto out_not_finished;
7845
7846 /* Prepare the mbox command to be posted */
7847 mqe = &mboxq->u.mqe;
7848 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7849
7850 /* Start timer for the mbox_tmo and log some mailbox post messages */
7851 mod_timer(&psli->mbox_tmo, (jiffies +
James Smart256ec0d2013-04-17 20:14:58 -04007852 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
James Smartda0436e2009-05-22 14:51:39 -04007853
7854 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007855 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
James Smartda0436e2009-05-22 14:51:39 -04007856 "x%x x%x\n",
7857 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
James Smarta183a152011-10-10 21:32:43 -04007858 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7859 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007860 phba->pport->port_state, psli->sli_flag);
7861
7862 if (mbx_cmnd != MBX_HEARTBEAT) {
7863 if (mboxq->vport) {
7864 lpfc_debugfs_disc_trc(mboxq->vport,
7865 LPFC_DISC_TRC_MBOX_VPORT,
7866 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7867 mbx_cmnd, mqe->un.mb_words[0],
7868 mqe->un.mb_words[1]);
7869 } else {
7870 lpfc_debugfs_disc_trc(phba->pport,
7871 LPFC_DISC_TRC_MBOX,
7872 "MBOX Send: cmd:x%x mb:x%x x%x",
7873 mbx_cmnd, mqe->un.mb_words[0],
7874 mqe->un.mb_words[1]);
7875 }
7876 }
7877 psli->slistat.mbox_cmd++;
7878
7879 /* Post the mailbox command to the port */
7880 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7881 if (rc != MBX_SUCCESS) {
7882 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarta183a152011-10-10 21:32:43 -04007883 "(%d):2533 Mailbox command x%x (x%x/x%x) "
James Smartda0436e2009-05-22 14:51:39 -04007884 "cannot issue Data: x%x x%x\n",
7885 mboxq->vport ? mboxq->vport->vpi : 0,
7886 mboxq->u.mb.mbxCommand,
James Smarta183a152011-10-10 21:32:43 -04007887 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7888 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
James Smartda0436e2009-05-22 14:51:39 -04007889 psli->sli_flag, MBX_NOWAIT);
7890 goto out_not_finished;
7891 }
7892
7893 return rc;
7894
7895out_not_finished:
7896 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartd7069f02012-03-01 22:36:29 -05007897 if (phba->sli.mbox_active) {
7898 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7899 __lpfc_mbox_cmpl_put(phba, mboxq);
7900 /* Release the token */
7901 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7902 phba->sli.mbox_active = NULL;
7903 }
James Smartda0436e2009-05-22 14:51:39 -04007904 spin_unlock_irqrestore(&phba->hbalock, iflags);
7905
7906 return MBX_NOT_FINISHED;
7907}
7908
7909/**
7910 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7911 * @phba: Pointer to HBA context object.
7912 * @pmbox: Pointer to mailbox object.
7913 * @flag: Flag indicating how the mailbox need to be processed.
7914 *
7915 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7916 * the API jump table function pointer from the lpfc_hba struct.
7917 *
7918 * Return codes the caller owns the mailbox command after the return of the
7919 * function.
7920 **/
7921int
7922lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7923{
7924 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7925}
7926
7927/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007928 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
James Smartda0436e2009-05-22 14:51:39 -04007929 * @phba: The hba struct for which this call is being executed.
7930 * @dev_grp: The HBA PCI-Device group number.
7931 *
7932 * This routine sets up the mbox interface API function jump table in @phba
7933 * struct.
7934 * Returns: 0 - success, -ENODEV - failure.
7935 **/
7936int
7937lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7938{
7939
7940 switch (dev_grp) {
7941 case LPFC_PCI_DEV_LP:
7942 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7943 phba->lpfc_sli_handle_slow_ring_event =
7944 lpfc_sli_handle_slow_ring_event_s3;
7945 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7946 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7947 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7948 break;
7949 case LPFC_PCI_DEV_OC:
7950 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7951 phba->lpfc_sli_handle_slow_ring_event =
7952 lpfc_sli_handle_slow_ring_event_s4;
7953 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7954 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7955 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7956 break;
7957 default:
7958 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7959 "1420 Invalid HBA PCI-device group: 0x%x\n",
7960 dev_grp);
7961 return -ENODEV;
7962 break;
7963 }
7964 return 0;
7965}
7966
7967/**
James Smart3621a712009-04-06 18:47:14 -04007968 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04007969 * @phba: Pointer to HBA context object.
7970 * @pring: Pointer to driver SLI ring object.
7971 * @piocb: Pointer to address of newly added command iocb.
7972 *
7973 * This function is called with hbalock held to add a command
7974 * iocb to the txq when SLI layer cannot submit the command iocb
7975 * to the ring.
7976 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04007977void
James Smart92d7f7b2007-06-17 19:56:38 -05007978__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05007979 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05007980{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01007981 lockdep_assert_held(&phba->hbalock);
dea31012005-04-17 16:05:31 -05007982 /* Insert the caller's iocb in the txq tail for later processing. */
7983 list_add_tail(&piocb->list, &pring->txq);
dea31012005-04-17 16:05:31 -05007984}
7985
James Smarte59058c2008-08-24 21:49:00 -04007986/**
James Smart3621a712009-04-06 18:47:14 -04007987 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04007988 * @phba: Pointer to HBA context object.
7989 * @pring: Pointer to driver SLI ring object.
7990 * @piocb: Pointer to address of newly added command iocb.
7991 *
7992 * This function is called with hbalock held before a new
7993 * iocb is submitted to the firmware. This function checks
7994 * txq to flush the iocbs in txq to Firmware before
7995 * submitting new iocbs to the Firmware.
7996 * If there are iocbs in the txq which need to be submitted
7997 * to firmware, lpfc_sli_next_iocb returns the first element
7998 * of the txq after dequeuing it from txq.
7999 * If there is no iocb in the txq then the function will return
8000 * *piocb and *piocb is set to NULL. Caller needs to check
8001 * *piocb to find if there are more commands in the txq.
8002 **/
dea31012005-04-17 16:05:31 -05008003static struct lpfc_iocbq *
8004lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05008005 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05008006{
8007 struct lpfc_iocbq * nextiocb;
8008
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008009 lockdep_assert_held(&phba->hbalock);
8010
dea31012005-04-17 16:05:31 -05008011 nextiocb = lpfc_sli_ringtx_get(phba, pring);
8012 if (!nextiocb) {
8013 nextiocb = *piocb;
8014 *piocb = NULL;
8015 }
8016
8017 return nextiocb;
8018}
8019
James Smarte59058c2008-08-24 21:49:00 -04008020/**
James Smart3772a992009-05-22 14:50:54 -04008021 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04008022 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04008023 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04008024 * @piocb: Pointer to command iocb.
8025 * @flag: Flag indicating if this command can be put into txq.
8026 *
James Smart3772a992009-05-22 14:50:54 -04008027 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8028 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8029 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8030 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8031 * this function allows only iocbs for posting buffers. This function finds
8032 * next available slot in the command ring and posts the command to the
8033 * available slot and writes the port attention register to request HBA start
8034 * processing new iocb. If there is no slot available in the ring and
8035 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8036 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04008037 *
James Smart3772a992009-05-22 14:50:54 -04008038 * This function is called with hbalock held. The function will return success
8039 * after it successfully submit the iocb to firmware or after adding to the
8040 * txq.
James Smarte59058c2008-08-24 21:49:00 -04008041 **/
James Smart98c9ea52007-10-27 13:37:33 -04008042static int
James Smart3772a992009-05-22 14:50:54 -04008043__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05008044 struct lpfc_iocbq *piocb, uint32_t flag)
8045{
8046 struct lpfc_iocbq *nextiocb;
8047 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04008048 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05008049
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008050 lockdep_assert_held(&phba->hbalock);
8051
James Smart92d7f7b2007-06-17 19:56:38 -05008052 if (piocb->iocb_cmpl && (!piocb->vport) &&
8053 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8054 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8055 lpfc_printf_log(phba, KERN_ERR,
8056 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04008057 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05008058 piocb->iocb.ulpCommand);
8059 dump_stack();
8060 return IOCB_ERROR;
8061 }
8062
8063
Linas Vepstas8d63f372007-02-14 14:28:36 -06008064 /* If the PCI channel is in offline state, do not post iocbs. */
8065 if (unlikely(pci_channel_offline(phba->pcidev)))
8066 return IOCB_ERROR;
8067
James Smarta257bf92009-04-06 18:48:10 -04008068 /* If HBA has a deferred error attention, fail the iocb. */
8069 if (unlikely(phba->hba_flag & DEFER_ERATT))
8070 return IOCB_ERROR;
8071
dea31012005-04-17 16:05:31 -05008072 /*
8073 * We should never get an IOCB if we are in a < LINK_DOWN state
8074 */
James Smart2e0fef82007-06-17 19:56:36 -05008075 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05008076 return IOCB_ERROR;
8077
8078 /*
8079 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04008080 * outstanding event.
dea31012005-04-17 16:05:31 -05008081 */
James Smart0b727fe2007-10-27 13:37:25 -04008082 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05008083 goto iocb_busy;
8084
James Smart2e0fef82007-06-17 19:56:36 -05008085 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05008086 /*
James Smart2680eea2007-04-25 09:52:55 -04008087 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05008088 * can be issued if the link is not up.
8089 */
8090 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04008091 case CMD_GEN_REQUEST64_CR:
8092 case CMD_GEN_REQUEST64_CX:
8093 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8094 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
James Smart6a9c52c2009-10-02 15:16:51 -04008095 FC_RCTL_DD_UNSOL_CMD) ||
James Smart84774a42008-08-24 21:50:06 -04008096 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8097 MENLO_TRANSPORT_TYPE))
8098
8099 goto iocb_busy;
8100 break;
dea31012005-04-17 16:05:31 -05008101 case CMD_QUE_RING_BUF_CN:
8102 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05008103 /*
8104 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8105 * completion, iocb_cmpl MUST be 0.
8106 */
8107 if (piocb->iocb_cmpl)
8108 piocb->iocb_cmpl = NULL;
8109 /*FALLTHROUGH*/
8110 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04008111 case CMD_CLOSE_XRI_CN:
8112 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05008113 break;
8114 default:
8115 goto iocb_busy;
8116 }
8117
8118 /*
8119 * For FCP commands, we must be in a state where we can process link
8120 * attention events.
8121 */
8122 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05008123 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05008124 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05008125 }
dea31012005-04-17 16:05:31 -05008126
dea31012005-04-17 16:05:31 -05008127 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8128 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8129 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8130
8131 if (iocb)
8132 lpfc_sli_update_ring(phba, pring);
8133 else
8134 lpfc_sli_update_full_ring(phba, pring);
8135
8136 if (!piocb)
8137 return IOCB_SUCCESS;
8138
8139 goto out_busy;
8140
8141 iocb_busy:
8142 pring->stats.iocb_cmd_delay++;
8143
8144 out_busy:
8145
8146 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008147 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05008148 return IOCB_SUCCESS;
8149 }
8150
8151 return IOCB_BUSY;
8152}
8153
James Smart3772a992009-05-22 14:50:54 -04008154/**
James Smart4f774512009-05-22 14:52:35 -04008155 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8156 * @phba: Pointer to HBA context object.
8157 * @piocb: Pointer to command iocb.
8158 * @sglq: Pointer to the scatter gather queue object.
8159 *
8160 * This routine converts the bpl or bde that is in the IOCB
8161 * to a sgl list for the sli4 hardware. The physical address
8162 * of the bpl/bde is converted back to a virtual address.
8163 * If the IOCB contains a BPL then the list of BDE's is
8164 * converted to sli4_sge's. If the IOCB contains a single
8165 * BDE then it is converted to a single sli_sge.
8166 * The IOCB is still in cpu endianess so the contents of
8167 * the bpl can be used without byte swapping.
8168 *
8169 * Returns valid XRI = Success, NO_XRI = Failure.
8170**/
8171static uint16_t
8172lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8173 struct lpfc_sglq *sglq)
8174{
8175 uint16_t xritag = NO_XRI;
8176 struct ulp_bde64 *bpl = NULL;
8177 struct ulp_bde64 bde;
8178 struct sli4_sge *sgl = NULL;
James Smart1b511972011-12-13 13:23:09 -05008179 struct lpfc_dmabuf *dmabuf;
James Smart4f774512009-05-22 14:52:35 -04008180 IOCB_t *icmd;
8181 int numBdes = 0;
8182 int i = 0;
James Smart63e801c2010-11-20 23:14:19 -05008183 uint32_t offset = 0; /* accumulated offset in the sg request list */
8184 int inbound = 0; /* number of sg reply entries inbound from firmware */
James Smart4f774512009-05-22 14:52:35 -04008185
8186 if (!piocbq || !sglq)
8187 return xritag;
8188
8189 sgl = (struct sli4_sge *)sglq->sgl;
8190 icmd = &piocbq->iocb;
James Smart6b5151f2012-01-18 16:24:06 -05008191 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8192 return sglq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008193 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8194 numBdes = icmd->un.genreq64.bdl.bdeSize /
8195 sizeof(struct ulp_bde64);
8196 /* The addrHigh and addrLow fields within the IOCB
8197 * have not been byteswapped yet so there is no
8198 * need to swap them back.
8199 */
James Smart1b511972011-12-13 13:23:09 -05008200 if (piocbq->context3)
8201 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8202 else
8203 return xritag;
James Smart4f774512009-05-22 14:52:35 -04008204
James Smart1b511972011-12-13 13:23:09 -05008205 bpl = (struct ulp_bde64 *)dmabuf->virt;
James Smart4f774512009-05-22 14:52:35 -04008206 if (!bpl)
8207 return xritag;
8208
8209 for (i = 0; i < numBdes; i++) {
8210 /* Should already be byte swapped. */
James Smart28baac72010-02-12 14:42:03 -05008211 sgl->addr_hi = bpl->addrHigh;
8212 sgl->addr_lo = bpl->addrLow;
8213
James Smart05580562011-05-24 11:40:48 -04008214 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008215 if ((i+1) == numBdes)
8216 bf_set(lpfc_sli4_sge_last, sgl, 1);
8217 else
8218 bf_set(lpfc_sli4_sge_last, sgl, 0);
James Smart28baac72010-02-12 14:42:03 -05008219 /* swap the size field back to the cpu so we
8220 * can assign it to the sgl.
8221 */
8222 bde.tus.w = le32_to_cpu(bpl->tus.w);
8223 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
James Smart63e801c2010-11-20 23:14:19 -05008224 /* The offsets in the sgl need to be accumulated
8225 * separately for the request and reply lists.
8226 * The request is always first, the reply follows.
8227 */
8228 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8229 /* add up the reply sg entries */
8230 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8231 inbound++;
8232 /* first inbound? reset the offset */
8233 if (inbound == 1)
8234 offset = 0;
8235 bf_set(lpfc_sli4_sge_offset, sgl, offset);
James Smartf9bb2da2011-10-10 21:34:11 -04008236 bf_set(lpfc_sli4_sge_type, sgl,
8237 LPFC_SGE_TYPE_DATA);
James Smart63e801c2010-11-20 23:14:19 -05008238 offset += bde.tus.f.bdeSize;
8239 }
James Smart546fc852011-03-11 16:06:29 -05008240 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008241 bpl++;
8242 sgl++;
8243 }
8244 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8245 /* The addrHigh and addrLow fields of the BDE have not
8246 * been byteswapped yet so they need to be swapped
8247 * before putting them in the sgl.
8248 */
8249 sgl->addr_hi =
8250 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8251 sgl->addr_lo =
8252 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
James Smart05580562011-05-24 11:40:48 -04008253 sgl->word2 = le32_to_cpu(sgl->word2);
James Smart4f774512009-05-22 14:52:35 -04008254 bf_set(lpfc_sli4_sge_last, sgl, 1);
8255 sgl->word2 = cpu_to_le32(sgl->word2);
James Smart28baac72010-02-12 14:42:03 -05008256 sgl->sge_len =
8257 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
James Smart4f774512009-05-22 14:52:35 -04008258 }
8259 return sglq->sli4_xritag;
8260}
8261
8262/**
James Smart4f774512009-05-22 14:52:35 -04008263 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8264 * @phba: Pointer to HBA context object.
8265 * @piocb: Pointer to command iocb.
8266 * @wqe: Pointer to the work queue entry.
8267 *
8268 * This routine converts the iocb command to its Work Queue Entry
8269 * equivalent. The wqe pointer should not have any fields set when
8270 * this routine is called because it will memcpy over them.
8271 * This routine does not set the CQ_ID or the WQEC bits in the
8272 * wqe.
8273 *
8274 * Returns: 0 = Success, IOCB_ERROR = Failure.
8275 **/
8276static int
8277lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8278 union lpfc_wqe *wqe)
8279{
James Smart5ffc2662009-11-18 15:39:44 -05008280 uint32_t xmit_len = 0, total_len = 0;
James Smart4f774512009-05-22 14:52:35 -04008281 uint8_t ct = 0;
8282 uint32_t fip;
8283 uint32_t abort_tag;
8284 uint8_t command_type = ELS_COMMAND_NON_FIP;
8285 uint8_t cmnd;
8286 uint16_t xritag;
James Smartdcf2a4e2010-09-29 11:18:53 -04008287 uint16_t abrt_iotag;
8288 struct lpfc_iocbq *abrtiocbq;
James Smart4f774512009-05-22 14:52:35 -04008289 struct ulp_bde64 *bpl = NULL;
James Smartf0d9bcc2010-10-22 11:07:09 -04008290 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
James Smart5ffc2662009-11-18 15:39:44 -05008291 int numBdes, i;
8292 struct ulp_bde64 bde;
James Smartc31098c2011-04-16 11:03:33 -04008293 struct lpfc_nodelist *ndlp;
James Smartff78d8f2011-12-13 13:21:35 -05008294 uint32_t *pcmd;
James Smart1b511972011-12-13 13:23:09 -05008295 uint32_t if_type;
James Smart4f774512009-05-22 14:52:35 -04008296
James Smart45ed1192009-10-02 15:17:02 -04008297 fip = phba->hba_flag & HBA_FIP_SUPPORT;
James Smart4f774512009-05-22 14:52:35 -04008298 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04008299 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04008300 command_type = FCP_COMMAND;
James Smartc8685952009-11-18 15:39:16 -05008301 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
James Smart0c287582009-06-10 17:22:56 -04008302 command_type = ELS_COMMAND_FIP;
8303 else
8304 command_type = ELS_COMMAND_NON_FIP;
8305
James Smartb5c53952016-03-31 14:12:30 -07008306 if (phba->fcp_embed_io)
8307 memset(wqe, 0, sizeof(union lpfc_wqe128));
James Smart4f774512009-05-22 14:52:35 -04008308 /* Some of the fields are in the right position already */
8309 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
James Smartf0d9bcc2010-10-22 11:07:09 -04008310 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
James Smart28d7f3d2014-05-21 08:05:28 -04008311 wqe->generic.wqe_com.word10 = 0;
James Smartb5c53952016-03-31 14:12:30 -07008312
8313 abort_tag = (uint32_t) iocbq->iotag;
8314 xritag = iocbq->sli4_xritag;
James Smart4f774512009-05-22 14:52:35 -04008315 /* words0-2 bpl convert bde */
8316 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
James Smart5ffc2662009-11-18 15:39:44 -05008317 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8318 sizeof(struct ulp_bde64);
James Smart4f774512009-05-22 14:52:35 -04008319 bpl = (struct ulp_bde64 *)
8320 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8321 if (!bpl)
8322 return IOCB_ERROR;
8323
8324 /* Should already be byte swapped. */
8325 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8326 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8327 /* swap the size field back to the cpu so we
8328 * can assign it to the sgl.
8329 */
8330 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
James Smart5ffc2662009-11-18 15:39:44 -05008331 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8332 total_len = 0;
8333 for (i = 0; i < numBdes; i++) {
8334 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8335 total_len += bde.tus.f.bdeSize;
8336 }
James Smart4f774512009-05-22 14:52:35 -04008337 } else
James Smart5ffc2662009-11-18 15:39:44 -05008338 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
James Smart4f774512009-05-22 14:52:35 -04008339
8340 iocbq->iocb.ulpIoTag = iocbq->iotag;
8341 cmnd = iocbq->iocb.ulpCommand;
8342
8343 switch (iocbq->iocb.ulpCommand) {
8344 case CMD_ELS_REQUEST64_CR:
James Smart93d1379e2012-05-09 21:19:34 -04008345 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8346 ndlp = iocbq->context_un.ndlp;
8347 else
8348 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008349 if (!iocbq->iocb.ulpLe) {
8350 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8351 "2007 Only Limited Edition cmd Format"
8352 " supported 0x%x\n",
8353 iocbq->iocb.ulpCommand);
8354 return IOCB_ERROR;
8355 }
James Smartff78d8f2011-12-13 13:21:35 -05008356
James Smart5ffc2662009-11-18 15:39:44 -05008357 wqe->els_req.payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008358 /* Els_reguest64 has a TMO */
8359 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8360 iocbq->iocb.ulpTimeout);
8361 /* Need a VF for word 4 set the vf bit*/
8362 bf_set(els_req64_vf, &wqe->els_req, 0);
8363 /* And a VFID for word 12 */
8364 bf_set(els_req64_vfid, &wqe->els_req, 0);
James Smart4f774512009-05-22 14:52:35 -04008365 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
James Smartf0d9bcc2010-10-22 11:07:09 -04008366 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8367 iocbq->iocb.ulpContext);
8368 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8369 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
James Smart4f774512009-05-22 14:52:35 -04008370 /* CCP CCPE PV PRI in word10 were set in the memcpy */
James Smartff78d8f2011-12-13 13:21:35 -05008371 if (command_type == ELS_COMMAND_FIP)
James Smartc8685952009-11-18 15:39:16 -05008372 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8373 >> LPFC_FIP_ELS_ID_SHIFT);
James Smartff78d8f2011-12-13 13:21:35 -05008374 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8375 iocbq->context2)->virt);
James Smart1b511972011-12-13 13:23:09 -05008376 if_type = bf_get(lpfc_sli_intf_if_type,
8377 &phba->sli4_hba.sli_intf);
8378 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
James Smartff78d8f2011-12-13 13:21:35 -05008379 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
James Smartcb69f7d2011-12-13 13:21:57 -05008380 *pcmd == ELS_CMD_SCR ||
James Smart6b5151f2012-01-18 16:24:06 -05008381 *pcmd == ELS_CMD_FDISC ||
James Smartbdcd2b92012-03-01 22:33:52 -05008382 *pcmd == ELS_CMD_LOGO ||
James Smartff78d8f2011-12-13 13:21:35 -05008383 *pcmd == ELS_CMD_PLOGI)) {
8384 bf_set(els_req64_sp, &wqe->els_req, 1);
8385 bf_set(els_req64_sid, &wqe->els_req,
8386 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008387 if ((*pcmd == ELS_CMD_FLOGI) &&
8388 !(phba->fc_topology ==
8389 LPFC_TOPOLOGY_LOOP))
8390 bf_set(els_req64_sid, &wqe->els_req, 0);
James Smartff78d8f2011-12-13 13:21:35 -05008391 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8392 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
James Smarta7dd9c02012-05-09 21:16:50 -04008393 phba->vpi_ids[iocbq->vport->vpi]);
James Smart3ef6d242012-01-18 16:23:48 -05008394 } else if (pcmd && iocbq->context1) {
James Smartff78d8f2011-12-13 13:21:35 -05008395 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8396 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8397 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8398 }
James Smartc8685952009-11-18 15:39:16 -05008399 }
James Smart6d368e52011-05-24 11:44:12 -04008400 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8401 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008402 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8403 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8404 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8405 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8406 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8407 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008408 wqe->els_req.max_response_payload_len = total_len - xmit_len;
James Smart7851fe22011-07-22 18:36:52 -04008409 break;
James Smart5ffc2662009-11-18 15:39:44 -05008410 case CMD_XMIT_SEQUENCE64_CX:
James Smartf0d9bcc2010-10-22 11:07:09 -04008411 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8412 iocbq->iocb.un.ulpWord[3]);
8413 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008414 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart5ffc2662009-11-18 15:39:44 -05008415 /* The entire sequence is transmitted for this IOCB */
8416 xmit_len = total_len;
8417 cmnd = CMD_XMIT_SEQUENCE64_CR;
James Smart1b511972011-12-13 13:23:09 -05008418 if (phba->link_flag & LS_LOOPBACK_MODE)
8419 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
James Smart4f774512009-05-22 14:52:35 -04008420 case CMD_XMIT_SEQUENCE64_CR:
James Smartf0d9bcc2010-10-22 11:07:09 -04008421 /* word3 iocb=io_tag32 wqe=reserved */
8422 wqe->xmit_sequence.rsvd3 = 0;
James Smart4f774512009-05-22 14:52:35 -04008423 /* word4 relative_offset memcpy */
8424 /* word5 r_ctl/df_ctl memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008425 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8426 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8427 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8428 LPFC_WQE_IOD_WRITE);
8429 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8430 LPFC_WQE_LENLOC_WORD12);
8431 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
James Smart5ffc2662009-11-18 15:39:44 -05008432 wqe->xmit_sequence.xmit_len = xmit_len;
8433 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008434 break;
James Smart4f774512009-05-22 14:52:35 -04008435 case CMD_XMIT_BCAST64_CN:
James Smartf0d9bcc2010-10-22 11:07:09 -04008436 /* word3 iocb=iotag32 wqe=seq_payload_len */
8437 wqe->xmit_bcast64.seq_payload_len = xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008438 /* word4 iocb=rsvd wqe=rsvd */
8439 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8440 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
James Smartf0d9bcc2010-10-22 11:07:09 -04008441 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008442 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
James Smartf0d9bcc2010-10-22 11:07:09 -04008443 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8444 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8445 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8446 LPFC_WQE_LENLOC_WORD3);
8447 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
James Smart7851fe22011-07-22 18:36:52 -04008448 break;
James Smart4f774512009-05-22 14:52:35 -04008449 case CMD_FCP_IWRITE64_CR:
8450 command_type = FCP_COMMAND_DATA_OUT;
James Smartf0d9bcc2010-10-22 11:07:09 -04008451 /* word3 iocb=iotag wqe=payload_offset_len */
8452 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008453 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8454 xmit_len + sizeof(struct fcp_rsp));
8455 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8456 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008457 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8458 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8459 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8460 iocbq->iocb.ulpFCP2Rcvy);
8461 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8462 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008463 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8464 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8465 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008466 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008467 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008468 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8469 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008470 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8471 if (iocbq->priority) {
8472 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8473 (iocbq->priority << 1));
8474 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008475 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8476 (phba->cfg_XLanePriority << 1));
8477 }
8478 }
James Smartb5c53952016-03-31 14:12:30 -07008479 /* Note, word 10 is already initialized to 0 */
8480
8481 if (phba->fcp_embed_io) {
8482 struct lpfc_scsi_buf *lpfc_cmd;
8483 struct sli4_sge *sgl;
8484 union lpfc_wqe128 *wqe128;
8485 struct fcp_cmnd *fcp_cmnd;
8486 uint32_t *ptr;
8487
8488 /* 128 byte wqe support here */
8489 wqe128 = (union lpfc_wqe128 *)wqe;
8490
8491 lpfc_cmd = iocbq->context1;
8492 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8493 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8494
8495 /* Word 0-2 - FCP_CMND */
8496 wqe128->generic.bde.tus.f.bdeFlags =
8497 BUFF_TYPE_BDE_IMMED;
8498 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8499 wqe128->generic.bde.addrHigh = 0;
8500 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8501
8502 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8503
8504 /* Word 22-29 FCP CMND Payload */
8505 ptr = &wqe128->words[22];
8506 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8507 }
James Smart7851fe22011-07-22 18:36:52 -04008508 break;
James Smartf0d9bcc2010-10-22 11:07:09 -04008509 case CMD_FCP_IREAD64_CR:
8510 /* word3 iocb=iotag wqe=payload_offset_len */
8511 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
James Smart0ba4b212013-10-10 12:22:38 -04008512 bf_set(payload_offset_len, &wqe->fcp_iread,
8513 xmit_len + sizeof(struct fcp_rsp));
8514 bf_set(cmd_buff_len, &wqe->fcp_iread,
8515 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008516 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8517 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8518 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8519 iocbq->iocb.ulpFCP2Rcvy);
8520 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
James Smart4f774512009-05-22 14:52:35 -04008521 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008522 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8523 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8524 LPFC_WQE_LENLOC_WORD4);
James Smartf0d9bcc2010-10-22 11:07:09 -04008525 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
James Smartacd68592012-01-18 16:25:09 -05008526 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
James Smart1ba981f2014-02-20 09:56:45 -05008527 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8528 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008529 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8530 if (iocbq->priority) {
8531 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8532 (iocbq->priority << 1));
8533 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008534 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8535 (phba->cfg_XLanePriority << 1));
8536 }
8537 }
James Smartb5c53952016-03-31 14:12:30 -07008538 /* Note, word 10 is already initialized to 0 */
8539
8540 if (phba->fcp_embed_io) {
8541 struct lpfc_scsi_buf *lpfc_cmd;
8542 struct sli4_sge *sgl;
8543 union lpfc_wqe128 *wqe128;
8544 struct fcp_cmnd *fcp_cmnd;
8545 uint32_t *ptr;
8546
8547 /* 128 byte wqe support here */
8548 wqe128 = (union lpfc_wqe128 *)wqe;
8549
8550 lpfc_cmd = iocbq->context1;
8551 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8552 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8553
8554 /* Word 0-2 - FCP_CMND */
8555 wqe128->generic.bde.tus.f.bdeFlags =
8556 BUFF_TYPE_BDE_IMMED;
8557 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8558 wqe128->generic.bde.addrHigh = 0;
8559 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8560
8561 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8562
8563 /* Word 22-29 FCP CMND Payload */
8564 ptr = &wqe128->words[22];
8565 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8566 }
James Smart7851fe22011-07-22 18:36:52 -04008567 break;
James Smartf1126682009-06-10 17:22:44 -04008568 case CMD_FCP_ICMND64_CR:
James Smart0ba4b212013-10-10 12:22:38 -04008569 /* word3 iocb=iotag wqe=payload_offset_len */
8570 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8571 bf_set(payload_offset_len, &wqe->fcp_icmd,
8572 xmit_len + sizeof(struct fcp_rsp));
8573 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8574 0);
James Smartf0d9bcc2010-10-22 11:07:09 -04008575 /* word3 iocb=IO_TAG wqe=reserved */
James Smartf0d9bcc2010-10-22 11:07:09 -04008576 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
James Smartf1126682009-06-10 17:22:44 -04008577 /* Always open the exchange */
James Smartf0d9bcc2010-10-22 11:07:09 -04008578 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8579 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8580 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8581 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8582 LPFC_WQE_LENLOC_NONE);
James Smart2a94aea2012-09-29 11:30:31 -04008583 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8584 iocbq->iocb.ulpFCP2Rcvy);
James Smart1ba981f2014-02-20 09:56:45 -05008585 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8586 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
James Smartc92c8412016-07-06 12:36:05 -07008587 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8588 if (iocbq->priority) {
8589 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8590 (iocbq->priority << 1));
8591 } else {
James Smart1ba981f2014-02-20 09:56:45 -05008592 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8593 (phba->cfg_XLanePriority << 1));
8594 }
8595 }
James Smartb5c53952016-03-31 14:12:30 -07008596 /* Note, word 10 is already initialized to 0 */
8597
8598 if (phba->fcp_embed_io) {
8599 struct lpfc_scsi_buf *lpfc_cmd;
8600 struct sli4_sge *sgl;
8601 union lpfc_wqe128 *wqe128;
8602 struct fcp_cmnd *fcp_cmnd;
8603 uint32_t *ptr;
8604
8605 /* 128 byte wqe support here */
8606 wqe128 = (union lpfc_wqe128 *)wqe;
8607
8608 lpfc_cmd = iocbq->context1;
8609 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8610 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8611
8612 /* Word 0-2 - FCP_CMND */
8613 wqe128->generic.bde.tus.f.bdeFlags =
8614 BUFF_TYPE_BDE_IMMED;
8615 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8616 wqe128->generic.bde.addrHigh = 0;
8617 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8618
8619 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8620
8621 /* Word 22-29 FCP CMND Payload */
8622 ptr = &wqe128->words[22];
8623 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8624 }
James Smart7851fe22011-07-22 18:36:52 -04008625 break;
James Smart4f774512009-05-22 14:52:35 -04008626 case CMD_GEN_REQUEST64_CR:
James Smart63e801c2010-11-20 23:14:19 -05008627 /* For this command calculate the xmit length of the
8628 * request bde.
8629 */
8630 xmit_len = 0;
8631 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8632 sizeof(struct ulp_bde64);
8633 for (i = 0; i < numBdes; i++) {
James Smart63e801c2010-11-20 23:14:19 -05008634 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
James Smart546fc852011-03-11 16:06:29 -05008635 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8636 break;
James Smart63e801c2010-11-20 23:14:19 -05008637 xmit_len += bde.tus.f.bdeSize;
8638 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008639 /* word3 iocb=IO_TAG wqe=request_payload_len */
8640 wqe->gen_req.request_payload_len = xmit_len;
8641 /* word4 iocb=parameter wqe=relative_offset memcpy */
8642 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
James Smart4f774512009-05-22 14:52:35 -04008643 /* word6 context tag copied in memcpy */
8644 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8645 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8646 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8647 "2015 Invalid CT %x command 0x%x\n",
8648 ct, iocbq->iocb.ulpCommand);
8649 return IOCB_ERROR;
8650 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008651 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8652 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8653 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8654 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8655 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8656 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8657 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8658 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
James Smartaf227412013-10-10 12:23:10 -04008659 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
James Smart4f774512009-05-22 14:52:35 -04008660 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008661 break;
James Smart4f774512009-05-22 14:52:35 -04008662 case CMD_XMIT_ELS_RSP64_CX:
James Smartc31098c2011-04-16 11:03:33 -04008663 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart4f774512009-05-22 14:52:35 -04008664 /* words0-2 BDE memcpy */
James Smartf0d9bcc2010-10-22 11:07:09 -04008665 /* word3 iocb=iotag32 wqe=response_payload_len */
8666 wqe->xmit_els_rsp.response_payload_len = xmit_len;
James Smart939723a2012-05-09 21:19:03 -04008667 /* word4 */
8668 wqe->xmit_els_rsp.word4 = 0;
James Smart4f774512009-05-22 14:52:35 -04008669 /* word5 iocb=rsvd wge=did */
8670 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
James Smart939723a2012-05-09 21:19:03 -04008671 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8672
8673 if_type = bf_get(lpfc_sli_intf_if_type,
8674 &phba->sli4_hba.sli_intf);
8675 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8676 if (iocbq->vport->fc_flag & FC_PT2PT) {
8677 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8678 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8679 iocbq->vport->fc_myDID);
8680 if (iocbq->vport->fc_myDID == Fabric_DID) {
8681 bf_set(wqe_els_did,
8682 &wqe->xmit_els_rsp.wqe_dest, 0);
8683 }
8684 }
8685 }
James Smartf0d9bcc2010-10-22 11:07:09 -04008686 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8687 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8688 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8689 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
James Smart7851fe22011-07-22 18:36:52 -04008690 iocbq->iocb.unsli3.rcvsli3.ox_id);
James Smart4f774512009-05-22 14:52:35 -04008691 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
James Smartf0d9bcc2010-10-22 11:07:09 -04008692 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smart6d368e52011-05-24 11:44:12 -04008693 phba->vpi_ids[iocbq->vport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008694 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8695 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8696 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8697 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8698 LPFC_WQE_LENLOC_WORD3);
8699 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
James Smart6d368e52011-05-24 11:44:12 -04008700 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8701 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
James Smartff78d8f2011-12-13 13:21:35 -05008702 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8703 iocbq->context2)->virt);
8704 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart939723a2012-05-09 21:19:03 -04008705 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8706 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
James Smartff78d8f2011-12-13 13:21:35 -05008707 iocbq->vport->fc_myDID);
James Smart939723a2012-05-09 21:19:03 -04008708 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8709 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
James Smartff78d8f2011-12-13 13:21:35 -05008710 phba->vpi_ids[phba->pport->vpi]);
8711 }
James Smart4f774512009-05-22 14:52:35 -04008712 command_type = OTHER_COMMAND;
James Smart7851fe22011-07-22 18:36:52 -04008713 break;
James Smart4f774512009-05-22 14:52:35 -04008714 case CMD_CLOSE_XRI_CN:
8715 case CMD_ABORT_XRI_CN:
8716 case CMD_ABORT_XRI_CX:
8717 /* words 0-2 memcpy should be 0 rserved */
8718 /* port will send abts */
James Smartdcf2a4e2010-09-29 11:18:53 -04008719 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8720 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8721 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8722 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8723 } else
8724 fip = 0;
8725
8726 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
James Smart4f774512009-05-22 14:52:35 -04008727 /*
James Smartdcf2a4e2010-09-29 11:18:53 -04008728 * The link is down, or the command was ELS_FIP
8729 * so the fw does not need to send abts
James Smart4f774512009-05-22 14:52:35 -04008730 * on the wire.
8731 */
8732 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8733 else
8734 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8735 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
James Smartf0d9bcc2010-10-22 11:07:09 -04008736 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8737 wqe->abort_cmd.rsrvd5 = 0;
8738 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
James Smart4f774512009-05-22 14:52:35 -04008739 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8740 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
James Smart4f774512009-05-22 14:52:35 -04008741 /*
8742 * The abort handler will send us CMD_ABORT_XRI_CN or
8743 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8744 */
James Smartf0d9bcc2010-10-22 11:07:09 -04008745 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8746 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8747 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8748 LPFC_WQE_LENLOC_NONE);
James Smart4f774512009-05-22 14:52:35 -04008749 cmnd = CMD_ABORT_XRI_CX;
8750 command_type = OTHER_COMMAND;
8751 xritag = 0;
James Smart7851fe22011-07-22 18:36:52 -04008752 break;
James Smart6669f9b2009-10-02 15:16:45 -04008753 case CMD_XMIT_BLS_RSP64_CX:
James Smart6b5151f2012-01-18 16:24:06 -05008754 ndlp = (struct lpfc_nodelist *)iocbq->context1;
James Smart546fc852011-03-11 16:06:29 -05008755 /* As BLS ABTS RSP WQE is very different from other WQEs,
James Smart6669f9b2009-10-02 15:16:45 -04008756 * we re-construct this WQE here based on information in
8757 * iocbq from scratch.
8758 */
8759 memset(wqe, 0, sizeof(union lpfc_wqe));
James Smart5ffc2662009-11-18 15:39:44 -05008760 /* OX_ID is invariable to who sent ABTS to CT exchange */
James Smart6669f9b2009-10-02 15:16:45 -04008761 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008762 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8763 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
James Smart5ffc2662009-11-18 15:39:44 -05008764 LPFC_ABTS_UNSOL_INT) {
8765 /* ABTS sent by initiator to CT exchange, the
8766 * RX_ID field will be filled with the newly
8767 * allocated responder XRI.
8768 */
8769 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8770 iocbq->sli4_xritag);
8771 } else {
8772 /* ABTS sent by responder to CT exchange, the
8773 * RX_ID field will be filled with the responder
8774 * RX_ID from ABTS.
8775 */
8776 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
James Smart546fc852011-03-11 16:06:29 -05008777 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
James Smart5ffc2662009-11-18 15:39:44 -05008778 }
James Smart6669f9b2009-10-02 15:16:45 -04008779 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8780 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
James Smart6b5151f2012-01-18 16:24:06 -05008781
8782 /* Use CT=VPI */
8783 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8784 ndlp->nlp_DID);
8785 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8786 iocbq->iocb.ulpContext);
8787 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
James Smart6669f9b2009-10-02 15:16:45 -04008788 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
James Smart6b5151f2012-01-18 16:24:06 -05008789 phba->vpi_ids[phba->pport->vpi]);
James Smartf0d9bcc2010-10-22 11:07:09 -04008790 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8791 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8792 LPFC_WQE_LENLOC_NONE);
James Smart6669f9b2009-10-02 15:16:45 -04008793 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8794 command_type = OTHER_COMMAND;
James Smart546fc852011-03-11 16:06:29 -05008795 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8796 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8797 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8798 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8799 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8800 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8801 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8802 }
8803
James Smart7851fe22011-07-22 18:36:52 -04008804 break;
James Smart4f774512009-05-22 14:52:35 -04008805 case CMD_XRI_ABORTED_CX:
8806 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
James Smart4f774512009-05-22 14:52:35 -04008807 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8808 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8809 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8810 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8811 default:
8812 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8813 "2014 Invalid command 0x%x\n",
8814 iocbq->iocb.ulpCommand);
8815 return IOCB_ERROR;
James Smart7851fe22011-07-22 18:36:52 -04008816 break;
James Smart4f774512009-05-22 14:52:35 -04008817 }
James Smart6d368e52011-05-24 11:44:12 -04008818
James Smart8012cc32012-10-31 14:44:49 -04008819 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8820 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8821 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8822 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8823 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8824 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8825 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8826 LPFC_IO_DIF_INSERT);
James Smartf0d9bcc2010-10-22 11:07:09 -04008827 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8828 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8829 wqe->generic.wqe_com.abort_tag = abort_tag;
8830 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8831 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8832 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8833 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
James Smart4f774512009-05-22 14:52:35 -04008834 return 0;
8835}
8836
8837/**
8838 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8839 * @phba: Pointer to HBA context object.
8840 * @ring_number: SLI ring number to issue iocb on.
8841 * @piocb: Pointer to command iocb.
8842 * @flag: Flag indicating if this command can be put into txq.
8843 *
8844 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8845 * an iocb command to an HBA with SLI-4 interface spec.
8846 *
8847 * This function is called with hbalock held. The function will return success
8848 * after it successfully submit the iocb to firmware or after adding to the
8849 * txq.
8850 **/
8851static int
8852__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8853 struct lpfc_iocbq *piocb, uint32_t flag)
8854{
8855 struct lpfc_sglq *sglq;
James Smartb5c53952016-03-31 14:12:30 -07008856 union lpfc_wqe *wqe;
8857 union lpfc_wqe128 wqe128;
James Smart1ba981f2014-02-20 09:56:45 -05008858 struct lpfc_queue *wq;
James Smart4f774512009-05-22 14:52:35 -04008859 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
James Smart4f774512009-05-22 14:52:35 -04008860
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01008861 lockdep_assert_held(&phba->hbalock);
8862
James Smartb5c53952016-03-31 14:12:30 -07008863 /*
8864 * The WQE can be either 64 or 128 bytes,
8865 * so allocate space on the stack assuming the largest.
8866 */
8867 wqe = (union lpfc_wqe *)&wqe128;
8868
James Smart4f774512009-05-22 14:52:35 -04008869 if (piocb->sli4_xritag == NO_XRI) {
8870 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6b5151f2012-01-18 16:24:06 -05008871 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04008872 sglq = NULL;
8873 else {
James Smart0e9bb8d2013-03-01 16:35:12 -05008874 if (!list_empty(&pring->txq)) {
James Smart2a9bf3d2010-06-07 15:24:45 -04008875 if (!(flag & SLI_IOCB_RET_IOCB)) {
8876 __lpfc_sli_ringtx_put(phba,
8877 pring, piocb);
8878 return IOCB_SUCCESS;
8879 } else {
8880 return IOCB_BUSY;
8881 }
8882 } else {
James Smart6d368e52011-05-24 11:44:12 -04008883 sglq = __lpfc_sli_get_sglq(phba, piocb);
James Smart2a9bf3d2010-06-07 15:24:45 -04008884 if (!sglq) {
8885 if (!(flag & SLI_IOCB_RET_IOCB)) {
8886 __lpfc_sli_ringtx_put(phba,
8887 pring,
8888 piocb);
8889 return IOCB_SUCCESS;
8890 } else
8891 return IOCB_BUSY;
8892 }
8893 }
James Smart4f774512009-05-22 14:52:35 -04008894 }
8895 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart6d368e52011-05-24 11:44:12 -04008896 /* These IO's already have an XRI and a mapped sgl. */
8897 sglq = NULL;
James Smart4f774512009-05-22 14:52:35 -04008898 } else {
James Smart6d368e52011-05-24 11:44:12 -04008899 /*
8900 * This is a continuation of a commandi,(CX) so this
James Smart4f774512009-05-22 14:52:35 -04008901 * sglq is on the active list
8902 */
James Smartedccdc12013-01-03 15:43:45 -05008903 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
James Smart4f774512009-05-22 14:52:35 -04008904 if (!sglq)
8905 return IOCB_ERROR;
8906 }
8907
8908 if (sglq) {
James Smart6d368e52011-05-24 11:44:12 -04008909 piocb->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008910 piocb->sli4_xritag = sglq->sli4_xritag;
James Smart2a9bf3d2010-06-07 15:24:45 -04008911 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
James Smart4f774512009-05-22 14:52:35 -04008912 return IOCB_ERROR;
8913 }
8914
James Smartb5c53952016-03-31 14:12:30 -07008915 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
James Smart4f774512009-05-22 14:52:35 -04008916 return IOCB_ERROR;
8917
James Smart341af102010-01-26 23:07:37 -05008918 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
James Smart1ba981f2014-02-20 09:56:45 -05008919 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
James Smartf38fa0b2014-04-04 13:52:21 -04008920 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
James Smart1ba981f2014-02-20 09:56:45 -05008921 wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
8922 } else {
8923 wq = phba->sli4_hba.oas_wq;
8924 }
James Smartb5c53952016-03-31 14:12:30 -07008925 if (lpfc_sli4_wq_put(wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008926 return IOCB_ERROR;
8927 } else {
James Smartea714f32013-04-17 20:18:47 -04008928 if (unlikely(!phba->sli4_hba.els_wq))
8929 return IOCB_ERROR;
James Smartb5c53952016-03-31 14:12:30 -07008930 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
James Smart4f774512009-05-22 14:52:35 -04008931 return IOCB_ERROR;
8932 }
8933 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8934
8935 return 0;
8936}
8937
8938/**
James Smart3772a992009-05-22 14:50:54 -04008939 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8940 *
8941 * This routine wraps the actual lockless version for issusing IOCB function
8942 * pointer from the lpfc_hba struct.
8943 *
8944 * Return codes:
James Smartb5c53952016-03-31 14:12:30 -07008945 * IOCB_ERROR - Error
8946 * IOCB_SUCCESS - Success
8947 * IOCB_BUSY - Busy
James Smart3772a992009-05-22 14:50:54 -04008948 **/
James Smart2a9bf3d2010-06-07 15:24:45 -04008949int
James Smart3772a992009-05-22 14:50:54 -04008950__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8951 struct lpfc_iocbq *piocb, uint32_t flag)
8952{
8953 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8954}
8955
8956/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008957 * lpfc_sli_api_table_setup - Set up sli api function jump table
James Smart3772a992009-05-22 14:50:54 -04008958 * @phba: The hba struct for which this call is being executed.
8959 * @dev_grp: The HBA PCI-Device group number.
8960 *
8961 * This routine sets up the SLI interface API function jump table in @phba
8962 * struct.
8963 * Returns: 0 - success, -ENODEV - failure.
8964 **/
8965int
8966lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8967{
8968
8969 switch (dev_grp) {
8970 case LPFC_PCI_DEV_LP:
8971 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8972 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8973 break;
James Smart4f774512009-05-22 14:52:35 -04008974 case LPFC_PCI_DEV_OC:
8975 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8976 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8977 break;
James Smart3772a992009-05-22 14:50:54 -04008978 default:
8979 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8980 "1419 Invalid HBA PCI-device group: 0x%x\n",
8981 dev_grp);
8982 return -ENODEV;
8983 break;
8984 }
8985 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8986 return 0;
8987}
James Smart92d7f7b2007-06-17 19:56:38 -05008988
James Smarta1efe162015-05-21 13:55:20 -04008989/**
8990 * lpfc_sli_calc_ring - Calculates which ring to use
8991 * @phba: Pointer to HBA context object.
8992 * @ring_number: Initial ring
8993 * @piocb: Pointer to command iocb.
8994 *
8995 * For SLI4, FCP IO can deferred to one fo many WQs, based on
8996 * fcp_wqidx, thus we need to calculate the corresponding ring.
8997 * Since ABORTS must go on the same WQ of the command they are
8998 * aborting, we use command's fcp_wqidx.
8999 */
Baoyou Xiebd4b3e52016-09-25 13:44:55 +08009000static int
James Smart9bd2bff52014-09-03 12:57:30 -04009001lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
9002 struct lpfc_iocbq *piocb)
9003{
James Smart8b0dff12015-05-22 10:42:38 -04009004 if (phba->sli_rev < LPFC_SLI_REV4)
9005 return ring_number;
9006
9007 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
9008 if (!(phba->cfg_fof) ||
9009 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
9010 if (unlikely(!phba->sli4_hba.fcp_wq))
9011 return LPFC_HBA_ERROR;
9012 /*
9013 * for abort iocb fcp_wqidx should already
9014 * be setup based on what work queue we used.
9015 */
9016 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
9017 piocb->fcp_wqidx =
9018 lpfc_sli4_scmd_to_wqidx_distr(phba,
9019 piocb->context1);
9020 ring_number = MAX_SLI3_CONFIGURED_RINGS +
9021 piocb->fcp_wqidx;
9022 } else {
9023 if (unlikely(!phba->sli4_hba.oas_wq))
9024 return LPFC_HBA_ERROR;
9025 piocb->fcp_wqidx = 0;
9026 ring_number = LPFC_FCP_OAS_RING;
James Smart9bd2bff52014-09-03 12:57:30 -04009027 }
9028 }
9029 return ring_number;
9030}
9031
James Smarte59058c2008-08-24 21:49:00 -04009032/**
James Smart3621a712009-04-06 18:47:14 -04009033 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04009034 * @phba: Pointer to HBA context object.
9035 * @pring: Pointer to driver SLI ring object.
9036 * @piocb: Pointer to command iocb.
9037 * @flag: Flag indicating if this command can be put into txq.
9038 *
9039 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9040 * function. This function gets the hbalock and calls
9041 * __lpfc_sli_issue_iocb function and will return the error returned
9042 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9043 * functions which do not hold hbalock.
9044 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009045int
James Smart3772a992009-05-22 14:50:54 -04009046lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05009047 struct lpfc_iocbq *piocb, uint32_t flag)
9048{
James Smartba20c852012-08-03 12:36:52 -04009049 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
James Smart2a76a282012-08-03 12:35:54 -04009050 struct lpfc_sli_ring *pring;
James Smartba20c852012-08-03 12:36:52 -04009051 struct lpfc_queue *fpeq;
9052 struct lpfc_eqe *eqe;
James Smart92d7f7b2007-06-17 19:56:38 -05009053 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -04009054 int rc, idx;
James Smart92d7f7b2007-06-17 19:56:38 -05009055
James Smart7e56aa22012-08-03 12:35:34 -04009056 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -04009057 ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
9058 if (unlikely(ring_number == LPFC_HBA_ERROR))
9059 return IOCB_ERROR;
9060 idx = piocb->fcp_wqidx;
James Smartba20c852012-08-03 12:36:52 -04009061
James Smart9bd2bff52014-09-03 12:57:30 -04009062 pring = &phba->sli.ring[ring_number];
9063 spin_lock_irqsave(&pring->ring_lock, iflags);
9064 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9065 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smartba20c852012-08-03 12:36:52 -04009066
James Smart9bd2bff52014-09-03 12:57:30 -04009067 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
9068 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
James Smartba20c852012-08-03 12:36:52 -04009069
James Smart9bd2bff52014-09-03 12:57:30 -04009070 if (atomic_dec_and_test(&fcp_eq_hdl->
9071 fcp_eq_in_use)) {
James Smartba20c852012-08-03 12:36:52 -04009072
James Smart9bd2bff52014-09-03 12:57:30 -04009073 /* Get associated EQ with this index */
9074 fpeq = phba->sli4_hba.hba_eq[idx];
James Smartba20c852012-08-03 12:36:52 -04009075
James Smart9bd2bff52014-09-03 12:57:30 -04009076 /* Turn off interrupts from this EQ */
9077 lpfc_sli4_eq_clr_intr(fpeq);
James Smartba20c852012-08-03 12:36:52 -04009078
James Smart9bd2bff52014-09-03 12:57:30 -04009079 /*
9080 * Process all the events on FCP EQ
9081 */
9082 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9083 lpfc_sli4_hba_handle_eqe(phba,
9084 eqe, idx);
9085 fpeq->EQ_processed++;
James Smartba20c852012-08-03 12:36:52 -04009086 }
James Smartba20c852012-08-03 12:36:52 -04009087
James Smart9bd2bff52014-09-03 12:57:30 -04009088 /* Always clear and re-arm the EQ */
9089 lpfc_sli4_eq_release(fpeq,
9090 LPFC_QUEUE_REARM);
9091 }
9092 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart2a76a282012-08-03 12:35:54 -04009093 }
James Smart7e56aa22012-08-03 12:35:34 -04009094 } else {
9095 /* For now, SLI2/3 will still use hbalock */
9096 spin_lock_irqsave(&phba->hbalock, iflags);
9097 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9098 spin_unlock_irqrestore(&phba->hbalock, iflags);
9099 }
James Smart92d7f7b2007-06-17 19:56:38 -05009100 return rc;
9101}
9102
James Smarte59058c2008-08-24 21:49:00 -04009103/**
James Smart3621a712009-04-06 18:47:14 -04009104 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009105 * @phba: Pointer to HBA context object.
9106 *
9107 * This function is called while driver attaches with the
9108 * HBA to setup the extra ring. The extra ring is used
9109 * only when driver needs to support target mode functionality
9110 * or IP over FC functionalities.
9111 *
9112 * This function is called with no lock held.
9113 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009114static int
9115lpfc_extra_ring_setup( struct lpfc_hba *phba)
9116{
9117 struct lpfc_sli *psli;
9118 struct lpfc_sli_ring *pring;
9119
9120 psli = &phba->sli;
9121
9122 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05009123
9124 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009125 pring = &psli->ring[psli->fcp_ring];
James Smart7e56aa22012-08-03 12:35:34 -04009126 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9127 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9128 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9129 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009130
James Smarta4bc3372006-12-02 13:34:16 -05009131 /* and give them to the extra ring */
9132 pring = &psli->ring[psli->extra_ring];
9133
James Smart7e56aa22012-08-03 12:35:34 -04009134 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9135 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9136 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9137 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009138
9139 /* Setup default profile for this ring */
9140 pring->iotag_max = 4096;
9141 pring->num_mask = 1;
9142 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05009143 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9144 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009145 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9146 return 0;
9147}
9148
James Smartcb69f7d2011-12-13 13:21:57 -05009149/* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9150 * @phba: Pointer to HBA context object.
9151 * @iocbq: Pointer to iocb object.
9152 *
9153 * The async_event handler calls this routine when it receives
9154 * an ASYNC_STATUS_CN event from the port. The port generates
9155 * this event when an Abort Sequence request to an rport fails
9156 * twice in succession. The abort could be originated by the
9157 * driver or by the port. The ABTS could have been for an ELS
9158 * or FCP IO. The port only generates this event when an ABTS
9159 * fails to complete after one retry.
9160 */
9161static void
9162lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9163 struct lpfc_iocbq *iocbq)
9164{
9165 struct lpfc_nodelist *ndlp = NULL;
9166 uint16_t rpi = 0, vpi = 0;
9167 struct lpfc_vport *vport = NULL;
9168
9169 /* The rpi in the ulpContext is vport-sensitive. */
9170 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9171 rpi = iocbq->iocb.ulpContext;
9172
9173 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9174 "3092 Port generated ABTS async event "
9175 "on vpi %d rpi %d status 0x%x\n",
9176 vpi, rpi, iocbq->iocb.ulpStatus);
9177
9178 vport = lpfc_find_vport_by_vpid(phba, vpi);
9179 if (!vport)
9180 goto err_exit;
9181 ndlp = lpfc_findnode_rpi(vport, rpi);
9182 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9183 goto err_exit;
9184
9185 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9186 lpfc_sli_abts_recover_port(vport, ndlp);
9187 return;
9188
9189 err_exit:
9190 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9191 "3095 Event Context not found, no "
9192 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9193 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9194 vpi, rpi);
9195}
9196
9197/* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9198 * @phba: pointer to HBA context object.
9199 * @ndlp: nodelist pointer for the impacted rport.
9200 * @axri: pointer to the wcqe containing the failed exchange.
9201 *
9202 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9203 * port. The port generates this event when an abort exchange request to an
9204 * rport fails twice in succession with no reply. The abort could be originated
9205 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9206 */
9207void
9208lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9209 struct lpfc_nodelist *ndlp,
9210 struct sli4_wcqe_xri_aborted *axri)
9211{
9212 struct lpfc_vport *vport;
James Smart5c1db2a2012-03-01 22:34:36 -05009213 uint32_t ext_status = 0;
James Smartcb69f7d2011-12-13 13:21:57 -05009214
James Smart6b5151f2012-01-18 16:24:06 -05009215 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartcb69f7d2011-12-13 13:21:57 -05009216 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9217 "3115 Node Context not found, driver "
9218 "ignoring abts err event\n");
James Smart6b5151f2012-01-18 16:24:06 -05009219 return;
9220 }
9221
James Smartcb69f7d2011-12-13 13:21:57 -05009222 vport = ndlp->vport;
9223 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9224 "3116 Port generated FCP XRI ABORT event on "
James Smart5c1db2a2012-03-01 22:34:36 -05009225 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
James Smart8e668af2013-05-31 17:04:28 -04009226 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
James Smartcb69f7d2011-12-13 13:21:57 -05009227 bf_get(lpfc_wcqe_xa_xri, axri),
James Smart5c1db2a2012-03-01 22:34:36 -05009228 bf_get(lpfc_wcqe_xa_status, axri),
9229 axri->parameter);
James Smartcb69f7d2011-12-13 13:21:57 -05009230
James Smart5c1db2a2012-03-01 22:34:36 -05009231 /*
9232 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9233 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9234 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9235 */
James Smarte3d2b802012-08-14 14:25:43 -04009236 ext_status = axri->parameter & IOERR_PARAM_MASK;
James Smart5c1db2a2012-03-01 22:34:36 -05009237 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9238 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
James Smartcb69f7d2011-12-13 13:21:57 -05009239 lpfc_sli_abts_recover_port(vport, ndlp);
9240}
9241
James Smarte59058c2008-08-24 21:49:00 -04009242/**
James Smart3621a712009-04-06 18:47:14 -04009243 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04009244 * @phba: Pointer to HBA context object.
9245 * @pring: Pointer to driver SLI ring object.
9246 * @iocbq: Pointer to iocb object.
9247 *
9248 * This function is called by the slow ring event handler
9249 * function when there is an ASYNC event iocb in the ring.
9250 * This function is called with no lock held.
9251 * Currently this function handles only temperature related
9252 * ASYNC events. The function decodes the temperature sensor
9253 * event message and posts events for the management applications.
9254 **/
James Smart98c9ea52007-10-27 13:37:33 -04009255static void
James Smart57127f12007-10-27 13:37:05 -04009256lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9257 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9258{
9259 IOCB_t *icmd;
9260 uint16_t evt_code;
James Smart57127f12007-10-27 13:37:05 -04009261 struct temp_event temp_event_data;
9262 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04009263 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04009264
9265 icmd = &iocbq->iocb;
9266 evt_code = icmd->un.asyncstat.evt_code;
James Smart57127f12007-10-27 13:37:05 -04009267
James Smartcb69f7d2011-12-13 13:21:57 -05009268 switch (evt_code) {
9269 case ASYNC_TEMP_WARN:
9270 case ASYNC_TEMP_SAFE:
9271 temp_event_data.data = (uint32_t) icmd->ulpContext;
9272 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9273 if (evt_code == ASYNC_TEMP_WARN) {
9274 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9275 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9276 "0347 Adapter is very hot, please take "
9277 "corrective action. temperature : %d Celsius\n",
9278 (uint32_t) icmd->ulpContext);
9279 } else {
9280 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9281 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9282 "0340 Adapter temperature is OK now. "
9283 "temperature : %d Celsius\n",
9284 (uint32_t) icmd->ulpContext);
9285 }
9286
9287 /* Send temperature change event to applications */
9288 shost = lpfc_shost_from_vport(phba->pport);
9289 fc_host_post_vendor_event(shost, fc_get_event_number(),
9290 sizeof(temp_event_data), (char *) &temp_event_data,
9291 LPFC_NL_VENDOR_ID);
9292 break;
9293 case ASYNC_STATUS_CN:
9294 lpfc_sli_abts_err_handler(phba, iocbq);
9295 break;
9296 default:
James Smarta257bf92009-04-06 18:48:10 -04009297 iocb_w = (uint32_t *) icmd;
James Smartcb69f7d2011-12-13 13:21:57 -05009298 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04009299 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04009300 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04009301 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9302 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9303 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9304 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smartcb69f7d2011-12-13 13:21:57 -05009305 pring->ringno, icmd->un.asyncstat.evt_code,
James Smarta257bf92009-04-06 18:48:10 -04009306 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9307 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9308 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9309 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9310
James Smartcb69f7d2011-12-13 13:21:57 -05009311 break;
James Smart57127f12007-10-27 13:37:05 -04009312 }
James Smart57127f12007-10-27 13:37:05 -04009313}
9314
9315
James Smarte59058c2008-08-24 21:49:00 -04009316/**
James Smart3621a712009-04-06 18:47:14 -04009317 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04009318 * @phba: Pointer to HBA context object.
9319 *
9320 * lpfc_sli_setup sets up rings of the SLI interface with
9321 * number of iocbs per ring and iotags. This function is
9322 * called while driver attach to the HBA and before the
9323 * interrupts are enabled. So there is no need for locking.
9324 *
9325 * This function always returns 0.
9326 **/
dea31012005-04-17 16:05:31 -05009327int
9328lpfc_sli_setup(struct lpfc_hba *phba)
9329{
James Smarted957682007-06-17 19:56:37 -05009330 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05009331 struct lpfc_sli *psli = &phba->sli;
9332 struct lpfc_sli_ring *pring;
9333
James Smart2a76a282012-08-03 12:35:54 -04009334 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9335 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart67d12732012-08-03 12:36:13 -04009336 psli->num_rings += phba->cfg_fcp_io_channel;
dea31012005-04-17 16:05:31 -05009337 psli->sli_flag = 0;
9338 psli->fcp_ring = LPFC_FCP_RING;
9339 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05009340 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05009341
James Bottomley604a3e32005-10-29 10:28:33 -05009342 psli->iocbq_lookup = NULL;
9343 psli->iocbq_lookup_len = 0;
9344 psli->last_iotag = 0;
9345
dea31012005-04-17 16:05:31 -05009346 for (i = 0; i < psli->num_rings; i++) {
9347 pring = &psli->ring[i];
9348 switch (i) {
9349 case LPFC_FCP_RING: /* ring 0 - FCP */
9350 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009351 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9352 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9353 pring->sli.sli3.numCiocb +=
9354 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9355 pring->sli.sli3.numRiocb +=
9356 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9357 pring->sli.sli3.numCiocb +=
9358 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9359 pring->sli.sli3.numRiocb +=
9360 SLI2_IOCB_RSP_R3XTRA_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;
dea31012005-04-17 16:05:31 -05009367 pring->iotag_ctr = 0;
9368 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05009369 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05009370 pring->fast_iotag = pring->iotag_max;
9371 pring->num_mask = 0;
9372 break;
James Smarta4bc3372006-12-02 13:34:16 -05009373 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05009374 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009375 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9376 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9377 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009378 SLI3_IOCB_CMD_SIZE :
9379 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009380 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009381 SLI3_IOCB_RSP_SIZE :
9382 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05009383 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05009384 pring->num_mask = 0;
9385 break;
9386 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9387 /* numCiocb and numRiocb are used in config_port */
James Smart7e56aa22012-08-03 12:35:34 -04009388 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9389 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9390 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009391 SLI3_IOCB_CMD_SIZE :
9392 SLI2_IOCB_CMD_SIZE;
James Smart7e56aa22012-08-03 12:35:34 -04009393 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05009394 SLI3_IOCB_RSP_SIZE :
9395 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05009396 pring->fast_iotag = 0;
9397 pring->iotag_ctr = 0;
9398 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04009399 pring->lpfc_sli_rcv_async_status =
9400 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04009401 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05009402 pring->prt[0].profile = 0; /* Mask 0 */
James Smart6a9c52c2009-10-02 15:16:51 -04009403 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9404 pring->prt[0].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009405 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009406 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009407 pring->prt[1].profile = 0; /* Mask 1 */
James Smart6a9c52c2009-10-02 15:16:51 -04009408 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9409 pring->prt[1].type = FC_TYPE_ELS;
dea31012005-04-17 16:05:31 -05009410 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009411 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05009412 pring->prt[2].profile = 0; /* Mask 2 */
9413 /* NameServer Inquiry */
James Smart6a9c52c2009-10-02 15:16:51 -04009414 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea31012005-04-17 16:05:31 -05009415 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009416 pring->prt[2].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009417 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009418 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009419 pring->prt[3].profile = 0; /* Mask 3 */
9420 /* NameServer response */
James Smart6a9c52c2009-10-02 15:16:51 -04009421 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea31012005-04-17 16:05:31 -05009422 /* NameServer */
James Smart6a9c52c2009-10-02 15:16:51 -04009423 pring->prt[3].type = FC_TYPE_CT;
dea31012005-04-17 16:05:31 -05009424 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05009425 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05009426 break;
9427 }
James Smart7e56aa22012-08-03 12:35:34 -04009428 totiocbsize += (pring->sli.sli3.numCiocb *
9429 pring->sli.sli3.sizeCiocb) +
9430 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
dea31012005-04-17 16:05:31 -05009431 }
James Smarted957682007-06-17 19:56:37 -05009432 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05009433 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04009434 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9435 "SLI2 SLIM Data: x%x x%lx\n",
9436 phba->brd_no, totiocbsize,
9437 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05009438 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05009439 if (phba->cfg_multi_ring_support == 2)
9440 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05009441
9442 return 0;
9443}
9444
James Smarte59058c2008-08-24 21:49:00 -04009445/**
James Smart3621a712009-04-06 18:47:14 -04009446 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04009447 * @phba: Pointer to HBA context object.
9448 *
9449 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9450 * ring. This function also initializes ring indices of each ring.
9451 * This function is called during the initialization of the SLI
9452 * interface of an HBA.
9453 * This function is called with no lock held and always returns
9454 * 1.
9455 **/
dea31012005-04-17 16:05:31 -05009456int
James Smart2e0fef82007-06-17 19:56:36 -05009457lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009458{
9459 struct lpfc_sli *psli;
9460 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05009461 int i;
dea31012005-04-17 16:05:31 -05009462
9463 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05009464 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009465 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05009466 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05009467 /* Initialize list headers for txq and txcmplq as double linked lists */
9468 for (i = 0; i < psli->num_rings; i++) {
9469 pring = &psli->ring[i];
9470 pring->ringno = i;
James Smart7e56aa22012-08-03 12:35:34 -04009471 pring->sli.sli3.next_cmdidx = 0;
9472 pring->sli.sli3.local_getidx = 0;
9473 pring->sli.sli3.cmdidx = 0;
James Smart68e814f2014-05-21 08:04:59 -04009474 pring->flag = 0;
dea31012005-04-17 16:05:31 -05009475 INIT_LIST_HEAD(&pring->txq);
9476 INIT_LIST_HEAD(&pring->txcmplq);
9477 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05009478 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05009479 INIT_LIST_HEAD(&pring->postbufq);
James Smart7e56aa22012-08-03 12:35:34 -04009480 spin_lock_init(&pring->ring_lock);
dea31012005-04-17 16:05:31 -05009481 }
James Smart2e0fef82007-06-17 19:56:36 -05009482 spin_unlock_irq(&phba->hbalock);
9483 return 1;
dea31012005-04-17 16:05:31 -05009484}
9485
James Smarte59058c2008-08-24 21:49:00 -04009486/**
James Smart04c68492009-05-22 14:52:52 -04009487 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9488 * @phba: Pointer to HBA context object.
9489 *
9490 * This routine flushes the mailbox command subsystem. It will unconditionally
9491 * flush all the mailbox commands in the three possible stages in the mailbox
9492 * command sub-system: pending mailbox command queue; the outstanding mailbox
9493 * command; and completed mailbox command queue. It is caller's responsibility
9494 * to make sure that the driver is in the proper state to flush the mailbox
9495 * command sub-system. Namely, the posting of mailbox commands into the
9496 * pending mailbox command queue from the various clients must be stopped;
9497 * either the HBA is in a state that it will never works on the outstanding
9498 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9499 * mailbox command has been completed.
9500 **/
9501static void
9502lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9503{
9504 LIST_HEAD(completions);
9505 struct lpfc_sli *psli = &phba->sli;
9506 LPFC_MBOXQ_t *pmb;
9507 unsigned long iflag;
9508
9509 /* Flush all the mailbox commands in the mbox system */
9510 spin_lock_irqsave(&phba->hbalock, iflag);
9511 /* The pending mailbox command queue */
9512 list_splice_init(&phba->sli.mboxq, &completions);
9513 /* The outstanding active mailbox command */
9514 if (psli->mbox_active) {
9515 list_add_tail(&psli->mbox_active->list, &completions);
9516 psli->mbox_active = NULL;
9517 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9518 }
9519 /* The completed mailbox command queue */
9520 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9521 spin_unlock_irqrestore(&phba->hbalock, iflag);
9522
9523 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9524 while (!list_empty(&completions)) {
9525 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9526 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9527 if (pmb->mbox_cmpl)
9528 pmb->mbox_cmpl(phba, pmb);
9529 }
9530}
9531
9532/**
James Smart3621a712009-04-06 18:47:14 -04009533 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04009534 * @vport: Pointer to virtual port object.
9535 *
9536 * lpfc_sli_host_down is called to clean up the resources
9537 * associated with a vport before destroying virtual
9538 * port data structures.
9539 * This function does following operations:
9540 * - Free discovery resources associated with this virtual
9541 * port.
9542 * - Free iocbs associated with this virtual port in
9543 * the txq.
9544 * - Send abort for all iocb commands associated with this
9545 * vport in txcmplq.
9546 *
9547 * This function is called with no lock held and always returns 1.
9548 **/
dea31012005-04-17 16:05:31 -05009549int
James Smart92d7f7b2007-06-17 19:56:38 -05009550lpfc_sli_host_down(struct lpfc_vport *vport)
9551{
James Smart858c9f62007-06-17 19:56:39 -05009552 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009553 struct lpfc_hba *phba = vport->phba;
9554 struct lpfc_sli *psli = &phba->sli;
9555 struct lpfc_sli_ring *pring;
9556 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05009557 int i;
9558 unsigned long flags = 0;
9559 uint16_t prev_pring_flag;
9560
9561 lpfc_cleanup_discovery_resources(vport);
9562
9563 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009564 for (i = 0; i < psli->num_rings; i++) {
9565 pring = &psli->ring[i];
9566 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04009567 /* Only slow rings */
9568 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009569 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009570 /* Set the lpfc data pending flag */
9571 set_bit(LPFC_DATA_READY, &phba->data_flags);
9572 }
James Smart92d7f7b2007-06-17 19:56:38 -05009573 /*
9574 * Error everything on the txq since these iocbs have not been
9575 * given to the FW yet.
9576 */
James Smart92d7f7b2007-06-17 19:56:38 -05009577 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9578 if (iocb->vport != vport)
9579 continue;
James Smart858c9f62007-06-17 19:56:39 -05009580 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05009581 }
9582
9583 /* Next issue ABTS for everything on the txcmplq */
9584 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9585 list) {
9586 if (iocb->vport != vport)
9587 continue;
9588 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9589 }
9590
9591 pring->flag = prev_pring_flag;
9592 }
9593
9594 spin_unlock_irqrestore(&phba->hbalock, flags);
9595
James Smarta257bf92009-04-06 18:48:10 -04009596 /* Cancel all the IOCBs from the completions list */
9597 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9598 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05009599 return 1;
9600}
9601
James Smarte59058c2008-08-24 21:49:00 -04009602/**
James Smart3621a712009-04-06 18:47:14 -04009603 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04009604 * @phba: Pointer to HBA context object.
9605 *
9606 * This function cleans up all iocb, buffers, mailbox commands
9607 * while shutting down the HBA. This function is called with no
9608 * lock held and always returns 1.
9609 * This function does the following to cleanup driver resources:
9610 * - Free discovery resources for each virtual port
9611 * - Cleanup any pending fabric iocbs
9612 * - Iterate through the iocb txq and free each entry
9613 * in the list.
9614 * - Free up any buffer posted to the HBA
9615 * - Free mailbox commands in the mailbox queue.
9616 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009617int
James Smart2e0fef82007-06-17 19:56:36 -05009618lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05009619{
James Smart2534ba72007-04-25 09:52:20 -04009620 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05009621 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05009622 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05009623 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05009624 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04009625 int i;
9626
9627 /* Shutdown the mailbox command sub-system */
James Smart618a5232012-06-12 13:54:36 -04009628 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
dea31012005-04-17 16:05:31 -05009629
dea31012005-04-17 16:05:31 -05009630 lpfc_hba_down_prep(phba);
9631
James Smart92d7f7b2007-06-17 19:56:38 -05009632 lpfc_fabric_abort_hba(phba);
9633
James Smart2e0fef82007-06-17 19:56:36 -05009634 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009635 for (i = 0; i < psli->num_rings; i++) {
9636 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04009637 /* Only slow rings */
9638 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05009639 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04009640 /* Set the lpfc data pending flag */
9641 set_bit(LPFC_DATA_READY, &phba->data_flags);
9642 }
dea31012005-04-17 16:05:31 -05009643
9644 /*
9645 * Error everything on the txq since these iocbs have not been
9646 * given to the FW yet.
9647 */
James Smart2534ba72007-04-25 09:52:20 -04009648 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05009649 }
James Smart2e0fef82007-06-17 19:56:36 -05009650 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05009651
James Smarta257bf92009-04-06 18:48:10 -04009652 /* Cancel all the IOCBs from the completions list */
9653 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9654 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04009655
James Smart0ff10d42008-01-11 01:52:36 -05009656 spin_lock_irqsave(&phba->hbalock, flags);
9657 list_splice_init(&phba->elsbuf, &completions);
9658 phba->elsbuf_cnt = 0;
9659 phba->elsbuf_prev_cnt = 0;
9660 spin_unlock_irqrestore(&phba->hbalock, flags);
9661
9662 while (!list_empty(&completions)) {
9663 list_remove_head(&completions, buf_ptr,
9664 struct lpfc_dmabuf, list);
9665 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9666 kfree(buf_ptr);
9667 }
9668
dea31012005-04-17 16:05:31 -05009669 /* Return any active mbox cmds */
9670 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05009671
James Smartda0436e2009-05-22 14:51:39 -04009672 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009673 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04009674 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05009675
James Smartda0436e2009-05-22 14:51:39 -04009676 return 1;
9677}
James Smart92d7f7b2007-06-17 19:56:38 -05009678
James Smartda0436e2009-05-22 14:51:39 -04009679/**
James Smart3621a712009-04-06 18:47:14 -04009680 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04009681 * @srcp: Source memory pointer.
9682 * @destp: Destination memory pointer.
9683 * @cnt: Number of words required to be copied.
9684 *
9685 * This function is used for copying data between driver memory
9686 * and the SLI memory. This function also changes the endianness
9687 * of each word if native endianness is different from SLI
9688 * endianness. This function can be called with or without
9689 * lock.
9690 **/
dea31012005-04-17 16:05:31 -05009691void
9692lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9693{
9694 uint32_t *src = srcp;
9695 uint32_t *dest = destp;
9696 uint32_t ldata;
9697 int i;
9698
9699 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9700 ldata = *src;
9701 ldata = le32_to_cpu(ldata);
9702 *dest = ldata;
9703 src++;
9704 dest++;
9705 }
9706}
9707
James Smarte59058c2008-08-24 21:49:00 -04009708
9709/**
James Smarta0c87cb2009-07-19 10:01:10 -04009710 * lpfc_sli_bemem_bcopy - SLI memory copy function
9711 * @srcp: Source memory pointer.
9712 * @destp: Destination memory pointer.
9713 * @cnt: Number of words required to be copied.
9714 *
9715 * This function is used for copying data between a data structure
9716 * with big endian representation to local endianness.
9717 * This function can be called with or without lock.
9718 **/
9719void
9720lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9721{
9722 uint32_t *src = srcp;
9723 uint32_t *dest = destp;
9724 uint32_t ldata;
9725 int i;
9726
9727 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9728 ldata = *src;
9729 ldata = be32_to_cpu(ldata);
9730 *dest = ldata;
9731 src++;
9732 dest++;
9733 }
9734}
9735
9736/**
James Smart3621a712009-04-06 18:47:14 -04009737 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04009738 * @phba: Pointer to HBA context object.
9739 * @pring: Pointer to driver SLI ring object.
9740 * @mp: Pointer to driver buffer object.
9741 *
9742 * This function is called with no lock held.
9743 * It always return zero after adding the buffer to the postbufq
9744 * buffer list.
9745 **/
dea31012005-04-17 16:05:31 -05009746int
James Smart2e0fef82007-06-17 19:56:36 -05009747lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9748 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05009749{
9750 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9751 later */
James Smart2e0fef82007-06-17 19:56:36 -05009752 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009753 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05009754 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05009755 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009756 return 0;
9757}
9758
James Smarte59058c2008-08-24 21:49:00 -04009759/**
James Smart3621a712009-04-06 18:47:14 -04009760 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04009761 * @phba: Pointer to HBA context object.
9762 *
9763 * When HBQ is enabled, buffers are searched based on tags. This function
9764 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9765 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9766 * does not conflict with tags of buffer posted for unsolicited events.
9767 * The function returns the allocated tag. The function is called with
9768 * no locks held.
9769 **/
James Smart76bb24e2007-10-27 13:38:00 -04009770uint32_t
9771lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9772{
9773 spin_lock_irq(&phba->hbalock);
9774 phba->buffer_tag_count++;
9775 /*
9776 * Always set the QUE_BUFTAG_BIT to distiguish between
9777 * a tag assigned by HBQ.
9778 */
9779 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9780 spin_unlock_irq(&phba->hbalock);
9781 return phba->buffer_tag_count;
9782}
9783
James Smarte59058c2008-08-24 21:49:00 -04009784/**
James Smart3621a712009-04-06 18:47:14 -04009785 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04009786 * @phba: Pointer to HBA context object.
9787 * @pring: Pointer to driver SLI ring object.
9788 * @tag: Buffer tag.
9789 *
9790 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9791 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9792 * iocb is posted to the response ring with the tag of the buffer.
9793 * This function searches the pring->postbufq list using the tag
9794 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9795 * iocb. If the buffer is found then lpfc_dmabuf object of the
9796 * buffer is returned to the caller else NULL is returned.
9797 * This function is called with no lock held.
9798 **/
James Smart76bb24e2007-10-27 13:38:00 -04009799struct lpfc_dmabuf *
9800lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9801 uint32_t tag)
9802{
9803 struct lpfc_dmabuf *mp, *next_mp;
9804 struct list_head *slp = &pring->postbufq;
9805
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009806 /* Search postbufq, from the beginning, looking for a match on tag */
James Smart76bb24e2007-10-27 13:38:00 -04009807 spin_lock_irq(&phba->hbalock);
9808 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9809 if (mp->buffer_tag == tag) {
9810 list_del_init(&mp->list);
9811 pring->postbufq_cnt--;
9812 spin_unlock_irq(&phba->hbalock);
9813 return mp;
9814 }
9815 }
9816
9817 spin_unlock_irq(&phba->hbalock);
9818 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04009819 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04009820 "ring %d Data x%lx x%p x%p x%x\n",
9821 pring->ringno, (unsigned long) tag,
9822 slp->next, slp->prev, pring->postbufq_cnt);
9823
9824 return NULL;
9825}
dea31012005-04-17 16:05:31 -05009826
James Smarte59058c2008-08-24 21:49:00 -04009827/**
James Smart3621a712009-04-06 18:47:14 -04009828 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04009829 * @phba: Pointer to HBA context object.
9830 * @pring: Pointer to driver SLI ring object.
9831 * @phys: DMA address of the buffer.
9832 *
9833 * This function searches the buffer list using the dma_address
9834 * of unsolicited event to find the driver's lpfc_dmabuf object
9835 * corresponding to the dma_address. The function returns the
9836 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9837 * This function is called by the ct and els unsolicited event
9838 * handlers to get the buffer associated with the unsolicited
9839 * event.
9840 *
9841 * This function is called with no lock held.
9842 **/
dea31012005-04-17 16:05:31 -05009843struct lpfc_dmabuf *
9844lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9845 dma_addr_t phys)
9846{
9847 struct lpfc_dmabuf *mp, *next_mp;
9848 struct list_head *slp = &pring->postbufq;
9849
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009850 /* Search postbufq, from the beginning, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05009851 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009852 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9853 if (mp->phys == phys) {
9854 list_del_init(&mp->list);
9855 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05009856 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009857 return mp;
9858 }
9859 }
9860
James Smart2e0fef82007-06-17 19:56:36 -05009861 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05009862 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04009863 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05009864 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009865 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05009866 slp->next, slp->prev, pring->postbufq_cnt);
9867 return NULL;
9868}
9869
James Smarte59058c2008-08-24 21:49:00 -04009870/**
James Smart3621a712009-04-06 18:47:14 -04009871 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04009872 * @phba: Pointer to HBA context object.
9873 * @cmdiocb: Pointer to driver command iocb object.
9874 * @rspiocb: Pointer to driver response iocb object.
9875 *
9876 * This function is the completion handler for the abort iocbs for
9877 * ELS commands. This function is called from the ELS ring event
9878 * handler with no lock held. This function frees memory resources
9879 * associated with the abort iocb.
9880 **/
dea31012005-04-17 16:05:31 -05009881static void
James Smart2e0fef82007-06-17 19:56:36 -05009882lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9883 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05009884{
James Smart2e0fef82007-06-17 19:56:36 -05009885 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04009886 uint16_t abort_iotag, abort_context;
James Smartff78d8f2011-12-13 13:21:35 -05009887 struct lpfc_iocbq *abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04009888
9889 if (irsp->ulpStatus) {
James Smartff78d8f2011-12-13 13:21:35 -05009890
9891 /*
9892 * Assume that the port already completed and returned, or
9893 * will return the iocb. Just Log the message.
9894 */
James Smart2680eea2007-04-25 09:52:55 -04009895 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9896 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9897
James Smart2e0fef82007-06-17 19:56:36 -05009898 spin_lock_irq(&phba->hbalock);
James Smart45ed1192009-10-02 15:17:02 -04009899 if (phba->sli_rev < LPFC_SLI_REV4) {
9900 if (abort_iotag != 0 &&
9901 abort_iotag <= phba->sli.last_iotag)
9902 abort_iocb =
9903 phba->sli.iocbq_lookup[abort_iotag];
9904 } else
9905 /* For sli4 the abort_tag is the XRI,
9906 * so the abort routine puts the iotag of the iocb
9907 * being aborted in the context field of the abort
9908 * IOCB.
9909 */
9910 abort_iocb = phba->sli.iocbq_lookup[abort_context];
James Smart2680eea2007-04-25 09:52:55 -04009911
James Smart2a9bf3d2010-06-07 15:24:45 -04009912 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9913 "0327 Cannot abort els iocb %p "
9914 "with tag %x context %x, abort status %x, "
9915 "abort code %x\n",
9916 abort_iocb, abort_iotag, abort_context,
9917 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04009918
James Smartff78d8f2011-12-13 13:21:35 -05009919 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04009920 }
James Bottomley604a3e32005-10-29 10:28:33 -05009921 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05009922 return;
9923}
9924
James Smarte59058c2008-08-24 21:49:00 -04009925/**
James Smart3621a712009-04-06 18:47:14 -04009926 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04009927 * @phba: Pointer to HBA context object.
9928 * @cmdiocb: Pointer to driver command iocb object.
9929 * @rspiocb: Pointer to driver response iocb object.
9930 *
9931 * The function is called from SLI ring event handler with no
9932 * lock held. This function is the completion handler for ELS commands
9933 * which are aborted. The function frees memory resources used for
9934 * the aborted ELS commands.
9935 **/
James Smart92d7f7b2007-06-17 19:56:38 -05009936static void
9937lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9938 struct lpfc_iocbq *rspiocb)
9939{
9940 IOCB_t *irsp = &rspiocb->iocb;
9941
9942 /* ELS cmd tag <ulpIoTag> completes */
9943 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04009944 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05009945 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04009946 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05009947 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05009948 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9949 lpfc_ct_free_iocb(phba, cmdiocb);
9950 else
9951 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05009952 return;
9953}
9954
James Smarte59058c2008-08-24 21:49:00 -04009955/**
James Smart5af5eee2010-10-22 11:06:38 -04009956 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04009957 * @phba: Pointer to HBA context object.
9958 * @pring: Pointer to driver SLI ring object.
9959 * @cmdiocb: Pointer to driver command iocb object.
9960 *
James Smart5af5eee2010-10-22 11:06:38 -04009961 * This function issues an abort iocb for the provided command iocb down to
9962 * the port. Other than the case the outstanding command iocb is an abort
9963 * request, this function issues abort out unconditionally. This function is
9964 * called with hbalock held. The function returns 0 when it fails due to
9965 * memory allocation failure or when the command iocb is an abort request.
James Smarte59058c2008-08-24 21:49:00 -04009966 **/
James Smart5af5eee2010-10-22 11:06:38 -04009967static int
9968lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05009969 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05009970{
James Smart2e0fef82007-06-17 19:56:36 -05009971 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04009972 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05009973 IOCB_t *icmd = NULL;
9974 IOCB_t *iabt = NULL;
James Smart9bd2bff52014-09-03 12:57:30 -04009975 int ring_number;
James Smart5af5eee2010-10-22 11:06:38 -04009976 int retval;
James Smart7e56aa22012-08-03 12:35:34 -04009977 unsigned long iflags;
James Smart07951072007-04-25 09:51:38 -04009978
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01009979 lockdep_assert_held(&phba->hbalock);
9980
James Smart92d7f7b2007-06-17 19:56:38 -05009981 /*
9982 * There are certain command types we don't want to abort. And we
9983 * don't want to abort commands that are already in the process of
9984 * being aborted.
James Smart07951072007-04-25 09:51:38 -04009985 */
9986 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05009987 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05009988 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9989 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04009990 return 0;
9991
dea31012005-04-17 16:05:31 -05009992 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05009993 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05009994 if (abtsiocbp == NULL)
9995 return 0;
dea31012005-04-17 16:05:31 -05009996
James Smart07951072007-04-25 09:51:38 -04009997 /* This signals the response to set the correct status
James Smart341af102010-01-26 23:07:37 -05009998 * before calling the completion handler
James Smart07951072007-04-25 09:51:38 -04009999 */
10000 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
10001
dea31012005-04-17 16:05:31 -050010002 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -040010003 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
10004 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smart45ed1192009-10-02 15:17:02 -040010005 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartda0436e2009-05-22 14:51:39 -040010006 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
James Smart45ed1192009-10-02 15:17:02 -040010007 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
10008 }
James Smartda0436e2009-05-22 14:51:39 -040010009 else
10010 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010011 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -040010012 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -050010013
James Smart5ffc2662009-11-18 15:39:44 -050010014 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10015 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -050010016 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
10017 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010018 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
10019 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010020
James Smart2e0fef82007-06-17 19:56:36 -050010021 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -040010022 iabt->ulpCommand = CMD_ABORT_XRI_CN;
10023 else
10024 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
10025
10026 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -040010027
James Smarte8b62012007-08-02 11:10:09 -040010028 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10029 "0339 Abort xri x%x, original iotag x%x, "
10030 "abort cmd iotag x%x\n",
James Smart2a9bf3d2010-06-07 15:24:45 -040010031 iabt->un.acxri.abortIoTag,
James Smarte8b62012007-08-02 11:10:09 -040010032 iabt->un.acxri.abortContextTag,
James Smart2a9bf3d2010-06-07 15:24:45 -040010033 abtsiocbp->iotag);
James Smart7e56aa22012-08-03 12:35:34 -040010034
10035 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smart9bd2bff52014-09-03 12:57:30 -040010036 ring_number =
10037 lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
10038 if (unlikely(ring_number == LPFC_HBA_ERROR))
10039 return 0;
10040 pring = &phba->sli.ring[ring_number];
James Smart7e56aa22012-08-03 12:35:34 -040010041 /* Note: both hbalock and ring_lock need to be set here */
10042 spin_lock_irqsave(&pring->ring_lock, iflags);
10043 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10044 abtsiocbp, 0);
10045 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10046 } else {
10047 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10048 abtsiocbp, 0);
10049 }
James Smart07951072007-04-25 09:51:38 -040010050
James Smartd7c255b2008-08-24 21:50:00 -040010051 if (retval)
10052 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart5af5eee2010-10-22 11:06:38 -040010053
10054 /*
10055 * Caller to this routine should check for IOCB_ERROR
10056 * and handle it properly. This routine no longer removes
10057 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10058 */
10059 return retval;
10060}
10061
10062/**
10063 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10064 * @phba: Pointer to HBA context object.
10065 * @pring: Pointer to driver SLI ring object.
10066 * @cmdiocb: Pointer to driver command iocb object.
10067 *
10068 * This function issues an abort iocb for the provided command iocb. In case
10069 * of unloading, the abort iocb will not be issued to commands on the ELS
10070 * ring. Instead, the callback function shall be changed to those commands
10071 * so that nothing happens when them finishes. This function is called with
10072 * hbalock held. The function returns 0 when the command iocb is an abort
10073 * request.
10074 **/
10075int
10076lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10077 struct lpfc_iocbq *cmdiocb)
10078{
10079 struct lpfc_vport *vport = cmdiocb->vport;
10080 int retval = IOCB_ERROR;
10081 IOCB_t *icmd = NULL;
10082
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010010083 lockdep_assert_held(&phba->hbalock);
10084
James Smart5af5eee2010-10-22 11:06:38 -040010085 /*
10086 * There are certain command types we don't want to abort. And we
10087 * don't want to abort commands that are already in the process of
10088 * being aborted.
10089 */
10090 icmd = &cmdiocb->iocb;
10091 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10092 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10093 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10094 return 0;
10095
10096 /*
10097 * If we're unloading, don't abort iocb on the ELS ring, but change
10098 * the callback so that nothing happens when it finishes.
10099 */
10100 if ((vport->load_flag & FC_UNLOADING) &&
10101 (pring->ringno == LPFC_ELS_RING)) {
10102 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10103 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10104 else
10105 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10106 goto abort_iotag_exit;
10107 }
10108
10109 /* Now, we try to issue the abort to the cmdiocb out */
10110 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10111
James Smart07951072007-04-25 09:51:38 -040010112abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -050010113 /*
10114 * Caller to this routine should check for IOCB_ERROR
10115 * and handle it properly. This routine no longer removes
10116 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -040010117 */
James Smart2e0fef82007-06-17 19:56:36 -050010118 return retval;
dea31012005-04-17 16:05:31 -050010119}
10120
James Smarte59058c2008-08-24 21:49:00 -040010121/**
James Smart5af5eee2010-10-22 11:06:38 -040010122 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10123 * @phba: pointer to lpfc HBA data structure.
10124 *
10125 * This routine will abort all pending and outstanding iocbs to an HBA.
10126 **/
10127void
10128lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10129{
10130 struct lpfc_sli *psli = &phba->sli;
10131 struct lpfc_sli_ring *pring;
10132 int i;
10133
10134 for (i = 0; i < psli->num_rings; i++) {
10135 pring = &psli->ring[i];
James Smartdb55fba2014-04-04 13:52:02 -040010136 lpfc_sli_abort_iocb_ring(phba, pring);
James Smart5af5eee2010-10-22 11:06:38 -040010137 }
10138}
10139
10140/**
James Smart3621a712009-04-06 18:47:14 -040010141 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -040010142 * @iocbq: Pointer to driver iocb object.
10143 * @vport: Pointer to driver virtual port object.
10144 * @tgt_id: SCSI ID of the target.
10145 * @lun_id: LUN ID of the scsi device.
10146 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10147 *
James Smart3621a712009-04-06 18:47:14 -040010148 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -040010149 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10150 * 0 if the filtering criteria is met for the given iocb and will return
10151 * 1 if the filtering criteria is not met.
10152 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10153 * given iocb is for the SCSI device specified by vport, tgt_id and
10154 * lun_id parameter.
10155 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10156 * given iocb is for the SCSI target specified by vport and tgt_id
10157 * parameters.
10158 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10159 * given iocb is for the SCSI host associated with the given vport.
10160 * This function is called with no locks held.
10161 **/
dea31012005-04-17 16:05:31 -050010162static int
James Smart51ef4c22007-08-02 11:10:31 -040010163lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10164 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010165 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010166{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010167 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010168 int rc = 1;
10169
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010170 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10171 return rc;
10172
James Smart51ef4c22007-08-02 11:10:31 -040010173 if (iocbq->vport != vport)
10174 return rc;
10175
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010176 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010177
James Smart495a7142008-06-14 22:52:59 -040010178 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -050010179 return rc;
10180
10181 switch (ctx_cmd) {
10182 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -040010183 if ((lpfc_cmd->rdata->pnode) &&
10184 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10185 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -050010186 rc = 0;
10187 break;
10188 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -040010189 if ((lpfc_cmd->rdata->pnode) &&
10190 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -050010191 rc = 0;
10192 break;
dea31012005-04-17 16:05:31 -050010193 case LPFC_CTX_HOST:
10194 rc = 0;
10195 break;
10196 default:
10197 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070010198 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -050010199 break;
10200 }
10201
10202 return rc;
10203}
10204
James Smarte59058c2008-08-24 21:49:00 -040010205/**
James Smart3621a712009-04-06 18:47:14 -040010206 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -040010207 * @vport: Pointer to virtual port.
10208 * @tgt_id: SCSI ID of the target.
10209 * @lun_id: LUN ID of the scsi device.
10210 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10211 *
10212 * This function returns number of FCP commands pending for the vport.
10213 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10214 * commands pending on the vport associated with SCSI device specified
10215 * by tgt_id and lun_id parameters.
10216 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10217 * commands pending on the vport associated with SCSI target specified
10218 * by tgt_id parameter.
10219 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10220 * commands pending on the vport.
10221 * This function returns the number of iocbs which satisfy the filter.
10222 * This function is called without any lock held.
10223 **/
dea31012005-04-17 16:05:31 -050010224int
James Smart51ef4c22007-08-02 11:10:31 -040010225lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10226 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -050010227{
James Smart51ef4c22007-08-02 11:10:31 -040010228 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010229 struct lpfc_iocbq *iocbq;
10230 int sum, i;
dea31012005-04-17 16:05:31 -050010231
Johannes Thumshirn31979002016-07-18 16:06:03 +020010232 spin_lock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010233 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10234 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010235
James Smart51ef4c22007-08-02 11:10:31 -040010236 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10237 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010238 sum++;
dea31012005-04-17 16:05:31 -050010239 }
Johannes Thumshirn31979002016-07-18 16:06:03 +020010240 spin_unlock_irq(&phba->hbalock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010241
dea31012005-04-17 16:05:31 -050010242 return sum;
10243}
10244
James Smarte59058c2008-08-24 21:49:00 -040010245/**
James Smart3621a712009-04-06 18:47:14 -040010246 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -040010247 * @phba: Pointer to HBA context object
10248 * @cmdiocb: Pointer to command iocb object.
10249 * @rspiocb: Pointer to response iocb object.
10250 *
10251 * This function is called when an aborted FCP iocb completes. This
10252 * function is called by the ring event handler with no lock held.
10253 * This function frees the iocb.
10254 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010255void
James Smart2e0fef82007-06-17 19:56:36 -050010256lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10257 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010258{
James Smartcb69f7d2011-12-13 13:21:57 -050010259 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smart8e668af2013-05-31 17:04:28 -040010260 "3096 ABORT_XRI_CN completing on rpi x%x "
James Smartcb69f7d2011-12-13 13:21:57 -050010261 "original iotag x%x, abort cmd iotag x%x "
10262 "status 0x%x, reason 0x%x\n",
10263 cmdiocb->iocb.un.acxri.abortContextTag,
10264 cmdiocb->iocb.un.acxri.abortIoTag,
10265 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10266 rspiocb->iocb.un.ulpWord[4]);
James Bottomley604a3e32005-10-29 10:28:33 -050010267 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010268 return;
10269}
10270
James Smarte59058c2008-08-24 21:49:00 -040010271/**
James Smart3621a712009-04-06 18:47:14 -040010272 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -040010273 * @vport: Pointer to virtual port.
10274 * @pring: Pointer to driver SLI ring object.
10275 * @tgt_id: SCSI ID of the target.
10276 * @lun_id: LUN ID of the scsi device.
10277 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10278 *
10279 * This function sends an abort command for every SCSI command
10280 * associated with the given virtual port pending on the ring
10281 * filtered by lpfc_sli_validate_fcp_iocb function.
10282 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10283 * FCP iocbs associated with lun specified by tgt_id and lun_id
10284 * parameters
10285 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10286 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10287 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10288 * FCP iocbs associated with virtual port.
10289 * This function returns number of iocbs it failed to abort.
10290 * This function is called with no locks held.
10291 **/
dea31012005-04-17 16:05:31 -050010292int
James Smart51ef4c22007-08-02 11:10:31 -040010293lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10294 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -050010295{
James Smart51ef4c22007-08-02 11:10:31 -040010296 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010297 struct lpfc_iocbq *iocbq;
10298 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -050010299 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -050010300 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010301 int i;
dea31012005-04-17 16:05:31 -050010302
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010303 for (i = 1; i <= phba->sli.last_iotag; i++) {
10304 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -050010305
James Smart51ef4c22007-08-02 11:10:31 -040010306 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -050010307 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -050010308 continue;
10309
James Smartafbd8d82013-09-06 12:22:13 -040010310 /*
10311 * If the iocbq is already being aborted, don't take a second
10312 * action, but do count it.
10313 */
10314 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10315 continue;
10316
dea31012005-04-17 16:05:31 -050010317 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010318 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -050010319 if (abtsiocb == NULL) {
10320 errcnt++;
10321 continue;
10322 }
dea31012005-04-17 16:05:31 -050010323
James Smartafbd8d82013-09-06 12:22:13 -040010324 /* indicate the IO is being aborted by the driver. */
10325 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10326
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040010327 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -050010328 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10329 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -040010330 if (phba->sli_rev == LPFC_SLI_REV4)
10331 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10332 else
10333 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -050010334 abtsiocb->iocb.ulpLe = 1;
10335 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smartafbd8d82013-09-06 12:22:13 -040010336 abtsiocb->vport = vport;
dea31012005-04-17 16:05:31 -050010337
James Smart5ffc2662009-11-18 15:39:44 -050010338 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10339 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
James Smart341af102010-01-26 23:07:37 -050010340 if (iocbq->iocb_flag & LPFC_IO_FCP)
10341 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010342 if (iocbq->iocb_flag & LPFC_IO_FOF)
10343 abtsiocb->iocb_flag |= LPFC_IO_FOF;
James Smart5ffc2662009-11-18 15:39:44 -050010344
James Smart2e0fef82007-06-17 19:56:36 -050010345 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -050010346 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10347 else
10348 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10349
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -040010350 /* Setup callback routine and issue the command. */
10351 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -040010352 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10353 abtsiocb, 0);
dea31012005-04-17 16:05:31 -050010354 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -050010355 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -050010356 errcnt++;
10357 continue;
10358 }
10359 }
10360
10361 return errcnt;
10362}
10363
James Smarte59058c2008-08-24 21:49:00 -040010364/**
James Smart98912dda2014-04-04 13:52:31 -040010365 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10366 * @vport: Pointer to virtual port.
10367 * @pring: Pointer to driver SLI ring object.
10368 * @tgt_id: SCSI ID of the target.
10369 * @lun_id: LUN ID of the scsi device.
10370 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10371 *
10372 * This function sends an abort command for every SCSI command
10373 * associated with the given virtual port pending on the ring
10374 * filtered by lpfc_sli_validate_fcp_iocb function.
10375 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10376 * FCP iocbs associated with lun specified by tgt_id and lun_id
10377 * parameters
10378 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10379 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10380 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10381 * FCP iocbs associated with virtual port.
10382 * This function returns number of iocbs it aborted .
10383 * This function is called with no locks held right after a taskmgmt
10384 * command is sent.
10385 **/
10386int
10387lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10388 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10389{
10390 struct lpfc_hba *phba = vport->phba;
James Smart8c50d252014-09-03 12:58:16 -040010391 struct lpfc_scsi_buf *lpfc_cmd;
James Smart98912dda2014-04-04 13:52:31 -040010392 struct lpfc_iocbq *abtsiocbq;
James Smart8c50d252014-09-03 12:58:16 -040010393 struct lpfc_nodelist *ndlp;
James Smart98912dda2014-04-04 13:52:31 -040010394 struct lpfc_iocbq *iocbq;
10395 IOCB_t *icmd;
10396 int sum, i, ret_val;
10397 unsigned long iflags;
10398 struct lpfc_sli_ring *pring_s4;
10399 uint32_t ring_number;
10400
10401 spin_lock_irq(&phba->hbalock);
10402
10403 /* all I/Os are in process of being flushed */
10404 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10405 spin_unlock_irq(&phba->hbalock);
10406 return 0;
10407 }
10408 sum = 0;
10409
10410 for (i = 1; i <= phba->sli.last_iotag; i++) {
10411 iocbq = phba->sli.iocbq_lookup[i];
10412
10413 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10414 cmd) != 0)
10415 continue;
10416
10417 /*
10418 * If the iocbq is already being aborted, don't take a second
10419 * action, but do count it.
10420 */
10421 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10422 continue;
10423
10424 /* issue ABTS for this IOCB based on iotag */
10425 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10426 if (abtsiocbq == NULL)
10427 continue;
10428
10429 icmd = &iocbq->iocb;
10430 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10431 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10432 if (phba->sli_rev == LPFC_SLI_REV4)
10433 abtsiocbq->iocb.un.acxri.abortIoTag =
10434 iocbq->sli4_xritag;
10435 else
10436 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10437 abtsiocbq->iocb.ulpLe = 1;
10438 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10439 abtsiocbq->vport = vport;
10440
10441 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10442 abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
10443 if (iocbq->iocb_flag & LPFC_IO_FCP)
10444 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
James Smart9bd2bff52014-09-03 12:57:30 -040010445 if (iocbq->iocb_flag & LPFC_IO_FOF)
10446 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
James Smart98912dda2014-04-04 13:52:31 -040010447
James Smart8c50d252014-09-03 12:58:16 -040010448 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10449 ndlp = lpfc_cmd->rdata->pnode;
10450
10451 if (lpfc_is_link_up(phba) &&
10452 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
James Smart98912dda2014-04-04 13:52:31 -040010453 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10454 else
10455 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10456
10457 /* Setup callback routine and issue the command. */
10458 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10459
10460 /*
10461 * Indicate the IO is being aborted by the driver and set
10462 * the caller's flag into the aborted IO.
10463 */
10464 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10465
10466 if (phba->sli_rev == LPFC_SLI_REV4) {
10467 ring_number = MAX_SLI3_CONFIGURED_RINGS +
10468 iocbq->fcp_wqidx;
10469 pring_s4 = &phba->sli.ring[ring_number];
10470 /* Note: both hbalock and ring_lock must be set here */
10471 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10472 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10473 abtsiocbq, 0);
10474 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10475 } else {
10476 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10477 abtsiocbq, 0);
10478 }
10479
10480
10481 if (ret_val == IOCB_ERROR)
10482 __lpfc_sli_release_iocbq(phba, abtsiocbq);
10483 else
10484 sum++;
10485 }
10486 spin_unlock_irq(&phba->hbalock);
10487 return sum;
10488}
10489
10490/**
James Smart3621a712009-04-06 18:47:14 -040010491 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -040010492 * @phba: Pointer to HBA context object.
10493 * @cmdiocbq: Pointer to command iocb.
10494 * @rspiocbq: Pointer to response iocb.
10495 *
10496 * This function is the completion handler for iocbs issued using
10497 * lpfc_sli_issue_iocb_wait function. This function is called by the
10498 * ring event handler function without any lock held. This function
10499 * can be called from both worker thread context and interrupt
10500 * context. This function also can be called from other thread which
10501 * cleans up the SLI layer objects.
10502 * This function copy the contents of the response iocb to the
10503 * response iocb memory object provided by the caller of
10504 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10505 * sleeps for the iocb completion.
10506 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010507static void
10508lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10509 struct lpfc_iocbq *cmdiocbq,
10510 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -050010511{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010512 wait_queue_head_t *pdone_q;
10513 unsigned long iflags;
James Smart0f65ff62010-02-26 14:14:23 -050010514 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -050010515
James Smart2e0fef82007-06-17 19:56:36 -050010516 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5a0916b2013-07-15 18:31:42 -040010517 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10518
10519 /*
10520 * A time out has occurred for the iocb. If a time out
10521 * completion handler has been supplied, call it. Otherwise,
10522 * just free the iocbq.
10523 */
10524
10525 spin_unlock_irqrestore(&phba->hbalock, iflags);
10526 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10527 cmdiocbq->wait_iocb_cmpl = NULL;
10528 if (cmdiocbq->iocb_cmpl)
10529 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10530 else
10531 lpfc_sli_release_iocbq(phba, cmdiocbq);
10532 return;
10533 }
10534
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010535 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10536 if (cmdiocbq->context2 && rspiocbq)
10537 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10538 &rspiocbq->iocb, sizeof(IOCB_t));
10539
James Smart0f65ff62010-02-26 14:14:23 -050010540 /* Set the exchange busy flag for task management commands */
10541 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10542 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10543 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10544 cur_iocbq);
10545 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10546 }
10547
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010548 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010549 if (pdone_q)
10550 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -050010551 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -050010552 return;
10553}
10554
James Smarte59058c2008-08-24 21:49:00 -040010555/**
James Smartd11e31d2009-06-10 17:23:06 -040010556 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10557 * @phba: Pointer to HBA context object..
10558 * @piocbq: Pointer to command iocb.
10559 * @flag: Flag to test.
10560 *
10561 * This routine grabs the hbalock and then test the iocb_flag to
10562 * see if the passed in flag is set.
10563 * Returns:
10564 * 1 if flag is set.
10565 * 0 if flag is not set.
10566 **/
10567static int
10568lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10569 struct lpfc_iocbq *piocbq, uint32_t flag)
10570{
10571 unsigned long iflags;
10572 int ret;
10573
10574 spin_lock_irqsave(&phba->hbalock, iflags);
10575 ret = piocbq->iocb_flag & flag;
10576 spin_unlock_irqrestore(&phba->hbalock, iflags);
10577 return ret;
10578
10579}
10580
10581/**
James Smart3621a712009-04-06 18:47:14 -040010582 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -040010583 * @phba: Pointer to HBA context object..
10584 * @pring: Pointer to sli ring.
10585 * @piocb: Pointer to command iocb.
10586 * @prspiocbq: Pointer to response iocb.
10587 * @timeout: Timeout in number of seconds.
10588 *
10589 * This function issues the iocb to firmware and waits for the
James Smart5a0916b2013-07-15 18:31:42 -040010590 * iocb to complete. The iocb_cmpl field of the shall be used
10591 * to handle iocbs which time out. If the field is NULL, the
10592 * function shall free the iocbq structure. If more clean up is
10593 * needed, the caller is expected to provide a completion function
10594 * that will provide the needed clean up. If the iocb command is
10595 * not completed within timeout seconds, the function will either
10596 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10597 * completion function set in the iocb_cmpl field and then return
10598 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10599 * resources if this function returns IOCB_TIMEDOUT.
James Smarte59058c2008-08-24 21:49:00 -040010600 * The function waits for the iocb completion using an
10601 * non-interruptible wait.
10602 * This function will sleep while waiting for iocb completion.
10603 * So, this function should not be called from any context which
10604 * does not allow sleeping. Due to the same reason, this function
10605 * cannot be called with interrupt disabled.
10606 * This function assumes that the iocb completions occur while
10607 * this function sleep. So, this function cannot be called from
10608 * the thread which process iocb completion for this ring.
10609 * This function clears the iocb_flag of the iocb object before
10610 * issuing the iocb and the iocb completion handler sets this
10611 * flag and wakes this thread when the iocb completes.
10612 * The contents of the response iocb will be copied to prspiocbq
10613 * by the completion handler when the command completes.
10614 * This function returns IOCB_SUCCESS when success.
10615 * This function is called with no lock held.
10616 **/
dea31012005-04-17 16:05:31 -050010617int
James Smart2e0fef82007-06-17 19:56:36 -050010618lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -040010619 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -050010620 struct lpfc_iocbq *piocb,
10621 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010622 uint32_t timeout)
dea31012005-04-17 16:05:31 -050010623{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010624 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010625 long timeleft, timeout_req = 0;
10626 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010627 uint32_t creg_val;
James Smart0e9bb8d2013-03-01 16:35:12 -050010628 struct lpfc_iocbq *iocb;
10629 int txq_cnt = 0;
10630 int txcmplq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040010631 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
James Smart5a0916b2013-07-15 18:31:42 -040010632 unsigned long iflags;
10633 bool iocb_completed = true;
10634
dea31012005-04-17 16:05:31 -050010635 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010636 * If the caller has provided a response iocbq buffer, then context2
10637 * is NULL or its an error.
dea31012005-04-17 16:05:31 -050010638 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010639 if (prspiocbq) {
10640 if (piocb->context2)
10641 return IOCB_ERROR;
10642 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -050010643 }
10644
James Smart5a0916b2013-07-15 18:31:42 -040010645 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010646 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10647 piocb->context_un.wait_queue = &done_q;
James Smart5a0916b2013-07-15 18:31:42 -040010648 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
dea31012005-04-17 16:05:31 -050010649
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010650 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010651 if (lpfc_readl(phba->HCregaddr, &creg_val))
10652 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010653 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10654 writel(creg_val, phba->HCregaddr);
10655 readl(phba->HCregaddr); /* flush */
10656 }
10657
James Smart2a9bf3d2010-06-07 15:24:45 -040010658 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10659 SLI_IOCB_RET_IOCB);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010660 if (retval == IOCB_SUCCESS) {
James Smart256ec0d2013-04-17 20:14:58 -040010661 timeout_req = msecs_to_jiffies(timeout * 1000);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010662 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -040010663 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010664 timeout_req);
James Smart5a0916b2013-07-15 18:31:42 -040010665 spin_lock_irqsave(&phba->hbalock, iflags);
10666 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
dea31012005-04-17 16:05:31 -050010667
James Smart5a0916b2013-07-15 18:31:42 -040010668 /*
10669 * IOCB timed out. Inform the wake iocb wait
10670 * completion function and set local status
10671 */
10672
10673 iocb_completed = false;
10674 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10675 }
10676 spin_unlock_irqrestore(&phba->hbalock, iflags);
10677 if (iocb_completed) {
James Smart7054a602007-04-25 09:52:34 -040010678 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010679 "0331 IOCB wake signaled\n");
James Smart53151bb2013-10-10 12:24:07 -040010680 /* Note: we are not indicating if the IOCB has a success
10681 * status or not - that's for the caller to check.
10682 * IOCB_SUCCESS means just that the command was sent and
10683 * completed. Not that it completed successfully.
10684 * */
James Smart7054a602007-04-25 09:52:34 -040010685 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010686 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010687 "0338 IOCB wait timeout error - no "
10688 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010689 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -040010690 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010691 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040010692 "0330 IOCB wake NOT set, "
10693 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010694 timeout, (timeleft / jiffies));
10695 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -050010696 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010697 } else if (retval == IOCB_BUSY) {
James Smart0e9bb8d2013-03-01 16:35:12 -050010698 if (phba->cfg_log_verbose & LOG_SLI) {
10699 list_for_each_entry(iocb, &pring->txq, list) {
10700 txq_cnt++;
10701 }
10702 list_for_each_entry(iocb, &pring->txcmplq, list) {
10703 txcmplq_cnt++;
10704 }
10705 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10706 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10707 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10708 }
James Smart2a9bf3d2010-06-07 15:24:45 -040010709 return retval;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010710 } else {
10711 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040010712 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040010713 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010714 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -050010715 }
10716
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010717 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
James Smart9940b972011-03-11 16:06:12 -050010718 if (lpfc_readl(phba->HCregaddr, &creg_val))
10719 return IOCB_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -050010720 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10721 writel(creg_val, phba->HCregaddr);
10722 readl(phba->HCregaddr); /* flush */
10723 }
10724
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010725 if (prspiocbq)
10726 piocb->context2 = NULL;
10727
10728 piocb->context_un.wait_queue = NULL;
10729 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -050010730 return retval;
10731}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -040010732
James Smarte59058c2008-08-24 21:49:00 -040010733/**
James Smart3621a712009-04-06 18:47:14 -040010734 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -040010735 * @phba: Pointer to HBA context object.
10736 * @pmboxq: Pointer to driver mailbox object.
10737 * @timeout: Timeout in number of seconds.
10738 *
10739 * This function issues the mailbox to firmware and waits for the
10740 * mailbox command to complete. If the mailbox command is not
10741 * completed within timeout seconds, it returns MBX_TIMEOUT.
10742 * The function waits for the mailbox completion using an
10743 * interruptible wait. If the thread is woken up due to a
10744 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10745 * should not free the mailbox resources, if this function returns
10746 * MBX_TIMEOUT.
10747 * This function will sleep while waiting for mailbox completion.
10748 * So, this function should not be called from any context which
10749 * does not allow sleeping. Due to the same reason, this function
10750 * cannot be called with interrupt disabled.
10751 * This function assumes that the mailbox completion occurs while
10752 * this function sleep. So, this function cannot be called from
10753 * the worker thread which processes mailbox completion.
10754 * This function is called in the context of HBA management
10755 * applications.
10756 * This function returns MBX_SUCCESS when successful.
10757 * This function is called with no lock held.
10758 **/
dea31012005-04-17 16:05:31 -050010759int
James Smart2e0fef82007-06-17 19:56:36 -050010760lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -050010761 uint32_t timeout)
10762{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080010763 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James Smartb230b8a2013-05-31 17:05:27 -040010764 MAILBOX_t *mb = NULL;
dea31012005-04-17 16:05:31 -050010765 int retval;
James Smart858c9f62007-06-17 19:56:39 -050010766 unsigned long flag;
dea31012005-04-17 16:05:31 -050010767
James Smartb230b8a2013-05-31 17:05:27 -040010768 /* The caller might set context1 for extended buffer */
James Smart98c9ea52007-10-27 13:37:33 -040010769 if (pmboxq->context1)
James Smartb230b8a2013-05-31 17:05:27 -040010770 mb = (MAILBOX_t *)pmboxq->context1;
dea31012005-04-17 16:05:31 -050010771
James Smart495a7142008-06-14 22:52:59 -040010772 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -050010773 /* setup wake call as IOCB callback */
10774 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10775 /* setup context field to pass wait_queue pointer to wake function */
10776 pmboxq->context1 = &done_q;
10777
dea31012005-04-17 16:05:31 -050010778 /* now issue the command */
10779 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea31012005-04-17 16:05:31 -050010780 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -040010781 wait_event_interruptible_timeout(done_q,
10782 pmboxq->mbox_flag & LPFC_MBX_WAKE,
James Smart256ec0d2013-04-17 20:14:58 -040010783 msecs_to_jiffies(timeout * 1000));
James Smart7054a602007-04-25 09:52:34 -040010784
James Smart858c9f62007-06-17 19:56:39 -050010785 spin_lock_irqsave(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010786 /* restore the possible extended buffer for free resource */
10787 pmboxq->context1 = (uint8_t *)mb;
James Smart7054a602007-04-25 09:52:34 -040010788 /*
10789 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10790 * else do not free the resources.
10791 */
James Smartd7c47992010-06-08 18:31:54 -040010792 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea31012005-04-17 16:05:31 -050010793 retval = MBX_SUCCESS;
James Smartd7c47992010-06-08 18:31:54 -040010794 } else {
James Smart7054a602007-04-25 09:52:34 -040010795 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -050010796 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10797 }
10798 spin_unlock_irqrestore(&phba->hbalock, flag);
James Smartb230b8a2013-05-31 17:05:27 -040010799 } else {
10800 /* restore the possible extended buffer for free resource */
10801 pmboxq->context1 = (uint8_t *)mb;
dea31012005-04-17 16:05:31 -050010802 }
10803
dea31012005-04-17 16:05:31 -050010804 return retval;
10805}
10806
James Smarte59058c2008-08-24 21:49:00 -040010807/**
James Smart3772a992009-05-22 14:50:54 -040010808 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -040010809 * @phba: Pointer to HBA context.
10810 *
James Smart3772a992009-05-22 14:50:54 -040010811 * This function is called to shutdown the driver's mailbox sub-system.
10812 * It first marks the mailbox sub-system is in a block state to prevent
10813 * the asynchronous mailbox command from issued off the pending mailbox
10814 * command queue. If the mailbox command sub-system shutdown is due to
10815 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10816 * the mailbox sub-system flush routine to forcefully bring down the
10817 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10818 * as with offline or HBA function reset), this routine will wait for the
10819 * outstanding mailbox command to complete before invoking the mailbox
10820 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -040010821 **/
James Smart3772a992009-05-22 14:50:54 -040010822void
James Smart618a5232012-06-12 13:54:36 -040010823lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
James Smartb4c02652006-07-06 15:50:43 -040010824{
James Smart3772a992009-05-22 14:50:54 -040010825 struct lpfc_sli *psli = &phba->sli;
James Smart3772a992009-05-22 14:50:54 -040010826 unsigned long timeout;
10827
James Smart618a5232012-06-12 13:54:36 -040010828 if (mbx_action == LPFC_MBX_NO_WAIT) {
10829 /* delay 100ms for port state */
10830 msleep(100);
10831 lpfc_sli_mbox_sys_flush(phba);
10832 return;
10833 }
James Smarta183a152011-10-10 21:32:43 -040010834 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
James Smartd7069f02012-03-01 22:36:29 -050010835
James Smart3772a992009-05-22 14:50:54 -040010836 spin_lock_irq(&phba->hbalock);
10837 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
James Smart3772a992009-05-22 14:50:54 -040010838
10839 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
James Smart3772a992009-05-22 14:50:54 -040010840 /* Determine how long we might wait for the active mailbox
10841 * command to be gracefully completed by firmware.
10842 */
James Smarta183a152011-10-10 21:32:43 -040010843 if (phba->sli.mbox_active)
10844 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10845 phba->sli.mbox_active) *
10846 1000) + jiffies;
10847 spin_unlock_irq(&phba->hbalock);
10848
James Smart3772a992009-05-22 14:50:54 -040010849 while (phba->sli.mbox_active) {
10850 /* Check active mailbox complete status every 2ms */
10851 msleep(2);
10852 if (time_after(jiffies, timeout))
10853 /* Timeout, let the mailbox flush routine to
10854 * forcefully release active mailbox command
10855 */
10856 break;
10857 }
James Smartd7069f02012-03-01 22:36:29 -050010858 } else
10859 spin_unlock_irq(&phba->hbalock);
10860
James Smart3772a992009-05-22 14:50:54 -040010861 lpfc_sli_mbox_sys_flush(phba);
10862}
10863
10864/**
10865 * lpfc_sli_eratt_read - read sli-3 error attention events
10866 * @phba: Pointer to HBA context.
10867 *
10868 * This function is called to read the SLI3 device error attention registers
10869 * for possible error attention events. The caller must hold the hostlock
10870 * with spin_lock_irq().
10871 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010872 * This function returns 1 when there is Error Attention in the Host Attention
James Smart3772a992009-05-22 14:50:54 -040010873 * Register and returns 0 otherwise.
10874 **/
10875static int
10876lpfc_sli_eratt_read(struct lpfc_hba *phba)
10877{
James Smarted957682007-06-17 19:56:37 -050010878 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -040010879
James Smart3772a992009-05-22 14:50:54 -040010880 /* Read chip Host Attention (HA) register */
James Smart9940b972011-03-11 16:06:12 -050010881 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10882 goto unplug_err;
10883
James Smart3772a992009-05-22 14:50:54 -040010884 if (ha_copy & HA_ERATT) {
10885 /* Read host status register to retrieve error event */
James Smart9940b972011-03-11 16:06:12 -050010886 if (lpfc_sli_read_hs(phba))
10887 goto unplug_err;
James Smartb4c02652006-07-06 15:50:43 -040010888
James Smart3772a992009-05-22 14:50:54 -040010889 /* Check if there is a deferred error condition is active */
10890 if ((HS_FFER1 & phba->work_hs) &&
10891 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040010892 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -040010893 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -040010894 /* Clear all interrupt enable conditions */
10895 writel(0, phba->HCregaddr);
10896 readl(phba->HCregaddr);
10897 }
10898
10899 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -040010900 phba->work_ha |= HA_ERATT;
10901 /* Indicate polling handles this ERATT */
10902 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040010903 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010904 }
James Smart3772a992009-05-22 14:50:54 -040010905 return 0;
James Smart9940b972011-03-11 16:06:12 -050010906
10907unplug_err:
10908 /* Set the driver HS work bitmap */
10909 phba->work_hs |= UNPLUG_ERR;
10910 /* Set the driver HA work bitmap */
10911 phba->work_ha |= HA_ERATT;
10912 /* Indicate polling handles this ERATT */
10913 phba->hba_flag |= HBA_ERATT_HANDLED;
10914 return 1;
James Smartb4c02652006-07-06 15:50:43 -040010915}
10916
James Smarte59058c2008-08-24 21:49:00 -040010917/**
James Smartda0436e2009-05-22 14:51:39 -040010918 * lpfc_sli4_eratt_read - read sli-4 error attention events
10919 * @phba: Pointer to HBA context.
10920 *
10921 * This function is called to read the SLI4 device error attention registers
10922 * for possible error attention events. The caller must hold the hostlock
10923 * with spin_lock_irq().
10924 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010925 * This function returns 1 when there is Error Attention in the Host Attention
James Smartda0436e2009-05-22 14:51:39 -040010926 * Register and returns 0 otherwise.
10927 **/
10928static int
10929lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10930{
10931 uint32_t uerr_sta_hi, uerr_sta_lo;
James Smart2fcee4b2010-12-15 17:57:46 -050010932 uint32_t if_type, portsmphr;
10933 struct lpfc_register portstat_reg;
James Smartda0436e2009-05-22 14:51:39 -040010934
James Smart2fcee4b2010-12-15 17:57:46 -050010935 /*
10936 * For now, use the SLI4 device internal unrecoverable error
James Smartda0436e2009-05-22 14:51:39 -040010937 * registers for error attention. This can be changed later.
10938 */
James Smart2fcee4b2010-12-15 17:57:46 -050010939 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10940 switch (if_type) {
10941 case LPFC_SLI_INTF_IF_TYPE_0:
James Smart9940b972011-03-11 16:06:12 -050010942 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10943 &uerr_sta_lo) ||
10944 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10945 &uerr_sta_hi)) {
10946 phba->work_hs |= UNPLUG_ERR;
10947 phba->work_ha |= HA_ERATT;
10948 phba->hba_flag |= HBA_ERATT_HANDLED;
10949 return 1;
10950 }
James Smart2fcee4b2010-12-15 17:57:46 -050010951 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10952 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10953 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10954 "1423 HBA Unrecoverable error: "
10955 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10956 "ue_mask_lo_reg=0x%x, "
10957 "ue_mask_hi_reg=0x%x\n",
10958 uerr_sta_lo, uerr_sta_hi,
10959 phba->sli4_hba.ue_mask_lo,
10960 phba->sli4_hba.ue_mask_hi);
10961 phba->work_status[0] = uerr_sta_lo;
10962 phba->work_status[1] = uerr_sta_hi;
10963 phba->work_ha |= HA_ERATT;
10964 phba->hba_flag |= HBA_ERATT_HANDLED;
10965 return 1;
10966 }
10967 break;
10968 case LPFC_SLI_INTF_IF_TYPE_2:
James Smart9940b972011-03-11 16:06:12 -050010969 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10970 &portstat_reg.word0) ||
10971 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10972 &portsmphr)){
10973 phba->work_hs |= UNPLUG_ERR;
10974 phba->work_ha |= HA_ERATT;
10975 phba->hba_flag |= HBA_ERATT_HANDLED;
10976 return 1;
10977 }
James Smart2fcee4b2010-12-15 17:57:46 -050010978 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10979 phba->work_status[0] =
10980 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10981 phba->work_status[1] =
10982 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10983 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2e90f4b2011-12-13 13:22:37 -050010984 "2885 Port Status Event: "
James Smart2fcee4b2010-12-15 17:57:46 -050010985 "port status reg 0x%x, "
10986 "port smphr reg 0x%x, "
10987 "error 1=0x%x, error 2=0x%x\n",
10988 portstat_reg.word0,
10989 portsmphr,
10990 phba->work_status[0],
10991 phba->work_status[1]);
10992 phba->work_ha |= HA_ERATT;
10993 phba->hba_flag |= HBA_ERATT_HANDLED;
10994 return 1;
10995 }
10996 break;
10997 case LPFC_SLI_INTF_IF_TYPE_1:
10998 default:
James Smarta747c9c2009-11-18 15:41:10 -050010999 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart2fcee4b2010-12-15 17:57:46 -050011000 "2886 HBA Error Attention on unsupported "
11001 "if type %d.", if_type);
James Smarta747c9c2009-11-18 15:41:10 -050011002 return 1;
James Smartda0436e2009-05-22 14:51:39 -040011003 }
James Smart2fcee4b2010-12-15 17:57:46 -050011004
James Smartda0436e2009-05-22 14:51:39 -040011005 return 0;
11006}
11007
11008/**
James Smart3621a712009-04-06 18:47:14 -040011009 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -040011010 * @phba: Pointer to HBA context.
11011 *
James Smart3772a992009-05-22 14:50:54 -040011012 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -040011013 * error attention register bit for error attention events.
11014 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030011015 * This function returns 1 when there is Error Attention in the Host Attention
James Smart93996272008-08-24 21:50:30 -040011016 * Register and returns 0 otherwise.
11017 **/
11018int
11019lpfc_sli_check_eratt(struct lpfc_hba *phba)
11020{
11021 uint32_t ha_copy;
11022
11023 /* If somebody is waiting to handle an eratt, don't process it
11024 * here. The brdkill function will do this.
11025 */
11026 if (phba->link_flag & LS_IGNORE_ERATT)
11027 return 0;
11028
11029 /* Check if interrupt handler handles this ERATT */
11030 spin_lock_irq(&phba->hbalock);
11031 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11032 /* Interrupt handler has handled ERATT */
11033 spin_unlock_irq(&phba->hbalock);
11034 return 0;
11035 }
11036
James Smarta257bf92009-04-06 18:48:10 -040011037 /*
11038 * If there is deferred error attention, do not check for error
11039 * attention
11040 */
11041 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11042 spin_unlock_irq(&phba->hbalock);
11043 return 0;
11044 }
11045
James Smart3772a992009-05-22 14:50:54 -040011046 /* If PCI channel is offline, don't process it */
11047 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -040011048 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011049 return 0;
11050 }
11051
11052 switch (phba->sli_rev) {
11053 case LPFC_SLI_REV2:
11054 case LPFC_SLI_REV3:
11055 /* Read chip Host Attention (HA) register */
11056 ha_copy = lpfc_sli_eratt_read(phba);
11057 break;
James Smartda0436e2009-05-22 14:51:39 -040011058 case LPFC_SLI_REV4:
James Smart2fcee4b2010-12-15 17:57:46 -050011059 /* Read device Uncoverable Error (UERR) registers */
James Smartda0436e2009-05-22 14:51:39 -040011060 ha_copy = lpfc_sli4_eratt_read(phba);
11061 break;
James Smart3772a992009-05-22 14:50:54 -040011062 default:
11063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11064 "0299 Invalid SLI revision (%d)\n",
11065 phba->sli_rev);
11066 ha_copy = 0;
11067 break;
James Smart93996272008-08-24 21:50:30 -040011068 }
11069 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -040011070
11071 return ha_copy;
11072}
11073
11074/**
11075 * lpfc_intr_state_check - Check device state for interrupt handling
11076 * @phba: Pointer to HBA context.
11077 *
11078 * This inline routine checks whether a device or its PCI slot is in a state
11079 * that the interrupt should be handled.
11080 *
11081 * This function returns 0 if the device or the PCI slot is in a state that
11082 * interrupt should be handled, otherwise -EIO.
11083 */
11084static inline int
11085lpfc_intr_state_check(struct lpfc_hba *phba)
11086{
11087 /* If the pci channel is offline, ignore all the interrupts */
11088 if (unlikely(pci_channel_offline(phba->pcidev)))
11089 return -EIO;
11090
11091 /* Update device level interrupt statistics */
11092 phba->sli.slistat.sli_intr++;
11093
11094 /* Ignore all interrupts during initialization. */
11095 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11096 return -EIO;
11097
James Smart93996272008-08-24 21:50:30 -040011098 return 0;
11099}
11100
11101/**
James Smart3772a992009-05-22 14:50:54 -040011102 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -040011103 * @irq: Interrupt number.
11104 * @dev_id: The device context pointer.
11105 *
James Smart93996272008-08-24 21:50:30 -040011106 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011107 * service routine when device with SLI-3 interface spec is enabled with
11108 * MSI-X multi-message interrupt mode and there are slow-path events in
11109 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11110 * interrupt mode, this function is called as part of the device-level
11111 * interrupt handler. When the PCI slot is in error recovery or the HBA
11112 * is undergoing initialization, the interrupt handler will not process
11113 * the interrupt. The link attention and ELS ring attention events are
11114 * handled by the worker thread. The interrupt handler signals the worker
11115 * thread and returns for these events. This function is called without
11116 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -040011117 * structures.
11118 *
11119 * This function returns IRQ_HANDLED when interrupt is handled else it
11120 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -040011121 **/
dea31012005-04-17 16:05:31 -050011122irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011123lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -050011124{
James Smart2e0fef82007-06-17 19:56:36 -050011125 struct lpfc_hba *phba;
James Smarta747c9c2009-11-18 15:41:10 -050011126 uint32_t ha_copy, hc_copy;
dea31012005-04-17 16:05:31 -050011127 uint32_t work_ha_copy;
11128 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011129 unsigned long iflag;
dea31012005-04-17 16:05:31 -050011130 uint32_t control;
11131
James Smart92d7f7b2007-06-17 19:56:38 -050011132 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -050011133 struct lpfc_vport *vport;
11134 struct lpfc_nodelist *ndlp;
11135 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -050011136 LPFC_MBOXQ_t *pmb;
11137 int rc;
11138
dea31012005-04-17 16:05:31 -050011139 /*
11140 * Get the driver's phba structure from the dev_id and
11141 * assume the HBA is not interrupting.
11142 */
James Smart93996272008-08-24 21:50:30 -040011143 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -050011144
11145 if (unlikely(!phba))
11146 return IRQ_NONE;
11147
dea31012005-04-17 16:05:31 -050011148 /*
James Smart93996272008-08-24 21:50:30 -040011149 * Stuff needs to be attented to when this function is invoked as an
11150 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011151 */
James Smart93996272008-08-24 21:50:30 -040011152 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011153 /* Check device state for handling interrupt */
11154 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011155 return IRQ_NONE;
11156 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -050011157 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011158 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11159 goto unplug_error;
James Smart93996272008-08-24 21:50:30 -040011160 /* If somebody is waiting to handle an eratt don't process it
11161 * here. The brdkill function will do this.
11162 */
11163 if (phba->link_flag & LS_IGNORE_ERATT)
11164 ha_copy &= ~HA_ERATT;
11165 /* Check the need for handling ERATT in interrupt handler */
11166 if (ha_copy & HA_ERATT) {
11167 if (phba->hba_flag & HBA_ERATT_HANDLED)
11168 /* ERATT polling has handled ERATT */
11169 ha_copy &= ~HA_ERATT;
11170 else
11171 /* Indicate interrupt handler handles ERATT */
11172 phba->hba_flag |= HBA_ERATT_HANDLED;
11173 }
James Smarta257bf92009-04-06 18:48:10 -040011174
11175 /*
11176 * If there is deferred error attention, do not check for any
11177 * interrupt.
11178 */
11179 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011180 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011181 return IRQ_NONE;
11182 }
11183
James Smart93996272008-08-24 21:50:30 -040011184 /* Clear up only attention source related to slow-path */
James Smart9940b972011-03-11 16:06:12 -050011185 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11186 goto unplug_error;
11187
James Smarta747c9c2009-11-18 15:41:10 -050011188 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11189 HC_LAINT_ENA | HC_ERINT_ENA),
11190 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011191 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11192 phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011193 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011194 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011195 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011196 } else
11197 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -050011198
dea31012005-04-17 16:05:31 -050011199 work_ha_copy = ha_copy & phba->work_ha_mask;
11200
James Smart93996272008-08-24 21:50:30 -040011201 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -050011202 if (work_ha_copy & HA_LATT) {
11203 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11204 /*
11205 * Turn off Link Attention interrupts
11206 * until CLEAR_LA done
11207 */
James Smart5b75da22008-12-04 22:39:35 -050011208 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011209 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
James Smart9940b972011-03-11 16:06:12 -050011210 if (lpfc_readl(phba->HCregaddr, &control))
11211 goto unplug_error;
dea31012005-04-17 16:05:31 -050011212 control &= ~HC_LAINT_ENA;
11213 writel(control, phba->HCregaddr);
11214 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011215 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011216 }
11217 else
11218 work_ha_copy &= ~HA_LATT;
11219 }
11220
James Smart93996272008-08-24 21:50:30 -040011221 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -050011222 /*
11223 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11224 * the only slow ring.
11225 */
11226 status = (work_ha_copy &
11227 (HA_RXMASK << (4*LPFC_ELS_RING)));
11228 status >>= (4*LPFC_ELS_RING);
11229 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -050011230 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart9940b972011-03-11 16:06:12 -050011231 if (lpfc_readl(phba->HCregaddr, &control))
11232 goto unplug_error;
James Smarta58cbd52007-08-02 11:09:43 -040011233
11234 lpfc_debugfs_slow_ring_trc(phba,
11235 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11236 control, status,
11237 (uint32_t)phba->sli.slistat.sli_intr);
11238
James Smart858c9f62007-06-17 19:56:39 -050011239 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -040011240 lpfc_debugfs_slow_ring_trc(phba,
11241 "ISR Disable ring:"
11242 "pwork:x%x hawork:x%x wait:x%x",
11243 phba->work_ha, work_ha_copy,
11244 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011245 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011246
James Smart858c9f62007-06-17 19:56:39 -050011247 control &=
11248 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -050011249 writel(control, phba->HCregaddr);
11250 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -050011251 }
James Smarta58cbd52007-08-02 11:09:43 -040011252 else {
11253 lpfc_debugfs_slow_ring_trc(phba,
11254 "ISR slow ring: pwork:"
11255 "x%x hawork:x%x wait:x%x",
11256 phba->work_ha, work_ha_copy,
11257 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -040011258 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -040011259 }
James Smart5b75da22008-12-04 22:39:35 -050011260 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011261 }
11262 }
James Smart5b75da22008-12-04 22:39:35 -050011263 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011264 if (work_ha_copy & HA_ERATT) {
James Smart9940b972011-03-11 16:06:12 -050011265 if (lpfc_sli_read_hs(phba))
11266 goto unplug_error;
James Smarta257bf92009-04-06 18:48:10 -040011267 /*
11268 * Check if there is a deferred error condition
11269 * is active
11270 */
11271 if ((HS_FFER1 & phba->work_hs) &&
11272 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
James Smartdcf2a4e2010-09-29 11:18:53 -040011273 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11274 phba->work_hs)) {
James Smarta257bf92009-04-06 18:48:10 -040011275 phba->hba_flag |= DEFER_ERATT;
11276 /* Clear all interrupt enable conditions */
11277 writel(0, phba->HCregaddr);
11278 readl(phba->HCregaddr);
11279 }
11280 }
11281
James Smart93996272008-08-24 21:50:30 -040011282 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -050011283 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -040011284 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -040011285 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -050011286 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -050011287
11288 /* First check out the status word */
11289 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11290 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -050011291 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -050011292 /*
11293 * Stray Mailbox Interrupt, mbxCommand <cmd>
11294 * mbxStatus <status>
11295 */
James Smart09372822008-01-11 01:52:54 -050011296 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -050011297 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -040011298 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -050011299 "Interrupt mbxCommand x%x "
11300 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -040011301 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -050011302 pmbox->mbxCommand,
11303 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -050011304 /* clear mailbox attention bit */
11305 work_ha_copy &= ~HA_MBATT;
11306 } else {
James Smart97eab632008-04-07 10:16:05 -040011307 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -050011308 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -050011309 phba->last_completion_time = jiffies;
11310 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -050011311 if (pmb->mbox_cmpl) {
11312 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11313 MAILBOX_CMD_SIZE);
James Smart7a470272010-03-15 11:25:20 -040011314 if (pmb->out_ext_byte_len &&
11315 pmb->context2)
11316 lpfc_sli_pcimem_bcopy(
11317 phba->mbox_ext,
11318 pmb->context2,
11319 pmb->out_ext_byte_len);
James Smart858c9f62007-06-17 19:56:39 -050011320 }
James Smart09372822008-01-11 01:52:54 -050011321 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11322 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11323
11324 lpfc_debugfs_disc_trc(vport,
11325 LPFC_DISC_TRC_MBOX_VPORT,
11326 "MBOX dflt rpi: : "
11327 "status:x%x rpi:x%x",
11328 (uint32_t)pmbox->mbxStatus,
11329 pmbox->un.varWords[0], 0);
11330
11331 if (!pmbox->mbxStatus) {
11332 mp = (struct lpfc_dmabuf *)
11333 (pmb->context1);
11334 ndlp = (struct lpfc_nodelist *)
11335 pmb->context2;
11336
11337 /* Reg_LOGIN of dflt RPI was
11338 * successful. new lets get
11339 * rid of the RPI using the
11340 * same mbox buffer.
11341 */
11342 lpfc_unreg_login(phba,
11343 vport->vpi,
11344 pmbox->un.varWords[0],
11345 pmb);
11346 pmb->mbox_cmpl =
11347 lpfc_mbx_cmpl_dflt_rpi;
11348 pmb->context1 = mp;
11349 pmb->context2 = ndlp;
11350 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -040011351 rc = lpfc_sli_issue_mbox(phba,
11352 pmb,
11353 MBX_NOWAIT);
11354 if (rc != MBX_BUSY)
11355 lpfc_printf_log(phba,
11356 KERN_ERR,
11357 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -040011358 "0350 rc should have"
James Smart6a9c52c2009-10-02 15:16:51 -040011359 "been MBX_BUSY\n");
James Smart3772a992009-05-22 14:50:54 -040011360 if (rc != MBX_NOT_FINISHED)
11361 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -050011362 }
11363 }
James Smart5b75da22008-12-04 22:39:35 -050011364 spin_lock_irqsave(
11365 &phba->pport->work_port_lock,
11366 iflag);
James Smart09372822008-01-11 01:52:54 -050011367 phba->pport->work_port_events &=
11368 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -050011369 spin_unlock_irqrestore(
11370 &phba->pport->work_port_lock,
11371 iflag);
James Smart09372822008-01-11 01:52:54 -050011372 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -050011373 }
James Smart97eab632008-04-07 10:16:05 -040011374 } else
James Smart5b75da22008-12-04 22:39:35 -050011375 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011376
James Smart92d7f7b2007-06-17 19:56:38 -050011377 if ((work_ha_copy & HA_MBATT) &&
11378 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -050011379send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -050011380 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -040011381 do {
11382 rc = lpfc_sli_issue_mbox(phba, NULL,
11383 MBX_NOWAIT);
11384 } while (rc == MBX_NOT_FINISHED);
11385 if (rc != MBX_SUCCESS)
11386 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11387 LOG_SLI, "0349 rc should be "
James Smart6a9c52c2009-10-02 15:16:51 -040011388 "MBX_SUCCESS\n");
James Smart92d7f7b2007-06-17 19:56:38 -050011389 }
11390
James Smart5b75da22008-12-04 22:39:35 -050011391 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -050011392 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -050011393 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -040011394 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -050011395 }
James Smart93996272008-08-24 21:50:30 -040011396 return IRQ_HANDLED;
James Smart9940b972011-03-11 16:06:12 -050011397unplug_error:
11398 spin_unlock_irqrestore(&phba->hbalock, iflag);
11399 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -050011400
James Smart3772a992009-05-22 14:50:54 -040011401} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -040011402
11403/**
James Smart3772a992009-05-22 14:50:54 -040011404 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -040011405 * @irq: Interrupt number.
11406 * @dev_id: The device context pointer.
11407 *
11408 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -040011409 * service routine when device with SLI-3 interface spec is enabled with
11410 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11411 * ring event in the HBA. However, when the device is enabled with either
11412 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11413 * device-level interrupt handler. When the PCI slot is in error recovery
11414 * or the HBA is undergoing initialization, the interrupt handler will not
11415 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11416 * the intrrupt context. This function is called without any lock held.
11417 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011418 *
11419 * This function returns IRQ_HANDLED when interrupt is handled else it
11420 * returns IRQ_NONE.
11421 **/
11422irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011423lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011424{
11425 struct lpfc_hba *phba;
11426 uint32_t ha_copy;
11427 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -050011428 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -040011429
11430 /* Get the driver's phba structure from the dev_id and
11431 * assume the HBA is not interrupting.
11432 */
11433 phba = (struct lpfc_hba *) dev_id;
11434
11435 if (unlikely(!phba))
11436 return IRQ_NONE;
dea31012005-04-17 16:05:31 -050011437
11438 /*
James Smart93996272008-08-24 21:50:30 -040011439 * Stuff needs to be attented to when this function is invoked as an
11440 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -050011441 */
James Smart93996272008-08-24 21:50:30 -040011442 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -040011443 /* Check device state for handling interrupt */
11444 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011445 return IRQ_NONE;
11446 /* Need to read HA REG for FCP ring and other ring events */
James Smart9940b972011-03-11 16:06:12 -050011447 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11448 return IRQ_HANDLED;
James Smart93996272008-08-24 21:50:30 -040011449 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -050011450 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011451 /*
11452 * If there is deferred error attention, do not check for
11453 * any interrupt.
11454 */
11455 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -040011456 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -040011457 return IRQ_NONE;
11458 }
James Smart93996272008-08-24 21:50:30 -040011459 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11460 phba->HAregaddr);
11461 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -050011462 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -040011463 } else
11464 ha_copy = phba->ha_copy;
11465
11466 /*
11467 * Process all events on FCP ring. Take the optimized path for FCP IO.
11468 */
11469 ha_copy &= ~(phba->work_ha_mask);
11470
11471 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -050011472 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -050011473 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -050011474 lpfc_sli_handle_fast_ring_event(phba,
11475 &phba->sli.ring[LPFC_FCP_RING],
11476 status);
James Smarta4bc3372006-12-02 13:34:16 -050011477
11478 if (phba->cfg_multi_ring_support == 2) {
11479 /*
James Smart93996272008-08-24 21:50:30 -040011480 * Process all events on extra ring. Take the optimized path
11481 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -050011482 */
James Smart93996272008-08-24 21:50:30 -040011483 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -050011484 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -050011485 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -050011486 lpfc_sli_handle_fast_ring_event(phba,
11487 &phba->sli.ring[LPFC_EXTRA_RING],
11488 status);
11489 }
11490 }
dea31012005-04-17 16:05:31 -050011491 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -040011492} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -050011493
James Smart93996272008-08-24 21:50:30 -040011494/**
James Smart3772a992009-05-22 14:50:54 -040011495 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -040011496 * @irq: Interrupt number.
11497 * @dev_id: The device context pointer.
11498 *
James Smart3772a992009-05-22 14:50:54 -040011499 * This function is the HBA device-level interrupt handler to device with
11500 * SLI-3 interface spec, called from the PCI layer when either MSI or
11501 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11502 * requires driver attention. This function invokes the slow-path interrupt
11503 * attention handling function and fast-path interrupt attention handling
11504 * function in turn to process the relevant HBA attention events. This
11505 * function is called without any lock held. It gets the hbalock to access
11506 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -040011507 *
11508 * This function returns IRQ_HANDLED when interrupt is handled, else it
11509 * returns IRQ_NONE.
11510 **/
11511irqreturn_t
James Smart3772a992009-05-22 14:50:54 -040011512lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -040011513{
11514 struct lpfc_hba *phba;
11515 irqreturn_t sp_irq_rc, fp_irq_rc;
11516 unsigned long status1, status2;
James Smarta747c9c2009-11-18 15:41:10 -050011517 uint32_t hc_copy;
James Smart93996272008-08-24 21:50:30 -040011518
11519 /*
11520 * Get the driver's phba structure from the dev_id and
11521 * assume the HBA is not interrupting.
11522 */
11523 phba = (struct lpfc_hba *) dev_id;
11524
11525 if (unlikely(!phba))
11526 return IRQ_NONE;
11527
James Smart3772a992009-05-22 14:50:54 -040011528 /* Check device state for handling interrupt */
11529 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -040011530 return IRQ_NONE;
11531
11532 spin_lock(&phba->hbalock);
James Smart9940b972011-03-11 16:06:12 -050011533 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11534 spin_unlock(&phba->hbalock);
11535 return IRQ_HANDLED;
11536 }
11537
James Smart93996272008-08-24 21:50:30 -040011538 if (unlikely(!phba->ha_copy)) {
11539 spin_unlock(&phba->hbalock);
11540 return IRQ_NONE;
11541 } else if (phba->ha_copy & HA_ERATT) {
11542 if (phba->hba_flag & HBA_ERATT_HANDLED)
11543 /* ERATT polling has handled ERATT */
11544 phba->ha_copy &= ~HA_ERATT;
11545 else
11546 /* Indicate interrupt handler handles ERATT */
11547 phba->hba_flag |= HBA_ERATT_HANDLED;
11548 }
11549
James Smarta257bf92009-04-06 18:48:10 -040011550 /*
11551 * If there is deferred error attention, do not check for any interrupt.
11552 */
11553 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020011554 spin_unlock(&phba->hbalock);
James Smarta257bf92009-04-06 18:48:10 -040011555 return IRQ_NONE;
11556 }
11557
James Smart93996272008-08-24 21:50:30 -040011558 /* Clear attention sources except link and error attentions */
James Smart9940b972011-03-11 16:06:12 -050011559 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11560 spin_unlock(&phba->hbalock);
11561 return IRQ_HANDLED;
11562 }
James Smarta747c9c2009-11-18 15:41:10 -050011563 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11564 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11565 phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011566 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
James Smarta747c9c2009-11-18 15:41:10 -050011567 writel(hc_copy, phba->HCregaddr);
James Smart93996272008-08-24 21:50:30 -040011568 readl(phba->HAregaddr); /* flush */
11569 spin_unlock(&phba->hbalock);
11570
11571 /*
11572 * Invokes slow-path host attention interrupt handling as appropriate.
11573 */
11574
11575 /* status of events with mailbox and link attention */
11576 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11577
11578 /* status of events with ELS ring */
11579 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11580 status2 >>= (4*LPFC_ELS_RING);
11581
11582 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011583 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011584 else
11585 sp_irq_rc = IRQ_NONE;
11586
11587 /*
11588 * Invoke fast-path host attention interrupt handling as appropriate.
11589 */
11590
11591 /* status of events with FCP ring */
11592 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11593 status1 >>= (4*LPFC_FCP_RING);
11594
11595 /* status of events with extra ring */
11596 if (phba->cfg_multi_ring_support == 2) {
11597 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11598 status2 >>= (4*LPFC_EXTRA_RING);
11599 } else
11600 status2 = 0;
11601
11602 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -040011603 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -040011604 else
11605 fp_irq_rc = IRQ_NONE;
11606
11607 /* Return device-level interrupt handling status */
11608 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -040011609} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -040011610
11611/**
11612 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11613 * @phba: pointer to lpfc hba data structure.
11614 *
11615 * This routine is invoked by the worker thread to process all the pending
11616 * SLI4 FCP abort XRI events.
11617 **/
11618void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11619{
11620 struct lpfc_cq_event *cq_event;
11621
11622 /* First, declare the fcp xri abort event has been handled */
11623 spin_lock_irq(&phba->hbalock);
11624 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11625 spin_unlock_irq(&phba->hbalock);
11626 /* Now, handle all the fcp xri abort events */
11627 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11628 /* Get the first event from the head of the event queue */
11629 spin_lock_irq(&phba->hbalock);
11630 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11631 cq_event, struct lpfc_cq_event, list);
11632 spin_unlock_irq(&phba->hbalock);
11633 /* Notify aborted XRI for FCP work queue */
11634 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11635 /* Free the event processed back to the free pool */
11636 lpfc_sli4_cq_event_release(phba, cq_event);
11637 }
11638}
11639
11640/**
11641 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11642 * @phba: pointer to lpfc hba data structure.
11643 *
11644 * This routine is invoked by the worker thread to process all the pending
11645 * SLI4 els abort xri events.
11646 **/
11647void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11648{
11649 struct lpfc_cq_event *cq_event;
11650
11651 /* First, declare the els xri abort event has been handled */
11652 spin_lock_irq(&phba->hbalock);
11653 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11654 spin_unlock_irq(&phba->hbalock);
11655 /* Now, handle all the els xri abort events */
11656 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11657 /* Get the first event from the head of the event queue */
11658 spin_lock_irq(&phba->hbalock);
11659 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11660 cq_event, struct lpfc_cq_event, list);
11661 spin_unlock_irq(&phba->hbalock);
11662 /* Notify aborted XRI for ELS work queue */
11663 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11664 /* Free the event processed back to the free pool */
11665 lpfc_sli4_cq_event_release(phba, cq_event);
11666 }
11667}
11668
James Smart341af102010-01-26 23:07:37 -050011669/**
11670 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11671 * @phba: pointer to lpfc hba data structure
11672 * @pIocbIn: pointer to the rspiocbq
11673 * @pIocbOut: pointer to the cmdiocbq
11674 * @wcqe: pointer to the complete wcqe
11675 *
11676 * This routine transfers the fields of a command iocbq to a response iocbq
11677 * by copying all the IOCB fields from command iocbq and transferring the
11678 * completion status information from the complete wcqe.
11679 **/
James Smart4f774512009-05-22 14:52:35 -040011680static void
James Smart341af102010-01-26 23:07:37 -050011681lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11682 struct lpfc_iocbq *pIocbIn,
James Smart4f774512009-05-22 14:52:35 -040011683 struct lpfc_iocbq *pIocbOut,
11684 struct lpfc_wcqe_complete *wcqe)
11685{
James Smartaf227412013-10-10 12:23:10 -040011686 int numBdes, i;
James Smart341af102010-01-26 23:07:37 -050011687 unsigned long iflags;
James Smartaf227412013-10-10 12:23:10 -040011688 uint32_t status, max_response;
11689 struct lpfc_dmabuf *dmabuf;
11690 struct ulp_bde64 *bpl, bde;
James Smart4f774512009-05-22 14:52:35 -040011691 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11692
11693 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11694 sizeof(struct lpfc_iocbq) - offset);
James Smart4f774512009-05-22 14:52:35 -040011695 /* Map WCQE parameters into irspiocb parameters */
James Smartacd68592012-01-18 16:25:09 -050011696 status = bf_get(lpfc_wcqe_c_status, wcqe);
11697 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
James Smart4f774512009-05-22 14:52:35 -040011698 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11699 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11700 pIocbIn->iocb.un.fcpi.fcpi_parm =
11701 pIocbOut->iocb.un.fcpi.fcpi_parm -
11702 wcqe->total_data_placed;
11703 else
11704 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart695a8142010-01-26 23:08:03 -050011705 else {
James Smart4f774512009-05-22 14:52:35 -040011706 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smartaf227412013-10-10 12:23:10 -040011707 switch (pIocbOut->iocb.ulpCommand) {
11708 case CMD_ELS_REQUEST64_CR:
11709 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11710 bpl = (struct ulp_bde64 *)dmabuf->virt;
11711 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11712 max_response = bde.tus.f.bdeSize;
11713 break;
11714 case CMD_GEN_REQUEST64_CR:
11715 max_response = 0;
11716 if (!pIocbOut->context3)
11717 break;
11718 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11719 sizeof(struct ulp_bde64);
11720 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11721 bpl = (struct ulp_bde64 *)dmabuf->virt;
11722 for (i = 0; i < numBdes; i++) {
11723 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11724 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11725 max_response += bde.tus.f.bdeSize;
11726 }
11727 break;
11728 default:
11729 max_response = wcqe->total_data_placed;
11730 break;
11731 }
11732 if (max_response < wcqe->total_data_placed)
11733 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11734 else
11735 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11736 wcqe->total_data_placed;
James Smart695a8142010-01-26 23:08:03 -050011737 }
James Smart341af102010-01-26 23:07:37 -050011738
James Smartacd68592012-01-18 16:25:09 -050011739 /* Convert BG errors for completion status */
11740 if (status == CQE_STATUS_DI_ERROR) {
11741 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11742
11743 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11744 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11745 else
11746 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11747
11748 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11749 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11750 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11751 BGS_GUARD_ERR_MASK;
11752 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11753 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11754 BGS_APPTAG_ERR_MASK;
11755 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11756 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11757 BGS_REFTAG_ERR_MASK;
11758
11759 /* Check to see if there was any good data before the error */
11760 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11761 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11762 BGS_HI_WATER_MARK_PRESENT_MASK;
11763 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11764 wcqe->total_data_placed;
11765 }
11766
11767 /*
11768 * Set ALL the error bits to indicate we don't know what
11769 * type of error it is.
11770 */
11771 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11772 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11773 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11774 BGS_GUARD_ERR_MASK);
11775 }
11776
James Smart341af102010-01-26 23:07:37 -050011777 /* Pick up HBA exchange busy condition */
11778 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11779 spin_lock_irqsave(&phba->hbalock, iflags);
11780 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11781 spin_unlock_irqrestore(&phba->hbalock, iflags);
11782 }
James Smart4f774512009-05-22 14:52:35 -040011783}
11784
11785/**
James Smart45ed1192009-10-02 15:17:02 -040011786 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11787 * @phba: Pointer to HBA context object.
11788 * @wcqe: Pointer to work-queue completion queue entry.
11789 *
11790 * This routine handles an ELS work-queue completion event and construct
11791 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11792 * discovery engine to handle.
11793 *
11794 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11795 **/
11796static struct lpfc_iocbq *
11797lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11798 struct lpfc_iocbq *irspiocbq)
11799{
11800 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11801 struct lpfc_iocbq *cmdiocbq;
11802 struct lpfc_wcqe_complete *wcqe;
11803 unsigned long iflags;
11804
11805 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
James Smart7e56aa22012-08-03 12:35:34 -040011806 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011807 pring->stats.iocb_event++;
11808 /* Look up the ELS command IOCB and create pseudo response IOCB */
11809 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11810 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040011811 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart45ed1192009-10-02 15:17:02 -040011812
11813 if (unlikely(!cmdiocbq)) {
11814 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11815 "0386 ELS complete with no corresponding "
11816 "cmdiocb: iotag (%d)\n",
11817 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11818 lpfc_sli_release_iocbq(phba, irspiocbq);
11819 return NULL;
11820 }
11821
11822 /* Fake the irspiocbq and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050011823 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
James Smart45ed1192009-10-02 15:17:02 -040011824
11825 return irspiocbq;
11826}
11827
11828/**
James Smart04c68492009-05-22 14:52:52 -040011829 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11830 * @phba: Pointer to HBA context object.
11831 * @cqe: Pointer to mailbox completion queue entry.
11832 *
11833 * This routine process a mailbox completion queue entry with asynchrous
11834 * event.
11835 *
11836 * Return: true if work posted to worker thread, otherwise false.
11837 **/
11838static bool
11839lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11840{
11841 struct lpfc_cq_event *cq_event;
11842 unsigned long iflags;
11843
11844 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11845 "0392 Async Event: word0:x%x, word1:x%x, "
11846 "word2:x%x, word3:x%x\n", mcqe->word0,
11847 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11848
11849 /* Allocate a new internal CQ_EVENT entry */
11850 cq_event = lpfc_sli4_cq_event_alloc(phba);
11851 if (!cq_event) {
11852 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11853 "0394 Failed to allocate CQ_EVENT entry\n");
11854 return false;
11855 }
11856
11857 /* Move the CQE into an asynchronous event entry */
11858 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11859 spin_lock_irqsave(&phba->hbalock, iflags);
11860 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11861 /* Set the async event flag */
11862 phba->hba_flag |= ASYNC_EVENT;
11863 spin_unlock_irqrestore(&phba->hbalock, iflags);
11864
11865 return true;
11866}
11867
11868/**
11869 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11870 * @phba: Pointer to HBA context object.
11871 * @cqe: Pointer to mailbox completion queue entry.
11872 *
11873 * This routine process a mailbox completion queue entry with mailbox
11874 * completion event.
11875 *
11876 * Return: true if work posted to worker thread, otherwise false.
11877 **/
11878static bool
11879lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11880{
11881 uint32_t mcqe_status;
11882 MAILBOX_t *mbox, *pmbox;
11883 struct lpfc_mqe *mqe;
11884 struct lpfc_vport *vport;
11885 struct lpfc_nodelist *ndlp;
11886 struct lpfc_dmabuf *mp;
11887 unsigned long iflags;
11888 LPFC_MBOXQ_t *pmb;
11889 bool workposted = false;
11890 int rc;
11891
11892 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11893 if (!bf_get(lpfc_trailer_completed, mcqe))
11894 goto out_no_mqe_complete;
11895
11896 /* Get the reference to the active mbox command */
11897 spin_lock_irqsave(&phba->hbalock, iflags);
11898 pmb = phba->sli.mbox_active;
11899 if (unlikely(!pmb)) {
11900 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11901 "1832 No pending MBOX command to handle\n");
11902 spin_unlock_irqrestore(&phba->hbalock, iflags);
11903 goto out_no_mqe_complete;
11904 }
11905 spin_unlock_irqrestore(&phba->hbalock, iflags);
11906 mqe = &pmb->u.mqe;
11907 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11908 mbox = phba->mbox;
11909 vport = pmb->vport;
11910
11911 /* Reset heartbeat timer */
11912 phba->last_completion_time = jiffies;
11913 del_timer(&phba->sli.mbox_tmo);
11914
11915 /* Move mbox data to caller's mailbox region, do endian swapping */
11916 if (pmb->mbox_cmpl && mbox)
11917 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
James Smart04c68492009-05-22 14:52:52 -040011918
James Smart73d91e52011-10-10 21:32:10 -040011919 /*
11920 * For mcqe errors, conditionally move a modified error code to
11921 * the mbox so that the error will not be missed.
11922 */
11923 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11924 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11925 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11926 bf_set(lpfc_mqe_status, mqe,
11927 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11928 }
James Smart04c68492009-05-22 14:52:52 -040011929 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11930 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11931 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11932 "MBOX dflt rpi: status:x%x rpi:x%x",
11933 mcqe_status,
11934 pmbox->un.varWords[0], 0);
11935 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11936 mp = (struct lpfc_dmabuf *)(pmb->context1);
11937 ndlp = (struct lpfc_nodelist *)pmb->context2;
11938 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11939 * RID of the PPI using the same mbox buffer.
11940 */
11941 lpfc_unreg_login(phba, vport->vpi,
11942 pmbox->un.varWords[0], pmb);
11943 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11944 pmb->context1 = mp;
11945 pmb->context2 = ndlp;
11946 pmb->vport = vport;
11947 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11948 if (rc != MBX_BUSY)
11949 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11950 LOG_SLI, "0385 rc should "
11951 "have been MBX_BUSY\n");
11952 if (rc != MBX_NOT_FINISHED)
11953 goto send_current_mbox;
11954 }
11955 }
11956 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11957 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11958 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11959
11960 /* There is mailbox completion work to do */
11961 spin_lock_irqsave(&phba->hbalock, iflags);
11962 __lpfc_mbox_cmpl_put(phba, pmb);
11963 phba->work_ha |= HA_MBATT;
11964 spin_unlock_irqrestore(&phba->hbalock, iflags);
11965 workposted = true;
11966
11967send_current_mbox:
11968 spin_lock_irqsave(&phba->hbalock, iflags);
11969 /* Release the mailbox command posting token */
11970 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11971 /* Setting active mailbox pointer need to be in sync to flag clear */
11972 phba->sli.mbox_active = NULL;
11973 spin_unlock_irqrestore(&phba->hbalock, iflags);
11974 /* Wake up worker thread to post the next pending mailbox command */
11975 lpfc_worker_wake_up(phba);
11976out_no_mqe_complete:
11977 if (bf_get(lpfc_trailer_consumed, mcqe))
11978 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11979 return workposted;
11980}
11981
11982/**
11983 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11984 * @phba: Pointer to HBA context object.
11985 * @cqe: Pointer to mailbox completion queue entry.
11986 *
11987 * This routine process a mailbox completion queue entry, it invokes the
11988 * proper mailbox complete handling or asynchrous event handling routine
11989 * according to the MCQE's async bit.
11990 *
11991 * Return: true if work posted to worker thread, otherwise false.
11992 **/
11993static bool
11994lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11995{
11996 struct lpfc_mcqe mcqe;
11997 bool workposted;
11998
11999 /* Copy the mailbox MCQE and convert endian order as needed */
12000 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
12001
12002 /* Invoke the proper event handling routine */
12003 if (!bf_get(lpfc_trailer_async, &mcqe))
12004 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
12005 else
12006 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
12007 return workposted;
12008}
12009
12010/**
James Smart4f774512009-05-22 14:52:35 -040012011 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
12012 * @phba: Pointer to HBA context object.
James Smart2a76a282012-08-03 12:35:54 -040012013 * @cq: Pointer to associated CQ
James Smart4f774512009-05-22 14:52:35 -040012014 * @wcqe: Pointer to work-queue completion queue entry.
12015 *
12016 * This routine handles an ELS work-queue completion event.
12017 *
12018 * Return: true if work posted to worker thread, otherwise false.
12019 **/
12020static bool
James Smart2a76a282012-08-03 12:35:54 -040012021lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012022 struct lpfc_wcqe_complete *wcqe)
12023{
James Smart4f774512009-05-22 14:52:35 -040012024 struct lpfc_iocbq *irspiocbq;
12025 unsigned long iflags;
James Smart2a76a282012-08-03 12:35:54 -040012026 struct lpfc_sli_ring *pring = cq->pring;
James Smart0e9bb8d2013-03-01 16:35:12 -050012027 int txq_cnt = 0;
12028 int txcmplq_cnt = 0;
12029 int fcp_txcmplq_cnt = 0;
James Smart4f774512009-05-22 14:52:35 -040012030
James Smart45ed1192009-10-02 15:17:02 -040012031 /* Get an irspiocbq for later ELS response processing use */
James Smart4f774512009-05-22 14:52:35 -040012032 irspiocbq = lpfc_sli_get_iocbq(phba);
12033 if (!irspiocbq) {
James Smart0e9bb8d2013-03-01 16:35:12 -050012034 if (!list_empty(&pring->txq))
12035 txq_cnt++;
12036 if (!list_empty(&pring->txcmplq))
12037 txcmplq_cnt++;
12038 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
12039 fcp_txcmplq_cnt++;
James Smart4f774512009-05-22 14:52:35 -040012040 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart2a9bf3d2010-06-07 15:24:45 -040012041 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12042 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
James Smart0e9bb8d2013-03-01 16:35:12 -050012043 txq_cnt, phba->iocb_cnt,
12044 fcp_txcmplq_cnt,
12045 txcmplq_cnt);
James Smart45ed1192009-10-02 15:17:02 -040012046 return false;
James Smart4f774512009-05-22 14:52:35 -040012047 }
James Smart4f774512009-05-22 14:52:35 -040012048
James Smart45ed1192009-10-02 15:17:02 -040012049 /* Save off the slow-path queue event for work thread to process */
12050 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
James Smart4f774512009-05-22 14:52:35 -040012051 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -040012052 list_add_tail(&irspiocbq->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012053 &phba->sli4_hba.sp_queue_event);
12054 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4f774512009-05-22 14:52:35 -040012055 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012056
James Smart45ed1192009-10-02 15:17:02 -040012057 return true;
James Smart4f774512009-05-22 14:52:35 -040012058}
12059
12060/**
12061 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12062 * @phba: Pointer to HBA context object.
12063 * @wcqe: Pointer to work-queue completion queue entry.
12064 *
12065 * This routine handles slow-path WQ entry comsumed event by invoking the
12066 * proper WQ release routine to the slow-path WQ.
12067 **/
12068static void
12069lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12070 struct lpfc_wcqe_release *wcqe)
12071{
James Smart2e90f4b2011-12-13 13:22:37 -050012072 /* sanity check on queue memory */
12073 if (unlikely(!phba->sli4_hba.els_wq))
12074 return;
James Smart4f774512009-05-22 14:52:35 -040012075 /* Check for the slow-path ELS work queue */
12076 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12077 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12078 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12079 else
12080 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12081 "2579 Slow-path wqe consume event carries "
12082 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12083 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12084 phba->sli4_hba.els_wq->queue_id);
12085}
12086
12087/**
12088 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12089 * @phba: Pointer to HBA context object.
12090 * @cq: Pointer to a WQ completion queue.
12091 * @wcqe: Pointer to work-queue completion queue entry.
12092 *
12093 * This routine handles an XRI abort event.
12094 *
12095 * Return: true if work posted to worker thread, otherwise false.
12096 **/
12097static bool
12098lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12099 struct lpfc_queue *cq,
12100 struct sli4_wcqe_xri_aborted *wcqe)
12101{
12102 bool workposted = false;
12103 struct lpfc_cq_event *cq_event;
12104 unsigned long iflags;
12105
12106 /* Allocate a new internal CQ_EVENT entry */
12107 cq_event = lpfc_sli4_cq_event_alloc(phba);
12108 if (!cq_event) {
12109 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12110 "0602 Failed to allocate CQ_EVENT entry\n");
12111 return false;
12112 }
12113
12114 /* Move the CQE into the proper xri abort event list */
12115 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12116 switch (cq->subtype) {
12117 case LPFC_FCP:
12118 spin_lock_irqsave(&phba->hbalock, iflags);
12119 list_add_tail(&cq_event->list,
12120 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12121 /* Set the fcp xri abort event flag */
12122 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12123 spin_unlock_irqrestore(&phba->hbalock, iflags);
12124 workposted = true;
12125 break;
12126 case LPFC_ELS:
12127 spin_lock_irqsave(&phba->hbalock, iflags);
12128 list_add_tail(&cq_event->list,
12129 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12130 /* Set the els xri abort event flag */
12131 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12132 spin_unlock_irqrestore(&phba->hbalock, iflags);
12133 workposted = true;
12134 break;
12135 default:
12136 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12137 "0603 Invalid work queue CQE subtype (x%x)\n",
12138 cq->subtype);
12139 workposted = false;
12140 break;
12141 }
12142 return workposted;
12143}
12144
12145/**
James Smart4d9ab992009-10-02 15:16:39 -040012146 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -040012147 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -040012148 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012149 *
James Smart4d9ab992009-10-02 15:16:39 -040012150 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012151 *
12152 * Return: true if work posted to worker thread, otherwise false.
12153 **/
12154static bool
James Smart4d9ab992009-10-02 15:16:39 -040012155lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12156{
12157 bool workposted = false;
12158 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12159 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12160 struct hbq_dmabuf *dma_buf;
James Smart7851fe22011-07-22 18:36:52 -040012161 uint32_t status, rq_id;
James Smart4d9ab992009-10-02 15:16:39 -040012162 unsigned long iflags;
12163
James Smart2e90f4b2011-12-13 13:22:37 -050012164 /* sanity check on queue memory */
12165 if (unlikely(!hrq) || unlikely(!drq))
12166 return workposted;
12167
James Smart7851fe22011-07-22 18:36:52 -040012168 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12169 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12170 else
12171 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12172 if (rq_id != hrq->queue_id)
James Smart4d9ab992009-10-02 15:16:39 -040012173 goto out;
12174
12175 status = bf_get(lpfc_rcqe_status, rcqe);
12176 switch (status) {
12177 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12178 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12179 "2537 Receive Frame Truncated!!\n");
James Smartb84daac2012-08-03 12:35:13 -040012180 hrq->RQ_buf_trunc++;
James Smart4d9ab992009-10-02 15:16:39 -040012181 case FC_STATUS_RQ_SUCCESS:
James Smart5ffc2662009-11-18 15:39:44 -050012182 lpfc_sli4_rq_release(hrq, drq);
James Smart4d9ab992009-10-02 15:16:39 -040012183 spin_lock_irqsave(&phba->hbalock, iflags);
12184 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12185 if (!dma_buf) {
James Smartb84daac2012-08-03 12:35:13 -040012186 hrq->RQ_no_buf_found++;
James Smart4d9ab992009-10-02 15:16:39 -040012187 spin_unlock_irqrestore(&phba->hbalock, iflags);
12188 goto out;
12189 }
James Smartb84daac2012-08-03 12:35:13 -040012190 hrq->RQ_rcv_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012191 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
12192 /* save off the frame for the word thread to process */
12193 list_add_tail(&dma_buf->cq_event.list,
James Smart45ed1192009-10-02 15:17:02 -040012194 &phba->sli4_hba.sp_queue_event);
James Smart4d9ab992009-10-02 15:16:39 -040012195 /* Frame received */
James Smart45ed1192009-10-02 15:17:02 -040012196 phba->hba_flag |= HBA_SP_QUEUE_EVT;
James Smart4d9ab992009-10-02 15:16:39 -040012197 spin_unlock_irqrestore(&phba->hbalock, iflags);
12198 workposted = true;
12199 break;
12200 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12201 case FC_STATUS_INSUFF_BUF_FRM_DISC:
James Smartb84daac2012-08-03 12:35:13 -040012202 hrq->RQ_no_posted_buf++;
James Smart4d9ab992009-10-02 15:16:39 -040012203 /* Post more buffers if possible */
12204 spin_lock_irqsave(&phba->hbalock, iflags);
12205 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12206 spin_unlock_irqrestore(&phba->hbalock, iflags);
12207 workposted = true;
12208 break;
12209 }
12210out:
12211 return workposted;
James Smart4d9ab992009-10-02 15:16:39 -040012212}
12213
12214/**
12215 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12216 * @phba: Pointer to HBA context object.
12217 * @cq: Pointer to the completion queue.
12218 * @wcqe: Pointer to a completion queue entry.
12219 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030012220 * This routine process a slow-path work-queue or receive queue completion queue
James Smart4d9ab992009-10-02 15:16:39 -040012221 * entry.
12222 *
12223 * Return: true if work posted to worker thread, otherwise false.
12224 **/
12225static bool
12226lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012227 struct lpfc_cqe *cqe)
12228{
James Smart45ed1192009-10-02 15:17:02 -040012229 struct lpfc_cqe cqevt;
James Smart4f774512009-05-22 14:52:35 -040012230 bool workposted = false;
12231
12232 /* Copy the work queue CQE and convert endian order if needed */
James Smart45ed1192009-10-02 15:17:02 -040012233 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
James Smart4f774512009-05-22 14:52:35 -040012234
12235 /* Check and process for different type of WCQE and dispatch */
James Smart45ed1192009-10-02 15:17:02 -040012236 switch (bf_get(lpfc_cqe_code, &cqevt)) {
James Smart4f774512009-05-22 14:52:35 -040012237 case CQE_CODE_COMPL_WQE:
James Smart45ed1192009-10-02 15:17:02 -040012238 /* Process the WQ/RQ complete event */
James Smartbc739052010-08-04 16:11:18 -040012239 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012240 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012241 (struct lpfc_wcqe_complete *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012242 break;
12243 case CQE_CODE_RELEASE_WQE:
12244 /* Process the WQ release event */
12245 lpfc_sli4_sp_handle_rel_wcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012246 (struct lpfc_wcqe_release *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012247 break;
12248 case CQE_CODE_XRI_ABORTED:
12249 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012250 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012251 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
James Smart45ed1192009-10-02 15:17:02 -040012252 (struct sli4_wcqe_xri_aborted *)&cqevt);
James Smart4f774512009-05-22 14:52:35 -040012253 break;
James Smart4d9ab992009-10-02 15:16:39 -040012254 case CQE_CODE_RECEIVE:
James Smart7851fe22011-07-22 18:36:52 -040012255 case CQE_CODE_RECEIVE_V1:
James Smart4d9ab992009-10-02 15:16:39 -040012256 /* Process the RQ event */
James Smartbc739052010-08-04 16:11:18 -040012257 phba->last_completion_time = jiffies;
James Smart4d9ab992009-10-02 15:16:39 -040012258 workposted = lpfc_sli4_sp_handle_rcqe(phba,
James Smart45ed1192009-10-02 15:17:02 -040012259 (struct lpfc_rcqe *)&cqevt);
James Smart4d9ab992009-10-02 15:16:39 -040012260 break;
James Smart4f774512009-05-22 14:52:35 -040012261 default:
12262 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12263 "0388 Not a valid WCQE code: x%x\n",
James Smart45ed1192009-10-02 15:17:02 -040012264 bf_get(lpfc_cqe_code, &cqevt));
James Smart4f774512009-05-22 14:52:35 -040012265 break;
12266 }
12267 return workposted;
12268}
12269
12270/**
James Smart4f774512009-05-22 14:52:35 -040012271 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12272 * @phba: Pointer to HBA context object.
12273 * @eqe: Pointer to fast-path event queue entry.
12274 *
12275 * This routine process a event queue entry from the slow-path event queue.
12276 * It will check the MajorCode and MinorCode to determine this is for a
12277 * completion event on a completion queue, if not, an error shall be logged
12278 * and just return. Otherwise, it will get to the corresponding completion
12279 * queue and process all the entries on that completion queue, rearm the
12280 * completion queue, and then return.
12281 *
12282 **/
12283static void
James Smart67d12732012-08-03 12:36:13 -040012284lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12285 struct lpfc_queue *speq)
James Smart4f774512009-05-22 14:52:35 -040012286{
James Smart67d12732012-08-03 12:36:13 -040012287 struct lpfc_queue *cq = NULL, *childq;
James Smart4f774512009-05-22 14:52:35 -040012288 struct lpfc_cqe *cqe;
12289 bool workposted = false;
12290 int ecount = 0;
12291 uint16_t cqid;
12292
James Smart4f774512009-05-22 14:52:35 -040012293 /* Get the reference to the corresponding CQ */
James Smartcb5172e2010-03-15 11:25:07 -040012294 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
James Smart4f774512009-05-22 14:52:35 -040012295
James Smart4f774512009-05-22 14:52:35 -040012296 list_for_each_entry(childq, &speq->child_list, list) {
12297 if (childq->queue_id == cqid) {
12298 cq = childq;
12299 break;
12300 }
12301 }
12302 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012303 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12304 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12305 "0365 Slow-path CQ identifier "
12306 "(%d) does not exist\n", cqid);
James Smart4f774512009-05-22 14:52:35 -040012307 return;
12308 }
12309
12310 /* Process all the entries to the CQ */
12311 switch (cq->type) {
12312 case LPFC_MCQ:
12313 while ((cqe = lpfc_sli4_cq_get(cq))) {
12314 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012315 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012316 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012317 cq->CQ_mbox++;
James Smart4f774512009-05-22 14:52:35 -040012318 }
12319 break;
12320 case LPFC_WCQ:
12321 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart05580562011-05-24 11:40:48 -040012322 if (cq->subtype == LPFC_FCP)
12323 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
12324 cqe);
12325 else
12326 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12327 cqe);
James Smart73d91e52011-10-10 21:32:10 -040012328 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012329 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12330 }
James Smartb84daac2012-08-03 12:35:13 -040012331
12332 /* Track the max number of CQEs processed in 1 EQ */
12333 if (ecount > cq->CQ_max_cqe)
12334 cq->CQ_max_cqe = ecount;
James Smart4f774512009-05-22 14:52:35 -040012335 break;
12336 default:
12337 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12338 "0370 Invalid completion queue type (%d)\n",
12339 cq->type);
12340 return;
12341 }
12342
12343 /* Catch the no cq entry condition, log an error */
12344 if (unlikely(ecount == 0))
12345 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12346 "0371 No entry from the CQ: identifier "
12347 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12348
12349 /* In any case, flash and re-arm the RCQ */
12350 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12351
12352 /* wake up worker thread if there are works to be done */
12353 if (workposted)
12354 lpfc_worker_wake_up(phba);
12355}
12356
12357/**
12358 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
James Smart2a76a282012-08-03 12:35:54 -040012359 * @phba: Pointer to HBA context object.
12360 * @cq: Pointer to associated CQ
12361 * @wcqe: Pointer to work-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -040012362 *
12363 * This routine process a fast-path work queue completion entry from fast-path
12364 * event queue for FCP command response completion.
12365 **/
12366static void
James Smart2a76a282012-08-03 12:35:54 -040012367lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -040012368 struct lpfc_wcqe_complete *wcqe)
12369{
James Smart2a76a282012-08-03 12:35:54 -040012370 struct lpfc_sli_ring *pring = cq->pring;
James Smart4f774512009-05-22 14:52:35 -040012371 struct lpfc_iocbq *cmdiocbq;
12372 struct lpfc_iocbq irspiocbq;
12373 unsigned long iflags;
12374
James Smart4f774512009-05-22 14:52:35 -040012375 /* Check for response status */
12376 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12377 /* If resource errors reported from HBA, reduce queue
12378 * depth of the SCSI device.
12379 */
James Smarte3d2b802012-08-14 14:25:43 -040012380 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12381 IOSTAT_LOCAL_REJECT)) &&
12382 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12383 IOERR_NO_RESOURCES))
James Smart4f774512009-05-22 14:52:35 -040012384 phba->lpfc_rampdown_queue_depth(phba);
James Smarte3d2b802012-08-14 14:25:43 -040012385
James Smart4f774512009-05-22 14:52:35 -040012386 /* Log the error status */
12387 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12388 "0373 FCP complete error: status=x%x, "
12389 "hw_status=x%x, total_data_specified=%d, "
12390 "parameter=x%x, word3=x%x\n",
12391 bf_get(lpfc_wcqe_c_status, wcqe),
12392 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12393 wcqe->total_data_placed, wcqe->parameter,
12394 wcqe->word3);
12395 }
12396
12397 /* Look up the FCP command IOCB and create pseudo response IOCB */
James Smart7e56aa22012-08-03 12:35:34 -040012398 spin_lock_irqsave(&pring->ring_lock, iflags);
12399 pring->stats.iocb_event++;
James Smart4f774512009-05-22 14:52:35 -040012400 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12401 bf_get(lpfc_wcqe_c_request_tag, wcqe));
James Smart7e56aa22012-08-03 12:35:34 -040012402 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart4f774512009-05-22 14:52:35 -040012403 if (unlikely(!cmdiocbq)) {
12404 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12405 "0374 FCP complete with no corresponding "
12406 "cmdiocb: iotag (%d)\n",
12407 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12408 return;
12409 }
12410 if (unlikely(!cmdiocbq->iocb_cmpl)) {
12411 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12412 "0375 FCP cmdiocb not callback function "
12413 "iotag: (%d)\n",
12414 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12415 return;
12416 }
12417
12418 /* Fake the irspiocb and copy necessary response information */
James Smart341af102010-01-26 23:07:37 -050012419 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
James Smart4f774512009-05-22 14:52:35 -040012420
James Smart0f65ff62010-02-26 14:14:23 -050012421 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12422 spin_lock_irqsave(&phba->hbalock, iflags);
12423 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12424 spin_unlock_irqrestore(&phba->hbalock, iflags);
12425 }
12426
James Smart4f774512009-05-22 14:52:35 -040012427 /* Pass the cmd_iocb and the rsp state to the upper layer */
12428 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12429}
12430
12431/**
12432 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12433 * @phba: Pointer to HBA context object.
12434 * @cq: Pointer to completion queue.
12435 * @wcqe: Pointer to work-queue completion queue entry.
12436 *
12437 * This routine handles an fast-path WQ entry comsumed event by invoking the
12438 * proper WQ release routine to the slow-path WQ.
12439 **/
12440static void
12441lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12442 struct lpfc_wcqe_release *wcqe)
12443{
12444 struct lpfc_queue *childwq;
12445 bool wqid_matched = false;
12446 uint16_t fcp_wqid;
12447
12448 /* Check for fast-path FCP work queue release */
12449 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
12450 list_for_each_entry(childwq, &cq->child_list, list) {
12451 if (childwq->queue_id == fcp_wqid) {
12452 lpfc_sli4_wq_release(childwq,
12453 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12454 wqid_matched = true;
12455 break;
12456 }
12457 }
12458 /* Report warning log message if no match found */
12459 if (wqid_matched != true)
12460 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12461 "2580 Fast-path wqe consume event carries "
12462 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
12463}
12464
12465/**
12466 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
12467 * @cq: Pointer to the completion queue.
12468 * @eqe: Pointer to fast-path completion queue entry.
12469 *
12470 * This routine process a fast-path work queue completion entry from fast-path
12471 * event queue for FCP command response completion.
12472 **/
12473static int
12474lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12475 struct lpfc_cqe *cqe)
12476{
12477 struct lpfc_wcqe_release wcqe;
12478 bool workposted = false;
12479
12480 /* Copy the work queue CQE and convert endian order if needed */
12481 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12482
12483 /* Check and process for different type of WCQE and dispatch */
12484 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12485 case CQE_CODE_COMPL_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012486 cq->CQ_wq++;
James Smart4f774512009-05-22 14:52:35 -040012487 /* Process the WQ complete event */
James Smart98fc5dd2010-06-07 15:24:29 -040012488 phba->last_completion_time = jiffies;
James Smart2a76a282012-08-03 12:35:54 -040012489 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
James Smart4f774512009-05-22 14:52:35 -040012490 (struct lpfc_wcqe_complete *)&wcqe);
12491 break;
12492 case CQE_CODE_RELEASE_WQE:
James Smartb84daac2012-08-03 12:35:13 -040012493 cq->CQ_release_wqe++;
James Smart4f774512009-05-22 14:52:35 -040012494 /* Process the WQ release event */
12495 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12496 (struct lpfc_wcqe_release *)&wcqe);
12497 break;
12498 case CQE_CODE_XRI_ABORTED:
James Smartb84daac2012-08-03 12:35:13 -040012499 cq->CQ_xri_aborted++;
James Smart4f774512009-05-22 14:52:35 -040012500 /* Process the WQ XRI abort event */
James Smartbc739052010-08-04 16:11:18 -040012501 phba->last_completion_time = jiffies;
James Smart4f774512009-05-22 14:52:35 -040012502 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12503 (struct sli4_wcqe_xri_aborted *)&wcqe);
12504 break;
12505 default:
12506 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12507 "0144 Not a valid WCQE code: x%x\n",
12508 bf_get(lpfc_wcqe_c_code, &wcqe));
12509 break;
12510 }
12511 return workposted;
12512}
12513
12514/**
James Smart67d12732012-08-03 12:36:13 -040012515 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
James Smart4f774512009-05-22 14:52:35 -040012516 * @phba: Pointer to HBA context object.
12517 * @eqe: Pointer to fast-path event queue entry.
12518 *
12519 * This routine process a event queue entry from the fast-path event queue.
12520 * It will check the MajorCode and MinorCode to determine this is for a
12521 * completion event on a completion queue, if not, an error shall be logged
12522 * and just return. Otherwise, it will get to the corresponding completion
12523 * queue and process all the entries on the completion queue, rearm the
12524 * completion queue, and then return.
12525 **/
12526static void
James Smart67d12732012-08-03 12:36:13 -040012527lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12528 uint32_t qidx)
James Smart4f774512009-05-22 14:52:35 -040012529{
12530 struct lpfc_queue *cq;
12531 struct lpfc_cqe *cqe;
12532 bool workposted = false;
12533 uint16_t cqid;
12534 int ecount = 0;
12535
James Smartcb5172e2010-03-15 11:25:07 -040012536 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
James Smart4f774512009-05-22 14:52:35 -040012537 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart67d12732012-08-03 12:36:13 -040012538 "0366 Not a valid completion "
James Smart4f774512009-05-22 14:52:35 -040012539 "event: majorcode=x%x, minorcode=x%x\n",
James Smartcb5172e2010-03-15 11:25:07 -040012540 bf_get_le32(lpfc_eqe_major_code, eqe),
12541 bf_get_le32(lpfc_eqe_minor_code, eqe));
James Smart4f774512009-05-22 14:52:35 -040012542 return;
12543 }
12544
James Smart67d12732012-08-03 12:36:13 -040012545 /* Get the reference to the corresponding CQ */
12546 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12547
12548 /* Check if this is a Slow path event */
12549 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12550 lpfc_sli4_sp_handle_eqe(phba, eqe,
12551 phba->sli4_hba.hba_eq[qidx]);
12552 return;
12553 }
12554
James Smart2e90f4b2011-12-13 13:22:37 -050012555 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12556 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12557 "3146 Fast-path completion queues "
12558 "does not exist\n");
12559 return;
12560 }
James Smart67d12732012-08-03 12:36:13 -040012561 cq = phba->sli4_hba.fcp_cq[qidx];
James Smart4f774512009-05-22 14:52:35 -040012562 if (unlikely(!cq)) {
James Smart75baf692010-06-08 18:31:21 -040012563 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12564 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12565 "0367 Fast-path completion queue "
James Smart67d12732012-08-03 12:36:13 -040012566 "(%d) does not exist\n", qidx);
James Smart4f774512009-05-22 14:52:35 -040012567 return;
12568 }
12569
James Smart4f774512009-05-22 14:52:35 -040012570 if (unlikely(cqid != cq->queue_id)) {
12571 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12572 "0368 Miss-matched fast-path completion "
12573 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12574 cqid, cq->queue_id);
12575 return;
12576 }
12577
12578 /* Process all the entries to the CQ */
12579 while ((cqe = lpfc_sli4_cq_get(cq))) {
12580 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
James Smart73d91e52011-10-10 21:32:10 -040012581 if (!(++ecount % cq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012582 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12583 }
12584
James Smartb84daac2012-08-03 12:35:13 -040012585 /* Track the max number of CQEs processed in 1 EQ */
12586 if (ecount > cq->CQ_max_cqe)
12587 cq->CQ_max_cqe = ecount;
12588
James Smart4f774512009-05-22 14:52:35 -040012589 /* Catch the no cq entry condition */
12590 if (unlikely(ecount == 0))
12591 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12592 "0369 No entry from fast-path completion "
12593 "queue fcpcqid=%d\n", cq->queue_id);
12594
12595 /* In any case, flash and re-arm the CQ */
12596 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12597
12598 /* wake up worker thread if there are works to be done */
12599 if (workposted)
12600 lpfc_worker_wake_up(phba);
12601}
12602
12603static void
12604lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12605{
12606 struct lpfc_eqe *eqe;
12607
12608 /* walk all the EQ entries and drop on the floor */
12609 while ((eqe = lpfc_sli4_eq_get(eq)))
12610 ;
12611
12612 /* Clear and re-arm the EQ */
12613 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12614}
12615
James Smart1ba981f2014-02-20 09:56:45 -050012616
12617/**
12618 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
12619 * entry
12620 * @phba: Pointer to HBA context object.
12621 * @eqe: Pointer to fast-path event queue entry.
12622 *
12623 * This routine process a event queue entry from the Flash Optimized Fabric
12624 * event queue. It will check the MajorCode and MinorCode to determine this
12625 * is for a completion event on a completion queue, if not, an error shall be
12626 * logged and just return. Otherwise, it will get to the corresponding
12627 * completion queue and process all the entries on the completion queue, rearm
12628 * the completion queue, and then return.
12629 **/
12630static void
12631lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
12632{
12633 struct lpfc_queue *cq;
12634 struct lpfc_cqe *cqe;
12635 bool workposted = false;
12636 uint16_t cqid;
12637 int ecount = 0;
12638
12639 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12640 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12641 "9147 Not a valid completion "
12642 "event: majorcode=x%x, minorcode=x%x\n",
12643 bf_get_le32(lpfc_eqe_major_code, eqe),
12644 bf_get_le32(lpfc_eqe_minor_code, eqe));
12645 return;
12646 }
12647
12648 /* Get the reference to the corresponding CQ */
12649 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12650
12651 /* Next check for OAS */
12652 cq = phba->sli4_hba.oas_cq;
12653 if (unlikely(!cq)) {
12654 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12655 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12656 "9148 OAS completion queue "
12657 "does not exist\n");
12658 return;
12659 }
12660
12661 if (unlikely(cqid != cq->queue_id)) {
12662 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12663 "9149 Miss-matched fast-path compl "
12664 "queue id: eqcqid=%d, fcpcqid=%d\n",
12665 cqid, cq->queue_id);
12666 return;
12667 }
12668
12669 /* Process all the entries to the OAS CQ */
12670 while ((cqe = lpfc_sli4_cq_get(cq))) {
12671 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12672 if (!(++ecount % cq->entry_repost))
12673 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12674 }
12675
12676 /* Track the max number of CQEs processed in 1 EQ */
12677 if (ecount > cq->CQ_max_cqe)
12678 cq->CQ_max_cqe = ecount;
12679
12680 /* Catch the no cq entry condition */
12681 if (unlikely(ecount == 0))
12682 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12683 "9153 No entry from fast-path completion "
12684 "queue fcpcqid=%d\n", cq->queue_id);
12685
12686 /* In any case, flash and re-arm the CQ */
12687 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12688
12689 /* wake up worker thread if there are works to be done */
12690 if (workposted)
12691 lpfc_worker_wake_up(phba);
12692}
12693
12694/**
12695 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
12696 * @irq: Interrupt number.
12697 * @dev_id: The device context pointer.
12698 *
12699 * This function is directly called from the PCI layer as an interrupt
12700 * service routine when device with SLI-4 interface spec is enabled with
12701 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
12702 * IOCB ring event in the HBA. However, when the device is enabled with either
12703 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12704 * device-level interrupt handler. When the PCI slot is in error recovery
12705 * or the HBA is undergoing initialization, the interrupt handler will not
12706 * process the interrupt. The Flash Optimized Fabric ring event are handled in
12707 * the intrrupt context. This function is called without any lock held.
12708 * It gets the hbalock to access and update SLI data structures. Note that,
12709 * the EQ to CQ are one-to-one map such that the EQ index is
12710 * equal to that of CQ index.
12711 *
12712 * This function returns IRQ_HANDLED when interrupt is handled else it
12713 * returns IRQ_NONE.
12714 **/
12715irqreturn_t
12716lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
12717{
12718 struct lpfc_hba *phba;
12719 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12720 struct lpfc_queue *eq;
12721 struct lpfc_eqe *eqe;
12722 unsigned long iflag;
12723 int ecount = 0;
James Smart1ba981f2014-02-20 09:56:45 -050012724
12725 /* Get the driver's phba structure from the dev_id */
12726 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12727 phba = fcp_eq_hdl->phba;
James Smart1ba981f2014-02-20 09:56:45 -050012728
12729 if (unlikely(!phba))
12730 return IRQ_NONE;
12731
12732 /* Get to the EQ struct associated with this vector */
12733 eq = phba->sli4_hba.fof_eq;
12734 if (unlikely(!eq))
12735 return IRQ_NONE;
12736
12737 /* Check device state for handling interrupt */
12738 if (unlikely(lpfc_intr_state_check(phba))) {
12739 eq->EQ_badstate++;
12740 /* Check again for link_state with lock held */
12741 spin_lock_irqsave(&phba->hbalock, iflag);
12742 if (phba->link_state < LPFC_LINK_DOWN)
12743 /* Flush, clear interrupt, and rearm the EQ */
12744 lpfc_sli4_eq_flush(phba, eq);
12745 spin_unlock_irqrestore(&phba->hbalock, iflag);
12746 return IRQ_NONE;
12747 }
12748
12749 /*
12750 * Process all the event on FCP fast-path EQ
12751 */
12752 while ((eqe = lpfc_sli4_eq_get(eq))) {
12753 lpfc_sli4_fof_handle_eqe(phba, eqe);
12754 if (!(++ecount % eq->entry_repost))
12755 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
12756 eq->EQ_processed++;
12757 }
12758
12759 /* Track the max number of EQEs processed in 1 intr */
12760 if (ecount > eq->EQ_max_eqe)
12761 eq->EQ_max_eqe = ecount;
12762
12763
12764 if (unlikely(ecount == 0)) {
12765 eq->EQ_no_entry++;
12766
12767 if (phba->intr_type == MSIX)
12768 /* MSI-X treated interrupt served as no EQ share INT */
12769 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12770 "9145 MSI-X interrupt with no EQE\n");
12771 else {
12772 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12773 "9146 ISR interrupt with no EQE\n");
12774 /* Non MSI-X treated on interrupt as EQ share INT */
12775 return IRQ_NONE;
12776 }
12777 }
12778 /* Always clear and re-arm the fast-path EQ */
12779 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12780 return IRQ_HANDLED;
12781}
12782
James Smart4f774512009-05-22 14:52:35 -040012783/**
James Smart67d12732012-08-03 12:36:13 -040012784 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
James Smart4f774512009-05-22 14:52:35 -040012785 * @irq: Interrupt number.
12786 * @dev_id: The device context pointer.
12787 *
12788 * This function is directly called from the PCI layer as an interrupt
12789 * service routine when device with SLI-4 interface spec is enabled with
12790 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12791 * ring event in the HBA. However, when the device is enabled with either
12792 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12793 * device-level interrupt handler. When the PCI slot is in error recovery
12794 * or the HBA is undergoing initialization, the interrupt handler will not
12795 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12796 * the intrrupt context. This function is called without any lock held.
12797 * It gets the hbalock to access and update SLI data structures. Note that,
12798 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12799 * equal to that of FCP CQ index.
12800 *
James Smart67d12732012-08-03 12:36:13 -040012801 * The link attention and ELS ring attention events are handled
12802 * by the worker thread. The interrupt handler signals the worker thread
12803 * and returns for these events. This function is called without any lock
12804 * held. It gets the hbalock to access and update SLI data structures.
12805 *
James Smart4f774512009-05-22 14:52:35 -040012806 * This function returns IRQ_HANDLED when interrupt is handled else it
12807 * returns IRQ_NONE.
12808 **/
12809irqreturn_t
James Smart67d12732012-08-03 12:36:13 -040012810lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
James Smart4f774512009-05-22 14:52:35 -040012811{
12812 struct lpfc_hba *phba;
12813 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12814 struct lpfc_queue *fpeq;
12815 struct lpfc_eqe *eqe;
12816 unsigned long iflag;
12817 int ecount = 0;
James Smart962bc512013-01-03 15:44:00 -050012818 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012819
12820 /* Get the driver's phba structure from the dev_id */
12821 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12822 phba = fcp_eq_hdl->phba;
12823 fcp_eqidx = fcp_eq_hdl->idx;
12824
12825 if (unlikely(!phba))
12826 return IRQ_NONE;
James Smart67d12732012-08-03 12:36:13 -040012827 if (unlikely(!phba->sli4_hba.hba_eq))
James Smart5350d872011-10-10 21:33:49 -040012828 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012829
12830 /* Get to the EQ struct associated with this vector */
James Smart67d12732012-08-03 12:36:13 -040012831 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart2e90f4b2011-12-13 13:22:37 -050012832 if (unlikely(!fpeq))
12833 return IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012834
James Smartba20c852012-08-03 12:36:52 -040012835 if (lpfc_fcp_look_ahead) {
12836 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12837 lpfc_sli4_eq_clr_intr(fpeq);
12838 else {
12839 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12840 return IRQ_NONE;
12841 }
12842 }
12843
James Smart4f774512009-05-22 14:52:35 -040012844 /* Check device state for handling interrupt */
12845 if (unlikely(lpfc_intr_state_check(phba))) {
James Smartb84daac2012-08-03 12:35:13 -040012846 fpeq->EQ_badstate++;
James Smart4f774512009-05-22 14:52:35 -040012847 /* Check again for link_state with lock held */
12848 spin_lock_irqsave(&phba->hbalock, iflag);
12849 if (phba->link_state < LPFC_LINK_DOWN)
12850 /* Flush, clear interrupt, and rearm the EQ */
12851 lpfc_sli4_eq_flush(phba, fpeq);
12852 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smartba20c852012-08-03 12:36:52 -040012853 if (lpfc_fcp_look_ahead)
12854 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012855 return IRQ_NONE;
12856 }
12857
12858 /*
12859 * Process all the event on FCP fast-path EQ
12860 */
12861 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
James Smarteb016562014-09-03 12:58:06 -040012862 if (eqe == NULL)
12863 break;
12864
James Smart67d12732012-08-03 12:36:13 -040012865 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
James Smart73d91e52011-10-10 21:32:10 -040012866 if (!(++ecount % fpeq->entry_repost))
James Smart4f774512009-05-22 14:52:35 -040012867 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
James Smartb84daac2012-08-03 12:35:13 -040012868 fpeq->EQ_processed++;
James Smart4f774512009-05-22 14:52:35 -040012869 }
12870
James Smartb84daac2012-08-03 12:35:13 -040012871 /* Track the max number of EQEs processed in 1 intr */
12872 if (ecount > fpeq->EQ_max_eqe)
12873 fpeq->EQ_max_eqe = ecount;
12874
James Smart4f774512009-05-22 14:52:35 -040012875 /* Always clear and re-arm the fast-path EQ */
12876 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12877
12878 if (unlikely(ecount == 0)) {
James Smartb84daac2012-08-03 12:35:13 -040012879 fpeq->EQ_no_entry++;
James Smartba20c852012-08-03 12:36:52 -040012880
12881 if (lpfc_fcp_look_ahead) {
12882 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12883 return IRQ_NONE;
12884 }
12885
James Smart4f774512009-05-22 14:52:35 -040012886 if (phba->intr_type == MSIX)
12887 /* MSI-X treated interrupt served as no EQ share INT */
12888 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12889 "0358 MSI-X interrupt with no EQE\n");
12890 else
12891 /* Non MSI-X treated on interrupt as EQ share INT */
12892 return IRQ_NONE;
12893 }
12894
James Smartba20c852012-08-03 12:36:52 -040012895 if (lpfc_fcp_look_ahead)
12896 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
James Smart4f774512009-05-22 14:52:35 -040012897 return IRQ_HANDLED;
12898} /* lpfc_sli4_fp_intr_handler */
12899
12900/**
12901 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12902 * @irq: Interrupt number.
12903 * @dev_id: The device context pointer.
12904 *
12905 * This function is the device-level interrupt handler to device with SLI-4
12906 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12907 * interrupt mode is enabled and there is an event in the HBA which requires
12908 * driver attention. This function invokes the slow-path interrupt attention
12909 * handling function and fast-path interrupt attention handling function in
12910 * turn to process the relevant HBA attention events. This function is called
12911 * without any lock held. It gets the hbalock to access and update SLI data
12912 * structures.
12913 *
12914 * This function returns IRQ_HANDLED when interrupt is handled, else it
12915 * returns IRQ_NONE.
12916 **/
12917irqreturn_t
12918lpfc_sli4_intr_handler(int irq, void *dev_id)
12919{
12920 struct lpfc_hba *phba;
James Smart67d12732012-08-03 12:36:13 -040012921 irqreturn_t hba_irq_rc;
12922 bool hba_handled = false;
James Smart962bc512013-01-03 15:44:00 -050012923 int fcp_eqidx;
James Smart4f774512009-05-22 14:52:35 -040012924
12925 /* Get the driver's phba structure from the dev_id */
12926 phba = (struct lpfc_hba *)dev_id;
12927
12928 if (unlikely(!phba))
12929 return IRQ_NONE;
12930
12931 /*
James Smart4f774512009-05-22 14:52:35 -040012932 * Invoke fast-path host attention interrupt handling as appropriate.
12933 */
James Smart67d12732012-08-03 12:36:13 -040012934 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12935 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
James Smart4f774512009-05-22 14:52:35 -040012936 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
James Smart67d12732012-08-03 12:36:13 -040012937 if (hba_irq_rc == IRQ_HANDLED)
12938 hba_handled |= true;
James Smart4f774512009-05-22 14:52:35 -040012939 }
12940
James Smart1ba981f2014-02-20 09:56:45 -050012941 if (phba->cfg_fof) {
12942 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
12943 &phba->sli4_hba.fcp_eq_hdl[0]);
12944 if (hba_irq_rc == IRQ_HANDLED)
12945 hba_handled |= true;
12946 }
12947
James Smart67d12732012-08-03 12:36:13 -040012948 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
James Smart4f774512009-05-22 14:52:35 -040012949} /* lpfc_sli4_intr_handler */
12950
12951/**
12952 * lpfc_sli4_queue_free - free a queue structure and associated memory
12953 * @queue: The queue structure to free.
12954 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040012955 * This function frees a queue structure and the DMAable memory used for
James Smart4f774512009-05-22 14:52:35 -040012956 * the host resident queue. This function must be called after destroying the
12957 * queue on the HBA.
12958 **/
12959void
12960lpfc_sli4_queue_free(struct lpfc_queue *queue)
12961{
12962 struct lpfc_dmabuf *dmabuf;
12963
12964 if (!queue)
12965 return;
12966
12967 while (!list_empty(&queue->page_list)) {
12968 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12969 list);
James Smart49198b32010-04-06 15:04:33 -040012970 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
James Smart4f774512009-05-22 14:52:35 -040012971 dmabuf->virt, dmabuf->phys);
12972 kfree(dmabuf);
12973 }
12974 kfree(queue);
12975 return;
12976}
12977
12978/**
12979 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12980 * @phba: The HBA that this queue is being created on.
12981 * @entry_size: The size of each queue entry for this queue.
12982 * @entry count: The number of entries that this queue will handle.
12983 *
12984 * This function allocates a queue structure and the DMAable memory used for
12985 * the host resident queue. This function must be called before creating the
12986 * queue on the HBA.
12987 **/
12988struct lpfc_queue *
12989lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12990 uint32_t entry_count)
12991{
12992 struct lpfc_queue *queue;
12993 struct lpfc_dmabuf *dmabuf;
12994 int x, total_qe_count;
12995 void *dma_pointer;
James Smartcb5172e2010-03-15 11:25:07 -040012996 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart4f774512009-05-22 14:52:35 -040012997
James Smartcb5172e2010-03-15 11:25:07 -040012998 if (!phba->sli4_hba.pc_sli4_params.supported)
12999 hw_page_size = SLI4_PAGE_SIZE;
13000
James Smart4f774512009-05-22 14:52:35 -040013001 queue = kzalloc(sizeof(struct lpfc_queue) +
13002 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
13003 if (!queue)
13004 return NULL;
James Smartcb5172e2010-03-15 11:25:07 -040013005 queue->page_count = (ALIGN(entry_size * entry_count,
13006 hw_page_size))/hw_page_size;
James Smart4f774512009-05-22 14:52:35 -040013007 INIT_LIST_HEAD(&queue->list);
13008 INIT_LIST_HEAD(&queue->page_list);
13009 INIT_LIST_HEAD(&queue->child_list);
13010 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
13011 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13012 if (!dmabuf)
13013 goto out_fail;
Joe Perches1aee3832014-09-03 12:56:12 -040013014 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
13015 hw_page_size, &dmabuf->phys,
13016 GFP_KERNEL);
James Smart4f774512009-05-22 14:52:35 -040013017 if (!dmabuf->virt) {
13018 kfree(dmabuf);
13019 goto out_fail;
13020 }
13021 dmabuf->buffer_tag = x;
13022 list_add_tail(&dmabuf->list, &queue->page_list);
13023 /* initialize queue's entry array */
13024 dma_pointer = dmabuf->virt;
13025 for (; total_qe_count < entry_count &&
James Smartcb5172e2010-03-15 11:25:07 -040013026 dma_pointer < (hw_page_size + dmabuf->virt);
James Smart4f774512009-05-22 14:52:35 -040013027 total_qe_count++, dma_pointer += entry_size) {
13028 queue->qe[total_qe_count].address = dma_pointer;
13029 }
13030 }
13031 queue->entry_size = entry_size;
13032 queue->entry_count = entry_count;
James Smart73d91e52011-10-10 21:32:10 -040013033
13034 /*
13035 * entry_repost is calculated based on the number of entries in the
13036 * queue. This works out except for RQs. If buffers are NOT initially
13037 * posted for every RQE, entry_repost should be adjusted accordingly.
13038 */
13039 queue->entry_repost = (entry_count >> 3);
13040 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13041 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
James Smart4f774512009-05-22 14:52:35 -040013042 queue->phba = phba;
13043
13044 return queue;
13045out_fail:
13046 lpfc_sli4_queue_free(queue);
13047 return NULL;
13048}
13049
13050/**
James Smart962bc512013-01-03 15:44:00 -050013051 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13052 * @phba: HBA structure that indicates port to create a queue on.
13053 * @pci_barset: PCI BAR set flag.
13054 *
13055 * This function shall perform iomap of the specified PCI BAR address to host
13056 * memory address if not already done so and return it. The returned host
13057 * memory address can be NULL.
13058 */
13059static void __iomem *
13060lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13061{
James Smart962bc512013-01-03 15:44:00 -050013062 if (!phba->pcidev)
13063 return NULL;
James Smart962bc512013-01-03 15:44:00 -050013064
13065 switch (pci_barset) {
13066 case WQ_PCI_BAR_0_AND_1:
James Smart962bc512013-01-03 15:44:00 -050013067 return phba->pci_bar0_memmap_p;
13068 case WQ_PCI_BAR_2_AND_3:
James Smart962bc512013-01-03 15:44:00 -050013069 return phba->pci_bar2_memmap_p;
13070 case WQ_PCI_BAR_4_AND_5:
James Smart962bc512013-01-03 15:44:00 -050013071 return phba->pci_bar4_memmap_p;
13072 default:
13073 break;
13074 }
13075 return NULL;
13076}
13077
13078/**
James Smart173edbb2012-06-12 13:54:50 -040013079 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
13080 * @phba: HBA structure that indicates port to create a queue on.
13081 * @startq: The starting FCP EQ to modify
13082 *
13083 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13084 *
13085 * The @phba struct is used to send mailbox command to HBA. The @startq
13086 * is used to get the starting FCP EQ to change.
13087 * This function is asynchronous and will wait for the mailbox
13088 * command to finish before continuing.
13089 *
13090 * On success this function will return a zero. If unable to allocate enough
13091 * memory this function will return -ENOMEM. If the queue create mailbox command
13092 * fails this function will return -ENXIO.
13093 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013094int
James Smart2c9c5a02015-04-07 15:07:15 -040013095lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
James Smart173edbb2012-06-12 13:54:50 -040013096{
13097 struct lpfc_mbx_modify_eq_delay *eq_delay;
13098 LPFC_MBOXQ_t *mbox;
13099 struct lpfc_queue *eq;
13100 int cnt, rc, length, status = 0;
13101 uint32_t shdr_status, shdr_add_status;
James Smartee020062012-09-29 11:28:52 -040013102 uint32_t result;
James Smart173edbb2012-06-12 13:54:50 -040013103 int fcp_eqidx;
13104 union lpfc_sli4_cfg_shdr *shdr;
13105 uint16_t dmult;
13106
James Smart67d12732012-08-03 12:36:13 -040013107 if (startq >= phba->cfg_fcp_io_channel)
James Smart173edbb2012-06-12 13:54:50 -040013108 return 0;
13109
13110 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13111 if (!mbox)
13112 return -ENOMEM;
13113 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13114 sizeof(struct lpfc_sli4_cfg_mhdr));
13115 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13116 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13117 length, LPFC_SLI4_MBX_EMBED);
13118 eq_delay = &mbox->u.mqe.un.eq_delay;
13119
13120 /* Calculate delay multiper from maximum interrupt per second */
James Smartee020062012-09-29 11:28:52 -040013121 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
13122 if (result > LPFC_DMULT_CONST)
13123 dmult = 0;
13124 else
13125 dmult = LPFC_DMULT_CONST/result - 1;
James Smart173edbb2012-06-12 13:54:50 -040013126
13127 cnt = 0;
James Smart67d12732012-08-03 12:36:13 -040013128 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
James Smart173edbb2012-06-12 13:54:50 -040013129 fcp_eqidx++) {
James Smart67d12732012-08-03 12:36:13 -040013130 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
James Smart173edbb2012-06-12 13:54:50 -040013131 if (!eq)
13132 continue;
13133 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13134 eq_delay->u.request.eq[cnt].phase = 0;
13135 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13136 cnt++;
13137 if (cnt >= LPFC_MAX_EQ_DELAY)
13138 break;
13139 }
13140 eq_delay->u.request.num_eq = cnt;
13141
13142 mbox->vport = phba->pport;
13143 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13144 mbox->context1 = NULL;
13145 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13146 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13147 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13148 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13149 if (shdr_status || shdr_add_status || rc) {
13150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13151 "2512 MODIFY_EQ_DELAY mailbox failed with "
13152 "status x%x add_status x%x, mbx status x%x\n",
13153 shdr_status, shdr_add_status, rc);
13154 status = -ENXIO;
13155 }
13156 mempool_free(mbox, phba->mbox_mem_pool);
13157 return status;
13158}
13159
13160/**
James Smart4f774512009-05-22 14:52:35 -040013161 * lpfc_eq_create - Create an Event Queue on the HBA
13162 * @phba: HBA structure that indicates port to create a queue on.
13163 * @eq: The queue structure to use to create the event queue.
13164 * @imax: The maximum interrupt per second limit.
13165 *
13166 * This function creates an event queue, as detailed in @eq, on a port,
13167 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13168 *
13169 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13170 * is used to get the entry count and entry size that are necessary to
13171 * determine the number of pages to allocate and use for this queue. This
13172 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13173 * event queue. This function is asynchronous and will wait for the mailbox
13174 * command to finish before continuing.
13175 *
13176 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013177 * memory this function will return -ENOMEM. If the queue create mailbox command
13178 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013179 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013180int
James Smartee020062012-09-29 11:28:52 -040013181lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
James Smart4f774512009-05-22 14:52:35 -040013182{
13183 struct lpfc_mbx_eq_create *eq_create;
13184 LPFC_MBOXQ_t *mbox;
13185 int rc, length, status = 0;
13186 struct lpfc_dmabuf *dmabuf;
13187 uint32_t shdr_status, shdr_add_status;
13188 union lpfc_sli4_cfg_shdr *shdr;
13189 uint16_t dmult;
James Smart49198b32010-04-06 15:04:33 -040013190 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13191
James Smart2e90f4b2011-12-13 13:22:37 -050013192 /* sanity check on queue memory */
13193 if (!eq)
13194 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013195 if (!phba->sli4_hba.pc_sli4_params.supported)
13196 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013197
13198 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13199 if (!mbox)
13200 return -ENOMEM;
13201 length = (sizeof(struct lpfc_mbx_eq_create) -
13202 sizeof(struct lpfc_sli4_cfg_mhdr));
13203 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13204 LPFC_MBOX_OPCODE_EQ_CREATE,
13205 length, LPFC_SLI4_MBX_EMBED);
13206 eq_create = &mbox->u.mqe.un.eq_create;
13207 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13208 eq->page_count);
13209 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13210 LPFC_EQE_SIZE);
13211 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
James Smart2c9c5a02015-04-07 15:07:15 -040013212 /* don't setup delay multiplier using EQ_CREATE */
13213 dmult = 0;
James Smart4f774512009-05-22 14:52:35 -040013214 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13215 dmult);
13216 switch (eq->entry_count) {
13217 default:
13218 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13219 "0360 Unsupported EQ count. (%d)\n",
13220 eq->entry_count);
13221 if (eq->entry_count < 256)
13222 return -EINVAL;
13223 /* otherwise default to smallest count (drop through) */
13224 case 256:
13225 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13226 LPFC_EQ_CNT_256);
13227 break;
13228 case 512:
13229 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13230 LPFC_EQ_CNT_512);
13231 break;
13232 case 1024:
13233 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13234 LPFC_EQ_CNT_1024);
13235 break;
13236 case 2048:
13237 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13238 LPFC_EQ_CNT_2048);
13239 break;
13240 case 4096:
13241 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13242 LPFC_EQ_CNT_4096);
13243 break;
13244 }
13245 list_for_each_entry(dmabuf, &eq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013246 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013247 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13248 putPaddrLow(dmabuf->phys);
13249 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13250 putPaddrHigh(dmabuf->phys);
13251 }
13252 mbox->vport = phba->pport;
13253 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13254 mbox->context1 = NULL;
13255 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13256 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13257 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13258 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13259 if (shdr_status || shdr_add_status || rc) {
13260 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13261 "2500 EQ_CREATE mailbox failed with "
13262 "status x%x add_status x%x, mbx status x%x\n",
13263 shdr_status, shdr_add_status, rc);
13264 status = -ENXIO;
13265 }
13266 eq->type = LPFC_EQ;
13267 eq->subtype = LPFC_NONE;
13268 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13269 if (eq->queue_id == 0xFFFF)
13270 status = -ENXIO;
13271 eq->host_index = 0;
13272 eq->hba_index = 0;
13273
James Smart8fa38512009-07-19 10:01:03 -040013274 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013275 return status;
13276}
13277
13278/**
13279 * lpfc_cq_create - Create a Completion Queue on the HBA
13280 * @phba: HBA structure that indicates port to create a queue on.
13281 * @cq: The queue structure to use to create the completion queue.
13282 * @eq: The event queue to bind this completion queue to.
13283 *
13284 * This function creates a completion queue, as detailed in @wq, on a port,
13285 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13286 *
13287 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13288 * is used to get the entry count and entry size that are necessary to
13289 * determine the number of pages to allocate and use for this queue. The @eq
13290 * is used to indicate which event queue to bind this completion queue to. This
13291 * function will send the CQ_CREATE mailbox command to the HBA to setup the
13292 * completion queue. This function is asynchronous and will wait for the mailbox
13293 * command to finish before continuing.
13294 *
13295 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013296 * memory this function will return -ENOMEM. If the queue create mailbox command
13297 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013298 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013299int
James Smart4f774512009-05-22 14:52:35 -040013300lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13301 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13302{
13303 struct lpfc_mbx_cq_create *cq_create;
13304 struct lpfc_dmabuf *dmabuf;
13305 LPFC_MBOXQ_t *mbox;
13306 int rc, length, status = 0;
13307 uint32_t shdr_status, shdr_add_status;
13308 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013309 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13310
James Smart2e90f4b2011-12-13 13:22:37 -050013311 /* sanity check on queue memory */
13312 if (!cq || !eq)
13313 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013314 if (!phba->sli4_hba.pc_sli4_params.supported)
13315 hw_page_size = SLI4_PAGE_SIZE;
13316
James Smart4f774512009-05-22 14:52:35 -040013317 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13318 if (!mbox)
13319 return -ENOMEM;
13320 length = (sizeof(struct lpfc_mbx_cq_create) -
13321 sizeof(struct lpfc_sli4_cfg_mhdr));
13322 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13323 LPFC_MBOX_OPCODE_CQ_CREATE,
13324 length, LPFC_SLI4_MBX_EMBED);
13325 cq_create = &mbox->u.mqe.un.cq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013326 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013327 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13328 cq->page_count);
13329 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13330 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013331 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13332 phba->sli4_hba.pc_sli4_params.cqv);
13333 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
James Smartc31098c2011-04-16 11:03:33 -040013334 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13335 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013336 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13337 eq->queue_id);
13338 } else {
13339 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13340 eq->queue_id);
13341 }
James Smart4f774512009-05-22 14:52:35 -040013342 switch (cq->entry_count) {
13343 default:
13344 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13345 "0361 Unsupported CQ count. (%d)\n",
13346 cq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013347 if (cq->entry_count < 256) {
13348 status = -EINVAL;
13349 goto out;
13350 }
James Smart4f774512009-05-22 14:52:35 -040013351 /* otherwise default to smallest count (drop through) */
13352 case 256:
13353 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13354 LPFC_CQ_CNT_256);
13355 break;
13356 case 512:
13357 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13358 LPFC_CQ_CNT_512);
13359 break;
13360 case 1024:
13361 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13362 LPFC_CQ_CNT_1024);
13363 break;
13364 }
13365 list_for_each_entry(dmabuf, &cq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013366 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013367 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13368 putPaddrLow(dmabuf->phys);
13369 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13370 putPaddrHigh(dmabuf->phys);
13371 }
13372 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13373
13374 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013375 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13376 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13377 if (shdr_status || shdr_add_status || rc) {
13378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13379 "2501 CQ_CREATE mailbox failed with "
13380 "status x%x add_status x%x, mbx status x%x\n",
13381 shdr_status, shdr_add_status, rc);
13382 status = -ENXIO;
13383 goto out;
13384 }
13385 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13386 if (cq->queue_id == 0xFFFF) {
13387 status = -ENXIO;
13388 goto out;
13389 }
13390 /* link the cq onto the parent eq child list */
13391 list_add_tail(&cq->list, &eq->child_list);
13392 /* Set up completion queue's type and subtype */
13393 cq->type = type;
13394 cq->subtype = subtype;
13395 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
James Smart2a622bf2011-02-16 12:40:06 -050013396 cq->assoc_qid = eq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013397 cq->host_index = 0;
13398 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -040013399
James Smart8fa38512009-07-19 10:01:03 -040013400out:
13401 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013402 return status;
13403}
13404
13405/**
James Smartb19a0612010-04-06 14:48:51 -040013406 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
James Smart04c68492009-05-22 14:52:52 -040013407 * @phba: HBA structure that indicates port to create a queue on.
13408 * @mq: The queue structure to use to create the mailbox queue.
James Smartb19a0612010-04-06 14:48:51 -040013409 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13410 * @cq: The completion queue to associate with this cq.
James Smart04c68492009-05-22 14:52:52 -040013411 *
James Smartb19a0612010-04-06 14:48:51 -040013412 * This function provides failback (fb) functionality when the
13413 * mq_create_ext fails on older FW generations. It's purpose is identical
13414 * to mq_create_ext otherwise.
James Smart04c68492009-05-22 14:52:52 -040013415 *
James Smartb19a0612010-04-06 14:48:51 -040013416 * This routine cannot fail as all attributes were previously accessed and
13417 * initialized in mq_create_ext.
James Smart04c68492009-05-22 14:52:52 -040013418 **/
James Smartb19a0612010-04-06 14:48:51 -040013419static void
13420lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13421 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
James Smart04c68492009-05-22 14:52:52 -040013422{
13423 struct lpfc_mbx_mq_create *mq_create;
13424 struct lpfc_dmabuf *dmabuf;
James Smartb19a0612010-04-06 14:48:51 -040013425 int length;
James Smart04c68492009-05-22 14:52:52 -040013426
James Smart04c68492009-05-22 14:52:52 -040013427 length = (sizeof(struct lpfc_mbx_mq_create) -
13428 sizeof(struct lpfc_sli4_cfg_mhdr));
13429 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13430 LPFC_MBOX_OPCODE_MQ_CREATE,
13431 length, LPFC_SLI4_MBX_EMBED);
13432 mq_create = &mbox->u.mqe.un.mq_create;
13433 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
James Smartb19a0612010-04-06 14:48:51 -040013434 mq->page_count);
James Smart04c68492009-05-22 14:52:52 -040013435 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
James Smartb19a0612010-04-06 14:48:51 -040013436 cq->queue_id);
James Smart04c68492009-05-22 14:52:52 -040013437 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13438 switch (mq->entry_count) {
James Smart04c68492009-05-22 14:52:52 -040013439 case 16:
James Smart5a6f1332011-03-11 16:05:35 -050013440 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13441 LPFC_MQ_RING_SIZE_16);
James Smart04c68492009-05-22 14:52:52 -040013442 break;
13443 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013444 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13445 LPFC_MQ_RING_SIZE_32);
James Smart04c68492009-05-22 14:52:52 -040013446 break;
13447 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013448 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13449 LPFC_MQ_RING_SIZE_64);
James Smart04c68492009-05-22 14:52:52 -040013450 break;
13451 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013452 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13453 LPFC_MQ_RING_SIZE_128);
James Smart04c68492009-05-22 14:52:52 -040013454 break;
13455 }
13456 list_for_each_entry(dmabuf, &mq->page_list, list) {
13457 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
James Smartb19a0612010-04-06 14:48:51 -040013458 putPaddrLow(dmabuf->phys);
James Smart04c68492009-05-22 14:52:52 -040013459 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smartb19a0612010-04-06 14:48:51 -040013460 putPaddrHigh(dmabuf->phys);
13461 }
13462}
13463
13464/**
13465 * lpfc_mq_create - Create a mailbox Queue on the HBA
13466 * @phba: HBA structure that indicates port to create a queue on.
13467 * @mq: The queue structure to use to create the mailbox queue.
13468 * @cq: The completion queue to associate with this cq.
13469 * @subtype: The queue's subtype.
13470 *
13471 * This function creates a mailbox queue, as detailed in @mq, on a port,
13472 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13473 *
13474 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13475 * is used to get the entry count and entry size that are necessary to
13476 * determine the number of pages to allocate and use for this queue. This
13477 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13478 * mailbox queue. This function is asynchronous and will wait for the mailbox
13479 * command to finish before continuing.
13480 *
13481 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013482 * memory this function will return -ENOMEM. If the queue create mailbox command
13483 * fails this function will return -ENXIO.
James Smartb19a0612010-04-06 14:48:51 -040013484 **/
13485int32_t
13486lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13487 struct lpfc_queue *cq, uint32_t subtype)
13488{
13489 struct lpfc_mbx_mq_create *mq_create;
13490 struct lpfc_mbx_mq_create_ext *mq_create_ext;
13491 struct lpfc_dmabuf *dmabuf;
13492 LPFC_MBOXQ_t *mbox;
13493 int rc, length, status = 0;
13494 uint32_t shdr_status, shdr_add_status;
13495 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013496 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smartb19a0612010-04-06 14:48:51 -040013497
James Smart2e90f4b2011-12-13 13:22:37 -050013498 /* sanity check on queue memory */
13499 if (!mq || !cq)
13500 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013501 if (!phba->sli4_hba.pc_sli4_params.supported)
13502 hw_page_size = SLI4_PAGE_SIZE;
James Smartb19a0612010-04-06 14:48:51 -040013503
13504 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13505 if (!mbox)
13506 return -ENOMEM;
13507 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13508 sizeof(struct lpfc_sli4_cfg_mhdr));
13509 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13510 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13511 length, LPFC_SLI4_MBX_EMBED);
13512
13513 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
James Smart5a6f1332011-03-11 16:05:35 -050013514 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
James Smart70f3c072010-12-15 17:57:33 -050013515 bf_set(lpfc_mbx_mq_create_ext_num_pages,
13516 &mq_create_ext->u.request, mq->page_count);
13517 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13518 &mq_create_ext->u.request, 1);
13519 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
James Smartb19a0612010-04-06 14:48:51 -040013520 &mq_create_ext->u.request, 1);
13521 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13522 &mq_create_ext->u.request, 1);
James Smart70f3c072010-12-15 17:57:33 -050013523 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13524 &mq_create_ext->u.request, 1);
13525 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13526 &mq_create_ext->u.request, 1);
James Smartb19a0612010-04-06 14:48:51 -040013527 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
James Smart5a6f1332011-03-11 16:05:35 -050013528 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13529 phba->sli4_hba.pc_sli4_params.mqv);
13530 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13531 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13532 cq->queue_id);
13533 else
13534 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
13535 cq->queue_id);
James Smartb19a0612010-04-06 14:48:51 -040013536 switch (mq->entry_count) {
13537 default:
13538 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13539 "0362 Unsupported MQ count. (%d)\n",
13540 mq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013541 if (mq->entry_count < 16) {
13542 status = -EINVAL;
13543 goto out;
13544 }
James Smartb19a0612010-04-06 14:48:51 -040013545 /* otherwise default to smallest count (drop through) */
13546 case 16:
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_16);
James Smartb19a0612010-04-06 14:48:51 -040013550 break;
13551 case 32:
James Smart5a6f1332011-03-11 16:05:35 -050013552 bf_set(lpfc_mq_context_ring_size,
13553 &mq_create_ext->u.request.context,
13554 LPFC_MQ_RING_SIZE_32);
James Smartb19a0612010-04-06 14:48:51 -040013555 break;
13556 case 64:
James Smart5a6f1332011-03-11 16:05:35 -050013557 bf_set(lpfc_mq_context_ring_size,
13558 &mq_create_ext->u.request.context,
13559 LPFC_MQ_RING_SIZE_64);
James Smartb19a0612010-04-06 14:48:51 -040013560 break;
13561 case 128:
James Smart5a6f1332011-03-11 16:05:35 -050013562 bf_set(lpfc_mq_context_ring_size,
13563 &mq_create_ext->u.request.context,
13564 LPFC_MQ_RING_SIZE_128);
James Smartb19a0612010-04-06 14:48:51 -040013565 break;
13566 }
13567 list_for_each_entry(dmabuf, &mq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013568 memset(dmabuf->virt, 0, hw_page_size);
James Smartb19a0612010-04-06 14:48:51 -040013569 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
13570 putPaddrLow(dmabuf->phys);
13571 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
James Smart04c68492009-05-22 14:52:52 -040013572 putPaddrHigh(dmabuf->phys);
13573 }
13574 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smartb19a0612010-04-06 14:48:51 -040013575 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13576 &mq_create_ext->u.response);
13577 if (rc != MBX_SUCCESS) {
13578 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13579 "2795 MQ_CREATE_EXT failed with "
13580 "status x%x. Failback to MQ_CREATE.\n",
13581 rc);
13582 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
13583 mq_create = &mbox->u.mqe.un.mq_create;
13584 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13585 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
13586 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13587 &mq_create->u.response);
13588 }
13589
James Smart04c68492009-05-22 14:52:52 -040013590 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart04c68492009-05-22 14:52:52 -040013591 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13592 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13593 if (shdr_status || shdr_add_status || rc) {
13594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13595 "2502 MQ_CREATE mailbox failed with "
13596 "status x%x add_status x%x, mbx status x%x\n",
13597 shdr_status, shdr_add_status, rc);
13598 status = -ENXIO;
13599 goto out;
13600 }
James Smart04c68492009-05-22 14:52:52 -040013601 if (mq->queue_id == 0xFFFF) {
13602 status = -ENXIO;
13603 goto out;
13604 }
13605 mq->type = LPFC_MQ;
James Smart2a622bf2011-02-16 12:40:06 -050013606 mq->assoc_qid = cq->queue_id;
James Smart04c68492009-05-22 14:52:52 -040013607 mq->subtype = subtype;
13608 mq->host_index = 0;
13609 mq->hba_index = 0;
13610
13611 /* link the mq onto the parent cq child list */
13612 list_add_tail(&mq->list, &cq->child_list);
13613out:
James Smart8fa38512009-07-19 10:01:03 -040013614 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040013615 return status;
13616}
13617
13618/**
James Smart4f774512009-05-22 14:52:35 -040013619 * lpfc_wq_create - Create a Work Queue on the HBA
13620 * @phba: HBA structure that indicates port to create a queue on.
13621 * @wq: The queue structure to use to create the work queue.
13622 * @cq: The completion queue to bind this work queue to.
13623 * @subtype: The subtype of the work queue indicating its functionality.
13624 *
13625 * This function creates a work queue, as detailed in @wq, on a port, described
13626 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
13627 *
13628 * The @phba struct is used to send mailbox command to HBA. The @wq struct
13629 * is used to get the entry count and entry size that are necessary to
13630 * determine the number of pages to allocate and use for this queue. The @cq
13631 * is used to indicate which completion queue to bind this work queue to. This
13632 * function will send the WQ_CREATE mailbox command to the HBA to setup the
13633 * work queue. This function is asynchronous and will wait for the mailbox
13634 * command to finish before continuing.
13635 *
13636 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013637 * memory this function will return -ENOMEM. If the queue create mailbox command
13638 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013639 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013640int
James Smart4f774512009-05-22 14:52:35 -040013641lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
13642 struct lpfc_queue *cq, uint32_t subtype)
13643{
13644 struct lpfc_mbx_wq_create *wq_create;
13645 struct lpfc_dmabuf *dmabuf;
13646 LPFC_MBOXQ_t *mbox;
13647 int rc, length, status = 0;
13648 uint32_t shdr_status, shdr_add_status;
13649 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013650 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart5a6f1332011-03-11 16:05:35 -050013651 struct dma_address *page;
James Smart962bc512013-01-03 15:44:00 -050013652 void __iomem *bar_memmap_p;
13653 uint32_t db_offset;
13654 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013655
James Smart2e90f4b2011-12-13 13:22:37 -050013656 /* sanity check on queue memory */
13657 if (!wq || !cq)
13658 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013659 if (!phba->sli4_hba.pc_sli4_params.supported)
13660 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013661
13662 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13663 if (!mbox)
13664 return -ENOMEM;
13665 length = (sizeof(struct lpfc_mbx_wq_create) -
13666 sizeof(struct lpfc_sli4_cfg_mhdr));
13667 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13668 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13669 length, LPFC_SLI4_MBX_EMBED);
13670 wq_create = &mbox->u.mqe.un.wq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013671 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
James Smart4f774512009-05-22 14:52:35 -040013672 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13673 wq->page_count);
13674 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13675 cq->queue_id);
James Smart0c651872013-07-15 18:33:23 -040013676
13677 /* wqv is the earliest version supported, NOT the latest */
James Smart5a6f1332011-03-11 16:05:35 -050013678 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13679 phba->sli4_hba.pc_sli4_params.wqv);
James Smart962bc512013-01-03 15:44:00 -050013680
James Smart0c651872013-07-15 18:33:23 -040013681 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13682 case LPFC_Q_CREATE_VERSION_0:
13683 switch (wq->entry_size) {
13684 default:
13685 case 64:
13686 /* Nothing to do, version 0 ONLY supports 64 byte */
13687 page = wq_create->u.request.page;
13688 break;
13689 case 128:
13690 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13691 LPFC_WQ_SZ128_SUPPORT)) {
13692 status = -ERANGE;
13693 goto out;
13694 }
13695 /* If we get here the HBA MUST also support V1 and
13696 * we MUST use it
13697 */
13698 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13699 LPFC_Q_CREATE_VERSION_1);
13700
13701 bf_set(lpfc_mbx_wq_create_wqe_count,
13702 &wq_create->u.request_1, wq->entry_count);
13703 bf_set(lpfc_mbx_wq_create_wqe_size,
13704 &wq_create->u.request_1,
13705 LPFC_WQ_WQE_SIZE_128);
13706 bf_set(lpfc_mbx_wq_create_page_size,
13707 &wq_create->u.request_1,
13708 (PAGE_SIZE/SLI4_PAGE_SIZE));
13709 page = wq_create->u.request_1.page;
13710 break;
13711 }
13712 break;
13713 case LPFC_Q_CREATE_VERSION_1:
James Smart5a6f1332011-03-11 16:05:35 -050013714 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13715 wq->entry_count);
13716 switch (wq->entry_size) {
13717 default:
13718 case 64:
13719 bf_set(lpfc_mbx_wq_create_wqe_size,
13720 &wq_create->u.request_1,
13721 LPFC_WQ_WQE_SIZE_64);
13722 break;
13723 case 128:
James Smart0c651872013-07-15 18:33:23 -040013724 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13725 LPFC_WQ_SZ128_SUPPORT)) {
13726 status = -ERANGE;
13727 goto out;
13728 }
James Smart5a6f1332011-03-11 16:05:35 -050013729 bf_set(lpfc_mbx_wq_create_wqe_size,
13730 &wq_create->u.request_1,
13731 LPFC_WQ_WQE_SIZE_128);
13732 break;
13733 }
13734 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
13735 (PAGE_SIZE/SLI4_PAGE_SIZE));
13736 page = wq_create->u.request_1.page;
James Smart0c651872013-07-15 18:33:23 -040013737 break;
13738 default:
13739 status = -ERANGE;
13740 goto out;
James Smart5a6f1332011-03-11 16:05:35 -050013741 }
James Smart0c651872013-07-15 18:33:23 -040013742
James Smart4f774512009-05-22 14:52:35 -040013743 list_for_each_entry(dmabuf, &wq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013744 memset(dmabuf->virt, 0, hw_page_size);
James Smart5a6f1332011-03-11 16:05:35 -050013745 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13746 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
James Smart4f774512009-05-22 14:52:35 -040013747 }
James Smart962bc512013-01-03 15:44:00 -050013748
13749 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13750 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13751
James Smart4f774512009-05-22 14:52:35 -040013752 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13753 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013754 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13755 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13756 if (shdr_status || shdr_add_status || rc) {
13757 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13758 "2503 WQ_CREATE mailbox failed with "
13759 "status x%x add_status x%x, mbx status x%x\n",
13760 shdr_status, shdr_add_status, rc);
13761 status = -ENXIO;
13762 goto out;
13763 }
13764 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13765 if (wq->queue_id == 0xFFFF) {
13766 status = -ENXIO;
13767 goto out;
13768 }
James Smart962bc512013-01-03 15:44:00 -050013769 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13770 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13771 &wq_create->u.response);
13772 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13773 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13774 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13775 "3265 WQ[%d] doorbell format not "
13776 "supported: x%x\n", wq->queue_id,
13777 wq->db_format);
13778 status = -EINVAL;
13779 goto out;
13780 }
13781 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13782 &wq_create->u.response);
13783 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13784 if (!bar_memmap_p) {
13785 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13786 "3263 WQ[%d] failed to memmap pci "
13787 "barset:x%x\n", wq->queue_id,
13788 pci_barset);
13789 status = -ENOMEM;
13790 goto out;
13791 }
13792 db_offset = wq_create->u.response.doorbell_offset;
13793 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13794 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13795 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13796 "3252 WQ[%d] doorbell offset not "
13797 "supported: x%x\n", wq->queue_id,
13798 db_offset);
13799 status = -EINVAL;
13800 goto out;
13801 }
13802 wq->db_regaddr = bar_memmap_p + db_offset;
13803 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040013804 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13805 "format:x%x\n", wq->queue_id, pci_barset,
13806 db_offset, wq->db_format);
James Smart962bc512013-01-03 15:44:00 -050013807 } else {
13808 wq->db_format = LPFC_DB_LIST_FORMAT;
13809 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13810 }
James Smart4f774512009-05-22 14:52:35 -040013811 wq->type = LPFC_WQ;
James Smart2a622bf2011-02-16 12:40:06 -050013812 wq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040013813 wq->subtype = subtype;
13814 wq->host_index = 0;
13815 wq->hba_index = 0;
James Smartff78d8f2011-12-13 13:21:35 -050013816 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
James Smart4f774512009-05-22 14:52:35 -040013817
13818 /* link the wq onto the parent cq child list */
13819 list_add_tail(&wq->list, &cq->child_list);
13820out:
James Smart8fa38512009-07-19 10:01:03 -040013821 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040013822 return status;
13823}
13824
13825/**
James Smart73d91e52011-10-10 21:32:10 -040013826 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13827 * @phba: HBA structure that indicates port to create a queue on.
13828 * @rq: The queue structure to use for the receive queue.
13829 * @qno: The associated HBQ number
13830 *
13831 *
13832 * For SLI4 we need to adjust the RQ repost value based on
13833 * the number of buffers that are initially posted to the RQ.
13834 */
13835void
13836lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13837{
13838 uint32_t cnt;
13839
James Smart2e90f4b2011-12-13 13:22:37 -050013840 /* sanity check on queue memory */
13841 if (!rq)
13842 return;
James Smart73d91e52011-10-10 21:32:10 -040013843 cnt = lpfc_hbq_defs[qno]->entry_count;
13844
13845 /* Recalc repost for RQs based on buffers initially posted */
13846 cnt = (cnt >> 3);
13847 if (cnt < LPFC_QUEUE_MIN_REPOST)
13848 cnt = LPFC_QUEUE_MIN_REPOST;
13849
13850 rq->entry_repost = cnt;
13851}
13852
13853/**
James Smart4f774512009-05-22 14:52:35 -040013854 * lpfc_rq_create - Create a Receive Queue on the HBA
13855 * @phba: HBA structure that indicates port to create a queue on.
13856 * @hrq: The queue structure to use to create the header receive queue.
13857 * @drq: The queue structure to use to create the data receive queue.
13858 * @cq: The completion queue to bind this work queue to.
13859 *
13860 * This function creates a receive buffer queue pair , as detailed in @hrq and
13861 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13862 * to the HBA.
13863 *
13864 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13865 * struct is used to get the entry count that is necessary to determine the
13866 * number of pages to use for this queue. The @cq is used to indicate which
13867 * completion queue to bind received buffers that are posted to these queues to.
13868 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13869 * receive queue pair. This function is asynchronous and will wait for the
13870 * mailbox command to finish before continuing.
13871 *
13872 * On success this function will return a zero. If unable to allocate enough
James Smartd439d282010-09-29 11:18:45 -040013873 * memory this function will return -ENOMEM. If the queue create mailbox command
13874 * fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040013875 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040013876int
James Smart4f774512009-05-22 14:52:35 -040013877lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13878 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13879{
13880 struct lpfc_mbx_rq_create *rq_create;
13881 struct lpfc_dmabuf *dmabuf;
13882 LPFC_MBOXQ_t *mbox;
13883 int rc, length, status = 0;
13884 uint32_t shdr_status, shdr_add_status;
13885 union lpfc_sli4_cfg_shdr *shdr;
James Smart49198b32010-04-06 15:04:33 -040013886 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
James Smart962bc512013-01-03 15:44:00 -050013887 void __iomem *bar_memmap_p;
13888 uint32_t db_offset;
13889 uint16_t pci_barset;
James Smart49198b32010-04-06 15:04:33 -040013890
James Smart2e90f4b2011-12-13 13:22:37 -050013891 /* sanity check on queue memory */
13892 if (!hrq || !drq || !cq)
13893 return -ENODEV;
James Smart49198b32010-04-06 15:04:33 -040013894 if (!phba->sli4_hba.pc_sli4_params.supported)
13895 hw_page_size = SLI4_PAGE_SIZE;
James Smart4f774512009-05-22 14:52:35 -040013896
13897 if (hrq->entry_count != drq->entry_count)
13898 return -EINVAL;
13899 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13900 if (!mbox)
13901 return -ENOMEM;
13902 length = (sizeof(struct lpfc_mbx_rq_create) -
13903 sizeof(struct lpfc_sli4_cfg_mhdr));
13904 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13905 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13906 length, LPFC_SLI4_MBX_EMBED);
13907 rq_create = &mbox->u.mqe.un.rq_create;
James Smart5a6f1332011-03-11 16:05:35 -050013908 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13909 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13910 phba->sli4_hba.pc_sli4_params.rqv);
13911 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13912 bf_set(lpfc_rq_context_rqe_count_1,
13913 &rq_create->u.request.context,
13914 hrq->entry_count);
13915 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040013916 bf_set(lpfc_rq_context_rqe_size,
13917 &rq_create->u.request.context,
13918 LPFC_RQE_SIZE_8);
13919 bf_set(lpfc_rq_context_page_size,
13920 &rq_create->u.request.context,
13921 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050013922 } else {
13923 switch (hrq->entry_count) {
13924 default:
13925 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13926 "2535 Unsupported RQ count. (%d)\n",
13927 hrq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040013928 if (hrq->entry_count < 512) {
13929 status = -EINVAL;
13930 goto out;
13931 }
James Smart5a6f1332011-03-11 16:05:35 -050013932 /* otherwise default to smallest count (drop through) */
13933 case 512:
13934 bf_set(lpfc_rq_context_rqe_count,
13935 &rq_create->u.request.context,
13936 LPFC_RQ_RING_SIZE_512);
13937 break;
13938 case 1024:
13939 bf_set(lpfc_rq_context_rqe_count,
13940 &rq_create->u.request.context,
13941 LPFC_RQ_RING_SIZE_1024);
13942 break;
13943 case 2048:
13944 bf_set(lpfc_rq_context_rqe_count,
13945 &rq_create->u.request.context,
13946 LPFC_RQ_RING_SIZE_2048);
13947 break;
13948 case 4096:
13949 bf_set(lpfc_rq_context_rqe_count,
13950 &rq_create->u.request.context,
13951 LPFC_RQ_RING_SIZE_4096);
13952 break;
13953 }
13954 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13955 LPFC_HDR_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040013956 }
13957 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13958 cq->queue_id);
13959 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13960 hrq->page_count);
James Smart4f774512009-05-22 14:52:35 -040013961 list_for_each_entry(dmabuf, &hrq->page_list, list) {
James Smart49198b32010-04-06 15:04:33 -040013962 memset(dmabuf->virt, 0, hw_page_size);
James Smart4f774512009-05-22 14:52:35 -040013963 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13964 putPaddrLow(dmabuf->phys);
13965 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13966 putPaddrHigh(dmabuf->phys);
13967 }
James Smart962bc512013-01-03 15:44:00 -050013968 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13969 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13970
James Smart4f774512009-05-22 14:52:35 -040013971 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13972 /* The IOCTL status is embedded in the mailbox subheader. */
James Smart4f774512009-05-22 14:52:35 -040013973 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13974 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13975 if (shdr_status || shdr_add_status || rc) {
13976 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13977 "2504 RQ_CREATE mailbox failed with "
13978 "status x%x add_status x%x, mbx status x%x\n",
13979 shdr_status, shdr_add_status, rc);
13980 status = -ENXIO;
13981 goto out;
13982 }
13983 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13984 if (hrq->queue_id == 0xFFFF) {
13985 status = -ENXIO;
13986 goto out;
13987 }
James Smart962bc512013-01-03 15:44:00 -050013988
13989 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13990 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13991 &rq_create->u.response);
13992 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13993 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13995 "3262 RQ [%d] doorbell format not "
13996 "supported: x%x\n", hrq->queue_id,
13997 hrq->db_format);
13998 status = -EINVAL;
13999 goto out;
14000 }
14001
14002 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
14003 &rq_create->u.response);
14004 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
14005 if (!bar_memmap_p) {
14006 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14007 "3269 RQ[%d] failed to memmap pci "
14008 "barset:x%x\n", hrq->queue_id,
14009 pci_barset);
14010 status = -ENOMEM;
14011 goto out;
14012 }
14013
14014 db_offset = rq_create->u.response.doorbell_offset;
14015 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
14016 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
14017 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14018 "3270 RQ[%d] doorbell offset not "
14019 "supported: x%x\n", hrq->queue_id,
14020 db_offset);
14021 status = -EINVAL;
14022 goto out;
14023 }
14024 hrq->db_regaddr = bar_memmap_p + db_offset;
14025 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarta22e7db2013-04-17 20:16:37 -040014026 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
14027 "format:x%x\n", hrq->queue_id, pci_barset,
14028 db_offset, hrq->db_format);
James Smart962bc512013-01-03 15:44:00 -050014029 } else {
14030 hrq->db_format = LPFC_DB_RING_FORMAT;
14031 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
14032 }
James Smart4f774512009-05-22 14:52:35 -040014033 hrq->type = LPFC_HRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014034 hrq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014035 hrq->subtype = subtype;
14036 hrq->host_index = 0;
14037 hrq->hba_index = 0;
14038
14039 /* now create the data queue */
14040 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14041 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14042 length, LPFC_SLI4_MBX_EMBED);
James Smart5a6f1332011-03-11 16:05:35 -050014043 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14044 phba->sli4_hba.pc_sli4_params.rqv);
14045 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14046 bf_set(lpfc_rq_context_rqe_count_1,
James Smartc31098c2011-04-16 11:03:33 -040014047 &rq_create->u.request.context, hrq->entry_count);
James Smart5a6f1332011-03-11 16:05:35 -050014048 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
James Smartc31098c2011-04-16 11:03:33 -040014049 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
14050 LPFC_RQE_SIZE_8);
14051 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
14052 (PAGE_SIZE/SLI4_PAGE_SIZE));
James Smart5a6f1332011-03-11 16:05:35 -050014053 } else {
14054 switch (drq->entry_count) {
14055 default:
14056 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14057 "2536 Unsupported RQ count. (%d)\n",
14058 drq->entry_count);
James Smart4f4c1862012-06-12 13:54:02 -040014059 if (drq->entry_count < 512) {
14060 status = -EINVAL;
14061 goto out;
14062 }
James Smart5a6f1332011-03-11 16:05:35 -050014063 /* otherwise default to smallest count (drop through) */
14064 case 512:
14065 bf_set(lpfc_rq_context_rqe_count,
14066 &rq_create->u.request.context,
14067 LPFC_RQ_RING_SIZE_512);
14068 break;
14069 case 1024:
14070 bf_set(lpfc_rq_context_rqe_count,
14071 &rq_create->u.request.context,
14072 LPFC_RQ_RING_SIZE_1024);
14073 break;
14074 case 2048:
14075 bf_set(lpfc_rq_context_rqe_count,
14076 &rq_create->u.request.context,
14077 LPFC_RQ_RING_SIZE_2048);
14078 break;
14079 case 4096:
14080 bf_set(lpfc_rq_context_rqe_count,
14081 &rq_create->u.request.context,
14082 LPFC_RQ_RING_SIZE_4096);
14083 break;
14084 }
14085 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14086 LPFC_DATA_BUF_SIZE);
James Smart4f774512009-05-22 14:52:35 -040014087 }
14088 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14089 cq->queue_id);
14090 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14091 drq->page_count);
James Smart4f774512009-05-22 14:52:35 -040014092 list_for_each_entry(dmabuf, &drq->page_list, list) {
14093 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14094 putPaddrLow(dmabuf->phys);
14095 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14096 putPaddrHigh(dmabuf->phys);
14097 }
James Smart962bc512013-01-03 15:44:00 -050014098 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14099 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
James Smart4f774512009-05-22 14:52:35 -040014100 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14101 /* The IOCTL status is embedded in the mailbox subheader. */
14102 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14103 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14104 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14105 if (shdr_status || shdr_add_status || rc) {
14106 status = -ENXIO;
14107 goto out;
14108 }
14109 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14110 if (drq->queue_id == 0xFFFF) {
14111 status = -ENXIO;
14112 goto out;
14113 }
14114 drq->type = LPFC_DRQ;
James Smart2a622bf2011-02-16 12:40:06 -050014115 drq->assoc_qid = cq->queue_id;
James Smart4f774512009-05-22 14:52:35 -040014116 drq->subtype = subtype;
14117 drq->host_index = 0;
14118 drq->hba_index = 0;
14119
14120 /* link the header and data RQs onto the parent cq child list */
14121 list_add_tail(&hrq->list, &cq->child_list);
14122 list_add_tail(&drq->list, &cq->child_list);
14123
14124out:
James Smart8fa38512009-07-19 10:01:03 -040014125 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014126 return status;
14127}
14128
14129/**
14130 * lpfc_eq_destroy - Destroy an event Queue on the HBA
14131 * @eq: The queue structure associated with the queue to destroy.
14132 *
14133 * This function destroys a queue, as detailed in @eq by sending an mailbox
14134 * command, specific to the type of queue, to the HBA.
14135 *
14136 * The @eq struct is used to get the queue ID of the queue to destroy.
14137 *
14138 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014139 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014140 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014141int
James Smart4f774512009-05-22 14:52:35 -040014142lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
14143{
14144 LPFC_MBOXQ_t *mbox;
14145 int rc, length, status = 0;
14146 uint32_t shdr_status, shdr_add_status;
14147 union lpfc_sli4_cfg_shdr *shdr;
14148
James Smart2e90f4b2011-12-13 13:22:37 -050014149 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014150 if (!eq)
14151 return -ENODEV;
14152 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
14153 if (!mbox)
14154 return -ENOMEM;
14155 length = (sizeof(struct lpfc_mbx_eq_destroy) -
14156 sizeof(struct lpfc_sli4_cfg_mhdr));
14157 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14158 LPFC_MBOX_OPCODE_EQ_DESTROY,
14159 length, LPFC_SLI4_MBX_EMBED);
14160 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
14161 eq->queue_id);
14162 mbox->vport = eq->phba->pport;
14163 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14164
14165 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
14166 /* The IOCTL status is embedded in the mailbox subheader. */
14167 shdr = (union lpfc_sli4_cfg_shdr *)
14168 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
14169 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14170 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14171 if (shdr_status || shdr_add_status || rc) {
14172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14173 "2505 EQ_DESTROY mailbox failed with "
14174 "status x%x add_status x%x, mbx status x%x\n",
14175 shdr_status, shdr_add_status, rc);
14176 status = -ENXIO;
14177 }
14178
14179 /* Remove eq from any list */
14180 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040014181 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014182 return status;
14183}
14184
14185/**
14186 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
14187 * @cq: The queue structure associated with the queue to destroy.
14188 *
14189 * This function destroys a queue, as detailed in @cq by sending an mailbox
14190 * command, specific to the type of queue, to the HBA.
14191 *
14192 * The @cq struct is used to get the queue ID of the queue to destroy.
14193 *
14194 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014195 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014196 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014197int
James Smart4f774512009-05-22 14:52:35 -040014198lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14199{
14200 LPFC_MBOXQ_t *mbox;
14201 int rc, length, status = 0;
14202 uint32_t shdr_status, shdr_add_status;
14203 union lpfc_sli4_cfg_shdr *shdr;
14204
James Smart2e90f4b2011-12-13 13:22:37 -050014205 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014206 if (!cq)
14207 return -ENODEV;
14208 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14209 if (!mbox)
14210 return -ENOMEM;
14211 length = (sizeof(struct lpfc_mbx_cq_destroy) -
14212 sizeof(struct lpfc_sli4_cfg_mhdr));
14213 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14214 LPFC_MBOX_OPCODE_CQ_DESTROY,
14215 length, LPFC_SLI4_MBX_EMBED);
14216 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14217 cq->queue_id);
14218 mbox->vport = cq->phba->pport;
14219 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14220 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14221 /* The IOCTL status is embedded in the mailbox subheader. */
14222 shdr = (union lpfc_sli4_cfg_shdr *)
14223 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14224 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14225 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14226 if (shdr_status || shdr_add_status || rc) {
14227 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14228 "2506 CQ_DESTROY mailbox failed with "
14229 "status x%x add_status x%x, mbx status x%x\n",
14230 shdr_status, shdr_add_status, rc);
14231 status = -ENXIO;
14232 }
14233 /* Remove cq from any list */
14234 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040014235 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014236 return status;
14237}
14238
14239/**
James Smart04c68492009-05-22 14:52:52 -040014240 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14241 * @qm: The queue structure associated with the queue to destroy.
14242 *
14243 * This function destroys a queue, as detailed in @mq by sending an mailbox
14244 * command, specific to the type of queue, to the HBA.
14245 *
14246 * The @mq struct is used to get the queue ID of the queue to destroy.
14247 *
14248 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014249 * command fails this function will return -ENXIO.
James Smart04c68492009-05-22 14:52:52 -040014250 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014251int
James Smart04c68492009-05-22 14:52:52 -040014252lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14253{
14254 LPFC_MBOXQ_t *mbox;
14255 int rc, length, status = 0;
14256 uint32_t shdr_status, shdr_add_status;
14257 union lpfc_sli4_cfg_shdr *shdr;
14258
James Smart2e90f4b2011-12-13 13:22:37 -050014259 /* sanity check on queue memory */
James Smart04c68492009-05-22 14:52:52 -040014260 if (!mq)
14261 return -ENODEV;
14262 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14263 if (!mbox)
14264 return -ENOMEM;
14265 length = (sizeof(struct lpfc_mbx_mq_destroy) -
14266 sizeof(struct lpfc_sli4_cfg_mhdr));
14267 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14268 LPFC_MBOX_OPCODE_MQ_DESTROY,
14269 length, LPFC_SLI4_MBX_EMBED);
14270 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14271 mq->queue_id);
14272 mbox->vport = mq->phba->pport;
14273 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14274 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14275 /* The IOCTL status is embedded in the mailbox subheader. */
14276 shdr = (union lpfc_sli4_cfg_shdr *)
14277 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14278 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14279 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14280 if (shdr_status || shdr_add_status || rc) {
14281 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14282 "2507 MQ_DESTROY mailbox failed with "
14283 "status x%x add_status x%x, mbx status x%x\n",
14284 shdr_status, shdr_add_status, rc);
14285 status = -ENXIO;
14286 }
14287 /* Remove mq from any list */
14288 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040014289 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040014290 return status;
14291}
14292
14293/**
James Smart4f774512009-05-22 14:52:35 -040014294 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14295 * @wq: The queue structure associated with the queue to destroy.
14296 *
14297 * This function destroys a queue, as detailed in @wq by sending an mailbox
14298 * command, specific to the type of queue, to the HBA.
14299 *
14300 * The @wq struct is used to get the queue ID of the queue to destroy.
14301 *
14302 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014303 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014304 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014305int
James Smart4f774512009-05-22 14:52:35 -040014306lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14307{
14308 LPFC_MBOXQ_t *mbox;
14309 int rc, length, status = 0;
14310 uint32_t shdr_status, shdr_add_status;
14311 union lpfc_sli4_cfg_shdr *shdr;
14312
James Smart2e90f4b2011-12-13 13:22:37 -050014313 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014314 if (!wq)
14315 return -ENODEV;
14316 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14317 if (!mbox)
14318 return -ENOMEM;
14319 length = (sizeof(struct lpfc_mbx_wq_destroy) -
14320 sizeof(struct lpfc_sli4_cfg_mhdr));
14321 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14322 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14323 length, LPFC_SLI4_MBX_EMBED);
14324 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14325 wq->queue_id);
14326 mbox->vport = wq->phba->pport;
14327 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14328 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14329 shdr = (union lpfc_sli4_cfg_shdr *)
14330 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14331 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14332 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14333 if (shdr_status || shdr_add_status || rc) {
14334 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14335 "2508 WQ_DESTROY mailbox failed with "
14336 "status x%x add_status x%x, mbx status x%x\n",
14337 shdr_status, shdr_add_status, rc);
14338 status = -ENXIO;
14339 }
14340 /* Remove wq from any list */
14341 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040014342 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014343 return status;
14344}
14345
14346/**
14347 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14348 * @rq: The queue structure associated with the queue to destroy.
14349 *
14350 * This function destroys a queue, as detailed in @rq by sending an mailbox
14351 * command, specific to the type of queue, to the HBA.
14352 *
14353 * The @rq struct is used to get the queue ID of the queue to destroy.
14354 *
14355 * On success this function will return a zero. If the queue destroy mailbox
James Smartd439d282010-09-29 11:18:45 -040014356 * command fails this function will return -ENXIO.
James Smart4f774512009-05-22 14:52:35 -040014357 **/
James Smarta2fc4aef2014-09-03 12:57:55 -040014358int
James Smart4f774512009-05-22 14:52:35 -040014359lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14360 struct lpfc_queue *drq)
14361{
14362 LPFC_MBOXQ_t *mbox;
14363 int rc, length, status = 0;
14364 uint32_t shdr_status, shdr_add_status;
14365 union lpfc_sli4_cfg_shdr *shdr;
14366
James Smart2e90f4b2011-12-13 13:22:37 -050014367 /* sanity check on queue memory */
James Smart4f774512009-05-22 14:52:35 -040014368 if (!hrq || !drq)
14369 return -ENODEV;
14370 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14371 if (!mbox)
14372 return -ENOMEM;
14373 length = (sizeof(struct lpfc_mbx_rq_destroy) -
James Smartfedd3b72011-02-16 12:39:24 -050014374 sizeof(struct lpfc_sli4_cfg_mhdr));
James Smart4f774512009-05-22 14:52:35 -040014375 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14376 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14377 length, LPFC_SLI4_MBX_EMBED);
14378 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14379 hrq->queue_id);
14380 mbox->vport = hrq->phba->pport;
14381 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14382 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14383 /* The IOCTL status is embedded in the mailbox subheader. */
14384 shdr = (union lpfc_sli4_cfg_shdr *)
14385 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14386 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14387 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14388 if (shdr_status || shdr_add_status || rc) {
14389 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14390 "2509 RQ_DESTROY mailbox failed with "
14391 "status x%x add_status x%x, mbx status x%x\n",
14392 shdr_status, shdr_add_status, rc);
14393 if (rc != MBX_TIMEOUT)
14394 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14395 return -ENXIO;
14396 }
14397 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14398 drq->queue_id);
14399 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14400 shdr = (union lpfc_sli4_cfg_shdr *)
14401 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14402 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14403 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14404 if (shdr_status || shdr_add_status || rc) {
14405 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14406 "2510 RQ_DESTROY mailbox failed with "
14407 "status x%x add_status x%x, mbx status x%x\n",
14408 shdr_status, shdr_add_status, rc);
14409 status = -ENXIO;
14410 }
14411 list_del_init(&hrq->list);
14412 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040014413 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040014414 return status;
14415}
14416
14417/**
14418 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14419 * @phba: The virtual port for which this call being executed.
14420 * @pdma_phys_addr0: Physical address of the 1st SGL page.
14421 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14422 * @xritag: the xritag that ties this io to the SGL pages.
14423 *
14424 * This routine will post the sgl pages for the IO that has the xritag
14425 * that is in the iocbq structure. The xritag is assigned during iocbq
14426 * creation and persists for as long as the driver is loaded.
14427 * if the caller has fewer than 256 scatter gather segments to map then
14428 * pdma_phys_addr1 should be 0.
14429 * If the caller needs to map more than 256 scatter gather segment then
14430 * pdma_phys_addr1 should be a valid physical address.
14431 * physical address for SGLs must be 64 byte aligned.
14432 * If you are going to map 2 SGL's then the first one must have 256 entries
14433 * the second sgl can have between 1 and 256 entries.
14434 *
14435 * Return codes:
14436 * 0 - Success
14437 * -ENXIO, -ENOMEM - Failure
14438 **/
14439int
14440lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14441 dma_addr_t pdma_phys_addr0,
14442 dma_addr_t pdma_phys_addr1,
14443 uint16_t xritag)
14444{
14445 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14446 LPFC_MBOXQ_t *mbox;
14447 int rc;
14448 uint32_t shdr_status, shdr_add_status;
James Smart6d368e52011-05-24 11:44:12 -040014449 uint32_t mbox_tmo;
James Smart4f774512009-05-22 14:52:35 -040014450 union lpfc_sli4_cfg_shdr *shdr;
14451
14452 if (xritag == NO_XRI) {
14453 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14454 "0364 Invalid param:\n");
14455 return -EINVAL;
14456 }
14457
14458 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14459 if (!mbox)
14460 return -ENOMEM;
14461
14462 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14463 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14464 sizeof(struct lpfc_mbx_post_sgl_pages) -
James Smartfedd3b72011-02-16 12:39:24 -050014465 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
James Smart4f774512009-05-22 14:52:35 -040014466
14467 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14468 &mbox->u.mqe.un.post_sgl_pages;
14469 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14470 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14471
14472 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14473 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14474 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14475 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14476
14477 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14478 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14479 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14480 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14481 if (!phba->sli4_hba.intr_enable)
14482 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
James Smart6d368e52011-05-24 11:44:12 -040014483 else {
James Smarta183a152011-10-10 21:32:43 -040014484 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart6d368e52011-05-24 11:44:12 -040014485 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14486 }
James Smart4f774512009-05-22 14:52:35 -040014487 /* The IOCTL status is embedded in the mailbox subheader. */
14488 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14489 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14490 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14491 if (rc != MBX_TIMEOUT)
14492 mempool_free(mbox, phba->mbox_mem_pool);
14493 if (shdr_status || shdr_add_status || rc) {
14494 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14495 "2511 POST_SGL mailbox failed with "
14496 "status x%x add_status x%x, mbx status x%x\n",
14497 shdr_status, shdr_add_status, rc);
James Smart4f774512009-05-22 14:52:35 -040014498 }
14499 return 0;
14500}
James Smart4f774512009-05-22 14:52:35 -040014501
14502/**
James Smart88a2cfb2011-07-22 18:36:33 -040014503 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
James Smart6d368e52011-05-24 11:44:12 -040014504 * @phba: pointer to lpfc hba data structure.
14505 *
14506 * This routine is invoked to post rpi header templates to the
James Smart88a2cfb2011-07-22 18:36:33 -040014507 * HBA consistent with the SLI-4 interface spec. This routine
14508 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14509 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6d368e52011-05-24 11:44:12 -040014510 *
James Smart88a2cfb2011-07-22 18:36:33 -040014511 * Returns
14512 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14513 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14514 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014515static uint16_t
James Smart6d368e52011-05-24 11:44:12 -040014516lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14517{
14518 unsigned long xri;
14519
14520 /*
14521 * Fetch the next logical xri. Because this index is logical,
14522 * the driver starts at 0 each time.
14523 */
14524 spin_lock_irq(&phba->hbalock);
14525 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14526 phba->sli4_hba.max_cfg_param.max_xri, 0);
14527 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
14528 spin_unlock_irq(&phba->hbalock);
14529 return NO_XRI;
14530 } else {
14531 set_bit(xri, phba->sli4_hba.xri_bmask);
14532 phba->sli4_hba.max_cfg_param.xri_used++;
James Smart6d368e52011-05-24 11:44:12 -040014533 }
James Smart6d368e52011-05-24 11:44:12 -040014534 spin_unlock_irq(&phba->hbalock);
14535 return xri;
14536}
14537
14538/**
14539 * lpfc_sli4_free_xri - Release an xri for reuse.
14540 * @phba: pointer to lpfc hba data structure.
14541 *
14542 * This routine is invoked to release an xri to the pool of
14543 * available rpis maintained by the driver.
14544 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014545static void
James Smart6d368e52011-05-24 11:44:12 -040014546__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14547{
14548 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
James Smart6d368e52011-05-24 11:44:12 -040014549 phba->sli4_hba.max_cfg_param.xri_used--;
14550 }
14551}
14552
14553/**
14554 * lpfc_sli4_free_xri - Release an xri for reuse.
14555 * @phba: pointer to lpfc hba data structure.
14556 *
14557 * This routine is invoked to release an xri to the pool of
14558 * available rpis maintained by the driver.
14559 **/
14560void
14561lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14562{
14563 spin_lock_irq(&phba->hbalock);
14564 __lpfc_sli4_free_xri(phba, xri);
14565 spin_unlock_irq(&phba->hbalock);
14566}
14567
14568/**
James Smart4f774512009-05-22 14:52:35 -040014569 * lpfc_sli4_next_xritag - Get an xritag for the io
14570 * @phba: Pointer to HBA context object.
14571 *
14572 * This function gets an xritag for the iocb. If there is no unused xritag
14573 * it will return 0xffff.
14574 * The function returns the allocated xritag if successful, else returns zero.
14575 * Zero is not a valid xritag.
14576 * The caller is not required to hold any lock.
14577 **/
14578uint16_t
14579lpfc_sli4_next_xritag(struct lpfc_hba *phba)
14580{
James Smart6d368e52011-05-24 11:44:12 -040014581 uint16_t xri_index;
James Smart4f774512009-05-22 14:52:35 -040014582
James Smart6d368e52011-05-24 11:44:12 -040014583 xri_index = lpfc_sli4_alloc_xri(phba);
James Smart81378052012-05-09 21:17:37 -040014584 if (xri_index == NO_XRI)
14585 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14586 "2004 Failed to allocate XRI.last XRITAG is %d"
14587 " Max XRI is %d, Used XRI is %d\n",
14588 xri_index,
14589 phba->sli4_hba.max_cfg_param.max_xri,
14590 phba->sli4_hba.max_cfg_param.xri_used);
14591 return xri_index;
James Smart4f774512009-05-22 14:52:35 -040014592}
14593
14594/**
James Smart6d368e52011-05-24 11:44:12 -040014595 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
James Smart4f774512009-05-22 14:52:35 -040014596 * @phba: pointer to lpfc hba data structure.
James Smart8a9d2e82012-05-09 21:16:12 -040014597 * @post_sgl_list: pointer to els sgl entry list.
14598 * @count: number of els sgl entries on the list.
James Smart4f774512009-05-22 14:52:35 -040014599 *
14600 * This routine is invoked to post a block of driver's sgl pages to the
14601 * HBA using non-embedded mailbox command. No Lock is held. This routine
14602 * is only called when the driver is loading and after all IO has been
14603 * stopped.
14604 **/
James Smart8a9d2e82012-05-09 21:16:12 -040014605static int
14606lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
14607 struct list_head *post_sgl_list,
14608 int post_cnt)
James Smart4f774512009-05-22 14:52:35 -040014609{
James Smart8a9d2e82012-05-09 21:16:12 -040014610 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
James Smart4f774512009-05-22 14:52:35 -040014611 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14612 struct sgl_page_pairs *sgl_pg_pairs;
14613 void *viraddr;
14614 LPFC_MBOXQ_t *mbox;
14615 uint32_t reqlen, alloclen, pg_pairs;
14616 uint32_t mbox_tmo;
James Smart8a9d2e82012-05-09 21:16:12 -040014617 uint16_t xritag_start = 0;
14618 int rc = 0;
James Smart4f774512009-05-22 14:52:35 -040014619 uint32_t shdr_status, shdr_add_status;
14620 union lpfc_sli4_cfg_shdr *shdr;
14621
James Smart8a9d2e82012-05-09 21:16:12 -040014622 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014623 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014624 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014625 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14626 "2559 Block sgl registration required DMA "
14627 "size (%d) great than a page\n", reqlen);
14628 return -ENOMEM;
14629 }
14630 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart6d368e52011-05-24 11:44:12 -040014631 if (!mbox)
James Smart4f774512009-05-22 14:52:35 -040014632 return -ENOMEM;
James Smart4f774512009-05-22 14:52:35 -040014633
14634 /* Allocate DMA memory and set up the non-embedded mailbox command */
14635 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14636 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14637 LPFC_SLI4_MBX_NEMBED);
14638
14639 if (alloclen < reqlen) {
14640 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14641 "0285 Allocated DMA memory size (%d) is "
14642 "less than the requested DMA memory "
14643 "size (%d)\n", alloclen, reqlen);
14644 lpfc_sli4_mbox_cmd_free(phba, mbox);
14645 return -ENOMEM;
14646 }
James Smart4f774512009-05-22 14:52:35 -040014647 /* Set up the SGL pages in the non-embedded DMA pages */
James Smart6d368e52011-05-24 11:44:12 -040014648 viraddr = mbox->sge_array->addr[0];
James Smart4f774512009-05-22 14:52:35 -040014649 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14650 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14651
James Smart8a9d2e82012-05-09 21:16:12 -040014652 pg_pairs = 0;
14653 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
James Smart4f774512009-05-22 14:52:35 -040014654 /* Set up the sge entry */
14655 sgl_pg_pairs->sgl_pg0_addr_lo =
14656 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14657 sgl_pg_pairs->sgl_pg0_addr_hi =
14658 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14659 sgl_pg_pairs->sgl_pg1_addr_lo =
14660 cpu_to_le32(putPaddrLow(0));
14661 sgl_pg_pairs->sgl_pg1_addr_hi =
14662 cpu_to_le32(putPaddrHigh(0));
James Smart6d368e52011-05-24 11:44:12 -040014663
James Smart4f774512009-05-22 14:52:35 -040014664 /* Keep the first xritag on the list */
14665 if (pg_pairs == 0)
14666 xritag_start = sglq_entry->sli4_xritag;
14667 sgl_pg_pairs++;
James Smart8a9d2e82012-05-09 21:16:12 -040014668 pg_pairs++;
James Smart4f774512009-05-22 14:52:35 -040014669 }
James Smart6d368e52011-05-24 11:44:12 -040014670
14671 /* Complete initialization and perform endian conversion. */
James Smart4f774512009-05-22 14:52:35 -040014672 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
James Smart8a9d2e82012-05-09 21:16:12 -040014673 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
James Smart4f774512009-05-22 14:52:35 -040014674 sgl->word0 = cpu_to_le32(sgl->word0);
James Smart4f774512009-05-22 14:52:35 -040014675 if (!phba->sli4_hba.intr_enable)
14676 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14677 else {
James Smarta183a152011-10-10 21:32:43 -040014678 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014679 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14680 }
14681 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14682 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14683 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14684 if (rc != MBX_TIMEOUT)
14685 lpfc_sli4_mbox_cmd_free(phba, mbox);
14686 if (shdr_status || shdr_add_status || rc) {
14687 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14688 "2513 POST_SGL_BLOCK mailbox command failed "
14689 "status x%x add_status x%x mbx status x%x\n",
14690 shdr_status, shdr_add_status, rc);
14691 rc = -ENXIO;
14692 }
14693 return rc;
14694}
14695
14696/**
14697 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14698 * @phba: pointer to lpfc hba data structure.
14699 * @sblist: pointer to scsi buffer list.
14700 * @count: number of scsi buffers on the list.
14701 *
14702 * This routine is invoked to post a block of @count scsi sgl pages from a
14703 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14704 * No Lock is held.
14705 *
14706 **/
14707int
James Smart8a9d2e82012-05-09 21:16:12 -040014708lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14709 struct list_head *sblist,
14710 int count)
James Smart4f774512009-05-22 14:52:35 -040014711{
14712 struct lpfc_scsi_buf *psb;
14713 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14714 struct sgl_page_pairs *sgl_pg_pairs;
14715 void *viraddr;
14716 LPFC_MBOXQ_t *mbox;
14717 uint32_t reqlen, alloclen, pg_pairs;
14718 uint32_t mbox_tmo;
14719 uint16_t xritag_start = 0;
14720 int rc = 0;
14721 uint32_t shdr_status, shdr_add_status;
14722 dma_addr_t pdma_phys_bpl1;
14723 union lpfc_sli4_cfg_shdr *shdr;
14724
14725 /* Calculate the requested length of the dma memory */
James Smart8a9d2e82012-05-09 21:16:12 -040014726 reqlen = count * sizeof(struct sgl_page_pairs) +
James Smart4f774512009-05-22 14:52:35 -040014727 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
James Smart49198b32010-04-06 15:04:33 -040014728 if (reqlen > SLI4_PAGE_SIZE) {
James Smart4f774512009-05-22 14:52:35 -040014729 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14730 "0217 Block sgl registration required DMA "
14731 "size (%d) great than a page\n", reqlen);
14732 return -ENOMEM;
14733 }
14734 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14735 if (!mbox) {
14736 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14737 "0283 Failed to allocate mbox cmd memory\n");
14738 return -ENOMEM;
14739 }
14740
14741 /* Allocate DMA memory and set up the non-embedded mailbox command */
14742 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14743 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14744 LPFC_SLI4_MBX_NEMBED);
14745
14746 if (alloclen < reqlen) {
14747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14748 "2561 Allocated DMA memory size (%d) is "
14749 "less than the requested DMA memory "
14750 "size (%d)\n", alloclen, reqlen);
14751 lpfc_sli4_mbox_cmd_free(phba, mbox);
14752 return -ENOMEM;
14753 }
James Smart6d368e52011-05-24 11:44:12 -040014754
James Smart4f774512009-05-22 14:52:35 -040014755 /* Get the first SGE entry from the non-embedded DMA memory */
James Smart4f774512009-05-22 14:52:35 -040014756 viraddr = mbox->sge_array->addr[0];
14757
14758 /* Set up the SGL pages in the non-embedded DMA pages */
14759 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14760 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14761
14762 pg_pairs = 0;
14763 list_for_each_entry(psb, sblist, list) {
14764 /* Set up the sge entry */
14765 sgl_pg_pairs->sgl_pg0_addr_lo =
14766 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14767 sgl_pg_pairs->sgl_pg0_addr_hi =
14768 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14769 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14770 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14771 else
14772 pdma_phys_bpl1 = 0;
14773 sgl_pg_pairs->sgl_pg1_addr_lo =
14774 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14775 sgl_pg_pairs->sgl_pg1_addr_hi =
14776 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14777 /* Keep the first xritag on the list */
14778 if (pg_pairs == 0)
14779 xritag_start = psb->cur_iocbq.sli4_xritag;
14780 sgl_pg_pairs++;
14781 pg_pairs++;
14782 }
14783 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14784 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14785 /* Perform endian conversion if necessary */
14786 sgl->word0 = cpu_to_le32(sgl->word0);
14787
14788 if (!phba->sli4_hba.intr_enable)
14789 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14790 else {
James Smarta183a152011-10-10 21:32:43 -040014791 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart4f774512009-05-22 14:52:35 -040014792 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14793 }
14794 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14795 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14796 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14797 if (rc != MBX_TIMEOUT)
14798 lpfc_sli4_mbox_cmd_free(phba, mbox);
14799 if (shdr_status || shdr_add_status || rc) {
14800 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14801 "2564 POST_SGL_BLOCK mailbox command failed "
14802 "status x%x add_status x%x mbx status x%x\n",
14803 shdr_status, shdr_add_status, rc);
14804 rc = -ENXIO;
14805 }
14806 return rc;
14807}
14808
14809/**
14810 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14811 * @phba: pointer to lpfc_hba struct that the frame was received on
14812 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14813 *
14814 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14815 * valid type of frame that the LPFC driver will handle. This function will
14816 * return a zero if the frame is a valid frame or a non zero value when the
14817 * frame does not pass the check.
14818 **/
14819static int
14820lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14821{
Tomas Henzl474ffb72010-12-22 16:52:40 +010014822 /* make rctl_names static to save stack space */
14823 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
James Smart4f774512009-05-22 14:52:35 -040014824 char *type_names[] = FC_TYPE_NAMES_INIT;
14825 struct fc_vft_header *fc_vft_hdr;
James Smart546fc852011-03-11 16:06:29 -050014826 uint32_t *header = (uint32_t *) fc_hdr;
James Smart4f774512009-05-22 14:52:35 -040014827
14828 switch (fc_hdr->fh_r_ctl) {
14829 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14830 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14831 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14832 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14833 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14834 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14835 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14836 case FC_RCTL_DD_CMD_STATUS: /* command status */
14837 case FC_RCTL_ELS_REQ: /* extended link services request */
14838 case FC_RCTL_ELS_REP: /* extended link services reply */
14839 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14840 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14841 case FC_RCTL_BA_NOP: /* basic link service NOP */
14842 case FC_RCTL_BA_ABTS: /* basic link service abort */
14843 case FC_RCTL_BA_RMC: /* remove connection */
14844 case FC_RCTL_BA_ACC: /* basic accept */
14845 case FC_RCTL_BA_RJT: /* basic reject */
14846 case FC_RCTL_BA_PRMT:
14847 case FC_RCTL_ACK_1: /* acknowledge_1 */
14848 case FC_RCTL_ACK_0: /* acknowledge_0 */
14849 case FC_RCTL_P_RJT: /* port reject */
14850 case FC_RCTL_F_RJT: /* fabric reject */
14851 case FC_RCTL_P_BSY: /* port busy */
14852 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14853 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14854 case FC_RCTL_LCR: /* link credit reset */
14855 case FC_RCTL_END: /* end */
14856 break;
14857 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14858 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14859 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14860 return lpfc_fc_frame_check(phba, fc_hdr);
14861 default:
14862 goto drop;
14863 }
14864 switch (fc_hdr->fh_type) {
14865 case FC_TYPE_BLS:
14866 case FC_TYPE_ELS:
14867 case FC_TYPE_FCP:
14868 case FC_TYPE_CT:
14869 break;
14870 case FC_TYPE_IP:
14871 case FC_TYPE_ILS:
14872 default:
14873 goto drop;
14874 }
James Smart546fc852011-03-11 16:06:29 -050014875
James Smart4f774512009-05-22 14:52:35 -040014876 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart88f43a02013-04-17 20:19:44 -040014877 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14878 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14879 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14880 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
James Smart546fc852011-03-11 16:06:29 -050014881 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14882 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
James Smart88f43a02013-04-17 20:19:44 -040014883 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14884 be32_to_cpu(header[6]));
James Smart4f774512009-05-22 14:52:35 -040014885 return 0;
14886drop:
14887 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14888 "2539 Dropped frame rctl:%s type:%s\n",
14889 rctl_names[fc_hdr->fh_r_ctl],
14890 type_names[fc_hdr->fh_type]);
14891 return 1;
14892}
14893
14894/**
14895 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14896 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14897 *
14898 * This function processes the FC header to retrieve the VFI from the VF
14899 * header, if one exists. This function will return the VFI if one exists
14900 * or 0 if no VSAN Header exists.
14901 **/
14902static uint32_t
14903lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14904{
14905 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14906
14907 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14908 return 0;
14909 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14910}
14911
14912/**
14913 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14914 * @phba: Pointer to the HBA structure to search for the vport on
14915 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14916 * @fcfi: The FC Fabric ID that the frame came from
14917 *
14918 * This function searches the @phba for a vport that matches the content of the
14919 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14920 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14921 * returns the matching vport pointer or NULL if unable to match frame to a
14922 * vport.
14923 **/
14924static struct lpfc_vport *
14925lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14926 uint16_t fcfi)
14927{
14928 struct lpfc_vport **vports;
14929 struct lpfc_vport *vport = NULL;
14930 int i;
14931 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14932 fc_hdr->fh_d_id[1] << 8 |
14933 fc_hdr->fh_d_id[2]);
James Smart939723a2012-05-09 21:19:03 -040014934
James Smartbf086112011-08-21 21:48:13 -040014935 if (did == Fabric_DID)
14936 return phba->pport;
James Smart939723a2012-05-09 21:19:03 -040014937 if ((phba->pport->fc_flag & FC_PT2PT) &&
14938 !(phba->link_state == LPFC_HBA_READY))
14939 return phba->pport;
14940
James Smart4f774512009-05-22 14:52:35 -040014941 vports = lpfc_create_vport_work_array(phba);
14942 if (vports != NULL)
14943 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14944 if (phba->fcf.fcfi == fcfi &&
14945 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14946 vports[i]->fc_myDID == did) {
14947 vport = vports[i];
14948 break;
14949 }
14950 }
14951 lpfc_destroy_vport_work_array(phba, vports);
14952 return vport;
14953}
14954
14955/**
James Smart45ed1192009-10-02 15:17:02 -040014956 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14957 * @vport: The vport to work on.
14958 *
14959 * This function updates the receive sequence time stamp for this vport. The
14960 * receive sequence time stamp indicates the time that the last frame of the
14961 * the sequence that has been idle for the longest amount of time was received.
14962 * the driver uses this time stamp to indicate if any received sequences have
14963 * timed out.
14964 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040014965static void
James Smart45ed1192009-10-02 15:17:02 -040014966lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14967{
14968 struct lpfc_dmabuf *h_buf;
14969 struct hbq_dmabuf *dmabuf = NULL;
14970
14971 /* get the oldest sequence on the rcv list */
14972 h_buf = list_get_first(&vport->rcv_buffer_list,
14973 struct lpfc_dmabuf, list);
14974 if (!h_buf)
14975 return;
14976 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14977 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14978}
14979
14980/**
14981 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14982 * @vport: The vport that the received sequences were sent to.
14983 *
14984 * This function cleans up all outstanding received sequences. This is called
14985 * by the driver when a link event or user action invalidates all the received
14986 * sequences.
14987 **/
14988void
14989lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14990{
14991 struct lpfc_dmabuf *h_buf, *hnext;
14992 struct lpfc_dmabuf *d_buf, *dnext;
14993 struct hbq_dmabuf *dmabuf = NULL;
14994
14995 /* start with the oldest sequence on the rcv list */
14996 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14997 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14998 list_del_init(&dmabuf->hbuf.list);
14999 list_for_each_entry_safe(d_buf, dnext,
15000 &dmabuf->dbuf.list, list) {
15001 list_del_init(&d_buf->list);
15002 lpfc_in_buf_free(vport->phba, d_buf);
15003 }
15004 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15005 }
15006}
15007
15008/**
15009 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
15010 * @vport: The vport that the received sequences were sent to.
15011 *
15012 * This function determines whether any received sequences have timed out by
15013 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
15014 * indicates that there is at least one timed out sequence this routine will
15015 * go through the received sequences one at a time from most inactive to most
15016 * active to determine which ones need to be cleaned up. Once it has determined
15017 * that a sequence needs to be cleaned up it will simply free up the resources
15018 * without sending an abort.
15019 **/
15020void
15021lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
15022{
15023 struct lpfc_dmabuf *h_buf, *hnext;
15024 struct lpfc_dmabuf *d_buf, *dnext;
15025 struct hbq_dmabuf *dmabuf = NULL;
15026 unsigned long timeout;
15027 int abort_count = 0;
15028
15029 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15030 vport->rcv_buffer_time_stamp);
15031 if (list_empty(&vport->rcv_buffer_list) ||
15032 time_before(jiffies, timeout))
15033 return;
15034 /* start with the oldest sequence on the rcv list */
15035 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15036 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15037 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15038 dmabuf->time_stamp);
15039 if (time_before(jiffies, timeout))
15040 break;
15041 abort_count++;
15042 list_del_init(&dmabuf->hbuf.list);
15043 list_for_each_entry_safe(d_buf, dnext,
15044 &dmabuf->dbuf.list, list) {
15045 list_del_init(&d_buf->list);
15046 lpfc_in_buf_free(vport->phba, d_buf);
15047 }
15048 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15049 }
15050 if (abort_count)
15051 lpfc_update_rcv_time_stamp(vport);
15052}
15053
15054/**
James Smart4f774512009-05-22 14:52:35 -040015055 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
15056 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
15057 *
15058 * This function searches through the existing incomplete sequences that have
15059 * been sent to this @vport. If the frame matches one of the incomplete
15060 * sequences then the dbuf in the @dmabuf is added to the list of frames that
15061 * make up that sequence. If no sequence is found that matches this frame then
15062 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
15063 * This function returns a pointer to the first dmabuf in the sequence list that
15064 * the frame was linked to.
15065 **/
15066static struct hbq_dmabuf *
15067lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15068{
15069 struct fc_frame_header *new_hdr;
15070 struct fc_frame_header *temp_hdr;
15071 struct lpfc_dmabuf *d_buf;
15072 struct lpfc_dmabuf *h_buf;
15073 struct hbq_dmabuf *seq_dmabuf = NULL;
15074 struct hbq_dmabuf *temp_dmabuf = NULL;
James Smart4360ca92015-12-16 18:12:04 -050015075 uint8_t found = 0;
James Smart4f774512009-05-22 14:52:35 -040015076
James Smart4d9ab992009-10-02 15:16:39 -040015077 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015078 dmabuf->time_stamp = jiffies;
James Smart4f774512009-05-22 14:52:35 -040015079 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
James Smart4360ca92015-12-16 18:12:04 -050015080
James Smart4f774512009-05-22 14:52:35 -040015081 /* Use the hdr_buf to find the sequence that this frame belongs to */
15082 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15083 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15084 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15085 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15086 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15087 continue;
15088 /* found a pending sequence that matches this frame */
15089 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15090 break;
15091 }
15092 if (!seq_dmabuf) {
15093 /*
15094 * This indicates first frame received for this sequence.
15095 * Queue the buffer on the vport's rcv_buffer_list.
15096 */
15097 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
James Smart45ed1192009-10-02 15:17:02 -040015098 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015099 return dmabuf;
15100 }
15101 temp_hdr = seq_dmabuf->hbuf.virt;
James Smarteeead812009-12-21 17:01:23 -050015102 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
15103 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4d9ab992009-10-02 15:16:39 -040015104 list_del_init(&seq_dmabuf->hbuf.list);
15105 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15106 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015107 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015108 return dmabuf;
15109 }
James Smart45ed1192009-10-02 15:17:02 -040015110 /* move this sequence to the tail to indicate a young sequence */
15111 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
15112 seq_dmabuf->time_stamp = jiffies;
15113 lpfc_update_rcv_time_stamp(vport);
James Smarteeead812009-12-21 17:01:23 -050015114 if (list_empty(&seq_dmabuf->dbuf.list)) {
15115 temp_hdr = dmabuf->hbuf.virt;
15116 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15117 return seq_dmabuf;
15118 }
James Smart4f774512009-05-22 14:52:35 -040015119 /* find the correct place in the sequence to insert this frame */
James Smart4360ca92015-12-16 18:12:04 -050015120 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
15121 while (!found) {
James Smart4f774512009-05-22 14:52:35 -040015122 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15123 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
15124 /*
15125 * If the frame's sequence count is greater than the frame on
15126 * the list then insert the frame right after this frame
15127 */
James Smarteeead812009-12-21 17:01:23 -050015128 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
15129 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
James Smart4f774512009-05-22 14:52:35 -040015130 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
James Smart4360ca92015-12-16 18:12:04 -050015131 found = 1;
15132 break;
James Smart4f774512009-05-22 14:52:35 -040015133 }
James Smart4360ca92015-12-16 18:12:04 -050015134
15135 if (&d_buf->list == &seq_dmabuf->dbuf.list)
15136 break;
15137 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
James Smart4f774512009-05-22 14:52:35 -040015138 }
James Smart4360ca92015-12-16 18:12:04 -050015139
15140 if (found)
15141 return seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015142 return NULL;
15143}
15144
15145/**
James Smart6669f9b2009-10-02 15:16:45 -040015146 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
15147 * @vport: pointer to a vitural port
15148 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15149 *
15150 * This function tries to abort from the partially assembed sequence, described
15151 * by the information from basic abbort @dmabuf. It checks to see whether such
15152 * partially assembled sequence held by the driver. If so, it shall free up all
15153 * the frames from the partially assembled sequence.
15154 *
15155 * Return
15156 * true -- if there is matching partially assembled sequence present and all
15157 * the frames freed with the sequence;
15158 * false -- if there is no matching partially assembled sequence present so
15159 * nothing got aborted in the lower layer driver
15160 **/
15161static bool
15162lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
15163 struct hbq_dmabuf *dmabuf)
15164{
15165 struct fc_frame_header *new_hdr;
15166 struct fc_frame_header *temp_hdr;
15167 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
15168 struct hbq_dmabuf *seq_dmabuf = NULL;
15169
15170 /* Use the hdr_buf to find the sequence that matches this frame */
15171 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15172 INIT_LIST_HEAD(&dmabuf->hbuf.list);
15173 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15174 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15175 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15176 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15177 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15178 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15179 continue;
15180 /* found a pending sequence that matches this frame */
15181 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15182 break;
15183 }
15184
15185 /* Free up all the frames from the partially assembled sequence */
15186 if (seq_dmabuf) {
15187 list_for_each_entry_safe(d_buf, n_buf,
15188 &seq_dmabuf->dbuf.list, list) {
15189 list_del_init(&d_buf->list);
15190 lpfc_in_buf_free(vport->phba, d_buf);
15191 }
15192 return true;
15193 }
15194 return false;
15195}
15196
15197/**
James Smart6dd9e312013-01-03 15:43:37 -050015198 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15199 * @vport: pointer to a vitural port
15200 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15201 *
15202 * This function tries to abort from the assembed sequence from upper level
15203 * protocol, described by the information from basic abbort @dmabuf. It
15204 * checks to see whether such pending context exists at upper level protocol.
15205 * If so, it shall clean up the pending context.
15206 *
15207 * Return
15208 * true -- if there is matching pending context of the sequence cleaned
15209 * at ulp;
15210 * false -- if there is no matching pending context of the sequence present
15211 * at ulp.
15212 **/
15213static bool
15214lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15215{
15216 struct lpfc_hba *phba = vport->phba;
15217 int handled;
15218
15219 /* Accepting abort at ulp with SLI4 only */
15220 if (phba->sli_rev < LPFC_SLI_REV4)
15221 return false;
15222
15223 /* Register all caring upper level protocols to attend abort */
15224 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15225 if (handled)
15226 return true;
15227
15228 return false;
15229}
15230
15231/**
James Smart546fc852011-03-11 16:06:29 -050015232 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
James Smart6669f9b2009-10-02 15:16:45 -040015233 * @phba: Pointer to HBA context object.
15234 * @cmd_iocbq: pointer to the command iocbq structure.
15235 * @rsp_iocbq: pointer to the response iocbq structure.
15236 *
James Smart546fc852011-03-11 16:06:29 -050015237 * This function handles the sequence abort response iocb command complete
James Smart6669f9b2009-10-02 15:16:45 -040015238 * event. It properly releases the memory allocated to the sequence abort
15239 * accept iocb.
15240 **/
15241static void
James Smart546fc852011-03-11 16:06:29 -050015242lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
James Smart6669f9b2009-10-02 15:16:45 -040015243 struct lpfc_iocbq *cmd_iocbq,
15244 struct lpfc_iocbq *rsp_iocbq)
15245{
James Smart6dd9e312013-01-03 15:43:37 -050015246 struct lpfc_nodelist *ndlp;
15247
15248 if (cmd_iocbq) {
15249 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15250 lpfc_nlp_put(ndlp);
15251 lpfc_nlp_not_used(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015252 lpfc_sli_release_iocbq(phba, cmd_iocbq);
James Smart6dd9e312013-01-03 15:43:37 -050015253 }
James Smart6b5151f2012-01-18 16:24:06 -050015254
15255 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15256 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15257 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15258 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
15259 rsp_iocbq->iocb.ulpStatus,
15260 rsp_iocbq->iocb.un.ulpWord[4]);
James Smart6669f9b2009-10-02 15:16:45 -040015261}
15262
15263/**
James Smart6d368e52011-05-24 11:44:12 -040015264 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15265 * @phba: Pointer to HBA context object.
15266 * @xri: xri id in transaction.
15267 *
15268 * This function validates the xri maps to the known range of XRIs allocated an
15269 * used by the driver.
15270 **/
James Smart7851fe22011-07-22 18:36:52 -040015271uint16_t
James Smart6d368e52011-05-24 11:44:12 -040015272lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15273 uint16_t xri)
15274{
James Smarta2fc4aef2014-09-03 12:57:55 -040015275 uint16_t i;
James Smart6d368e52011-05-24 11:44:12 -040015276
15277 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15278 if (xri == phba->sli4_hba.xri_ids[i])
15279 return i;
15280 }
15281 return NO_XRI;
15282}
15283
James Smart6d368e52011-05-24 11:44:12 -040015284/**
James Smart546fc852011-03-11 16:06:29 -050015285 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015286 * @phba: Pointer to HBA context object.
15287 * @fc_hdr: pointer to a FC frame header.
15288 *
James Smart546fc852011-03-11 16:06:29 -050015289 * This function sends a basic response to a previous unsol sequence abort
James Smart6669f9b2009-10-02 15:16:45 -040015290 * event after aborting the sequence handling.
15291 **/
15292static void
James Smart6dd9e312013-01-03 15:43:37 -050015293lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15294 struct fc_frame_header *fc_hdr, bool aborted)
James Smart6669f9b2009-10-02 15:16:45 -040015295{
James Smart6dd9e312013-01-03 15:43:37 -050015296 struct lpfc_hba *phba = vport->phba;
James Smart6669f9b2009-10-02 15:16:45 -040015297 struct lpfc_iocbq *ctiocb = NULL;
15298 struct lpfc_nodelist *ndlp;
James Smartee0f4fe2012-05-09 21:19:14 -040015299 uint16_t oxid, rxid, xri, lxri;
James Smart5ffc2662009-11-18 15:39:44 -050015300 uint32_t sid, fctl;
James Smart6669f9b2009-10-02 15:16:45 -040015301 IOCB_t *icmd;
James Smart546fc852011-03-11 16:06:29 -050015302 int rc;
James Smart6669f9b2009-10-02 15:16:45 -040015303
15304 if (!lpfc_is_link_up(phba))
15305 return;
15306
15307 sid = sli4_sid_from_fc_hdr(fc_hdr);
15308 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
James Smart5ffc2662009-11-18 15:39:44 -050015309 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
James Smart6669f9b2009-10-02 15:16:45 -040015310
James Smart6dd9e312013-01-03 15:43:37 -050015311 ndlp = lpfc_findnode_did(vport, sid);
James Smart6669f9b2009-10-02 15:16:45 -040015312 if (!ndlp) {
James Smart6dd9e312013-01-03 15:43:37 -050015313 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15314 if (!ndlp) {
15315 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15316 "1268 Failed to allocate ndlp for "
15317 "oxid:x%x SID:x%x\n", oxid, sid);
15318 return;
15319 }
15320 lpfc_nlp_init(vport, ndlp, sid);
15321 /* Put ndlp onto pport node list */
15322 lpfc_enqueue_node(vport, ndlp);
15323 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15324 /* re-setup ndlp without removing from node list */
15325 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15326 if (!ndlp) {
15327 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15328 "3275 Failed to active ndlp found "
15329 "for oxid:x%x SID:x%x\n", oxid, sid);
15330 return;
15331 }
James Smart6669f9b2009-10-02 15:16:45 -040015332 }
15333
James Smart546fc852011-03-11 16:06:29 -050015334 /* Allocate buffer for rsp iocb */
James Smart6669f9b2009-10-02 15:16:45 -040015335 ctiocb = lpfc_sli_get_iocbq(phba);
15336 if (!ctiocb)
15337 return;
15338
James Smart5ffc2662009-11-18 15:39:44 -050015339 /* Extract the F_CTL field from FC_HDR */
15340 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15341
James Smart6669f9b2009-10-02 15:16:45 -040015342 icmd = &ctiocb->iocb;
James Smart6669f9b2009-10-02 15:16:45 -040015343 icmd->un.xseq64.bdl.bdeSize = 0;
James Smart5ffc2662009-11-18 15:39:44 -050015344 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
James Smart6669f9b2009-10-02 15:16:45 -040015345 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15346 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15347 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15348
15349 /* Fill in the rest of iocb fields */
15350 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15351 icmd->ulpBdeCount = 0;
15352 icmd->ulpLe = 1;
15353 icmd->ulpClass = CLASS3;
James Smart6d368e52011-05-24 11:44:12 -040015354 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart6dd9e312013-01-03 15:43:37 -050015355 ctiocb->context1 = lpfc_nlp_get(ndlp);
James Smart6669f9b2009-10-02 15:16:45 -040015356
James Smart6669f9b2009-10-02 15:16:45 -040015357 ctiocb->iocb_cmpl = NULL;
15358 ctiocb->vport = phba->pport;
James Smart546fc852011-03-11 16:06:29 -050015359 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
James Smart6d368e52011-05-24 11:44:12 -040015360 ctiocb->sli4_lxritag = NO_XRI;
James Smart546fc852011-03-11 16:06:29 -050015361 ctiocb->sli4_xritag = NO_XRI;
15362
James Smartee0f4fe2012-05-09 21:19:14 -040015363 if (fctl & FC_FC_EX_CTX)
15364 /* Exchange responder sent the abort so we
15365 * own the oxid.
15366 */
15367 xri = oxid;
15368 else
15369 xri = rxid;
15370 lxri = lpfc_sli4_xri_inrange(phba, xri);
15371 if (lxri != NO_XRI)
15372 lpfc_set_rrq_active(phba, ndlp, lxri,
15373 (xri == oxid) ? rxid : oxid, 0);
James Smart6dd9e312013-01-03 15:43:37 -050015374 /* For BA_ABTS from exchange responder, if the logical xri with
15375 * the oxid maps to the FCP XRI range, the port no longer has
15376 * that exchange context, send a BLS_RJT. Override the IOCB for
15377 * a BA_RJT.
James Smart546fc852011-03-11 16:06:29 -050015378 */
James Smart6dd9e312013-01-03 15:43:37 -050015379 if ((fctl & FC_FC_EX_CTX) &&
15380 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
15381 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 }
15386
15387 /* If BA_ABTS failed to abort a partially assembled receive sequence,
15388 * the driver no longer has that exchange, send a BLS_RJT. Override
15389 * the IOCB for a BA_RJT.
15390 */
15391 if (aborted == false) {
James Smart546fc852011-03-11 16:06:29 -050015392 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15393 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15394 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15395 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15396 }
James Smart6669f9b2009-10-02 15:16:45 -040015397
James Smart5ffc2662009-11-18 15:39:44 -050015398 if (fctl & FC_FC_EX_CTX) {
15399 /* ABTS sent by responder to CT exchange, construction
15400 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15401 * field and RX_ID from ABTS for RX_ID field.
15402 */
James Smart546fc852011-03-11 16:06:29 -050015403 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
James Smart5ffc2662009-11-18 15:39:44 -050015404 } else {
15405 /* ABTS sent by initiator to CT exchange, construction
15406 * of BA_ACC will need to allocate a new XRI as for the
James Smartf09c3ac2012-03-01 22:33:29 -050015407 * XRI_TAG field.
James Smart5ffc2662009-11-18 15:39:44 -050015408 */
James Smart546fc852011-03-11 16:06:29 -050015409 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
James Smart5ffc2662009-11-18 15:39:44 -050015410 }
James Smartf09c3ac2012-03-01 22:33:29 -050015411 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
James Smart546fc852011-03-11 16:06:29 -050015412 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
James Smart5ffc2662009-11-18 15:39:44 -050015413
James Smart546fc852011-03-11 16:06:29 -050015414 /* Xmit CT abts response on exchange <xid> */
James Smart6dd9e312013-01-03 15:43:37 -050015415 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15416 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15417 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
James Smart546fc852011-03-11 16:06:29 -050015418
15419 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15420 if (rc == IOCB_ERROR) {
James Smart6dd9e312013-01-03 15:43:37 -050015421 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15422 "2925 Failed to issue CT ABTS RSP x%x on "
15423 "xri x%x, Data x%x\n",
15424 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15425 phba->link_state);
15426 lpfc_nlp_put(ndlp);
15427 ctiocb->context1 = NULL;
James Smart546fc852011-03-11 16:06:29 -050015428 lpfc_sli_release_iocbq(phba, ctiocb);
15429 }
James Smart6669f9b2009-10-02 15:16:45 -040015430}
15431
15432/**
15433 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15434 * @vport: Pointer to the vport on which this sequence was received
15435 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15436 *
15437 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15438 * receive sequence is only partially assembed by the driver, it shall abort
15439 * the partially assembled frames for the sequence. Otherwise, if the
15440 * unsolicited receive sequence has been completely assembled and passed to
15441 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15442 * unsolicited sequence has been aborted. After that, it will issue a basic
15443 * accept to accept the abort.
15444 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015445static void
James Smart6669f9b2009-10-02 15:16:45 -040015446lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15447 struct hbq_dmabuf *dmabuf)
15448{
15449 struct lpfc_hba *phba = vport->phba;
15450 struct fc_frame_header fc_hdr;
James Smart5ffc2662009-11-18 15:39:44 -050015451 uint32_t fctl;
James Smart6dd9e312013-01-03 15:43:37 -050015452 bool aborted;
James Smart6669f9b2009-10-02 15:16:45 -040015453
James Smart6669f9b2009-10-02 15:16:45 -040015454 /* Make a copy of fc_hdr before the dmabuf being released */
15455 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
James Smart5ffc2662009-11-18 15:39:44 -050015456 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
James Smart6669f9b2009-10-02 15:16:45 -040015457
James Smart5ffc2662009-11-18 15:39:44 -050015458 if (fctl & FC_FC_EX_CTX) {
James Smart6dd9e312013-01-03 15:43:37 -050015459 /* ABTS by responder to exchange, no cleanup needed */
15460 aborted = true;
James Smart5ffc2662009-11-18 15:39:44 -050015461 } else {
James Smart6dd9e312013-01-03 15:43:37 -050015462 /* ABTS by initiator to exchange, need to do cleanup */
15463 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15464 if (aborted == false)
15465 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
James Smart5ffc2662009-11-18 15:39:44 -050015466 }
James Smart6dd9e312013-01-03 15:43:37 -050015467 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15468
15469 /* Respond with BA_ACC or BA_RJT accordingly */
15470 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
James Smart6669f9b2009-10-02 15:16:45 -040015471}
15472
15473/**
James Smart4f774512009-05-22 14:52:35 -040015474 * lpfc_seq_complete - Indicates if a sequence is complete
15475 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15476 *
15477 * This function checks the sequence, starting with the frame described by
15478 * @dmabuf, to see if all the frames associated with this sequence are present.
15479 * the frames associated with this sequence are linked to the @dmabuf using the
15480 * dbuf list. This function looks for two major things. 1) That the first frame
15481 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15482 * set. 3) That there are no holes in the sequence count. The function will
15483 * return 1 when the sequence is complete, otherwise it will return 0.
15484 **/
15485static int
15486lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15487{
15488 struct fc_frame_header *hdr;
15489 struct lpfc_dmabuf *d_buf;
15490 struct hbq_dmabuf *seq_dmabuf;
15491 uint32_t fctl;
15492 int seq_count = 0;
15493
15494 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15495 /* make sure first fame of sequence has a sequence count of zero */
15496 if (hdr->fh_seq_cnt != seq_count)
15497 return 0;
15498 fctl = (hdr->fh_f_ctl[0] << 16 |
15499 hdr->fh_f_ctl[1] << 8 |
15500 hdr->fh_f_ctl[2]);
15501 /* If last frame of sequence we can return success. */
15502 if (fctl & FC_FC_END_SEQ)
15503 return 1;
15504 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15505 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15506 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15507 /* If there is a hole in the sequence count then fail. */
James Smarteeead812009-12-21 17:01:23 -050015508 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
James Smart4f774512009-05-22 14:52:35 -040015509 return 0;
15510 fctl = (hdr->fh_f_ctl[0] << 16 |
15511 hdr->fh_f_ctl[1] << 8 |
15512 hdr->fh_f_ctl[2]);
15513 /* If last frame of sequence we can return success. */
15514 if (fctl & FC_FC_END_SEQ)
15515 return 1;
15516 }
15517 return 0;
15518}
15519
15520/**
15521 * lpfc_prep_seq - Prep sequence for ULP processing
15522 * @vport: Pointer to the vport on which this sequence was received
15523 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15524 *
15525 * This function takes a sequence, described by a list of frames, and creates
15526 * a list of iocbq structures to describe the sequence. This iocbq list will be
15527 * used to issue to the generic unsolicited sequence handler. This routine
15528 * returns a pointer to the first iocbq in the list. If the function is unable
15529 * to allocate an iocbq then it throw out the received frames that were not
15530 * able to be described and return a pointer to the first iocbq. If unable to
15531 * allocate any iocbqs (including the first) this function will return NULL.
15532 **/
15533static struct lpfc_iocbq *
15534lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
15535{
James Smart7851fe22011-07-22 18:36:52 -040015536 struct hbq_dmabuf *hbq_buf;
James Smart4f774512009-05-22 14:52:35 -040015537 struct lpfc_dmabuf *d_buf, *n_buf;
15538 struct lpfc_iocbq *first_iocbq, *iocbq;
15539 struct fc_frame_header *fc_hdr;
15540 uint32_t sid;
James Smart7851fe22011-07-22 18:36:52 -040015541 uint32_t len, tot_len;
James Smarteeead812009-12-21 17:01:23 -050015542 struct ulp_bde64 *pbde;
James Smart4f774512009-05-22 14:52:35 -040015543
15544 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15545 /* remove from receive buffer list */
15546 list_del_init(&seq_dmabuf->hbuf.list);
James Smart45ed1192009-10-02 15:17:02 -040015547 lpfc_update_rcv_time_stamp(vport);
James Smart4f774512009-05-22 14:52:35 -040015548 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040015549 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart7851fe22011-07-22 18:36:52 -040015550 tot_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015551 /* Get an iocbq struct to fill in. */
15552 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
15553 if (first_iocbq) {
15554 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040015555 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040015556 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
James Smart939723a2012-05-09 21:19:03 -040015557
15558 /* Check FC Header to see what TYPE of frame we are rcv'ing */
15559 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
15560 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
15561 first_iocbq->iocb.un.rcvels.parmRo =
15562 sli4_did_from_fc_hdr(fc_hdr);
15563 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
15564 } else
15565 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
James Smart7851fe22011-07-22 18:36:52 -040015566 first_iocbq->iocb.ulpContext = NO_XRI;
15567 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
15568 be16_to_cpu(fc_hdr->fh_ox_id);
15569 /* iocbq is prepped for internal consumption. Physical vpi. */
15570 first_iocbq->iocb.unsli3.rcvsli3.vpi =
15571 vport->phba->vpi_ids[vport->vpi];
James Smart4f774512009-05-22 14:52:35 -040015572 /* put the first buffer into the first IOCBq */
James Smart48a5a662013-07-15 18:32:28 -040015573 tot_len = bf_get(lpfc_rcqe_length,
15574 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
15575
James Smart4f774512009-05-22 14:52:35 -040015576 first_iocbq->context2 = &seq_dmabuf->dbuf;
15577 first_iocbq->context3 = NULL;
15578 first_iocbq->iocb.ulpBdeCount = 1;
James Smart48a5a662013-07-15 18:32:28 -040015579 if (tot_len > LPFC_DATA_BUF_SIZE)
15580 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
James Smart4f774512009-05-22 14:52:35 -040015581 LPFC_DATA_BUF_SIZE;
James Smart48a5a662013-07-15 18:32:28 -040015582 else
15583 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
15584
James Smart4f774512009-05-22 14:52:35 -040015585 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart48a5a662013-07-15 18:32:28 -040015586
James Smart7851fe22011-07-22 18:36:52 -040015587 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
James Smart4f774512009-05-22 14:52:35 -040015588 }
15589 iocbq = first_iocbq;
15590 /*
15591 * Each IOCBq can have two Buffers assigned, so go through the list
15592 * of buffers for this sequence and save two buffers in each IOCBq
15593 */
15594 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
15595 if (!iocbq) {
15596 lpfc_in_buf_free(vport->phba, d_buf);
15597 continue;
15598 }
15599 if (!iocbq->context3) {
15600 iocbq->context3 = d_buf;
15601 iocbq->iocb.ulpBdeCount++;
James Smart7851fe22011-07-22 18:36:52 -040015602 /* We need to get the size out of the right CQE */
15603 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15604 len = bf_get(lpfc_rcqe_length,
15605 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015606 pbde = (struct ulp_bde64 *)
15607 &iocbq->iocb.unsli3.sli3Words[4];
15608 if (len > LPFC_DATA_BUF_SIZE)
15609 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
15610 else
15611 pbde->tus.f.bdeSize = len;
15612
James Smart7851fe22011-07-22 18:36:52 -040015613 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
15614 tot_len += len;
James Smart4f774512009-05-22 14:52:35 -040015615 } else {
15616 iocbq = lpfc_sli_get_iocbq(vport->phba);
15617 if (!iocbq) {
15618 if (first_iocbq) {
15619 first_iocbq->iocb.ulpStatus =
15620 IOSTAT_FCP_RSP_ERROR;
15621 first_iocbq->iocb.un.ulpWord[4] =
15622 IOERR_NO_RESOURCES;
15623 }
15624 lpfc_in_buf_free(vport->phba, d_buf);
15625 continue;
15626 }
James Smart7851fe22011-07-22 18:36:52 -040015627 /* We need to get the size out of the right CQE */
15628 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15629 len = bf_get(lpfc_rcqe_length,
15630 &hbq_buf->cq_event.cqe.rcqe_cmpl);
James Smart48a5a662013-07-15 18:32:28 -040015631 iocbq->context2 = d_buf;
15632 iocbq->context3 = NULL;
15633 iocbq->iocb.ulpBdeCount = 1;
15634 if (len > LPFC_DATA_BUF_SIZE)
15635 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15636 LPFC_DATA_BUF_SIZE;
15637 else
15638 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
15639
James Smart7851fe22011-07-22 18:36:52 -040015640 tot_len += len;
15641 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15642
James Smart4f774512009-05-22 14:52:35 -040015643 iocbq->iocb.un.rcvels.remoteID = sid;
15644 list_add_tail(&iocbq->list, &first_iocbq->list);
15645 }
15646 }
15647 return first_iocbq;
15648}
15649
James Smart6669f9b2009-10-02 15:16:45 -040015650static void
15651lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
15652 struct hbq_dmabuf *seq_dmabuf)
15653{
15654 struct fc_frame_header *fc_hdr;
15655 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15656 struct lpfc_hba *phba = vport->phba;
15657
15658 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15659 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15660 if (!iocbq) {
15661 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15662 "2707 Ring %d handler: Failed to allocate "
15663 "iocb Rctl x%x Type x%x received\n",
15664 LPFC_ELS_RING,
15665 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15666 return;
15667 }
15668 if (!lpfc_complete_unsol_iocb(phba,
15669 &phba->sli.ring[LPFC_ELS_RING],
15670 iocbq, fc_hdr->fh_r_ctl,
15671 fc_hdr->fh_type))
James Smart6d368e52011-05-24 11:44:12 -040015672 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smart6669f9b2009-10-02 15:16:45 -040015673 "2540 Ring %d handler: unexpected Rctl "
15674 "x%x Type x%x received\n",
15675 LPFC_ELS_RING,
15676 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15677
15678 /* Free iocb created in lpfc_prep_seq */
15679 list_for_each_entry_safe(curr_iocb, next_iocb,
15680 &iocbq->list, list) {
15681 list_del_init(&curr_iocb->list);
15682 lpfc_sli_release_iocbq(phba, curr_iocb);
15683 }
15684 lpfc_sli_release_iocbq(phba, iocbq);
15685}
15686
James Smart4f774512009-05-22 14:52:35 -040015687/**
15688 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15689 * @phba: Pointer to HBA context object.
15690 *
15691 * This function is called with no lock held. This function processes all
15692 * the received buffers and gives it to upper layers when a received buffer
15693 * indicates that it is the final frame in the sequence. The interrupt
15694 * service routine processes received buffers at interrupt contexts and adds
15695 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15696 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15697 * appropriate receive function when the final frame in a sequence is received.
15698 **/
James Smart4d9ab992009-10-02 15:16:39 -040015699void
15700lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15701 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040015702{
James Smart4d9ab992009-10-02 15:16:39 -040015703 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040015704 struct fc_frame_header *fc_hdr;
15705 struct lpfc_vport *vport;
15706 uint32_t fcfi;
James Smart939723a2012-05-09 21:19:03 -040015707 uint32_t did;
James Smart4f774512009-05-22 14:52:35 -040015708
James Smart4f774512009-05-22 14:52:35 -040015709 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040015710 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15711 /* check to see if this a valid type of frame */
15712 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15713 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15714 return;
15715 }
James Smart7851fe22011-07-22 18:36:52 -040015716 if ((bf_get(lpfc_cqe_code,
15717 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15718 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15719 &dmabuf->cq_event.cqe.rcqe_cmpl);
15720 else
15721 fcfi = bf_get(lpfc_rcqe_fcf_id,
15722 &dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart939723a2012-05-09 21:19:03 -040015723
James Smart4d9ab992009-10-02 15:16:39 -040015724 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
James Smart939723a2012-05-09 21:19:03 -040015725 if (!vport) {
James Smart4d9ab992009-10-02 15:16:39 -040015726 /* throw out the frame */
15727 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15728 return;
15729 }
James Smart939723a2012-05-09 21:19:03 -040015730
15731 /* d_id this frame is directed to */
15732 did = sli4_did_from_fc_hdr(fc_hdr);
15733
15734 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15735 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15736 (did != Fabric_DID)) {
15737 /*
15738 * Throw out the frame if we are not pt2pt.
15739 * The pt2pt protocol allows for discovery frames
15740 * to be received without a registered VPI.
15741 */
15742 if (!(vport->fc_flag & FC_PT2PT) ||
15743 (phba->link_state == LPFC_HBA_READY)) {
15744 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15745 return;
15746 }
15747 }
15748
James Smart6669f9b2009-10-02 15:16:45 -040015749 /* Handle the basic abort sequence (BA_ABTS) event */
15750 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15751 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15752 return;
15753 }
15754
James Smart4d9ab992009-10-02 15:16:39 -040015755 /* Link this frame */
15756 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15757 if (!seq_dmabuf) {
15758 /* unable to add frame to vport - throw it out */
15759 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15760 return;
15761 }
15762 /* If not last frame in sequence continue processing frames. */
James Smartdef9c7a2009-12-21 17:02:28 -050015763 if (!lpfc_seq_complete(seq_dmabuf))
James Smart4d9ab992009-10-02 15:16:39 -040015764 return;
James Smartdef9c7a2009-12-21 17:02:28 -050015765
James Smart6669f9b2009-10-02 15:16:45 -040015766 /* Send the complete sequence to the upper layer protocol */
15767 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040015768}
James Smart6fb120a2009-05-22 14:52:59 -040015769
15770/**
15771 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15772 * @phba: pointer to lpfc hba data structure.
15773 *
15774 * This routine is invoked to post rpi header templates to the
15775 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015776 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15777 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015778 *
15779 * This routine does not require any locks. It's usage is expected
15780 * to be driver load or reset recovery when the driver is
15781 * sequential.
15782 *
15783 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015784 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015785 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015786 * When this error occurs, the driver is not guaranteed
15787 * to have any rpi regions posted to the device and
15788 * must either attempt to repost the regions or take a
15789 * fatal error.
15790 **/
15791int
15792lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15793{
15794 struct lpfc_rpi_hdr *rpi_page;
15795 uint32_t rc = 0;
James Smart6d368e52011-05-24 11:44:12 -040015796 uint16_t lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015797
James Smart6d368e52011-05-24 11:44:12 -040015798 /* SLI4 ports that support extents do not require RPI headers. */
15799 if (!phba->sli4_hba.rpi_hdrs_in_use)
15800 goto exit;
15801 if (phba->sli4_hba.extents_in_use)
15802 return -EIO;
15803
James Smart6fb120a2009-05-22 14:52:59 -040015804 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
James Smart6d368e52011-05-24 11:44:12 -040015805 /*
15806 * Assign the rpi headers a physical rpi only if the driver
15807 * has not initialized those resources. A port reset only
15808 * needs the headers posted.
15809 */
15810 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15811 LPFC_RPI_RSRC_RDY)
15812 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15813
James Smart6fb120a2009-05-22 14:52:59 -040015814 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15815 if (rc != MBX_SUCCESS) {
15816 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15817 "2008 Error %d posting all rpi "
15818 "headers\n", rc);
15819 rc = -EIO;
15820 break;
15821 }
15822 }
15823
James Smart6d368e52011-05-24 11:44:12 -040015824 exit:
15825 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15826 LPFC_RPI_RSRC_RDY);
James Smart6fb120a2009-05-22 14:52:59 -040015827 return rc;
15828}
15829
15830/**
15831 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15832 * @phba: pointer to lpfc hba data structure.
15833 * @rpi_page: pointer to the rpi memory region.
15834 *
15835 * This routine is invoked to post a single rpi header to the
15836 * HBA consistent with the SLI-4 interface spec. This memory region
15837 * maps up to 64 rpi context regions.
15838 *
15839 * Return codes
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015840 * 0 - successful
James Smartd439d282010-09-29 11:18:45 -040015841 * -ENOMEM - No available memory
15842 * -EIO - The mailbox failed to complete successfully.
James Smart6fb120a2009-05-22 14:52:59 -040015843 **/
15844int
15845lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15846{
15847 LPFC_MBOXQ_t *mboxq;
15848 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15849 uint32_t rc = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015850 uint32_t shdr_status, shdr_add_status;
15851 union lpfc_sli4_cfg_shdr *shdr;
15852
James Smart6d368e52011-05-24 11:44:12 -040015853 /* SLI4 ports that support extents do not require RPI headers. */
15854 if (!phba->sli4_hba.rpi_hdrs_in_use)
15855 return rc;
15856 if (phba->sli4_hba.extents_in_use)
15857 return -EIO;
15858
James Smart6fb120a2009-05-22 14:52:59 -040015859 /* The port is notified of the header region via a mailbox command. */
15860 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15861 if (!mboxq) {
15862 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15863 "2001 Unable to allocate memory for issuing "
15864 "SLI_CONFIG_SPECIAL mailbox command\n");
15865 return -ENOMEM;
15866 }
15867
15868 /* Post all rpi memory regions to the port. */
15869 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
James Smart6fb120a2009-05-22 14:52:59 -040015870 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15871 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15872 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
James Smartfedd3b72011-02-16 12:39:24 -050015873 sizeof(struct lpfc_sli4_cfg_mhdr),
15874 LPFC_SLI4_MBX_EMBED);
James Smart6d368e52011-05-24 11:44:12 -040015875
15876
15877 /* Post the physical rpi to the port for this rpi header. */
James Smart6fb120a2009-05-22 14:52:59 -040015878 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15879 rpi_page->start_rpi);
James Smart6d368e52011-05-24 11:44:12 -040015880 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15881 hdr_tmpl, rpi_page->page_count);
15882
James Smart6fb120a2009-05-22 14:52:59 -040015883 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15884 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040015885 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040015886 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15887 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15888 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15889 if (rc != MBX_TIMEOUT)
15890 mempool_free(mboxq, phba->mbox_mem_pool);
15891 if (shdr_status || shdr_add_status || rc) {
15892 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15893 "2514 POST_RPI_HDR mailbox failed with "
15894 "status x%x add_status x%x, mbx status x%x\n",
15895 shdr_status, shdr_add_status, rc);
15896 rc = -ENXIO;
15897 }
15898 return rc;
15899}
15900
15901/**
15902 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15903 * @phba: pointer to lpfc hba data structure.
15904 *
15905 * This routine is invoked to post rpi header templates to the
15906 * HBA consistent with the SLI-4 interface spec. This routine
James Smart49198b32010-04-06 15:04:33 -040015907 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15908 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
James Smart6fb120a2009-05-22 14:52:59 -040015909 *
15910 * Returns
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020015911 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
James Smart6fb120a2009-05-22 14:52:59 -040015912 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15913 **/
15914int
15915lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15916{
James Smart6d368e52011-05-24 11:44:12 -040015917 unsigned long rpi;
15918 uint16_t max_rpi, rpi_limit;
15919 uint16_t rpi_remaining, lrpi = 0;
James Smart6fb120a2009-05-22 14:52:59 -040015920 struct lpfc_rpi_hdr *rpi_hdr;
James Smart4902b382013-10-10 12:20:35 -040015921 unsigned long iflag;
James Smart6fb120a2009-05-22 14:52:59 -040015922
James Smart6fb120a2009-05-22 14:52:59 -040015923 /*
James Smart6d368e52011-05-24 11:44:12 -040015924 * Fetch the next logical rpi. Because this index is logical,
15925 * the driver starts at 0 each time.
James Smart6fb120a2009-05-22 14:52:59 -040015926 */
James Smart4902b382013-10-10 12:20:35 -040015927 spin_lock_irqsave(&phba->hbalock, iflag);
James Smartbe6bb942015-04-07 15:07:22 -040015928 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
15929 rpi_limit = phba->sli4_hba.next_rpi;
15930
James Smart6d368e52011-05-24 11:44:12 -040015931 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15932 if (rpi >= rpi_limit)
James Smart6fb120a2009-05-22 14:52:59 -040015933 rpi = LPFC_RPI_ALLOC_ERROR;
15934 else {
15935 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15936 phba->sli4_hba.max_cfg_param.rpi_used++;
15937 phba->sli4_hba.rpi_count++;
15938 }
James Smartbe6bb942015-04-07 15:07:22 -040015939 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15940 "0001 rpi:%x max:%x lim:%x\n",
15941 (int) rpi, max_rpi, rpi_limit);
James Smart6fb120a2009-05-22 14:52:59 -040015942
15943 /*
15944 * Don't try to allocate more rpi header regions if the device limit
James Smart6d368e52011-05-24 11:44:12 -040015945 * has been exhausted.
James Smart6fb120a2009-05-22 14:52:59 -040015946 */
15947 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15948 (phba->sli4_hba.rpi_count >= max_rpi)) {
James Smart4902b382013-10-10 12:20:35 -040015949 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015950 return rpi;
15951 }
15952
15953 /*
James Smart6d368e52011-05-24 11:44:12 -040015954 * RPI header postings are not required for SLI4 ports capable of
15955 * extents.
15956 */
15957 if (!phba->sli4_hba.rpi_hdrs_in_use) {
James Smart4902b382013-10-10 12:20:35 -040015958 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6d368e52011-05-24 11:44:12 -040015959 return rpi;
15960 }
15961
15962 /*
James Smart6fb120a2009-05-22 14:52:59 -040015963 * If the driver is running low on rpi resources, allocate another
15964 * page now. Note that the next_rpi value is used because
15965 * it represents how many are actually in use whereas max_rpi notes
15966 * how many are supported max by the device.
15967 */
James Smart6d368e52011-05-24 11:44:12 -040015968 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
James Smart4902b382013-10-10 12:20:35 -040015969 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart6fb120a2009-05-22 14:52:59 -040015970 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15971 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15972 if (!rpi_hdr) {
15973 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15974 "2002 Error Could not grow rpi "
15975 "count\n");
15976 } else {
James Smart6d368e52011-05-24 11:44:12 -040015977 lrpi = rpi_hdr->start_rpi;
15978 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
James Smart6fb120a2009-05-22 14:52:59 -040015979 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15980 }
15981 }
15982
15983 return rpi;
15984}
15985
15986/**
15987 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15988 * @phba: pointer to lpfc hba data structure.
15989 *
15990 * This routine is invoked to release an rpi to the pool of
15991 * available rpis maintained by the driver.
15992 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040015993static void
James Smartd7c47992010-06-08 18:31:54 -040015994__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15995{
15996 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15997 phba->sli4_hba.rpi_count--;
15998 phba->sli4_hba.max_cfg_param.rpi_used--;
15999 }
16000}
16001
16002/**
16003 * lpfc_sli4_free_rpi - Release an rpi for reuse.
16004 * @phba: pointer to lpfc hba data structure.
16005 *
16006 * This routine is invoked to release an rpi to the pool of
16007 * available rpis maintained by the driver.
16008 **/
16009void
James Smart6fb120a2009-05-22 14:52:59 -040016010lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
16011{
16012 spin_lock_irq(&phba->hbalock);
James Smartd7c47992010-06-08 18:31:54 -040016013 __lpfc_sli4_free_rpi(phba, rpi);
James Smart6fb120a2009-05-22 14:52:59 -040016014 spin_unlock_irq(&phba->hbalock);
16015}
16016
16017/**
16018 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
16019 * @phba: pointer to lpfc hba data structure.
16020 *
16021 * This routine is invoked to remove the memory region that
16022 * provided rpi via a bitmask.
16023 **/
16024void
16025lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
16026{
16027 kfree(phba->sli4_hba.rpi_bmask);
James Smart6d368e52011-05-24 11:44:12 -040016028 kfree(phba->sli4_hba.rpi_ids);
16029 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
James Smart6fb120a2009-05-22 14:52:59 -040016030}
16031
16032/**
16033 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
16034 * @phba: pointer to lpfc hba data structure.
16035 *
16036 * This routine is invoked to remove the memory region that
16037 * provided rpi via a bitmask.
16038 **/
16039int
James Smart6b5151f2012-01-18 16:24:06 -050016040lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
16041 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
James Smart6fb120a2009-05-22 14:52:59 -040016042{
16043 LPFC_MBOXQ_t *mboxq;
16044 struct lpfc_hba *phba = ndlp->phba;
16045 int rc;
16046
16047 /* The port is notified of the header region via a mailbox command. */
16048 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16049 if (!mboxq)
16050 return -ENOMEM;
16051
16052 /* Post all rpi memory regions to the port. */
16053 lpfc_resume_rpi(mboxq, ndlp);
James Smart6b5151f2012-01-18 16:24:06 -050016054 if (cmpl) {
16055 mboxq->mbox_cmpl = cmpl;
16056 mboxq->context1 = arg;
16057 mboxq->context2 = ndlp;
James Smart72859902012-01-18 16:25:38 -050016058 } else
16059 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart6b5151f2012-01-18 16:24:06 -050016060 mboxq->vport = ndlp->vport;
James Smart6fb120a2009-05-22 14:52:59 -040016061 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16062 if (rc == MBX_NOT_FINISHED) {
16063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16064 "2010 Resume RPI Mailbox failed "
16065 "status %d, mbxStatus x%x\n", rc,
16066 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16067 mempool_free(mboxq, phba->mbox_mem_pool);
16068 return -EIO;
16069 }
16070 return 0;
16071}
16072
16073/**
16074 * lpfc_sli4_init_vpi - Initialize a vpi with the port
James Smart76a95d72010-11-20 23:11:48 -050016075 * @vport: Pointer to the vport for which the vpi is being initialized
James Smart6fb120a2009-05-22 14:52:59 -040016076 *
James Smart76a95d72010-11-20 23:11:48 -050016077 * This routine is invoked to activate a vpi with the port.
James Smart6fb120a2009-05-22 14:52:59 -040016078 *
16079 * Returns:
16080 * 0 success
16081 * -Evalue otherwise
16082 **/
16083int
James Smart76a95d72010-11-20 23:11:48 -050016084lpfc_sli4_init_vpi(struct lpfc_vport *vport)
James Smart6fb120a2009-05-22 14:52:59 -040016085{
16086 LPFC_MBOXQ_t *mboxq;
16087 int rc = 0;
James Smart6a9c52c2009-10-02 15:16:51 -040016088 int retval = MBX_SUCCESS;
James Smart6fb120a2009-05-22 14:52:59 -040016089 uint32_t mbox_tmo;
James Smart76a95d72010-11-20 23:11:48 -050016090 struct lpfc_hba *phba = vport->phba;
James Smart6fb120a2009-05-22 14:52:59 -040016091 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16092 if (!mboxq)
16093 return -ENOMEM;
James Smart76a95d72010-11-20 23:11:48 -050016094 lpfc_init_vpi(phba, mboxq, vport->vpi);
James Smarta183a152011-10-10 21:32:43 -040016095 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
James Smart6fb120a2009-05-22 14:52:59 -040016096 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
James Smart6fb120a2009-05-22 14:52:59 -040016097 if (rc != MBX_SUCCESS) {
James Smart76a95d72010-11-20 23:11:48 -050016098 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
James Smart6fb120a2009-05-22 14:52:59 -040016099 "2022 INIT VPI Mailbox failed "
16100 "status %d, mbxStatus x%x\n", rc,
16101 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
James Smart6a9c52c2009-10-02 15:16:51 -040016102 retval = -EIO;
James Smart6fb120a2009-05-22 14:52:59 -040016103 }
James Smart6a9c52c2009-10-02 15:16:51 -040016104 if (rc != MBX_TIMEOUT)
James Smart76a95d72010-11-20 23:11:48 -050016105 mempool_free(mboxq, vport->phba->mbox_mem_pool);
James Smart6a9c52c2009-10-02 15:16:51 -040016106
16107 return retval;
James Smart6fb120a2009-05-22 14:52:59 -040016108}
16109
16110/**
16111 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
16112 * @phba: pointer to lpfc hba data structure.
16113 * @mboxq: Pointer to mailbox object.
16114 *
16115 * This routine is invoked to manually add a single FCF record. The caller
16116 * must pass a completely initialized FCF_Record. This routine takes
16117 * care of the nonembedded mailbox operations.
16118 **/
16119static void
16120lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
16121{
16122 void *virt_addr;
16123 union lpfc_sli4_cfg_shdr *shdr;
16124 uint32_t shdr_status, shdr_add_status;
16125
16126 virt_addr = mboxq->sge_array->addr[0];
16127 /* The IOCTL status is embedded in the mailbox subheader. */
16128 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
16129 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16130 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16131
16132 if ((shdr_status || shdr_add_status) &&
16133 (shdr_status != STATUS_FCF_IN_USE))
16134 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16135 "2558 ADD_FCF_RECORD mailbox failed with "
16136 "status x%x add_status x%x\n",
16137 shdr_status, shdr_add_status);
16138
16139 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16140}
16141
16142/**
16143 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
16144 * @phba: pointer to lpfc hba data structure.
16145 * @fcf_record: pointer to the initialized fcf record to add.
16146 *
16147 * This routine is invoked to manually add a single FCF record. The caller
16148 * must pass a completely initialized FCF_Record. This routine takes
16149 * care of the nonembedded mailbox operations.
16150 **/
16151int
16152lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
16153{
16154 int rc = 0;
16155 LPFC_MBOXQ_t *mboxq;
16156 uint8_t *bytep;
16157 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -040016158 struct lpfc_mbx_sge sge;
16159 uint32_t alloc_len, req_len;
16160 uint32_t fcfindex;
16161
16162 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16163 if (!mboxq) {
16164 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16165 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
16166 return -ENOMEM;
16167 }
16168
16169 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
16170 sizeof(uint32_t);
16171
16172 /* Allocate DMA memory and set up the non-embedded mailbox command */
16173 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16174 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
16175 req_len, LPFC_SLI4_MBX_NEMBED);
16176 if (alloc_len < req_len) {
16177 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16178 "2523 Allocated DMA memory size (x%x) is "
16179 "less than the requested DMA memory "
16180 "size (x%x)\n", alloc_len, req_len);
16181 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16182 return -ENOMEM;
16183 }
16184
16185 /*
16186 * Get the first SGE entry from the non-embedded DMA memory. This
16187 * routine only uses a single SGE.
16188 */
16189 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -040016190 virt_addr = mboxq->sge_array->addr[0];
16191 /*
16192 * Configure the FCF record for FCFI 0. This is the driver's
16193 * hardcoded default and gets used in nonFIP mode.
16194 */
16195 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16196 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16197 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16198
16199 /*
16200 * Copy the fcf_index and the FCF Record Data. The data starts after
16201 * the FCoE header plus word10. The data copy needs to be endian
16202 * correct.
16203 */
16204 bytep += sizeof(uint32_t);
16205 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16206 mboxq->vport = phba->pport;
16207 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16208 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16209 if (rc == MBX_NOT_FINISHED) {
16210 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16211 "2515 ADD_FCF_RECORD mailbox failed with "
16212 "status 0x%x\n", rc);
16213 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16214 rc = -EIO;
16215 } else
16216 rc = 0;
16217
16218 return rc;
16219}
16220
16221/**
16222 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16223 * @phba: pointer to lpfc hba data structure.
16224 * @fcf_record: pointer to the fcf record to write the default data.
16225 * @fcf_index: FCF table entry index.
16226 *
16227 * This routine is invoked to build the driver's default FCF record. The
16228 * values used are hardcoded. This routine handles memory initialization.
16229 *
16230 **/
16231void
16232lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16233 struct fcf_record *fcf_record,
16234 uint16_t fcf_index)
16235{
16236 memset(fcf_record, 0, sizeof(struct fcf_record));
16237 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16238 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16239 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16240 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16241 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16242 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16243 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16244 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16245 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16246 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16247 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16248 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16249 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040016250 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040016251 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16252 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16253 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16254 /* Set the VLAN bit map */
16255 if (phba->valid_vlan) {
16256 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16257 = 1 << (phba->vlan_id % 8);
16258 }
16259}
16260
16261/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016262 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
James Smart6fb120a2009-05-22 14:52:59 -040016263 * @phba: pointer to lpfc hba data structure.
16264 * @fcf_index: FCF table entry offset.
16265 *
James Smart0c9ab6f2010-02-26 14:15:57 -050016266 * This routine is invoked to scan the entire FCF table by reading FCF
16267 * record and processing it one at a time starting from the @fcf_index
16268 * for initial FCF discovery or fast FCF failover rediscovery.
16269 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016270 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016271 * otherwise.
James Smart6fb120a2009-05-22 14:52:59 -040016272 **/
16273int
James Smart0c9ab6f2010-02-26 14:15:57 -050016274lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -040016275{
16276 int rc = 0, error;
16277 LPFC_MBOXQ_t *mboxq;
James Smart6fb120a2009-05-22 14:52:59 -040016278
James Smart32b97932009-07-19 10:01:21 -040016279 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart80c17842012-03-01 22:35:45 -050016280 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040016281 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16282 if (!mboxq) {
16283 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16284 "2000 Failed to allocate mbox for "
16285 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040016286 error = -ENOMEM;
James Smart0c9ab6f2010-02-26 14:15:57 -050016287 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016288 }
James Smartecfd03c2010-02-12 14:41:27 -050016289 /* Construct the read FCF record mailbox command */
James Smart0c9ab6f2010-02-26 14:15:57 -050016290 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
James Smartecfd03c2010-02-12 14:41:27 -050016291 if (rc) {
16292 error = -EINVAL;
James Smart0c9ab6f2010-02-26 14:15:57 -050016293 goto fail_fcf_scan;
James Smart6fb120a2009-05-22 14:52:59 -040016294 }
James Smartecfd03c2010-02-12 14:41:27 -050016295 /* Issue the mailbox command asynchronously */
James Smart6fb120a2009-05-22 14:52:59 -040016296 mboxq->vport = phba->pport;
James Smart0c9ab6f2010-02-26 14:15:57 -050016297 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
James Smarta93ff372010-10-22 11:06:08 -040016298
16299 spin_lock_irq(&phba->hbalock);
16300 phba->hba_flag |= FCF_TS_INPROG;
16301 spin_unlock_irq(&phba->hbalock);
16302
James Smart6fb120a2009-05-22 14:52:59 -040016303 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
James Smartecfd03c2010-02-12 14:41:27 -050016304 if (rc == MBX_NOT_FINISHED)
James Smart6fb120a2009-05-22 14:52:59 -040016305 error = -EIO;
James Smartecfd03c2010-02-12 14:41:27 -050016306 else {
James Smart38b92ef2010-08-04 16:11:39 -040016307 /* Reset eligible FCF count for new scan */
16308 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
James Smart999d8132010-03-15 11:24:56 -040016309 phba->fcf.eligible_fcf_cnt = 0;
James Smart6fb120a2009-05-22 14:52:59 -040016310 error = 0;
James Smart32b97932009-07-19 10:01:21 -040016311 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016312fail_fcf_scan:
James Smart4d9ab992009-10-02 15:16:39 -040016313 if (error) {
16314 if (mboxq)
16315 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smarta93ff372010-10-22 11:06:08 -040016316 /* FCF scan failed, clear FCF_TS_INPROG flag */
James Smart4d9ab992009-10-02 15:16:39 -040016317 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -040016318 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart4d9ab992009-10-02 15:16:39 -040016319 spin_unlock_irq(&phba->hbalock);
16320 }
James Smart6fb120a2009-05-22 14:52:59 -040016321 return error;
16322}
James Smarta0c87cb2009-07-19 10:01:10 -040016323
16324/**
James Smarta93ff372010-10-22 11:06:08 -040016325 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
James Smart0c9ab6f2010-02-26 14:15:57 -050016326 * @phba: pointer to lpfc hba data structure.
16327 * @fcf_index: FCF table entry offset.
16328 *
16329 * This routine is invoked to read an FCF record indicated by @fcf_index
James Smarta93ff372010-10-22 11:06:08 -040016330 * and to use it for FLOGI roundrobin FCF failover.
James Smart0c9ab6f2010-02-26 14:15:57 -050016331 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016332 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016333 * otherwise.
16334 **/
16335int
16336lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16337{
16338 int rc = 0, error;
16339 LPFC_MBOXQ_t *mboxq;
16340
16341 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16342 if (!mboxq) {
16343 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16344 "2763 Failed to allocate mbox for "
16345 "READ_FCF cmd\n");
16346 error = -ENOMEM;
16347 goto fail_fcf_read;
16348 }
16349 /* Construct the read FCF record mailbox command */
16350 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16351 if (rc) {
16352 error = -EINVAL;
16353 goto fail_fcf_read;
16354 }
16355 /* Issue the mailbox command asynchronously */
16356 mboxq->vport = phba->pport;
16357 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16358 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16359 if (rc == MBX_NOT_FINISHED)
16360 error = -EIO;
16361 else
16362 error = 0;
16363
16364fail_fcf_read:
16365 if (error && mboxq)
16366 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16367 return error;
16368}
16369
16370/**
16371 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16372 * @phba: pointer to lpfc hba data structure.
16373 * @fcf_index: FCF table entry offset.
16374 *
16375 * This routine is invoked to read an FCF record indicated by @fcf_index to
James Smarta93ff372010-10-22 11:06:08 -040016376 * determine whether it's eligible for FLOGI roundrobin failover list.
James Smart0c9ab6f2010-02-26 14:15:57 -050016377 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -030016378 * Return 0 if the mailbox command is submitted successfully, none 0
James Smart0c9ab6f2010-02-26 14:15:57 -050016379 * otherwise.
16380 **/
16381int
16382lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16383{
16384 int rc = 0, error;
16385 LPFC_MBOXQ_t *mboxq;
16386
16387 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16388 if (!mboxq) {
16389 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16390 "2758 Failed to allocate mbox for "
16391 "READ_FCF cmd\n");
16392 error = -ENOMEM;
16393 goto fail_fcf_read;
16394 }
16395 /* Construct the read FCF record mailbox command */
16396 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16397 if (rc) {
16398 error = -EINVAL;
16399 goto fail_fcf_read;
16400 }
16401 /* Issue the mailbox command asynchronously */
16402 mboxq->vport = phba->pport;
16403 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16404 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16405 if (rc == MBX_NOT_FINISHED)
16406 error = -EIO;
16407 else
16408 error = 0;
16409
16410fail_fcf_read:
16411 if (error && mboxq)
16412 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16413 return error;
16414}
16415
16416/**
James Smartf5cb5302015-12-16 18:11:52 -050016417 * lpfc_check_next_fcf_pri_level
James Smart7d791df2011-07-22 18:37:52 -040016418 * phba pointer to the lpfc_hba struct for this port.
16419 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16420 * routine when the rr_bmask is empty. The FCF indecies are put into the
16421 * rr_bmask based on their priority level. Starting from the highest priority
16422 * to the lowest. The most likely FCF candidate will be in the highest
16423 * priority group. When this routine is called it searches the fcf_pri list for
16424 * next lowest priority group and repopulates the rr_bmask with only those
16425 * fcf_indexes.
16426 * returns:
16427 * 1=success 0=failure
16428 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016429static int
James Smart7d791df2011-07-22 18:37:52 -040016430lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16431{
16432 uint16_t next_fcf_pri;
16433 uint16_t last_index;
16434 struct lpfc_fcf_pri *fcf_pri;
16435 int rc;
16436 int ret = 0;
16437
16438 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16439 LPFC_SLI4_FCF_TBL_INDX_MAX);
16440 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16441 "3060 Last IDX %d\n", last_index);
James Smart25626692013-03-01 16:36:54 -050016442
16443 /* Verify the priority list has 2 or more entries */
16444 spin_lock_irq(&phba->hbalock);
16445 if (list_empty(&phba->fcf.fcf_pri_list) ||
16446 list_is_singular(&phba->fcf.fcf_pri_list)) {
16447 spin_unlock_irq(&phba->hbalock);
James Smart7d791df2011-07-22 18:37:52 -040016448 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16449 "3061 Last IDX %d\n", last_index);
16450 return 0; /* Empty rr list */
16451 }
James Smart25626692013-03-01 16:36:54 -050016452 spin_unlock_irq(&phba->hbalock);
16453
James Smart7d791df2011-07-22 18:37:52 -040016454 next_fcf_pri = 0;
16455 /*
16456 * Clear the rr_bmask and set all of the bits that are at this
16457 * priority.
16458 */
16459 memset(phba->fcf.fcf_rr_bmask, 0,
16460 sizeof(*phba->fcf.fcf_rr_bmask));
16461 spin_lock_irq(&phba->hbalock);
16462 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16463 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16464 continue;
16465 /*
16466 * the 1st priority that has not FLOGI failed
16467 * will be the highest.
16468 */
16469 if (!next_fcf_pri)
16470 next_fcf_pri = fcf_pri->fcf_rec.priority;
16471 spin_unlock_irq(&phba->hbalock);
16472 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16473 rc = lpfc_sli4_fcf_rr_index_set(phba,
16474 fcf_pri->fcf_rec.fcf_index);
16475 if (rc)
16476 return 0;
16477 }
16478 spin_lock_irq(&phba->hbalock);
16479 }
16480 /*
16481 * if next_fcf_pri was not set above and the list is not empty then
16482 * we have failed flogis on all of them. So reset flogi failed
Anatol Pomozov4907cb72012-09-01 10:31:09 -070016483 * and start at the beginning.
James Smart7d791df2011-07-22 18:37:52 -040016484 */
16485 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16486 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16487 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16488 /*
16489 * the 1st priority that has not FLOGI failed
16490 * will be the highest.
16491 */
16492 if (!next_fcf_pri)
16493 next_fcf_pri = fcf_pri->fcf_rec.priority;
16494 spin_unlock_irq(&phba->hbalock);
16495 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16496 rc = lpfc_sli4_fcf_rr_index_set(phba,
16497 fcf_pri->fcf_rec.fcf_index);
16498 if (rc)
16499 return 0;
16500 }
16501 spin_lock_irq(&phba->hbalock);
16502 }
16503 } else
16504 ret = 1;
16505 spin_unlock_irq(&phba->hbalock);
16506
16507 return ret;
16508}
16509/**
James Smart0c9ab6f2010-02-26 14:15:57 -050016510 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16511 * @phba: pointer to lpfc hba data structure.
16512 *
16513 * This routine is to get the next eligible FCF record index in a round
16514 * robin fashion. If the next eligible FCF record index equals to the
James Smarta93ff372010-10-22 11:06:08 -040016515 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
James Smart0c9ab6f2010-02-26 14:15:57 -050016516 * shall be returned, otherwise, the next eligible FCF record's index
16517 * shall be returned.
16518 **/
16519uint16_t
16520lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16521{
16522 uint16_t next_fcf_index;
16523
James Smart421c6622013-01-03 15:44:16 -050016524initial_priority:
James Smart3804dc82010-07-14 15:31:37 -040016525 /* Search start from next bit of currently registered FCF index */
James Smart421c6622013-01-03 15:44:16 -050016526 next_fcf_index = phba->fcf.current_rec.fcf_indx;
16527
James Smart7d791df2011-07-22 18:37:52 -040016528next_priority:
James Smart421c6622013-01-03 15:44:16 -050016529 /* Determine the next fcf index to check */
16530 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
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,
James Smart3804dc82010-07-14 15:31:37 -040016533 next_fcf_index);
16534
James Smart0c9ab6f2010-02-26 14:15:57 -050016535 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
James Smart7d791df2011-07-22 18:37:52 -040016536 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16537 /*
16538 * If we have wrapped then we need to clear the bits that
16539 * have been tested so that we can detect when we should
16540 * change the priority level.
16541 */
James Smart0c9ab6f2010-02-26 14:15:57 -050016542 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16543 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
James Smart7d791df2011-07-22 18:37:52 -040016544 }
16545
James Smart0c9ab6f2010-02-26 14:15:57 -050016546
James Smart3804dc82010-07-14 15:31:37 -040016547 /* Check roundrobin failover list empty condition */
James Smart7d791df2011-07-22 18:37:52 -040016548 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
16549 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
16550 /*
16551 * If next fcf index is not found check if there are lower
16552 * Priority level fcf's in the fcf_priority list.
16553 * Set up the rr_bmask with all of the avaiable fcf bits
16554 * at that level and continue the selection process.
16555 */
16556 if (lpfc_check_next_fcf_pri_level(phba))
James Smart421c6622013-01-03 15:44:16 -050016557 goto initial_priority;
James Smart3804dc82010-07-14 15:31:37 -040016558 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16559 "2844 No roundrobin failover FCF available\n");
James Smart7d791df2011-07-22 18:37:52 -040016560 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
16561 return LPFC_FCOE_FCF_NEXT_NONE;
16562 else {
16563 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16564 "3063 Only FCF available idx %d, flag %x\n",
16565 next_fcf_index,
16566 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
16567 return next_fcf_index;
16568 }
James Smart3804dc82010-07-14 15:31:37 -040016569 }
16570
James Smart7d791df2011-07-22 18:37:52 -040016571 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
16572 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
James Smartf5cb5302015-12-16 18:11:52 -050016573 LPFC_FCF_FLOGI_FAILED) {
16574 if (list_is_singular(&phba->fcf.fcf_pri_list))
16575 return LPFC_FCOE_FCF_NEXT_NONE;
16576
James Smart7d791df2011-07-22 18:37:52 -040016577 goto next_priority;
James Smartf5cb5302015-12-16 18:11:52 -050016578 }
James Smart7d791df2011-07-22 18:37:52 -040016579
James Smart3804dc82010-07-14 15:31:37 -040016580 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016581 "2845 Get next roundrobin failover FCF (x%x)\n",
16582 next_fcf_index);
16583
James Smart0c9ab6f2010-02-26 14:15:57 -050016584 return next_fcf_index;
16585}
16586
16587/**
16588 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
16589 * @phba: pointer to lpfc hba data structure.
16590 *
16591 * This routine sets the FCF record index in to the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016592 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016593 * does not go beyond the range of the driver allocated bmask dimension
16594 * before setting the bit.
16595 *
16596 * Returns 0 if the index bit successfully set, otherwise, it returns
16597 * -EINVAL.
16598 **/
16599int
16600lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
16601{
16602 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16603 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016604 "2610 FCF (x%x) reached driver's book "
16605 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016606 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16607 return -EINVAL;
16608 }
16609 /* Set the eligible FCF record index bmask */
16610 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16611
James Smart3804dc82010-07-14 15:31:37 -040016612 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016613 "2790 Set FCF (x%x) to roundrobin FCF failover "
James Smart3804dc82010-07-14 15:31:37 -040016614 "bmask\n", fcf_index);
16615
James Smart0c9ab6f2010-02-26 14:15:57 -050016616 return 0;
16617}
16618
16619/**
James Smart3804dc82010-07-14 15:31:37 -040016620 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
James Smart0c9ab6f2010-02-26 14:15:57 -050016621 * @phba: pointer to lpfc hba data structure.
16622 *
16623 * This routine clears the FCF record index from the eligible bmask for
James Smarta93ff372010-10-22 11:06:08 -040016624 * roundrobin failover search. It checks to make sure that the index
James Smart0c9ab6f2010-02-26 14:15:57 -050016625 * does not go beyond the range of the driver allocated bmask dimension
16626 * before clearing the bit.
16627 **/
16628void
16629lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
16630{
James Smart9a803a72013-09-06 12:17:56 -040016631 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
James Smart0c9ab6f2010-02-26 14:15:57 -050016632 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16633 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016634 "2762 FCF (x%x) reached driver's book "
16635 "keeping dimension:x%x\n",
James Smart0c9ab6f2010-02-26 14:15:57 -050016636 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16637 return;
16638 }
16639 /* Clear the eligible FCF record index bmask */
James Smart7d791df2011-07-22 18:37:52 -040016640 spin_lock_irq(&phba->hbalock);
James Smart9a803a72013-09-06 12:17:56 -040016641 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
16642 list) {
James Smart7d791df2011-07-22 18:37:52 -040016643 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
16644 list_del_init(&fcf_pri->list);
16645 break;
16646 }
16647 }
16648 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016649 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
James Smart3804dc82010-07-14 15:31:37 -040016650
16651 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016652 "2791 Clear FCF (x%x) from roundrobin failover "
James Smart3804dc82010-07-14 15:31:37 -040016653 "bmask\n", fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -050016654}
16655
16656/**
James Smartecfd03c2010-02-12 14:41:27 -050016657 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
16658 * @phba: pointer to lpfc hba data structure.
16659 *
16660 * This routine is the completion routine for the rediscover FCF table mailbox
16661 * command. If the mailbox command returned failure, it will try to stop the
16662 * FCF rediscover wait timer.
16663 **/
Rashika Kheria5d8b8162014-09-03 12:55:04 -040016664static void
James Smartecfd03c2010-02-12 14:41:27 -050016665lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16666{
16667 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16668 uint32_t shdr_status, shdr_add_status;
16669
16670 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16671
16672 shdr_status = bf_get(lpfc_mbox_hdr_status,
16673 &redisc_fcf->header.cfg_shdr.response);
16674 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16675 &redisc_fcf->header.cfg_shdr.response);
16676 if (shdr_status || shdr_add_status) {
James Smart0c9ab6f2010-02-26 14:15:57 -050016677 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartecfd03c2010-02-12 14:41:27 -050016678 "2746 Requesting for FCF rediscovery failed "
16679 "status x%x add_status x%x\n",
16680 shdr_status, shdr_add_status);
James Smart0c9ab6f2010-02-26 14:15:57 -050016681 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
James Smartfc2b9892010-02-26 14:15:29 -050016682 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016683 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016684 spin_unlock_irq(&phba->hbalock);
16685 /*
16686 * CVL event triggered FCF rediscover request failed,
16687 * last resort to re-try current registered FCF entry.
16688 */
16689 lpfc_retry_pport_discovery(phba);
16690 } else {
16691 spin_lock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -050016692 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
James Smartfc2b9892010-02-26 14:15:29 -050016693 spin_unlock_irq(&phba->hbalock);
16694 /*
16695 * DEAD FCF event triggered FCF rediscover request
16696 * failed, last resort to fail over as a link down
16697 * to FCF registration.
16698 */
16699 lpfc_sli4_fcf_dead_failthrough(phba);
16700 }
James Smart0c9ab6f2010-02-26 14:15:57 -050016701 } else {
16702 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -040016703 "2775 Start FCF rediscover quiescent timer\n");
James Smartecfd03c2010-02-12 14:41:27 -050016704 /*
16705 * Start FCF rediscovery wait timer for pending FCF
16706 * before rescan FCF record table.
16707 */
16708 lpfc_fcf_redisc_wait_start_timer(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -050016709 }
James Smartecfd03c2010-02-12 14:41:27 -050016710
16711 mempool_free(mbox, phba->mbox_mem_pool);
16712}
16713
16714/**
James Smart3804dc82010-07-14 15:31:37 -040016715 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
James Smartecfd03c2010-02-12 14:41:27 -050016716 * @phba: pointer to lpfc hba data structure.
16717 *
16718 * This routine is invoked to request for rediscovery of the entire FCF table
16719 * by the port.
16720 **/
16721int
16722lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16723{
16724 LPFC_MBOXQ_t *mbox;
16725 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16726 int rc, length;
16727
James Smart0c9ab6f2010-02-26 14:15:57 -050016728 /* Cancel retry delay timers to all vports before FCF rediscover */
16729 lpfc_cancel_all_vport_retry_delay_timer(phba);
16730
James Smartecfd03c2010-02-12 14:41:27 -050016731 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16732 if (!mbox) {
16733 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16734 "2745 Failed to allocate mbox for "
16735 "requesting FCF rediscover.\n");
16736 return -ENOMEM;
16737 }
16738
16739 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16740 sizeof(struct lpfc_sli4_cfg_mhdr));
16741 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16742 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16743 length, LPFC_SLI4_MBX_EMBED);
16744
16745 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16746 /* Set count to 0 for invalidating the entire FCF database */
16747 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16748
16749 /* Issue the mailbox command asynchronously */
16750 mbox->vport = phba->pport;
16751 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16752 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16753
16754 if (rc == MBX_NOT_FINISHED) {
16755 mempool_free(mbox, phba->mbox_mem_pool);
16756 return -EIO;
16757 }
16758 return 0;
16759}
16760
16761/**
James Smartfc2b9892010-02-26 14:15:29 -050016762 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16763 * @phba: pointer to lpfc hba data structure.
16764 *
16765 * This function is the failover routine as a last resort to the FCF DEAD
16766 * event when driver failed to perform fast FCF failover.
16767 **/
16768void
16769lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16770{
16771 uint32_t link_state;
16772
16773 /*
16774 * Last resort as FCF DEAD event failover will treat this as
16775 * a link down, but save the link state because we don't want
16776 * it to be changed to Link Down unless it is already down.
16777 */
16778 link_state = phba->link_state;
16779 lpfc_linkdown(phba);
16780 phba->link_state = link_state;
16781
16782 /* Unregister FCF if no devices connected to it */
16783 lpfc_unregister_unused_fcf(phba);
16784}
16785
16786/**
James Smart026abb82011-12-13 13:20:45 -050016787 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016788 * @phba: pointer to lpfc hba data structure.
James Smart026abb82011-12-13 13:20:45 -050016789 * @rgn23_data: pointer to configure region 23 data.
James Smarta0c87cb2009-07-19 10:01:10 -040016790 *
James Smart026abb82011-12-13 13:20:45 -050016791 * This function gets SLI3 port configure region 23 data through memory dump
16792 * mailbox command. When it successfully retrieves data, the size of the data
16793 * will be returned, otherwise, 0 will be returned.
James Smarta0c87cb2009-07-19 10:01:10 -040016794 **/
James Smart026abb82011-12-13 13:20:45 -050016795static uint32_t
16796lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
James Smarta0c87cb2009-07-19 10:01:10 -040016797{
16798 LPFC_MBOXQ_t *pmb = NULL;
16799 MAILBOX_t *mb;
James Smart026abb82011-12-13 13:20:45 -050016800 uint32_t offset = 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016801 int rc;
16802
James Smart026abb82011-12-13 13:20:45 -050016803 if (!rgn23_data)
16804 return 0;
16805
James Smarta0c87cb2009-07-19 10:01:10 -040016806 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16807 if (!pmb) {
16808 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016809 "2600 failed to allocate mailbox memory\n");
16810 return 0;
James Smarta0c87cb2009-07-19 10:01:10 -040016811 }
16812 mb = &pmb->u.mb;
16813
James Smarta0c87cb2009-07-19 10:01:10 -040016814 do {
16815 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16816 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16817
16818 if (rc != MBX_SUCCESS) {
16819 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smart026abb82011-12-13 13:20:45 -050016820 "2601 failed to read config "
16821 "region 23, rc 0x%x Status 0x%x\n",
16822 rc, mb->mbxStatus);
James Smarta0c87cb2009-07-19 10:01:10 -040016823 mb->un.varDmp.word_cnt = 0;
16824 }
16825 /*
16826 * dump mem may return a zero when finished or we got a
16827 * mailbox error, either way we are done.
16828 */
16829 if (mb->un.varDmp.word_cnt == 0)
16830 break;
16831 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16832 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16833
16834 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
James Smart026abb82011-12-13 13:20:45 -050016835 rgn23_data + offset,
16836 mb->un.varDmp.word_cnt);
James Smarta0c87cb2009-07-19 10:01:10 -040016837 offset += mb->un.varDmp.word_cnt;
16838 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16839
James Smart026abb82011-12-13 13:20:45 -050016840 mempool_free(pmb, phba->mbox_mem_pool);
16841 return offset;
16842}
16843
16844/**
16845 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16846 * @phba: pointer to lpfc hba data structure.
16847 * @rgn23_data: pointer to configure region 23 data.
16848 *
16849 * This function gets SLI4 port configure region 23 data through memory dump
16850 * mailbox command. When it successfully retrieves data, the size of the data
16851 * will be returned, otherwise, 0 will be returned.
16852 **/
16853static uint32_t
16854lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16855{
16856 LPFC_MBOXQ_t *mboxq = NULL;
16857 struct lpfc_dmabuf *mp = NULL;
16858 struct lpfc_mqe *mqe;
16859 uint32_t data_length = 0;
16860 int rc;
16861
16862 if (!rgn23_data)
16863 return 0;
16864
16865 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16866 if (!mboxq) {
16867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16868 "3105 failed to allocate mailbox memory\n");
16869 return 0;
16870 }
16871
16872 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16873 goto out;
16874 mqe = &mboxq->u.mqe;
16875 mp = (struct lpfc_dmabuf *) mboxq->context1;
16876 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16877 if (rc)
16878 goto out;
16879 data_length = mqe->un.mb_words[5];
16880 if (data_length == 0)
16881 goto out;
16882 if (data_length > DMP_RGN23_SIZE) {
16883 data_length = 0;
16884 goto out;
16885 }
16886 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16887out:
16888 mempool_free(mboxq, phba->mbox_mem_pool);
16889 if (mp) {
16890 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16891 kfree(mp);
16892 }
16893 return data_length;
16894}
16895
16896/**
16897 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16898 * @phba: pointer to lpfc hba data structure.
16899 *
16900 * This function read region 23 and parse TLV for port status to
16901 * decide if the user disaled the port. If the TLV indicates the
16902 * port is disabled, the hba_flag is set accordingly.
16903 **/
16904void
16905lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16906{
16907 uint8_t *rgn23_data = NULL;
16908 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16909 uint32_t offset = 0;
16910
16911 /* Get adapter Region 23 data */
16912 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16913 if (!rgn23_data)
16914 goto out;
16915
16916 if (phba->sli_rev < LPFC_SLI_REV4)
16917 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16918 else {
16919 if_type = bf_get(lpfc_sli_intf_if_type,
16920 &phba->sli4_hba.sli_intf);
16921 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16922 goto out;
16923 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16924 }
James Smarta0c87cb2009-07-19 10:01:10 -040016925
16926 if (!data_size)
16927 goto out;
16928
16929 /* Check the region signature first */
16930 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16931 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16932 "2619 Config region 23 has bad signature\n");
16933 goto out;
16934 }
16935 offset += 4;
16936
16937 /* Check the data structure version */
16938 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16939 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16940 "2620 Config region 23 has bad version\n");
16941 goto out;
16942 }
16943 offset += 4;
16944
16945 /* Parse TLV entries in the region */
16946 while (offset < data_size) {
16947 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16948 break;
16949 /*
16950 * If the TLV is not driver specific TLV or driver id is
16951 * not linux driver id, skip the record.
16952 */
16953 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16954 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16955 (rgn23_data[offset + 3] != 0)) {
16956 offset += rgn23_data[offset + 1] * 4 + 4;
16957 continue;
16958 }
16959
16960 /* Driver found a driver specific TLV in the config region */
16961 sub_tlv_len = rgn23_data[offset + 1] * 4;
16962 offset += 4;
16963 tlv_offset = 0;
16964
16965 /*
16966 * Search for configured port state sub-TLV.
16967 */
16968 while ((offset < data_size) &&
16969 (tlv_offset < sub_tlv_len)) {
16970 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16971 offset += 4;
16972 tlv_offset += 4;
16973 break;
16974 }
16975 if (rgn23_data[offset] != PORT_STE_TYPE) {
16976 offset += rgn23_data[offset + 1] * 4 + 4;
16977 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16978 continue;
16979 }
16980
16981 /* This HBA contains PORT_STE configured */
16982 if (!rgn23_data[offset + 2])
16983 phba->hba_flag |= LINK_DISABLED;
16984
16985 goto out;
16986 }
16987 }
James Smart026abb82011-12-13 13:20:45 -050016988
James Smarta0c87cb2009-07-19 10:01:10 -040016989out:
James Smarta0c87cb2009-07-19 10:01:10 -040016990 kfree(rgn23_data);
16991 return;
16992}
James Smart695a8142010-01-26 23:08:03 -050016993
16994/**
James Smart52d52442011-05-24 11:42:45 -040016995 * lpfc_wr_object - write an object to the firmware
16996 * @phba: HBA structure that indicates port to create a queue on.
16997 * @dmabuf_list: list of dmabufs to write to the port.
16998 * @size: the total byte value of the objects to write to the port.
16999 * @offset: the current offset to be used to start the transfer.
17000 *
17001 * This routine will create a wr_object mailbox command to send to the port.
17002 * the mailbox command will be constructed using the dma buffers described in
17003 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
17004 * BDEs that the imbedded mailbox can support. The @offset variable will be
17005 * used to indicate the starting offset of the transfer and will also return
17006 * the offset after the write object mailbox has completed. @size is used to
17007 * determine the end of the object and whether the eof bit should be set.
17008 *
17009 * Return 0 is successful and offset will contain the the new offset to use
17010 * for the next write.
17011 * Return negative value for error cases.
17012 **/
17013int
17014lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
17015 uint32_t size, uint32_t *offset)
17016{
17017 struct lpfc_mbx_wr_object *wr_object;
17018 LPFC_MBOXQ_t *mbox;
17019 int rc = 0, i = 0;
17020 uint32_t shdr_status, shdr_add_status;
17021 uint32_t mbox_tmo;
17022 union lpfc_sli4_cfg_shdr *shdr;
17023 struct lpfc_dmabuf *dmabuf;
17024 uint32_t written = 0;
17025
17026 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17027 if (!mbox)
17028 return -ENOMEM;
17029
17030 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17031 LPFC_MBOX_OPCODE_WRITE_OBJECT,
17032 sizeof(struct lpfc_mbx_wr_object) -
17033 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17034
17035 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
17036 wr_object->u.request.write_offset = *offset;
17037 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
17038 wr_object->u.request.object_name[0] =
17039 cpu_to_le32(wr_object->u.request.object_name[0]);
17040 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
17041 list_for_each_entry(dmabuf, dmabuf_list, list) {
17042 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
17043 break;
17044 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
17045 wr_object->u.request.bde[i].addrHigh =
17046 putPaddrHigh(dmabuf->phys);
17047 if (written + SLI4_PAGE_SIZE >= size) {
17048 wr_object->u.request.bde[i].tus.f.bdeSize =
17049 (size - written);
17050 written += (size - written);
17051 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
17052 } else {
17053 wr_object->u.request.bde[i].tus.f.bdeSize =
17054 SLI4_PAGE_SIZE;
17055 written += SLI4_PAGE_SIZE;
17056 }
17057 i++;
17058 }
17059 wr_object->u.request.bde_count = i;
17060 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
17061 if (!phba->sli4_hba.intr_enable)
17062 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17063 else {
James Smarta183a152011-10-10 21:32:43 -040017064 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
James Smart52d52442011-05-24 11:42:45 -040017065 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17066 }
17067 /* The IOCTL status is embedded in the mailbox subheader. */
17068 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
17069 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17070 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17071 if (rc != MBX_TIMEOUT)
17072 mempool_free(mbox, phba->mbox_mem_pool);
17073 if (shdr_status || shdr_add_status || rc) {
17074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17075 "3025 Write Object mailbox failed with "
17076 "status x%x add_status x%x, mbx status x%x\n",
17077 shdr_status, shdr_add_status, rc);
17078 rc = -ENXIO;
17079 } else
17080 *offset += wr_object->u.response.actual_write_length;
17081 return rc;
17082}
17083
17084/**
James Smart695a8142010-01-26 23:08:03 -050017085 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
17086 * @vport: pointer to vport data structure.
17087 *
17088 * This function iterate through the mailboxq and clean up all REG_LOGIN
17089 * and REG_VPI mailbox commands associated with the vport. This function
17090 * is called when driver want to restart discovery of the vport due to
17091 * a Clear Virtual Link event.
17092 **/
17093void
17094lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
17095{
17096 struct lpfc_hba *phba = vport->phba;
17097 LPFC_MBOXQ_t *mb, *nextmb;
17098 struct lpfc_dmabuf *mp;
James Smart78730cf2010-04-06 15:06:30 -040017099 struct lpfc_nodelist *ndlp;
James Smartd439d282010-09-29 11:18:45 -040017100 struct lpfc_nodelist *act_mbx_ndlp = NULL;
James Smart589a52d2010-07-14 15:30:54 -040017101 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartd439d282010-09-29 11:18:45 -040017102 LIST_HEAD(mbox_cmd_list);
James Smart63e801c2010-11-20 23:14:19 -050017103 uint8_t restart_loop;
James Smart695a8142010-01-26 23:08:03 -050017104
James Smartd439d282010-09-29 11:18:45 -040017105 /* Clean up internally queued mailbox commands with the vport */
James Smart695a8142010-01-26 23:08:03 -050017106 spin_lock_irq(&phba->hbalock);
17107 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
17108 if (mb->vport != vport)
17109 continue;
17110
17111 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17112 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17113 continue;
17114
James Smartd439d282010-09-29 11:18:45 -040017115 list_del(&mb->list);
17116 list_add_tail(&mb->list, &mbox_cmd_list);
17117 }
17118 /* Clean up active mailbox command with the vport */
17119 mb = phba->sli.mbox_active;
17120 if (mb && (mb->vport == vport)) {
17121 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
17122 (mb->u.mb.mbxCommand == MBX_REG_VPI))
17123 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17124 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17125 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
17126 /* Put reference count for delayed processing */
17127 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
17128 /* Unregister the RPI when mailbox complete */
17129 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17130 }
17131 }
James Smart63e801c2010-11-20 23:14:19 -050017132 /* Cleanup any mailbox completions which are not yet processed */
17133 do {
17134 restart_loop = 0;
17135 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
17136 /*
17137 * If this mailox is already processed or it is
17138 * for another vport ignore it.
17139 */
17140 if ((mb->vport != vport) ||
17141 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
17142 continue;
17143
17144 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17145 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17146 continue;
17147
17148 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17149 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17150 ndlp = (struct lpfc_nodelist *)mb->context2;
17151 /* Unregister the RPI when mailbox complete */
17152 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17153 restart_loop = 1;
17154 spin_unlock_irq(&phba->hbalock);
17155 spin_lock(shost->host_lock);
17156 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17157 spin_unlock(shost->host_lock);
17158 spin_lock_irq(&phba->hbalock);
17159 break;
17160 }
17161 }
17162 } while (restart_loop);
17163
James Smartd439d282010-09-29 11:18:45 -040017164 spin_unlock_irq(&phba->hbalock);
17165
17166 /* Release the cleaned-up mailbox commands */
17167 while (!list_empty(&mbox_cmd_list)) {
17168 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
James Smart695a8142010-01-26 23:08:03 -050017169 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17170 mp = (struct lpfc_dmabuf *) (mb->context1);
17171 if (mp) {
17172 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
17173 kfree(mp);
17174 }
James Smart78730cf2010-04-06 15:06:30 -040017175 ndlp = (struct lpfc_nodelist *) mb->context2;
James Smartd439d282010-09-29 11:18:45 -040017176 mb->context2 = NULL;
James Smart78730cf2010-04-06 15:06:30 -040017177 if (ndlp) {
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017178 spin_lock(shost->host_lock);
James Smart589a52d2010-07-14 15:30:54 -040017179 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
Dan Carpenterec21b3b2010-08-08 00:15:17 +020017180 spin_unlock(shost->host_lock);
James Smart78730cf2010-04-06 15:06:30 -040017181 lpfc_nlp_put(ndlp);
James Smart78730cf2010-04-06 15:06:30 -040017182 }
James Smart695a8142010-01-26 23:08:03 -050017183 }
James Smart695a8142010-01-26 23:08:03 -050017184 mempool_free(mb, phba->mbox_mem_pool);
17185 }
James Smartd439d282010-09-29 11:18:45 -040017186
17187 /* Release the ndlp with the cleaned-up active mailbox command */
17188 if (act_mbx_ndlp) {
17189 spin_lock(shost->host_lock);
17190 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17191 spin_unlock(shost->host_lock);
17192 lpfc_nlp_put(act_mbx_ndlp);
James Smart695a8142010-01-26 23:08:03 -050017193 }
James Smart695a8142010-01-26 23:08:03 -050017194}
17195
James Smart2a9bf3d2010-06-07 15:24:45 -040017196/**
17197 * lpfc_drain_txq - Drain the txq
17198 * @phba: Pointer to HBA context object.
17199 *
17200 * This function attempt to submit IOCBs on the txq
17201 * to the adapter. For SLI4 adapters, the txq contains
17202 * ELS IOCBs that have been deferred because the there
17203 * are no SGLs. This congestion can occur with large
17204 * vport counts during node discovery.
17205 **/
17206
17207uint32_t
17208lpfc_drain_txq(struct lpfc_hba *phba)
17209{
17210 LIST_HEAD(completions);
17211 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
Daeseok Youn2e706372014-02-21 09:03:32 +090017212 struct lpfc_iocbq *piocbq = NULL;
James Smart2a9bf3d2010-06-07 15:24:45 -040017213 unsigned long iflags = 0;
17214 char *fail_msg = NULL;
17215 struct lpfc_sglq *sglq;
17216 union lpfc_wqe wqe;
James Smarta2fc4aef2014-09-03 12:57:55 -040017217 uint32_t txq_cnt = 0;
James Smart2a9bf3d2010-06-07 15:24:45 -040017218
James Smart398d81c2013-05-31 17:04:19 -040017219 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart0e9bb8d2013-03-01 16:35:12 -050017220 list_for_each_entry(piocbq, &pring->txq, list) {
17221 txq_cnt++;
17222 }
17223
17224 if (txq_cnt > pring->txq_max)
17225 pring->txq_max = txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017226
James Smart398d81c2013-05-31 17:04:19 -040017227 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017228
James Smart0e9bb8d2013-03-01 16:35:12 -050017229 while (!list_empty(&pring->txq)) {
James Smart398d81c2013-05-31 17:04:19 -040017230 spin_lock_irqsave(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017231
James Smart19ca7602010-11-20 23:11:55 -050017232 piocbq = lpfc_sli_ringtx_get(phba, pring);
James Smarta6298522012-06-12 13:54:11 -040017233 if (!piocbq) {
James Smart398d81c2013-05-31 17:04:19 -040017234 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smarta6298522012-06-12 13:54:11 -040017235 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17236 "2823 txq empty and txq_cnt is %d\n ",
James Smart0e9bb8d2013-03-01 16:35:12 -050017237 txq_cnt);
James Smarta6298522012-06-12 13:54:11 -040017238 break;
17239 }
James Smart19ca7602010-11-20 23:11:55 -050017240 sglq = __lpfc_sli_get_sglq(phba, piocbq);
James Smart2a9bf3d2010-06-07 15:24:45 -040017241 if (!sglq) {
James Smart19ca7602010-11-20 23:11:55 -050017242 __lpfc_sli_ringtx_put(phba, pring, piocbq);
James Smart398d81c2013-05-31 17:04:19 -040017243 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017244 break;
James Smart2a9bf3d2010-06-07 15:24:45 -040017245 }
James Smart0e9bb8d2013-03-01 16:35:12 -050017246 txq_cnt--;
James Smart2a9bf3d2010-06-07 15:24:45 -040017247
17248 /* The xri and iocb resources secured,
17249 * attempt to issue request
17250 */
James Smart6d368e52011-05-24 11:44:12 -040017251 piocbq->sli4_lxritag = sglq->sli4_lxritag;
James Smart2a9bf3d2010-06-07 15:24:45 -040017252 piocbq->sli4_xritag = sglq->sli4_xritag;
17253 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17254 fail_msg = "to convert bpl to sgl";
17255 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
17256 fail_msg = "to convert iocb to wqe";
17257 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
17258 fail_msg = " - Wq is full";
17259 else
17260 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17261
17262 if (fail_msg) {
17263 /* Failed means we can't issue and need to cancel */
17264 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17265 "2822 IOCB failed %s iotag 0x%x "
17266 "xri 0x%x\n",
17267 fail_msg,
17268 piocbq->iotag, piocbq->sli4_xritag);
17269 list_add_tail(&piocbq->list, &completions);
17270 }
James Smart398d81c2013-05-31 17:04:19 -040017271 spin_unlock_irqrestore(&pring->ring_lock, iflags);
James Smart2a9bf3d2010-06-07 15:24:45 -040017272 }
17273
James Smart2a9bf3d2010-06-07 15:24:45 -040017274 /* Cancel all the IOCBs that cannot be issued */
17275 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17276 IOERR_SLI_ABORTED);
17277
James Smart0e9bb8d2013-03-01 16:35:12 -050017278 return txq_cnt;
James Smart2a9bf3d2010-06-07 15:24:45 -040017279}